From 29845b6445979cdc739913c674786aa1286aba0e Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Tue, 24 Nov 2026 21:38:08 -0800 Subject: [PATCH 1/4] charset: convert UTF-8 to an 8-bit set given by a map odfs_utf8_to_latin1() turns names into ISO 8859-1, the character set most Amiga systems use. A system set up for another language uses another 8-bit set, ISO 8859-2 for Polish, and every letter of a name that Latin-1 does not have comes out as a question mark there. Rename the function to odfs_utf8_to_8bit() and let it take a map: the Unicode code point of each byte from 0xA0 to 0xFF. A NULL map still means Latin-1. Bytes 0x80 to 0x9F are never produced, as before: the Amiga console takes them as control codes whatever the character set says is there. A combining accent merges as before, into the Latin-1 letter, and that letter is then looked up in the map: it merges only into a letter the set has. The map code is under ODFS_FEATURE_CHARSET_EXTERNAL, which the ROM profile has off. Nothing passes a map yet. --- core/charset.c | 30 ++++++++- include/odfs/charset.h | 27 +++++--- include/odfs/config.h | 8 ++- tests/unit/test_charset.c | 128 ++++++++++++++++++++++++++++++-------- 4 files changed, 152 insertions(+), 41 deletions(-) diff --git a/core/charset.c b/core/charset.c index 3ad3672..5067eee 100644 --- a/core/charset.c +++ b/core/charset.c @@ -209,7 +209,27 @@ static unsigned int odfs_latin1_compose(unsigned int base, unsigned int mark) } #endif -size_t odfs_utf8_to_latin1(char *s) +/* the byte character cp has in the 8-bit set of map, or 0 for none */ +static unsigned int odfs_8bit_code(unsigned int cp, const uint16_t *map) +{ +#if ODFS_FEATURE_CHARSET_EXTERNAL + if (map) { + unsigned int i; + + for (i = 0; i < ODFS_CHARSET_MAP_SIZE; i++) { + if (map[i] == cp) + return ODFS_CHARSET_MAP_FIRST + i; + } + return 0; + } +#else + (void)map; +#endif + /* U+0080-U+009F are control codes, CSI among them */ + return (cp >= 0xA0u && cp <= 0xFFu) ? cp : 0; +} + +size_t odfs_utf8_to_8bit(char *s, const uint16_t *map) { const unsigned char *src = (const unsigned char *)s; unsigned char *dst = (unsigned char *)s; @@ -235,15 +255,19 @@ size_t odfs_utf8_to_latin1(char *s) src += more; #if ODFS_FEATURE_CHARSET_COMPOSE if (cp - 0x300u < 0x28u && dst != (unsigned char *)s) { + /* a Latin-1 code is the letter's code point too */ c = odfs_latin1_compose(dst[-1], cp - 0x300u); + if (c != 0) + c = odfs_8bit_code(c, map); if (c != 0) { dst[-1] = (unsigned char)c; continue; } } #endif - /* U+0080-U+009F are control codes, CSI among them */ - c = (cp >= 0xA0u && cp <= 0xFFu) ? cp : ODFS_CHAR_REPLACEMENT; + c = odfs_8bit_code(cp, map); + if (c == 0) + c = ODFS_CHAR_REPLACEMENT; } } *dst++ = (unsigned char)c; diff --git a/include/odfs/charset.h b/include/odfs/charset.h index 67e2498..57e8510 100644 --- a/include/odfs/charset.h +++ b/include/odfs/charset.h @@ -62,21 +62,32 @@ odfs_err_t odfs_iso_name_to_display(const char *src, size_t src_len, char *dst, size_t dst_size, int lowercase); +/* + * An 8-bit character set that has ASCII in its lower half is described + * by a map: the Unicode code point of each byte from 0xA0 to 0xFF, or 0 + * for a byte without a character. Bytes 0x80 to 0x9F are left out: the + * Amiga console takes them as control codes in every character set. + */ +#define ODFS_CHARSET_MAP_FIRST 0xA0u +#define ODFS_CHARSET_MAP_SIZE 96u + #if ODFS_FEATURE_CHARSET_BUILTIN /* - * Convert a UTF-8 string to ISO 8859-1 (Latin-1) in place and return - * its new length; the result is never longer than the input. + * Convert a UTF-8 string to an 8-bit character set in place and return + * its new length; the result is never longer than the input. A NULL + * map stands for ISO 8859-1 (Latin-1); only a build with + * ODFS_FEATURE_CHARSET_EXTERNAL takes another one. * - * A character Latin-1 does not have becomes '_'. A byte that is not + * A character the set does not have becomes '_'. A byte that is not * part of a well-formed UTF-8 sequence is kept as it is, so a string * that already is in an 8-bit character set, such as a Rock Ridge name * written by an Amiga mastering tool, passes through unchanged. * * With ODFS_FEATURE_CHARSET_COMPOSE, a combining accent merges with - * the letter before it where Latin-1 has the accented letter: "u" - * followed by U+0308 becomes u-diaeresis. + * the letter before it where Latin-1 and the set have the accented + * letter: "u" followed by U+0308 becomes u-diaeresis. */ -size_t odfs_utf8_to_latin1(char *s); +size_t odfs_utf8_to_8bit(char *s, const uint16_t *map); #endif /* @@ -85,14 +96,14 @@ size_t odfs_utf8_to_latin1(char *s); * symbolic link targets and the volume name converted to it. */ #define ODFS_NAMES_UTF8 0u -#define ODFS_NAMES_LATIN1 1u /* ISO 8859-1, see odfs_utf8_to_latin1() */ +#define ODFS_NAMES_LATIN1 1u /* ISO 8859-1 */ /* convert a UTF-8 name in place to character set charset */ static inline void odfs_name_to_charset(char *name, unsigned int charset) { #if ODFS_FEATURE_CHARSET_BUILTIN if (charset == ODFS_NAMES_LATIN1) - odfs_utf8_to_latin1(name); + odfs_utf8_to_8bit(name, NULL); #else (void)name; (void)charset; diff --git a/include/odfs/config.h b/include/odfs/config.h index 3f5cb21..c2b468b 100644 --- a/include/odfs/config.h +++ b/include/odfs/config.h @@ -105,9 +105,11 @@ /* ---------- charset feature flags ---------- */ /* - * BUILTIN: convert names from UTF-8 to Latin-1, see odfs/charset.h. - * COMPOSE: on the way, merge a letter and a combining accent that - * follows it. HFS+ stores every name decomposed like that. + * BUILTIN: convert names from UTF-8 to Latin-1, see odfs/charset.h. + * COMPOSE: on the way, merge a letter and a combining accent that + * follows it. HFS+ stores every name decomposed like that. + * EXTERNAL: convert to another 8-bit character set instead, given its + * map. */ #ifdef ODFS_PROFILE_ROM #define ODFS_FEATURE_CHARSET_BUILTIN 1 diff --git a/tests/unit/test_charset.c b/tests/unit/test_charset.c index 84f5c40..e579e39 100644 --- a/tests/unit/test_charset.c +++ b/tests/unit/test_charset.c @@ -165,7 +165,7 @@ TEST(utf8_to_latin1_ascii) { char s[] = "README.TXT"; - ASSERT_EQ(odfs_utf8_to_latin1(s), 10); + ASSERT_EQ(odfs_utf8_to_8bit(s, NULL), 10); ASSERT_STR_EQ(s, "README.TXT"); } @@ -174,7 +174,7 @@ TEST(utf8_to_latin1_converts_latin1_range) /* "Gr" U+00FC U+00DF "e" U+00A0 U+00FF */ char s[] = "Gr\xC3\xBC\xC3\x9F" "e\xC2\xA0\xC3\xBF"; - ASSERT_EQ(odfs_utf8_to_latin1(s), 7); + ASSERT_EQ(odfs_utf8_to_8bit(s, NULL), 7); ASSERT_STR_EQ(s, "Gr\xFC\xDF" "e\xA0\xFF"); } @@ -184,7 +184,7 @@ TEST(utf8_to_latin1_replaces_other_characters) char s[] = "a\xC4\x80" "b\xE2\x82\xAC" "c\xE6\x97\xA5\xE6\x9C\xAC" "d\xF0\x9F\x98\x80" "e"; - ASSERT_EQ(odfs_utf8_to_latin1(s), 10); + ASSERT_EQ(odfs_utf8_to_8bit(s, NULL), 10); ASSERT_STR_EQ(s, "a_b_c__d_e"); } @@ -193,7 +193,7 @@ TEST(utf8_to_latin1_replaces_c1_controls) /* U+0080, U+009B (CSI), U+009F */ char s[] = "\xC2\x80\xC2\x9B\xC2\x9F"; - ASSERT_EQ(odfs_utf8_to_latin1(s), 3); + ASSERT_EQ(odfs_utf8_to_8bit(s, NULL), 3); ASSERT_STR_EQ(s, "___"); } @@ -207,15 +207,15 @@ TEST(utf8_to_latin1_keeps_raw_8bit_names) char d[] = "caf\xE9"; char e[] = "Men\xFC"; - ASSERT_EQ(odfs_utf8_to_latin1(a), 5); + ASSERT_EQ(odfs_utf8_to_8bit(a, NULL), 5); ASSERT_STR_EQ(a, "Gr\xFC\xDF" "e"); - odfs_utf8_to_latin1(b); + odfs_utf8_to_8bit(b, NULL); ASSERT_STR_EQ(b, "\xC4rger.txt"); - odfs_utf8_to_latin1(c); + odfs_utf8_to_8bit(c, NULL); ASSERT_STR_EQ(c, "Se\xF1or \xD1"); - odfs_utf8_to_latin1(d); + odfs_utf8_to_8bit(d, NULL); ASSERT_STR_EQ(d, "caf\xE9"); - odfs_utf8_to_latin1(e); + odfs_utf8_to_8bit(e, NULL); ASSERT_STR_EQ(e, "Men\xFC"); } @@ -228,13 +228,13 @@ TEST(utf8_to_latin1_keeps_malformed_sequences) char c[] = "x\xF0\x9F\x98" "y"; char d[] = "\xC0\x80\xC1\xBF\xF5\x80\xFF"; - odfs_utf8_to_latin1(a); + odfs_utf8_to_8bit(a, NULL); ASSERT_STR_EQ(a, "a\x80" "b\xBF"); - odfs_utf8_to_latin1(b); + odfs_utf8_to_8bit(b, NULL); ASSERT_STR_EQ(b, "x\xE2\x82"); - odfs_utf8_to_latin1(c); + odfs_utf8_to_8bit(c, NULL); ASSERT_STR_EQ(c, "x\xF0\x9F\x98" "y"); - odfs_utf8_to_latin1(d); + odfs_utf8_to_8bit(d, NULL); ASSERT_STR_EQ(d, "\xC0\x80\xC1\xBF\xF5\x80\xFF"); } @@ -246,11 +246,11 @@ TEST(utf8_to_latin1_keeps_overlong_forms) char b[] = "a\xE0\x80\xAF" "b"; /* '/' in three bytes */ char c[] = "a\xF0\x80\x83\xA9" "b"; /* U+00E9 in four bytes */ - ASSERT_EQ(odfs_utf8_to_latin1(a), 5); + ASSERT_EQ(odfs_utf8_to_8bit(a, NULL), 5); ASSERT_STR_EQ(a, "a\xE0\x80\x80" "b"); - ASSERT_EQ(odfs_utf8_to_latin1(b), 5); + ASSERT_EQ(odfs_utf8_to_8bit(b, NULL), 5); ASSERT_STR_EQ(b, "a\xE0\x80\xAF" "b"); - ASSERT_EQ(odfs_utf8_to_latin1(c), 6); + ASSERT_EQ(odfs_utf8_to_8bit(c, NULL), 6); ASSERT_STR_EQ(c, "a\xF0\x80\x83\xA9" "b"); } @@ -284,36 +284,110 @@ TEST(utf8_to_latin1_composes_accents) /* the mark only joins the letter right before it */ char l[] = "a\xCC\x88\xCC\x81"; - ASSERT_EQ(odfs_utf8_to_latin1(a), 5); + ASSERT_EQ(odfs_utf8_to_8bit(a, NULL), 5); ASSERT_STR_EQ(a, "\xC4rger"); - odfs_utf8_to_latin1(b); + odfs_utf8_to_8bit(b, NULL); ASSERT_STR_EQ(b, "Gr\xFC\xDF" "e"); - odfs_utf8_to_latin1(c); + odfs_utf8_to_8bit(c, NULL); ASSERT_STR_EQ(c, "caf\xE9"); - odfs_utf8_to_latin1(d); + odfs_utf8_to_8bit(d, NULL); ASSERT_STR_EQ(d, "gar\xE7on"); - odfs_utf8_to_latin1(e); + odfs_utf8_to_8bit(e, NULL); ASSERT_STR_EQ(e, "\xC5 \xF1 \xFF \xDD"); - odfs_utf8_to_latin1(f); + odfs_utf8_to_8bit(f, NULL); ASSERT_STR_EQ(f, "\xC0\xC1\xC2\xC3\xC4\xC5\xC7\xC8\xC9\xCA\xCB" "\xCC\xCD\xCE\xCF\xD1\xD2\xD3\xD4\xD5\xD6" "\xD9\xDA\xDB\xDC\xDD"); - odfs_utf8_to_latin1(g); + odfs_utf8_to_8bit(g, NULL); ASSERT_STR_EQ(g, "s_ Y_ q_"); - odfs_utf8_to_latin1(h); + odfs_utf8_to_8bit(h, NULL); ASSERT_STR_EQ(h, "_a"); - odfs_utf8_to_latin1(k); + odfs_utf8_to_8bit(k, NULL); ASSERT_STR_EQ(k, "1_ @_ `_"); - odfs_utf8_to_latin1(l); + odfs_utf8_to_8bit(l, NULL); ASSERT_STR_EQ(l, "\xE4_"); #endif } +#if ODFS_FEATURE_CHARSET_EXTERNAL +/* ISO 8859-2, 0xA0 to 0xFF */ +static const uint16_t iso8859_2[ODFS_CHARSET_MAP_SIZE] = { + 0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, + 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, + 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7, + 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C, + 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, + 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, + 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, + 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, + 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, + 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, + 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, + 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9 +}; +#endif + +TEST(utf8_to_8bit_follows_a_map) +{ +#if ODFS_FEATURE_CHARSET_EXTERNAL + /* U+017B U+00F3 U+0142 U+0107 ".txt", and U+010C "e" U+0161 + * "tina" */ + char a[] = "\xC5\xBB\xC3\xB3\xC5\x82\xC4\x87.txt"; + char b[] = "\xC4\x8C" "e\xC5\xA1tina"; + /* in Latin-1 but not in this set: n-tilde, a-ring, yen; and + * outside both: U+20AC, U+65E5 */ + char c[] = "Se\xC3\xB1or \xC3\xA5 \xC2\xA5 \xE2\x82\xAC \xE6\x97\xA5"; + /* where the set and Latin-1 agree */ + char d[] = "Gr\xC3\xBC\xC3\x9F" "e"; + /* bytes that are not UTF-8 stay, as they do for Latin-1 */ + char e[] = "\xAF\xF3\xB3\xE6.txt"; + + ASSERT_EQ(odfs_utf8_to_8bit(a, iso8859_2), 8); + ASSERT_STR_EQ(a, "\xAF\xF3\xB3\xE6.txt"); + odfs_utf8_to_8bit(b, iso8859_2); + ASSERT_STR_EQ(b, "\xC8" "e\xB9tina"); + odfs_utf8_to_8bit(c, iso8859_2); + ASSERT_STR_EQ(c, "Se_or _ _ _ _"); + odfs_utf8_to_8bit(d, iso8859_2); + ASSERT_STR_EQ(d, "Gr\xFC\xDF" "e"); + odfs_utf8_to_8bit(e, iso8859_2); + ASSERT_STR_EQ(e, "\xAF\xF3\xB3\xE6.txt"); +#endif +} + +/* a 0 in the map is a byte without a character, never a match */ +TEST(utf8_to_8bit_skips_unused_map_entries) +{ +#if ODFS_FEATURE_CHARSET_EXTERNAL + static uint16_t map[ODFS_CHARSET_MAP_SIZE]; + /* U+20AC, U+00A4, U+00E9 */ + char s[] = "5\xE2\x82\xAC \xC2\xA4 \xC3\xA9"; + + /* Latin-1 with the Euro sign in place of the currency sign */ + map[0xA4 - ODFS_CHARSET_MAP_FIRST] = 0x20AC; + ASSERT_EQ(odfs_utf8_to_8bit(s, map), 6); + ASSERT_STR_EQ(s, "5\xA4 _ _"); +#endif +} + +/* an accent merges only into a letter the set has */ +TEST(utf8_to_8bit_composes_accents_through_a_map) +{ +#if ODFS_FEATURE_CHARSET_EXTERNAL && ODFS_FEATURE_CHARSET_COMPOSE + /* A + diaeresis and e + acute are in ISO 8859-2; a + grave and + * n + tilde are not */ + char s[] = "A\xCC\x88 e\xCC\x81 a\xCC\x80 n\xCC\x83"; + + odfs_utf8_to_8bit(s, iso8859_2); + ASSERT_STR_EQ(s, "\xC4 \xE9 a_ n_"); +#endif +} + TEST(utf8_to_latin1_empty) { char s[] = ""; - ASSERT_EQ(odfs_utf8_to_latin1(s), 0); + ASSERT_EQ(odfs_utf8_to_8bit(s, NULL), 0); ASSERT_STR_EQ(s, ""); } From d75bcac52b5ac9c66b890c515a0aecd2ba5d5740 Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Tue, 24 Nov 2026 21:38:21 -0800 Subject: [PATCH 2/4] mount: let a mount give the character set of its names as a map A mount could ask for names in UTF-8 or in Latin-1. Add a third choice, ODFS_NAMES_MAPPED: the mount options carry a map as odfs_utf8_to_8bit() takes it, and node names, symbolic link targets and the volume name are converted with it. The backends learn the character set from the cache they are handed, which held its number. Put the number and the map into a struct, odfs_names_t, and hand that to odfs_namefix_init() and odfs_name_to_charset() in place of the number. The map belongs to whoever mounts and has to stay valid until the unmount. --- backends/hfs/hfs_common.c | 2 +- backends/iso9660/iso9660.c | 4 ++-- backends/udf/udf.c | 2 +- core/mount.c | 7 +++--- core/namefix.c | 11 +++++---- include/odfs/api.h | 2 ++ include/odfs/cache.h | 9 ++++---- include/odfs/charset.h | 18 +++++++++++---- include/odfs/namefix.h | 6 +++-- tests/unit/test_joliet.c | 2 +- tests/unit/test_namefix.c | 47 ++++++++++++++++++++++++++++++-------- tests/unit/test_symlink.c | 25 ++++++++++++++++++++ tests/unit/test_udf.c | 2 +- 13 files changed, 103 insertions(+), 34 deletions(-) diff --git a/backends/hfs/hfs_common.c b/backends/hfs/hfs_common.c index e7d6d4f..1c29fe0 100644 --- a/backends/hfs/hfs_common.c +++ b/backends/hfs/hfs_common.c @@ -444,7 +444,7 @@ odfs_err_t odfs_hfs_readdir(void *backend_ctx, rc.skip_to = (resume_offset && *resume_offset) ? *resume_offset : 0; rc.resume_offset = resume_offset; rc.last_err = ODFS_OK; - odfs_namefix_init(&rc.namefix, cache->name_charset); + odfs_namefix_init(&rc.namefix, &cache->names); /* dir->extent.lba stores the directory's CNID */ err = odfs_hfs_walk_catalog(vol, cache, dir->extent.lba, diff --git a/backends/iso9660/iso9660.c b/backends/iso9660/iso9660.c index e52d23c..fad1fe5 100644 --- a/backends/iso9660/iso9660.c +++ b/backends/iso9660/iso9660.c @@ -524,7 +524,7 @@ odfs_err_t odfs_iso_readdir(void *backend_ctx, ODFS_TRACE(log, ODFS_SUB_ISO, "readdir: LBA %" PRIu32 ", size %" PRIu32, dir_lba, dir_size); - odfs_namefix_init(&namefix, cache->name_charset); + odfs_namefix_init(&namefix, &cache->names); while (offset < dir_size) { uint32_t entry_start = offset; @@ -854,7 +854,7 @@ static odfs_err_t iso_readlink(void *backend_ctx, } /* name is the node's, which readdir converted */ - odfs_name_to_charset(node.name, cache->name_charset); + odfs_name_to_charset(node.name, &cache->names); if (odfs_strcasecmp(node.name, name) != 0) continue; diff --git a/backends/udf/udf.c b/backends/udf/udf.c index 9812b6e..a57fead 100644 --- a/backends/udf/udf.c +++ b/backends/udf/udf.c @@ -1256,7 +1256,7 @@ static odfs_err_t udf_walk_dir_entries(udf_context_t *ctx, walk.dir = dir; walk.callback = callback; walk.arg = arg; - odfs_namefix_init(&walk.namefix, cache->name_charset); + odfs_namefix_init(&walk.namefix, &cache->names); err = udf_walk_fids(ctx, cache, dir, udf_decode_dir_entry, &walk, dir_size_out); odfs_namefix_destroy(&walk.namefix); diff --git a/core/mount.c b/core/mount.c index f42e510..18e0a8d 100644 --- a/core/mount.c +++ b/core/mount.c @@ -445,7 +445,8 @@ odfs_err_t odfs_mount(odfs_media_t *media, err = odfs_cache_init(&mnt->cache, &mnt->media, cache_size); if (err != ODFS_OK) return err; - mnt->cache.name_charset = (uint8_t)mnt->opts.name_charset; + mnt->cache.names.charset = (uint8_t)mnt->opts.name_charset; + mnt->cache.names.map = mnt->opts.name_map; ODFS_INFO(&mnt->log, ODFS_SUB_MOUNT, "cache initialized: %" PRIu32 " blocks", cache_size); @@ -507,7 +508,7 @@ odfs_err_t odfs_mount(odfs_media_t *media, mnt->backend_ops->get_volume_name(mnt->backend_ctx, mnt->volume_name, sizeof(mnt->volume_name)); - odfs_name_to_charset(mnt->volume_name, mnt->cache.name_charset); + odfs_name_to_charset(mnt->volume_name, &mnt->cache.names); if (mnt->backend_ops->get_volume_size) mnt->total_blocks = mnt->backend_ops->get_volume_size(mnt->backend_ctx); @@ -717,7 +718,7 @@ odfs_err_t odfs_readlink(odfs_mount_t *mnt, buf, buf_size); /* the target names nodes, so it has to be spelled as they are */ if (err == ODFS_OK) - odfs_name_to_charset(buf, mnt->cache.name_charset); + odfs_name_to_charset(buf, &mnt->cache.names); return err; } diff --git a/core/namefix.c b/core/namefix.c index 013355a..252d11a 100644 --- a/core/namefix.c +++ b/core/namefix.c @@ -16,13 +16,14 @@ #define ODFS_NAMEFIX_CHUNK_SIZE 4096u -void odfs_namefix_init(odfs_namefix_state_t *state, unsigned int charset) +void odfs_namefix_init(odfs_namefix_state_t *state, + const odfs_names_t *names) { memset(state->buckets, 0, sizeof(state->buckets)); state->chunks = NULL; state->free = (unsigned char *)state->inline_buf; state->avail = sizeof(state->inline_buf); - state->charset = (unsigned char)charset; + state->names = names; } void odfs_namefix_destroy(odfs_namefix_state_t *state) @@ -35,7 +36,7 @@ void odfs_namefix_destroy(odfs_namefix_state_t *state) chunk = next; } - odfs_namefix_init(state, state->charset); + odfs_namefix_init(state, state->names); } static void *odfs_namefix_alloc(odfs_namefix_state_t *state, size_t size) @@ -142,7 +143,7 @@ odfs_err_t odfs_namefix_apply(odfs_namefix_state_t *state, * Convert before looking for duplicates, so that two names * which come out the same are told apart like any others. */ - odfs_name_to_charset(name, state->charset); + odfs_name_to_charset(name, state->names); hash = odfs_namefix_hash(name, &len); } else { hash ^= hash >> 16; @@ -182,7 +183,7 @@ odfs_err_t odfs_namefix_apply(odfs_namefix_state_t *state, if (keep + (size_t)suffix_len >= name_size) { keep = name_size - (size_t)suffix_len - 1; /* cut before a character, not inside its UTF-8 sequence */ - while (state->charset == ODFS_NAMES_UTF8 && keep > 0 && + while (state->names->charset == ODFS_NAMES_UTF8 && keep > 0 && ((unsigned char)base[keep] & 0xC0u) == 0x80u) keep--; } diff --git a/include/odfs/api.h b/include/odfs/api.h index c29d6ff..4a61454 100644 --- a/include/odfs/api.h +++ b/include/odfs/api.h @@ -27,6 +27,8 @@ typedef struct odfs_mount_opts { int lowercase_iso; /* lowercase plain ISO names */ int prefer_aiff; /* expose CDDA tracks as AIFF instead of WAV */ int name_charset; /* ODFS_NAMES_xxx: character set of names */ + const uint16_t *name_map; /* ODFS_NAMES_MAPPED: its map, which must + stay valid while mounted */ uint32_t cache_blocks; /* block cache size (0 = use default) */ uint32_t meta_cache_kib; /* parsed-dir cache budget in KiB (default from config.h; 0 disables) */ diff --git a/include/odfs/cache.h b/include/odfs/cache.h index 9e3dde9..20de6d9 100644 --- a/include/odfs/cache.h +++ b/include/odfs/cache.h @@ -8,6 +8,7 @@ #define ODFS_CACHE_H #include "odfs/config.h" +#include "odfs/charset.h" #include "odfs/error.h" #include "odfs/media.h" #include @@ -65,10 +66,10 @@ typedef struct odfs_cache { it still holds the same sector */ uint8_t *data_slab; /* all sector buffers, or NULL when each entry has its own */ - uint8_t name_charset; /* ODFS_NAMES_xxx the mount asked - for; kept here because the - cache is what every backend - call is handed */ + odfs_names_t names; /* character set the mount asked + for; kept here because the + cache is what every backend + call is handed */ } odfs_cache_t; /* lifecycle */ diff --git a/include/odfs/charset.h b/include/odfs/charset.h index 57e8510..8f10511 100644 --- a/include/odfs/charset.h +++ b/include/odfs/charset.h @@ -97,16 +97,24 @@ size_t odfs_utf8_to_8bit(char *s, const uint16_t *map); */ #define ODFS_NAMES_UTF8 0u #define ODFS_NAMES_LATIN1 1u /* ISO 8859-1 */ +#define ODFS_NAMES_MAPPED 2u /* the 8-bit set of odfs_names_t.map */ -/* convert a UTF-8 name in place to character set charset */ -static inline void odfs_name_to_charset(char *name, unsigned int charset) +typedef struct odfs_names { + uint8_t charset; /* ODFS_NAMES_xxx */ + const uint16_t *map; /* for ODFS_NAMES_MAPPED; the mount's + owner keeps it while mounted */ +} odfs_names_t; + +/* convert a UTF-8 name in place to the character set of names */ +static inline void odfs_name_to_charset(char *name, const odfs_names_t *names) { #if ODFS_FEATURE_CHARSET_BUILTIN - if (charset == ODFS_NAMES_LATIN1) - odfs_utf8_to_8bit(name, NULL); + if (names->charset != ODFS_NAMES_UTF8) + odfs_utf8_to_8bit(name, names->charset == ODFS_NAMES_MAPPED ? + names->map : NULL); #else (void)name; - (void)charset; + (void)names; #endif } diff --git a/include/odfs/namefix.h b/include/odfs/namefix.h index 48757a5..106a1b3 100644 --- a/include/odfs/namefix.h +++ b/include/odfs/namefix.h @@ -7,6 +7,7 @@ #ifndef ODFS_NAMEFIX_H #define ODFS_NAMEFIX_H +#include "odfs/charset.h" #include "odfs/error.h" #include @@ -31,12 +32,13 @@ typedef struct odfs_namefix_state { odfs_namefix_chunk_t *chunks; unsigned char *free; /* next unused byte */ size_t avail; /* bytes left at free */ - unsigned char charset; /* ODFS_NAMES_xxx of the fixed names */ + const odfs_names_t *names; /* character set of the fixed names */ void *inline_buf[ODFS_NAMEFIX_INLINE / sizeof(void *)]; } odfs_namefix_state_t; /* charset is the ODFS_NAMES_xxx set the names are wanted in */ -void odfs_namefix_init(odfs_namefix_state_t *state, unsigned int charset); +void odfs_namefix_init(odfs_namefix_state_t *state, + const odfs_names_t *names); void odfs_namefix_destroy(odfs_namefix_state_t *state); /* diff --git a/tests/unit/test_joliet.c b/tests/unit/test_joliet.c index a55e28d..e0a13ed 100644 --- a/tests/unit/test_joliet.c +++ b/tests/unit/test_joliet.c @@ -297,7 +297,7 @@ TEST(joliet_names_in_latin1) ASSERT_STR_EQ(collect.nodes[0].name, "Gr\xC3\xBC\xC3\x9F" "e"); ASSERT_STR_EQ(collect.nodes[1].name, "\xE6\x97\xA5\xE6\x9C\xAC"); - cache.name_charset = ODFS_NAMES_LATIN1; + cache.names.charset = ODFS_NAMES_LATIN1; memset(&collect, 0, sizeof(collect)); ASSERT_OK(joliet_backend_ops.readdir(ctx, &cache, &log, &root, joliet_collect_cb, &collect, NULL)); diff --git a/tests/unit/test_namefix.c b/tests/unit/test_namefix.c index 2d36e67..e5084a1 100644 --- a/tests/unit/test_namefix.c +++ b/tests/unit/test_namefix.c @@ -9,12 +9,15 @@ #include "odfs/node.h" #include "test_harness.h" +static const odfs_names_t names_utf8 = { ODFS_NAMES_UTF8, NULL }; +static const odfs_names_t names_latin1 = { ODFS_NAMES_LATIN1, NULL }; + TEST(namefix_keeps_unique_name) { odfs_namefix_state_t state; char name[ODFS_NAME_MAX] = "README"; - odfs_namefix_init(&state, ODFS_NAMES_UTF8); + odfs_namefix_init(&state, &names_utf8); ASSERT_OK(odfs_namefix_apply(&state, name, sizeof(name))); ASSERT_STR_EQ(name, "README"); odfs_namefix_destroy(&state); @@ -26,7 +29,7 @@ TEST(namefix_suffixes_duplicate_name) char first[ODFS_NAME_MAX] = "README"; char second[ODFS_NAME_MAX] = "README"; - odfs_namefix_init(&state, ODFS_NAMES_UTF8); + odfs_namefix_init(&state, &names_utf8); ASSERT_OK(odfs_namefix_apply(&state, first, sizeof(first))); ASSERT_OK(odfs_namefix_apply(&state, second, sizeof(second))); ASSERT_STR_EQ(first, "README"); @@ -41,7 +44,7 @@ TEST(namefix_handles_suffix_collisions) char second[ODFS_NAME_MAX] = "README"; char third[ODFS_NAME_MAX] = "README~2"; - odfs_namefix_init(&state, ODFS_NAMES_UTF8); + odfs_namefix_init(&state, &names_utf8); ASSERT_OK(odfs_namefix_apply(&state, first, sizeof(first))); ASSERT_OK(odfs_namefix_apply(&state, second, sizeof(second))); ASSERT_OK(odfs_namefix_apply(&state, third, sizeof(third))); @@ -56,7 +59,7 @@ TEST(namefix_is_case_insensitive) char first[ODFS_NAME_MAX] = "ReadMe"; char second[ODFS_NAME_MAX] = "README"; - odfs_namefix_init(&state, ODFS_NAMES_UTF8); + odfs_namefix_init(&state, &names_utf8); ASSERT_OK(odfs_namefix_apply(&state, first, sizeof(first))); ASSERT_OK(odfs_namefix_apply(&state, second, sizeof(second))); ASSERT_STR_EQ(second, "README~2"); @@ -84,7 +87,7 @@ TEST(namefix_truncates_at_utf8_boundaries) strcpy(a, names[i]); memcpy(b, a, sizeof(b)); - odfs_namefix_init(&state, ODFS_NAMES_UTF8); + odfs_namefix_init(&state, &names_utf8); ASSERT_OK(odfs_namefix_apply(&state, a, sizes[i])); ASSERT_OK(odfs_namefix_apply(&state, b, sizes[i])); ASSERT_STR_EQ(b, want[i]); @@ -99,7 +102,7 @@ TEST(namefix_converts_names_to_latin1) char name[ODFS_NAME_MAX] = "Gr\xC3\xBC\xC3\x9F" "e.txt"; char ascii[ODFS_NAME_MAX] = "README"; - odfs_namefix_init(&state, ODFS_NAMES_LATIN1); + odfs_namefix_init(&state, &names_latin1); ASSERT_OK(odfs_namefix_apply(&state, name, sizeof(name))); ASSERT_STR_EQ(name, "Gr\xFC\xDF" "e.txt"); ASSERT_OK(odfs_namefix_apply(&state, ascii, sizeof(ascii))); @@ -112,7 +115,7 @@ TEST(namefix_keeps_utf8_names_by_default) odfs_namefix_state_t state; char name[ODFS_NAME_MAX] = "Gr\xC3\xBC\xC3\x9F" "e.txt"; - odfs_namefix_init(&state, ODFS_NAMES_UTF8); + odfs_namefix_init(&state, &names_utf8); ASSERT_OK(odfs_namefix_apply(&state, name, sizeof(name))); ASSERT_STR_EQ(name, "Gr\xC3\xBC\xC3\x9F" "e.txt"); odfs_namefix_destroy(&state); @@ -130,7 +133,7 @@ TEST(namefix_separates_names_that_convert_alike) char d[ODFS_NAME_MAX] = "caf\xC3\xA9"; char e[ODFS_NAME_MAX] = "caf\xE9"; - odfs_namefix_init(&state, ODFS_NAMES_LATIN1); + odfs_namefix_init(&state, &names_latin1); ASSERT_OK(odfs_namefix_apply(&state, a, sizeof(a))); ASSERT_OK(odfs_namefix_apply(&state, b, sizeof(b))); ASSERT_OK(odfs_namefix_apply(&state, c, sizeof(c))); @@ -151,7 +154,7 @@ TEST(namefix_truncates_latin1_names_at_any_byte) char a[8] = "ab\xA9"; char b[8] = "ab\xA9"; - odfs_namefix_init(&state, ODFS_NAMES_LATIN1); + odfs_namefix_init(&state, &names_latin1); ASSERT_OK(odfs_namefix_apply(&state, a, 5)); ASSERT_OK(odfs_namefix_apply(&state, b, 5)); ASSERT_STR_EQ(a, "ab\xA9"); @@ -159,4 +162,30 @@ TEST(namefix_truncates_latin1_names_at_any_byte) odfs_namefix_destroy(&state); } +/* a mount can name its own 8-bit set */ +TEST(namefix_converts_names_through_a_map) +{ +#if ODFS_FEATURE_CHARSET_EXTERNAL + /* U+0142 at 0xB3 and U+00F3 at 0xF3, as in ISO 8859-2 */ + static uint16_t map[ODFS_CHARSET_MAP_SIZE]; + const odfs_names_t names = { ODFS_NAMES_MAPPED, map }; + odfs_namefix_state_t state; + /* "L" U+00F3 "d" U+017A ".txt" and "l" U+00F3 "d_.txt" */ + char a[ODFS_NAME_MAX] = "\xC5\x81\xC3\xB3" "d\xC5\xBA.txt"; + char b[ODFS_NAME_MAX] = "\xC5\x82\xC3\xB3" "d_.txt"; + + map[0xA3 - ODFS_CHARSET_MAP_FIRST] = 0x0141; + map[0xB3 - ODFS_CHARSET_MAP_FIRST] = 0x0142; + map[0xF3 - ODFS_CHARSET_MAP_FIRST] = 0x00F3; + + odfs_namefix_init(&state, &names); + ASSERT_OK(odfs_namefix_apply(&state, a, sizeof(a))); + ASSERT_STR_EQ(a, "\xA3\xF3" "d_.txt"); + /* the same name but for the case of its first letter */ + ASSERT_OK(odfs_namefix_apply(&state, b, sizeof(b))); + ASSERT_STR_EQ(b, "\xB3\xF3" "d_.txt"); + odfs_namefix_destroy(&state); +#endif +} + TEST_MAIN() diff --git a/tests/unit/test_symlink.c b/tests/unit/test_symlink.c index 14e669d..4e4dfb4 100644 --- a/tests/unit/test_symlink.c +++ b/tests/unit/test_symlink.c @@ -358,6 +358,31 @@ TEST(readlink_follows_the_mount_charset) ODFS_ERR_NOT_FOUND); odfs_unmount(&mnt); + +#if ODFS_FEATURE_CHARSET_EXTERNAL + { + /* a set of its own: U+00DC, U+00E4, U+00FC and U+65E5 */ + static uint16_t map[ODFS_CHARSET_MAP_SIZE] = { + 0x00DC, 0x00E4, 0x00FC, 0x65E5 + }; + + opts.name_charset = ODFS_NAMES_MAPPED; + opts.name_map = map; + ASSERT_OK(odfs_mount(&m, &opts, NULL, &mnt)); + ASSERT_STR_EQ(mnt.volume_name, "SL\xA0TEST"); + + memset(&collect, 0, sizeof(collect)); + ASSERT_OK(odfs_readdir(&mnt, &mnt.root, sl_collect_cb, &collect, + NULL)); + ASSERT_EQ(collect.count, 4); + ASSERT_STR_EQ(collect.entries[3].name, "L\xA1nk"); + + ASSERT_OK(odfs_readlink(&mnt, &mnt.root, "L\xA1nk", + target, sizeof(target))); + ASSERT_STR_EQ(target, "z\xA2l/\xA3"); + odfs_unmount(&mnt); + } +#endif } TEST(posix_to_amiga_conversion) diff --git a/tests/unit/test_udf.c b/tests/unit/test_udf.c index 8f3ef3d..c85e120 100644 --- a/tests/unit/test_udf.c +++ b/tests/unit/test_udf.c @@ -1758,7 +1758,7 @@ TEST(udf_cs0_8bit_name_decodes_latin1_to_utf8) ASSERT_STR_EQ(out.name, "caf\xC3\xA9"); /* a mount that names nodes in Latin-1 gets the bytes on the disc */ - cache.name_charset = ODFS_NAMES_LATIN1; + cache.names.charset = ODFS_NAMES_LATIN1; ASSERT_OK(udf_backend_ops.lookup(&ctx, &cache, &log, &dir, "caf\xE9", &out)); ASSERT_STR_EQ(out.name, "caf\xE9"); From 385b5ea5162a7407750103b9e35b7a5075e3ef69 Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Tue, 24 Nov 2026 21:42:25 -0800 Subject: [PATCH 3/4] amiga: take the system's character set from codesets.library Names are converted to Latin-1. That is wrong on a system set up for a language with another 8-bit character set: a Polish name on a Polish system shows an underscore for every letter Latin-1 does not have. If codesets.library is installed, ask it for the system's default character set and convert names to that. Without the library, and in the ROM build, names stay Latin-1. The library is told to leave memory again once the table is copied; it and its tables take some 300 KB. The handler asks when the first packet that concerns the disc comes in, not when it starts. Mount starts a handler from DEVS:DOSDrivers before IPrefs has loaded the locale preferences, and codesets.library works out its default when it is first opened and keeps it: asked that early it answers with the English default, and gives that answer to every program that opens it later. This must not cost anything per packet. A test in the packet loop shows in CDBench as one timer tick per packet, and any other change to that function, or to one inlined into it, makes the compiler arrange the whole loop differently. So the loop is left alone: - mount_volume() calls odfs_amiga_system_charset(). The first call makes the packet port run a software interrupt. - The interrupt runs for each packet until one is not ACTION_IS_FILESYSTEM, which Workbench sends first and with the DOS list locked. It then turns the port back into a signalling one and marks the change count as unknown, so that handle_media_change() sets out to mount the disc again before the packet is taken. - unmount_volume() calls odfs_amiga_system_charset() as well, and that call asks the library. A disc that is in the drive by then was mounted for Latin-1, but none of its names has been handed out. If the disc is still the same and its volume name is plain ASCII, the character set of the mount is switched and unmount_volume() returns without doing anything. The same if names stay Latin-1. Only a disc whose volume name has other characters is mounted again. The backend is asked for the name as it is on the disc: in the mount's copy an underscore may be one from the disc or stand for a letter Latin-1 does not have. The library is loaded from disk while the handler waits. The process that loads it, ramlib or LDDemon on AROS, serves one request at a time, so it must not need this handler meanwhile. Names stay Latin-1 - if a packet of the loader's own is waiting, or - if the boot volume, LIBS: or DEVS: has a directory on this handler's volume, does not exist, or is a late or non-binding assign, which shows where it leads only when it is used. The DOS list is locked for reading with LockDosList(), as the loader does. AttemptLockDosList() fails whenever another task has the list, even for reading as Workbench does around its first packet. What is left is a program that opens a library or device by a path on the disc at the very moment of that one lookup, or one that holds the DOS list for writing while it sends the handler its first packet. The code is in a file of its own, listed first among the handler's sources: the linker places handler_main.o in front of the core, so code added to it moves every routine of the core and the backends. AmigaOS 4 keeps Latin-1 here. Requests reach the handler as calls there, not as packets, and need a trigger of their own. --- Makefile | 15 +- README.md | 13 +- include/odfs/config.h | 9 +- platform/amiga/aros/amiga_target_compat.h | 3 + platform/amiga/handler.h | 20 ++ platform/amiga/handler_main.c | 17 ++ platform/amiga/os3/amiga_target_compat.h | 3 + platform/amiga/system_charset.c | 306 ++++++++++++++++++++++ 8 files changed, 377 insertions(+), 9 deletions(-) create mode 100644 platform/amiga/system_charset.c diff --git a/Makefile b/Makefile index 33b47b4..164b70a 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,8 @@ AMIGA_PLATFORM_INCLUDES = -I platform/amiga \ -I platform/amiga/$(AMIGA_OSDIR) AMIGA_INCLUDES = $(INCLUDES) $(AMIGA_PLATFORM_INCLUDES) \ $(if $(NDK_PATH),-I$(NDK_PATH)) +# codesets.library headers, after the system's own +AMIGA_INCLUDES += -idirafter 3rdparty/libcodesets/include # ---- optional 3rdparty submodules ---- @@ -153,8 +155,11 @@ AMIGA_CPUFLAGS ?= -mcpu=powerpc # and the kickstart loader expects plain PT_LOAD program headers only. AMIGA_SYSFLAGS ?= -mcrt=$(AMIGA_CRT) -fno-asynchronous-unwind-tables AMIGA_WARNFLAGS = +# No lookup of the system's character set: on AmigaOS 4 requests reach +# the handler as calls, not as packets it could catch. AMIGA_DEFS = -DAMIGA -D__USE_INLINE__ -D__USE_BASETYPE__ \ - -DODFS_FEATURE_CACHE_RETAIN=0 + -DODFS_FEATURE_CACHE_RETAIN=0 \ + -DODFS_FEATURE_CHARSET_EXTERNAL=0 LDFLAGS = $(AMIGA_SYSFLAGS) $(LTO) # Keep OS4 library/interface ownership explicit in os4/sys_compat.c. # Do not add -lauto to the handler link. @@ -217,8 +222,12 @@ CORE_SRCS = $(ODFS_CORE_SRCS) # Host-only sources HOST_SRCS = platform/host/file_media.c -# Amiga handler sources -AMIGA_SRCS = platform/amiga/handler_main.c \ +# Amiga handler sources. The linker places handler_main.o and the +# objects after it in front of the core, so that code added to them moves +# every routine of the core. system_charset.c is listed first to end up +# behind the core and the backends instead. +AMIGA_SRCS = platform/amiga/system_charset.c \ + platform/amiga/handler_main.c \ platform/amiga/printf_local.c \ platform/amiga/$(AMIGA_OSDIR)/sys_compat.c ifeq ($(AMIGA_TARGET),os4) diff --git a/README.md b/README.md index bd6fd6d..8c273d3 100644 --- a/README.md +++ b/README.md @@ -190,9 +190,16 @@ To prefer HFS or HFS+, use `Control = "HFSFIRST"`. Options can be combined, for ### Character sets Names from Joliet, UDF, HFS and HFS+ volumes, and Rock Ridge names written in -UTF-8, are converted to ISO 8859-1 (Latin-1), the character set of AmigaOS. A -character that Latin-1 does not have is shown as `_`. Rock Ridge names that -already are in an 8-bit character set are left as they are. +UTF-8, are converted to the 8-bit character set of the system. A character +that set does not have is shown as `_`. Rock Ridge names that already are in +an 8-bit character set are left as they are. + +The character set is ISO 8859-1 (Latin-1), unless `codesets.library` is +installed. On AmigaOS 3 and AROS the handler then asks it, the first time the +drive is used, for the system's default character set: a system set up for +Polish gets names in ISO 8859-2, for example. Names stay in Latin-1 in the ROM +build, on AmigaOS 4, and when the system was booted from the disc in the +drive. ### Name collisions diff --git a/include/odfs/config.h b/include/odfs/config.h index c2b468b..6a96b6d 100644 --- a/include/odfs/config.h +++ b/include/odfs/config.h @@ -108,8 +108,9 @@ * BUILTIN: convert names from UTF-8 to Latin-1, see odfs/charset.h. * COMPOSE: on the way, merge a letter and a combining accent that * follows it. HFS+ stores every name decomposed like that. - * EXTERNAL: convert to another 8-bit character set instead, given its - * map. + * EXTERNAL: convert to another 8-bit character set instead when the + * mount hands in its map. The Amiga handler asks + * codesets.library for the map of the system's set. */ #ifdef ODFS_PROFILE_ROM #define ODFS_FEATURE_CHARSET_BUILTIN 1 @@ -118,7 +119,9 @@ #else #define ODFS_FEATURE_CHARSET_BUILTIN 1 #define ODFS_FEATURE_CHARSET_COMPOSE 1 - #define ODFS_FEATURE_CHARSET_EXTERNAL 1 + #ifndef ODFS_FEATURE_CHARSET_EXTERNAL + #define ODFS_FEATURE_CHARSET_EXTERNAL 1 + #endif #endif /* ---------- cache sizing defaults ---------- */ diff --git a/platform/amiga/aros/amiga_target_compat.h b/platform/amiga/aros/amiga_target_compat.h index f9c2f57..304030e 100644 --- a/platform/amiga/aros/amiga_target_compat.h +++ b/platform/amiga/aros/amiga_target_compat.h @@ -17,6 +17,9 @@ struct ExecBase; extern struct ExecBase *SysBase; +/* the process that loads libraries and devices from disk */ +#define ODFS_AMIGA_LOADER_TASK "Lib & Dev Loader Daemon" + #ifndef ACTION_SHUTDOWN #define ACTION_SHUTDOWN 3000 #endif diff --git a/platform/amiga/handler.h b/platform/amiga/handler.h index 116f8c1..f588a4d 100644 --- a/platform/amiga/handler.h +++ b/platform/amiga/handler.h @@ -209,6 +209,21 @@ typedef struct handler_global { struct MinList fhlist; /* active file handles */ struct MinList volumes; /* current and stale volumes */ ULONG next_volume_id;/* next volume generation */ + +#if ODFS_FEATURE_CHARSET_EXTERNAL + /* + * The system's character set, see system_charset.c. Kept at the + * end so that it moves no field the packet path uses. + */ + odfs_amiga_interrupt_t names_int; /* runs for the first packet */ + struct Task *names_task; /* the handler, for names_int */ + const struct Node *names_msg; /* the packet it ran for */ + ULONG names_count; /* change count when it ran */ + uint8_t names_counted; /* names_count is valid */ + uint8_t names_state; /* how far asking has got */ + uint8_t names_mapped; /* name_map is filled in */ + uint16_t name_map[ODFS_CHARSET_MAP_SIZE]; +#endif } handler_global_t; /* ---- volume tracking ---- */ @@ -512,6 +527,11 @@ LONG odfs_handler_inhibit(handler_global_t *g, LONG state); #endif /* handler entry point (called from startup.S) */ +#if ODFS_FEATURE_CHARSET_EXTERNAL +/* see system_charset.c */ +int odfs_amiga_system_charset(handler_global_t *g); +#endif + void handler_main(void); void handler_main_startup(struct Message *startup_msg); diff --git a/platform/amiga/handler_main.c b/platform/amiga/handler_main.c index d4a8b7b..60503b5 100644 --- a/platform/amiga/handler_main.c +++ b/platform/amiga/handler_main.c @@ -6150,6 +6150,13 @@ static void mount_volume(handler_global_t *g) odfs_mount_opts_default(&opts); /* AmigaDOS shows and compares names in an 8-bit character set */ opts.name_charset = ODFS_NAMES_LATIN1; +#if ODFS_FEATURE_CHARSET_EXTERNAL + (void)odfs_amiga_system_charset(g); + if (g->names_mapped) { + opts.name_charset = ODFS_NAMES_MAPPED; + opts.name_map = g->name_map; + } +#endif #if !defined(ODFS_PROFILE_ROM) || !ODFS_PROFILE_ROM if (g->envec) @@ -6274,6 +6281,16 @@ static void unmount_volume(handler_global_t *g) if (!g->mounted) return; +#if ODFS_FEATURE_CHARSET_EXTERNAL + /* + * If it is time to ask, ask before the disc's memory is given + * back: loading the library would take it, and the caches of the + * mount that follows would end up somewhere slower. And if the + * answer changes nothing, this was no media change at all. + */ + if (odfs_amiga_system_charset(g)) + return; +#endif cancel_volume_publish_retry(g); volume = g->current_volume; diff --git a/platform/amiga/os3/amiga_target_compat.h b/platform/amiga/os3/amiga_target_compat.h index e0c54db..0a22ffb 100644 --- a/platform/amiga/os3/amiga_target_compat.h +++ b/platform/amiga/os3/amiga_target_compat.h @@ -16,6 +16,9 @@ struct ExecBase; #define SysBase (*(struct ExecBase **)4UL) +/* the process that loads libraries and devices from disk */ +#define ODFS_AMIGA_LOADER_TASK "ramlib" + /* * OS4 V51+ shutdown packet. OS3 DOS never sends it, but accepting it * unconditionally keeps the shared packet loop free of OS conditionals. diff --git a/platform/amiga/system_charset.c b/platform/amiga/system_charset.c new file mode 100644 index 0000000..0a5ef71 --- /dev/null +++ b/platform/amiga/system_charset.c @@ -0,0 +1,306 @@ +/* + * system_charset.c - the system's character set, from codesets.library + * + * SPDX-License-Identifier: BSD-2-Clause + * + * This is in a file of its own, listed ahead of handler_main.c in the + * Makefile, to keep it out of the way: the linker places handler_main.o + * and the objects after it in front of the core, so code added there + * moves every routine of the core and the backends. + */ + +#include "handler.h" + +#if ODFS_FEATURE_CHARSET_EXTERNAL + +#include + +#include +#include +#include + +#define DOSBase (g->libs.dos) + +#include + +#include "odfs/charset.h" +#include "odfs/log.h" + +/* + * Nonzero if every directory of assign name is on another handler's + * volume, so that loading a file through it asks nothing of this one. + * A late or non-binding assign shows where it leads only once it is + * used; that and a missing assign count as no. dl is the locked list. + */ +static int assign_is_elsewhere(const handler_global_t *g, struct DosList *dl, + const char *name) +{ + const struct AssignList *al; + BPTR lock; + + dl = FindDosEntry(dl, (CONST_STRPTR)name, LDF_ASSIGNS); + if (!dl || dl->dol_Type != DLT_DIRECTORY) + return 0; + + lock = dl->dol_Lock; + al = dl->dol_misc.dol_assign.dol_List; + for (;;) { + if (!lock || + ((struct FileLock *)BADDR(lock))->fl_Task == g->dosport) + return 0; + if (!al) + return 1; + lock = al->al_Lock; + al = al->al_Next; + } +} + +/* + * Whether the volume name is plain ASCII on the disc, which reads the + * same in every character set. The backend is asked, since in the + * mount's copy a '_' may be one from the disc or stand for a letter + * Latin-1 does not have. + */ +static int volume_name_is_ascii(handler_global_t *g) +{ + char name[sizeof(g->mount.volume_name)]; + const unsigned char *p = (const unsigned char *)name; + + if (!g->mount.backend_ops || !g->mount.backend_ops->get_volume_name || + g->mount.backend_ops->get_volume_name(g->mount.backend_ctx, name, + sizeof(name)) != ODFS_OK) + return 0; + while (*p && *p < 0x80) + p++; + return *p == '\0'; +} + +/* + * Copy the system's default character set out of codesets.library, in + * the form odfs/charset.h describes. Fails if the library is not + * installed or the set is not an 8-bit one built on ASCII. + */ +static int read_codeset(uint16_t *map) +{ + struct Process *proc = (struct Process *)FindTask(NULL); + APTR window = proc->pr_WindowPtr; + struct Library *CodesetsBase; + int ok = 0; + + /* a volume that is not there is a failure, not a requester */ + proc->pr_WindowPtr = (APTR)-1; + CodesetsBase = OpenLibrary((CONST_STRPTR)CODESETSNAME, CODESETSVER); + if (CodesetsBase) { + const struct codeset *cs = CodesetsFindA(NULL, NULL); + unsigned int i; + + /* "UTF-8" is in the list too, with an empty table */ + ok = cs != NULL; + for (i = 0x20; ok && i < 0x7F; i++) + ok = cs->table[i].ucs4 == i; + for (i = 0; ok && i < ODFS_CHARSET_MAP_SIZE; i++) { + ULONG ch = cs->table[ODFS_CHARSET_MAP_FIRST + i].ucs4; + + /* nothing below U+00A0: those are control codes again */ + map[i] = (ch >= 0xA0 && ch <= 0xFFFF) ? (uint16_t)ch : 0; + } + /* + * The library and its tables take some 300 KB. Have it leave + * memory again when its last user closes it, which is now + * unless a program has it open. + */ + Forbid(); + RemLibrary(CodesetsBase); + CloseLibrary(CodesetsBase); + Permit(); + } + proc->pr_WindowPtr = window; + + return ok; +} + +/* + * The character set is asked for when the first packet comes in. This + * is not done at startup: a handler that Mount starts from + * DEVS:DOSDrivers runs before the locale preferences are loaded, and + * codesets.library keeps the default it works out when it is first + * opened, for every program that opens it later. + * + * The packet loop must not notice. A test per packet there costs time + * that CDBench shows, and so does any other change to that function: + * the compiler then lays the whole loop out anew. So: + * + * - mount_volume() calls odfs_amiga_system_charset() every time. The + * first call, at startup, turns the packet port into one that runs + * a software interrupt. + * - The interrupt runs for every packet until one asks about the + * disc. Then it turns the port back, and makes the handler believe + * that the disc may have been changed, which mounts it again before + * the packet is taken. + * - unmount_volume() calls odfs_amiga_system_charset() too, and that + * call asks. If the disc is the same and its volume name does not + * depend on the answer, it tells unmount_volume() to leave the disc + * mounted. + */ +enum { + NAMES_UNASKED, /* nothing done yet */ + NAMES_AWAITED, /* waiting for the first packet */ + NAMES_DUE, /* ask at the next mount */ + NAMES_DONE +}; + +static LONG first_packet(APTR data) +{ + handler_global_t *g = data; + struct MsgPort *port = g->dosport; + ULONG sigs = (ULONG)1 << port->mp_SigBit; + const struct Node *node; + + if (g->names_state != NAMES_AWAITED) + return 0; + + /* + * ACTION_IS_FILESYSTEM asks nothing about the disc, so it does not + * count. Workbench sends it first, with the DOS list locked, and + * the disc's volume node cannot be taken out of the list then. + */ + for (node = port->mp_MsgList.lh_Head; node->ln_Succ; + node = node->ln_Succ) { + const struct DosPacket *pkt = + (const struct DosPacket *)(uintptr_t)node->ln_Name; + + if (pkt->dp_Type != ACTION_IS_FILESYSTEM) { + g->names_state = NAMES_DUE; + g->names_msg = node; + port->mp_SigTask = g->names_task; + port->mp_Flags = (UBYTE)((port->mp_Flags & ~PF_ACTION) | + PA_SIGNAL); + /* handle_media_change() mounts again if it cannot tell */ + g->names_count = g->change_count; + g->names_counted = (uint8_t)(g->change_count_valid != 0); + g->change_count_valid = 0; + sigs |= (ULONG)1 << g->chgsigbit; + break; + } + } + Signal(g->names_task, sigs); + return 0; +} + +/* + * Find out which character set the system uses, so that names are + * converted to it instead of to Latin-1. Returns nonzero if the disc + * can stay mounted: see above. + * + * The library comes from disk, and the handler waits here while the + * system's loader process fetches it. The loader must not in turn wait + * for this handler. So names stay in Latin-1 if a packet of the + * loader's own is here, or if a directory the loader searches is on + * this handler's volume or cannot be told: the boot volume, LIBS:, or + * DEVS:, from where a request the loader has queued may be loading a + * device. + */ +int odfs_amiga_system_charset(handler_global_t *g) +{ + struct MsgPort *port = g->dosport; + const struct Node *node; + struct DosList *dl; + int safe = 1; + int same; + + if (g->names_state == NAMES_UNASKED) { + /* without a media change signal there is no way back here */ + g->names_state = NAMES_DONE; + if (g->chgsigbit < 0) + return 0; + + g->names_task = port->mp_SigTask; + odfs_amiga_init_interrupt(&g->names_int, "odfs-firstpacket", g, + first_packet); + Disable(); + if (!port->mp_MsgList.lh_Head->ln_Succ) { + g->names_state = NAMES_AWAITED; + port->mp_SoftInt = &g->names_int.intr; + port->mp_Flags = (UBYTE)((port->mp_Flags & ~PF_ACTION) | + PA_SOFTINT); + } + Enable(); + /* else a packet is here already: ask now */ + if (g->names_state == NAMES_AWAITED) + return 0; + } else if (g->names_state != NAMES_DUE) { + return 0; + } + g->names_state = NAMES_DONE; + + /* + * Is the loader waiting for one of the packets that are here? And + * is the packet the interrupt ran for still among them? If it has + * been taken, this is not the pass the interrupt asked for, and + * the unmount is somebody's wish. + */ + same = 0; + Forbid(); + for (node = port->mp_MsgList.lh_Head; node->ln_Succ; + node = node->ln_Succ) { + const struct MsgPort *reply = + ((const struct DosPacket *)(uintptr_t)node->ln_Name)->dp_Port; + + if (node == g->names_msg) + same = 1; + if (reply && (reply->mp_Flags & PF_ACTION) == PA_SIGNAL) { + const struct Task *waiter = reply->mp_SigTask; + + if (waiter && waiter->tc_Node.ln_Name && + strcmp(waiter->tc_Node.ln_Name, + ODFS_AMIGA_LOADER_TASK) == 0) + safe = 0; + } + } + Permit(); + + if (safe) { + /* + * Not AttemptLockDosList(): it fails while anyone else has + * the list, even for reading as Workbench does around its + * first packet. Waiting for a read lock is what the loader + * does too. + */ + dl = LockDosList(LDF_ASSIGNS | LDF_READ); + safe = assign_is_elsewhere(g, dl, "SYS") && + assign_is_elsewhere(g, dl, "LIBS") && + assign_is_elsewhere(g, dl, "DEVS"); + UnLockDosList(LDF_ASSIGNS | LDF_READ); + } + + /* a different disc than when the interrupt ran is mounted anew */ + if (!g->names_counted || !g->change_count_valid || + g->change_count != g->names_count) + same = 0; + + if (safe && read_codeset(g->name_map)) { + ODFS_INFO(&g->log, ODFS_SUB_CORE, + "names follow the system's character set"); + g->names_mapped = 1; + + /* + * The disc was mounted for Latin-1, but none of its names has + * been handed out. If its volume name is plain ASCII, switching + * the set is all it takes. + */ + if (same && (!g->mounted || + g->mount.cache.names.charset != ODFS_NAMES_LATIN1 || + !volume_name_is_ascii(g))) + same = 0; + if (same) { + g->mount.opts.name_charset = ODFS_NAMES_MAPPED; + g->mount.opts.name_map = g->name_map; + g->mount.cache.names.charset = ODFS_NAMES_MAPPED; + g->mount.cache.names.map = g->name_map; + } + } + + return same; +} + +#endif /* ODFS_FEATURE_CHARSET_EXTERNAL */ From 0a4a41f54c04948a02ab3c7692bb2f8f58b58d05 Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Wed, 25 Nov 2026 09:34:13 -0800 Subject: [PATCH 4/4] os4: take the system's character set from codesets.library too On AmigaOS 4 requests reach the handler as calls into its vector port, made in the caller's context, so there is no packet for a software interrupt to catch. The first call that concerns the disc does what the interrupt does elsewhere: it marks the change count as unknown and signals a media change. Then it waits, with the filesystem semaphore released, until the handler process has been through that pass, where the library is asked as on AmigaOS 3. FSFileSystemAttr does not count as such a call. DOS makes it from the startup script's process at boot, before the locale is loaded. Every other call during a boot of AmigaOS 4.1 FE comes from Workbench. A handler does not shut down while a caller is waiting like this. The checks of the assigns and of who is waiting need no code of their own: dos/obsolete.h maps the names of the AmigaOS 3 DOS list to this one, and the loader is a process called ramlib here too. --- Makefile | 5 +- README.md | 9 +- platform/amiga/handler.h | 19 +- platform/amiga/handler_main.c | 4 + platform/amiga/os4/amiga_target_compat.h | 3 + platform/amiga/os4/vector_guard.h | 5 + platform/amiga/os4/vector_port.c | 22 ++- platform/amiga/system_charset.c | 217 ++++++++++++++++------- 8 files changed, 205 insertions(+), 79 deletions(-) diff --git a/Makefile b/Makefile index 164b70a..f80b067 100644 --- a/Makefile +++ b/Makefile @@ -155,11 +155,8 @@ AMIGA_CPUFLAGS ?= -mcpu=powerpc # and the kickstart loader expects plain PT_LOAD program headers only. AMIGA_SYSFLAGS ?= -mcrt=$(AMIGA_CRT) -fno-asynchronous-unwind-tables AMIGA_WARNFLAGS = -# No lookup of the system's character set: on AmigaOS 4 requests reach -# the handler as calls, not as packets it could catch. AMIGA_DEFS = -DAMIGA -D__USE_INLINE__ -D__USE_BASETYPE__ \ - -DODFS_FEATURE_CACHE_RETAIN=0 \ - -DODFS_FEATURE_CHARSET_EXTERNAL=0 + -DODFS_FEATURE_CACHE_RETAIN=0 LDFLAGS = $(AMIGA_SYSFLAGS) $(LTO) # Keep OS4 library/interface ownership explicit in os4/sys_compat.c. # Do not add -lauto to the handler link. diff --git a/README.md b/README.md index 8c273d3..75472a3 100644 --- a/README.md +++ b/README.md @@ -195,11 +195,10 @@ that set does not have is shown as `_`. Rock Ridge names that already are in an 8-bit character set are left as they are. The character set is ISO 8859-1 (Latin-1), unless `codesets.library` is -installed. On AmigaOS 3 and AROS the handler then asks it, the first time the -drive is used, for the system's default character set: a system set up for -Polish gets names in ISO 8859-2, for example. Names stay in Latin-1 in the ROM -build, on AmigaOS 4, and when the system was booted from the disc in the -drive. +installed. The handler then asks it, the first time the drive is used, for the +system's default character set: a system set up for Polish gets names in +ISO 8859-2, for example. Names stay in Latin-1 in the ROM build, and when the +system was booted from the disc in the drive. ### Name collisions diff --git a/platform/amiga/handler.h b/platform/amiga/handler.h index f588a4d..7b0fcfd 100644 --- a/platform/amiga/handler.h +++ b/platform/amiga/handler.h @@ -215,12 +215,16 @@ typedef struct handler_global { * The system's character set, see system_charset.c. Kept at the * end so that it moves no field the packet path uses. */ +#if ODFS_AMIGA_OS4 + struct Task *names_waiter; /* waits in odfs_amiga_first_call() */ +#else odfs_amiga_interrupt_t names_int; /* runs for the first packet */ struct Task *names_task; /* the handler, for names_int */ const struct Node *names_msg; /* the packet it ran for */ - ULONG names_count; /* change count when it ran */ +#endif + ULONG names_count; /* change count at that request */ uint8_t names_counted; /* names_count is valid */ - uint8_t names_state; /* how far asking has got */ + uint8_t names_state; /* ODFS_NAMES_xxx below */ uint8_t names_mapped; /* name_map is filled in */ uint16_t name_map[ODFS_CHARSET_MAP_SIZE]; #endif @@ -529,7 +533,18 @@ LONG odfs_handler_inhibit(handler_global_t *g, LONG state); /* handler entry point (called from startup.S) */ #if ODFS_FEATURE_CHARSET_EXTERNAL /* see system_charset.c */ +enum { + ODFS_NAMES_UNASKED, /* nothing done yet */ + ODFS_NAMES_AWAITED, /* waiting for the first request */ + ODFS_NAMES_DUE, /* ask at the next mount or unmount */ + ODFS_NAMES_DONE +}; + int odfs_amiga_system_charset(handler_global_t *g); +#if ODFS_AMIGA_OS4 +void odfs_amiga_first_call(handler_global_t *g); +void odfs_amiga_media_pass_done(handler_global_t *g); +#endif #endif void handler_main(void); diff --git a/platform/amiga/handler_main.c b/platform/amiga/handler_main.c index 60503b5..bd14273 100644 --- a/platform/amiga/handler_main.c +++ b/platform/amiga/handler_main.c @@ -6978,6 +6978,10 @@ void handler_main_startup(struct Message *startup_msg) /* re-init media adapter after remount */ g->media_ctx.g = g; } +#if ODFS_AMIGA_OS4 && ODFS_FEATURE_CHARSET_EXTERNAL + if (sigs & chgsig) + odfs_amiga_media_pass_done(g); +#endif /* DOS packets */ if (sigs & dossig) { diff --git a/platform/amiga/os4/amiga_target_compat.h b/platform/amiga/os4/amiga_target_compat.h index 27be916..be0b2df 100644 --- a/platform/amiga/os4/amiga_target_compat.h +++ b/platform/amiga/os4/amiga_target_compat.h @@ -16,4 +16,7 @@ */ #include +/* the process that loads libraries and devices from disk */ +#define ODFS_AMIGA_LOADER_TASK "ramlib" + #endif /* ODFS_AMIGA_TARGET_COMPAT_H */ diff --git a/platform/amiga/os4/vector_guard.h b/platform/amiga/os4/vector_guard.h index 0afc2aa..57248e9 100644 --- a/platform/amiga/os4/vector_guard.h +++ b/platform/amiga/os4/vector_guard.h @@ -37,6 +37,11 @@ static inline int odfs_os4_begin_shutdown(handler_global_t *g) (struct MinNode *)&g->locklist.mlh_Head && g->fhlist.mlh_TailPred == (struct MinNode *)&g->fhlist.mlh_Head; +#if ODFS_FEATURE_CHARSET_EXTERNAL + /* a caller in odfs_amiga_first_call() comes back for fs_sem */ + if (g->names_waiter) + idle = 0; +#endif if (idle && g->vector_port) { g->vector_port->FSV.Version = 0; g->vector_port->FSV.FSPrivate = NULL; diff --git a/platform/amiga/os4/vector_port.c b/platform/amiga/os4/vector_port.c index 089f8e5..9fb03aa 100644 --- a/platform/amiga/os4/vector_port.c +++ b/platform/amiga/os4/vector_port.c @@ -36,7 +36,7 @@ static void set_dos_error(int32 *res2, LONG err) } /* Keep the admission sequence shared by the many vector callbacks. */ -static __attribute__((noinline)) handler_global_t *vp_require_global( +static __attribute__((noinline)) handler_global_t *vp_require_handler( struct FSVP *vp, int32 *res2) { handler_global_t *g = odfs_os4_acquire_handler(vp); @@ -53,6 +53,24 @@ static __attribute__((noinline)) handler_global_t *vp_require_global( return g; } +/* + * The same for a callback that concerns the disc. The first of these + * has the system's character set asked for: see system_charset.c. + * vp_filesystem_attr() is not one of them. DOS calls it from the + * startup script's process at boot, before the locale is loaded. + */ +static __attribute__((noinline)) handler_global_t *vp_require_global( + struct FSVP *vp, int32 *res2) +{ + handler_global_t *g = vp_require_handler(vp, res2); + +#if ODFS_FEATURE_CHARSET_EXTERNAL + if (g && g->names_state == ODFS_NAMES_AWAITED) + odfs_amiga_first_call(g); +#endif + return g; +} + static void fs_unlock(handler_global_t *g) { /* drop the caller-task I/O request while this task still owns it */ @@ -827,7 +845,7 @@ static int32 vp_filesystem_attr(struct FSVP *vp, int32 *res2, struct TagItem *taglist) { - handler_global_t *g = vp_require_global(vp, res2); + handler_global_t *g = vp_require_handler(vp, res2); struct TagItem *state = taglist; struct TagItem *tag; uint32 version_buf_size; diff --git a/platform/amiga/system_charset.c b/platform/amiga/system_charset.c index 0a5ef71..b5dc60c 100644 --- a/platform/amiga/system_charset.c +++ b/platform/amiga/system_charset.c @@ -17,9 +17,15 @@ #include #include +#if ODFS_AMIGA_OS4 +/* the interface is a local variable here, not a global */ +#define __NOGLOBALIFACE__ +#endif #include +#if !ODFS_AMIGA_OS4 #define DOSBase (g->libs.dos) +#endif #include @@ -85,12 +91,22 @@ static int read_codeset(uint16_t *map) struct Process *proc = (struct Process *)FindTask(NULL); APTR window = proc->pr_WindowPtr; struct Library *CodesetsBase; +#if ODFS_AMIGA_OS4 + struct CodesetsIFace *ICodesets = NULL; +#endif int ok = 0; /* a volume that is not there is a failure, not a requester */ proc->pr_WindowPtr = (APTR)-1; CodesetsBase = OpenLibrary((CONST_STRPTR)CODESETSNAME, CODESETSVER); +#if ODFS_AMIGA_OS4 + if (CodesetsBase) + ICodesets = (struct CodesetsIFace *) + GetInterface(CodesetsBase, "main", 1, NULL); + if (ICodesets) { +#else if (CodesetsBase) { +#endif const struct codeset *cs = CodesetsFindA(NULL, NULL); unsigned int i; @@ -104,6 +120,11 @@ static int read_codeset(uint16_t *map) /* nothing below U+00A0: those are control codes again */ map[i] = (ch >= 0xA0 && ch <= 0xFFFF) ? (uint16_t)ch : 0; } +#if ODFS_AMIGA_OS4 + DropInterface((struct Interface *)ICodesets); +#endif + } + if (CodesetsBase) { /* * The library and its tables take some 300 KB. Have it leave * memory again when its last user closes it, which is now @@ -120,15 +141,16 @@ static int read_codeset(uint16_t *map) } /* - * The character set is asked for when the first packet comes in. This - * is not done at startup: a handler that Mount starts from - * DEVS:DOSDrivers runs before the locale preferences are loaded, and - * codesets.library keeps the default it works out when it is first + * The character set is asked for at the first request that concerns + * the disc. This is not done at startup: a handler that Mount starts + * from DEVS:DOSDrivers runs before the locale preferences are loaded, + * and codesets.library keeps the default it works out when it is first * opened, for every program that opens it later. * - * The packet loop must not notice. A test per packet there costs time - * that CDBench shows, and so does any other change to that function: - * the compiler then lays the whole loop out anew. So: + * On AmigaOS 3 and AROS the packet loop must not notice. A test per + * packet there costs time that CDBench shows, and so does any other + * change to that function: the compiler then lays the whole loop out + * anew. So: * * - mount_volume() calls odfs_amiga_system_charset() every time. The * first call, at startup, turns the packet port into one that runs @@ -141,13 +163,53 @@ static int read_codeset(uint16_t *map) * call asks. If the disc is the same and its volume name does not * depend on the answer, it tells unmount_volume() to leave the disc * mounted. + * + * On AmigaOS 4 requests are calls in the caller's context. The first + * one, odfs_amiga_first_call(), sends the handler process down the + * same path and waits for it. */ -enum { - NAMES_UNASKED, /* nothing done yet */ - NAMES_AWAITED, /* waiting for the first packet */ - NAMES_DUE, /* ask at the next mount */ - NAMES_DONE -}; + +/* the handler acts as if the disc may have been changed */ +static void force_media_pass(handler_global_t *g) +{ + g->names_state = ODFS_NAMES_DUE; + g->names_count = g->change_count; + g->names_counted = (uint8_t)(g->change_count_valid != 0); + /* handle_media_change() mounts again if it cannot tell */ + g->change_count_valid = 0; +} + +#if ODFS_AMIGA_OS4 + +void odfs_amiga_first_call(handler_global_t *g) +{ + struct Task *me = FindTask(NULL); + + if (me == g->handler_task || g->inhibited || g->names_waiter) + return; + + ODFS_INFO(&g->log, ODFS_SUB_CORE, "names: first call, from %s", + me->tc_Node.ln_Name ? me->tc_Node.ln_Name : "?"); + force_media_pass(g); + g->names_waiter = me; + SetSignal(0, SIGF_SINGLE); + ReleaseSemaphore(&g->fs_sem); + Signal(g->handler_task, (ULONG)1 << g->chgsigbit); + Wait(SIGF_SINGLE); + ObtainSemaphore(&g->fs_sem); +} + +void odfs_amiga_media_pass_done(handler_global_t *g) +{ + struct Task *waiter = g->names_waiter; + + if (waiter) { + g->names_waiter = NULL; + Signal(waiter, SIGF_SINGLE); + } +} + +#else /* !ODFS_AMIGA_OS4 */ static LONG first_packet(APTR data) { @@ -156,7 +218,7 @@ static LONG first_packet(APTR data) ULONG sigs = (ULONG)1 << port->mp_SigBit; const struct Node *node; - if (g->names_state != NAMES_AWAITED) + if (g->names_state != ODFS_NAMES_AWAITED) return 0; /* @@ -170,15 +232,11 @@ static LONG first_packet(APTR data) (const struct DosPacket *)(uintptr_t)node->ln_Name; if (pkt->dp_Type != ACTION_IS_FILESYSTEM) { - g->names_state = NAMES_DUE; + force_media_pass(g); g->names_msg = node; port->mp_SigTask = g->names_task; port->mp_Flags = (UBYTE)((port->mp_Flags & ~PF_ACTION) | PA_SIGNAL); - /* handle_media_change() mounts again if it cannot tell */ - g->names_count = g->change_count; - g->names_counted = (uint8_t)(g->change_count_valid != 0); - g->change_count_valid = 0; sigs |= (ULONG)1 << g->chgsigbit; break; } @@ -187,6 +245,49 @@ static LONG first_packet(APTR data) return 0; } +#endif /* !ODFS_AMIGA_OS4 */ + +/* + * Is the system's loader waiting for this handler, and is the request + * that asked for this pass still waiting? If that one has been taken, + * this is not the pass it asked for, and an unmount is somebody's wish. + */ +static int loader_is_waiting(handler_global_t *g, int *pending) +{ + int loader = 0; +#if ODFS_AMIGA_OS4 + const struct Task *waiter = g->names_waiter; + + *pending = waiter != NULL; + if (waiter && waiter->tc_Node.ln_Name && + strcmp(waiter->tc_Node.ln_Name, ODFS_AMIGA_LOADER_TASK) == 0) + loader = 1; +#else + const struct Node *node; + + *pending = 0; + Forbid(); + for (node = g->dosport->mp_MsgList.lh_Head; node->ln_Succ; + node = node->ln_Succ) { + const struct MsgPort *reply = + ((const struct DosPacket *)(uintptr_t)node->ln_Name)->dp_Port; + + if (node == g->names_msg) + *pending = 1; + if (reply && (reply->mp_Flags & PF_ACTION) == PA_SIGNAL) { + const struct Task *waiter = reply->mp_SigTask; + + if (waiter && waiter->tc_Node.ln_Name && + strcmp(waiter->tc_Node.ln_Name, + ODFS_AMIGA_LOADER_TASK) == 0) + loader = 1; + } + } + Permit(); +#endif + return loader; +} + /* * Find out which character set the system uses, so that names are * converted to it instead of to Latin-1. Returns nonzero if the disc @@ -194,71 +295,52 @@ static LONG first_packet(APTR data) * * The library comes from disk, and the handler waits here while the * system's loader process fetches it. The loader must not in turn wait - * for this handler. So names stay in Latin-1 if a packet of the - * loader's own is here, or if a directory the loader searches is on + * for this handler. So names stay in Latin-1 if a request of the + * loader's own is waiting, or if a directory the loader searches is on * this handler's volume or cannot be told: the boot volume, LIBS:, or * DEVS:, from where a request the loader has queued may be loading a * device. */ int odfs_amiga_system_charset(handler_global_t *g) { - struct MsgPort *port = g->dosport; - const struct Node *node; struct DosList *dl; - int safe = 1; + int safe; int same; - if (g->names_state == NAMES_UNASKED) { + if (g->names_state == ODFS_NAMES_UNASKED) { /* without a media change signal there is no way back here */ - g->names_state = NAMES_DONE; + g->names_state = ODFS_NAMES_DONE; if (g->chgsigbit < 0) return 0; - g->names_task = port->mp_SigTask; - odfs_amiga_init_interrupt(&g->names_int, "odfs-firstpacket", g, - first_packet); - Disable(); - if (!port->mp_MsgList.lh_Head->ln_Succ) { - g->names_state = NAMES_AWAITED; - port->mp_SoftInt = &g->names_int.intr; - port->mp_Flags = (UBYTE)((port->mp_Flags & ~PF_ACTION) | - PA_SOFTINT); +#if ODFS_AMIGA_OS4 + g->names_state = ODFS_NAMES_AWAITED; +#else + { + struct MsgPort *port = g->dosport; + + g->names_task = port->mp_SigTask; + odfs_amiga_init_interrupt(&g->names_int, "odfs-firstpacket", + g, first_packet); + Disable(); + if (!port->mp_MsgList.lh_Head->ln_Succ) { + g->names_state = ODFS_NAMES_AWAITED; + port->mp_SoftInt = &g->names_int.intr; + port->mp_Flags = (UBYTE)((port->mp_Flags & ~PF_ACTION) | + PA_SOFTINT); + } + Enable(); } - Enable(); +#endif /* else a packet is here already: ask now */ - if (g->names_state == NAMES_AWAITED) + if (g->names_state == ODFS_NAMES_AWAITED) return 0; - } else if (g->names_state != NAMES_DUE) { + } else if (g->names_state != ODFS_NAMES_DUE) { return 0; } - g->names_state = NAMES_DONE; - - /* - * Is the loader waiting for one of the packets that are here? And - * is the packet the interrupt ran for still among them? If it has - * been taken, this is not the pass the interrupt asked for, and - * the unmount is somebody's wish. - */ - same = 0; - Forbid(); - for (node = port->mp_MsgList.lh_Head; node->ln_Succ; - node = node->ln_Succ) { - const struct MsgPort *reply = - ((const struct DosPacket *)(uintptr_t)node->ln_Name)->dp_Port; - - if (node == g->names_msg) - same = 1; - if (reply && (reply->mp_Flags & PF_ACTION) == PA_SIGNAL) { - const struct Task *waiter = reply->mp_SigTask; - - if (waiter && waiter->tc_Node.ln_Name && - strcmp(waiter->tc_Node.ln_Name, - ODFS_AMIGA_LOADER_TASK) == 0) - safe = 0; - } - } - Permit(); + g->names_state = ODFS_NAMES_DONE; + safe = !loader_is_waiting(g, &same); if (safe) { /* * Not AttemptLockDosList(): it fails while anyone else has @@ -273,7 +355,7 @@ int odfs_amiga_system_charset(handler_global_t *g) UnLockDosList(LDF_ASSIGNS | LDF_READ); } - /* a different disc than when the interrupt ran is mounted anew */ + /* a different disc than when the pass was asked for is mounted anew */ if (!g->names_counted || !g->change_count_valid || g->change_count != g->names_count) same = 0; @@ -298,6 +380,9 @@ int odfs_amiga_system_charset(handler_global_t *g) g->mount.cache.names.charset = ODFS_NAMES_MAPPED; g->mount.cache.names.map = g->name_map; } + } else { + ODFS_INFO(&g->log, ODFS_SUB_CORE, "names stay Latin-1%s", + safe ? "" : ": not safe to load a library now"); } return same;