FAQ - Help and questions

Are file names case-sensitive on Windows and macOS, and how should my app compare them?

#paths#windows#macos#filespublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Windows file systems normally ignore case, so Readme.TXT and readme.txt are one file, and the macOS default volume format also ignores case but can be set up to respect it. Do not compare names with a plain string equality to decide whether two paths are the same file. The Windows result below was run.

Checked with rustc 1.93.0, Windows 11; compiled and run on an ordinary Windows volume. The macOS statement is from Apple's published documentation and was not run on a Mac.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Whether readme.txt finds Readme.TXT depends on the file system, not on Rust:

  • Windows (NTFS): names are case-preserving but compared without regard to case by default, so both spellings open the same file. Windows can mark individual folders case-sensitive, so this is a default, not a law.
  • macOS: Apple's file system, APFS, is case-insensitive by default and can be formatted case-sensitive. A user can have either kind of volume.
  • Linux (relevant when you ship to it too): usually case-sensitive.

This program writes Readme.TXT and opens it as readme.txt:

src/main.rsrust
use std::fs;

fn main() -> std::io::Result<()> {
    let dir = std::env::temp_dir().join("rs-forum-case-demo");
    fs::create_dir_all(&dir)?;
    fs::write(dir.join("Readme.TXT"), "hello")?;
    // Same file on a case-insensitive file system; NotFound on a case-sensitive one.
    match fs::read_to_string(dir.join("readme.txt")) {
        Ok(t) => println!("readme.txt found: {t}"),
        Err(e) => println!("readme.txt: {:?}", e.kind()),
    }
    fs::remove_dir_all(&dir)
}
text
readme.txt found: hello

What it means for your code:

  1. Do not decide "same file" with string equality. Two spellings can be one file, and on a case-sensitive volume two files that differ only by case can exist side by side.
  2. Keep the spelling the user typed or the system returned when you store a name, so it looks right when you show it again.
  3. Test on a case-sensitive volume if you ship to Linux or to Macs set up that way. Code that loads Config.json as config.json works on your Windows machine and fails there.
  4. Do not create two names that differ only by case in your own folders (Data and data); they collide on a case-insensitive volume.

Common mistakes#

  • Hard-coding a file name in a different case from the one you ship.
  • Using a case-insensitive comparison (for example eq_ignore_ascii_case) on names for security checks. It is not the same as what the file system does for non-ASCII letters.

Can I ask the system whether a volume is case-sensitive?#

Yes, but the way differs per system and is not part of std. Check the platform's own documentation if you need it.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet