Expand description
NsPath: a validated namespace path, and the portable name grammar every backend enforces through it.
The grammar is one deliberately strict subset of what Linux, macOS and Windows each allow. The point is that a name accepted here is legal, unambiguous and round-trippable on every platform, so “it worked on my Linux box and then collided on the Mac” is refused at the namespace, not discovered at a backend. It is intentionally stricter than any single OS: the rules below are not “what the host would accept”.
A path is absolute (/a/b), /-separated, and the root is /. There is no relative form, no ./.., no //, no
trailing /. A name is refused (never silently repaired) when it:
- is empty,
.or..; - is not already in Unicode NFC (the namespace never normalises, because normalising silently changes which entry a caller addressed);
- contains a control character (
char::is_control, so C0, DEL and C1), a format character from the fixed list inis_format_character(bidirectional controls, zero-width spaces, the soft hyphen, the byte-order mark: all invisible or reordering characters),\,:or any of<>"|?*; - ends in a dot or a space (Windows strips them, so the name would not round-trip);
- is a Windows reserved device name (
CON PRN AUX NUL COM1-9 LPT1-9, plusCONIN$,CONOUT$and the superscript digit forms), case-insensitively, with or without an extension (con.txtis as reserved asCON); - is longer than
MAX_NAME_BYTES(255) UTF-8 bytes, or the whole path is longer thanMAX_PATH_BYTES(4096); - begins with
TEMP_PREFIX(compared case-folded, so.SKNS-TMP-on a case-insensitive volume is refused too), whichHostDirreserves for its atomic-replace temporary files.
Why the format characters: a name is something a person reads (a confirmation diff of a launcher entry, a file
manager, a log line). A name that can display as something other than what it is (a right-to-left override that
reverses txt.exe into exe.txt, a zero-width space that makes two different names look identical) lets one entry
impersonate another in front of the person deciding. The list is fixed, not a Unicode category lookup, so a Unicode
update cannot silently change which names are legal. Zero-width joiner (U+200D) and non-joiner (U+200C) stay legal:
scripts such as Persian, Devanagari and emoji sequences need them.
Two names that differ only by case (or only by Unicode normalisation form) are distinct paths but cannot
coexist in one directory: creating the second is refused with CaseCollision (see case_fold_key), on every
backend, even a case-sensitive one.
Structs§
- NsPath
- A validated namespace path: absolute,
/-separated, every component inside the portable name grammar. See the module docs for the grammar.
Enums§
- Path
Reason - Why a path or name was refused. Carried by
NsErrorKind::InvalidPath.
Constants§
- MAX_
NAME_ BYTES - Longest single path component, in UTF-8 bytes. Matches the limit of ext4, APFS, NTFS and most others.
- MAX_
PATH_ BYTES - Longest whole path, in UTF-8 bytes, separators included.
- TEMP_
PREFIX - The prefix
HostDirgives its temporary files while it does an atomic replace.
Functions§
- case_
fold_ key - The key under which two names count as “the same name differing only by case or normalisation form”.
- is_
format_ character - The characters the grammar refuses as format characters: a fixed list, deliberately not a Unicode category.
- validate_
name - Check one component against the portable grammar. This is the single validator:
NsPathuses it, and a backend that reads names back from a host directory uses it to decide which entries are addressable at all.