in reply to Munging file name, to be safe- & usable enough on Unix-like OSen & FAT32 file system

Learned the hard way that NTFS would allow file names to be written to FAT32 even if some characters are outside of FAT32 specification.

NTFS won't write to FAT32. NTFS is a filesystem just like FAT32. And so the NTFS driver will only ever write to NTFS-formatted media, as will the (V)FAT driver only ever write to (V)FAT-formatted media.

FAT32 isn't the problem here. Windows' history is the problem explaining both the "forbidden" names (AUX, CON, PRN, NUL, ...) and the "forbidden" characters.

The first problem is due to someone during the early history of MS-DOS deciding that having a separate \DEV\ directory for Unix-style devices was not needed, and so the devices just existed in every directory, invisible and not in the filesystem, but still kind-of there. Early versions of MS-DOS still accepted the \DEV\ directory, even if it did not exist. This grew to bug-compatibility. In DOS batch files, you could use IF EXIST SOMENAME to check if a file named SOMENAME existed. But EXIST did not find directories, only files. To check if a directory existed, you had to use a bug, where EXIST finds all of the "forbidden" device names, because they kind-of exist in every existing directory. So, you use IF EXIST SOMENAME\NUL to check if a directory named SOMENAME exists.

The second problem is that ancient MS-DOS with its strictly limited filenames mostly did get the job done without proper quoting rules. Yes, it was not possible to pass <, >, and a few other characters as arguments to a program, but it was just a little bit annoying. "Long filenames" (VFAT) came and removed a lot of filename restrictions, quotes were needed, but nobody made a clear set of rules. It was a mess, and became worse and worse, as quoting and unquoting needs to be implemented by the applications and it is handled differently by different implementations. See Re^3: Having to manually escape quote character in args to "system"? for some ugly details.

If you do not need to interact with DOS/Windows, e.g. because your firmware expects a FAT filesystem for booting (UEFI-based PCs, Raspberry Pi, embedded systems, ...), you are free to use "forbidden" names and characters on a FAT filesystem. You could even make the filesystem case-sensitive or store long ("VFAT") filenames as UTF-16 instead of UCS-2. The firmware might expect some DOS compatibility, so you will probably not be able to boot your system from a file named "\" or "<". But perhaps it will boot from "AUX".

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
  • Comment on Re: Munging file name, to be safe- & usable enough on Unix-like OSen & FAT32 file system
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Munging file name, to be safe- & usable enough on Unix-like OSen & FAT32 file system
by parv (Parson) on Nov 26, 2023 at 19:54 UTC
    NTFS won't write to FAT32. NTFS is a filesystem just like FAT32. And so the NTFS driver will only ever write to NTFS-formatted media, as will the (V)FAT driver only ever write to (V)FAT-formatted media.

    Perhaps that was bad|sloppy phrasing on my part. What do you call the operation of copying files from NTFS (Windows 10) to FAT32 (Windows knows of the removable mass storage device) then?

      What do you call the operation of copying files from NTFS ... to FAT32 ... then?

      Copying. Or, if you look at the filesystem driver level, reading from NTFS and writing to VFAT.


      Regarding FAT names - see also File Allocation Table:

      FAT-12, FAT-16, FAT-32 specify the size of a FAT entry, in bits, and thus indirectly also the maximum filesystem size.

      VFAT extends the FAT by adding long filenames (everything beyond "8.3", including mixed case, spaces, and more allowed characters).

      Linux has several closely related filesystem drivers, "fat" is a common part of the "msdos", "umsdos", and "vfat" drivers. The "msdos" driver is for plain old DOS disks, "8.3" names, no extras. "umsdos" (no longer enabled by default) implements Unix file attributes and long filenames on top of a plain, DOS-compatible FAT filesystem, by storing extra data in a special file per directory. "vfat" implements FAT with long filenames à la Microsoft.

      ExFAT (as found on SDXC-Cards) and FATX (Xbox) do use a FAT, but with very incompatible structures due to the very different feature set.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)