Scripts: refused writes create no file; Linux reads the casefold flag

A refused rename that falls back to copying into the target no longer
creates a missing target before the final authorization check. The
target is created only after that check, on every platform, so a
refusal at the final check leaves the path absent. An existing target
is still opened with O_CREAT, so Linux's fs.protected_regular applies.

imageCapture no longer treats an empty Linux folder as ignoring the
Unicode normalization form. It reads the folder's casefold inode flag
(FS_IOC_GETFLAGS) instead, so a missing linked cafe\u{301}.png no longer
matches cafe\u{e9}.png in an ordinary folder, and a casefolded folder
still matches both spellings.
This commit is contained in:
dotCooCoo committed 2026-12-03 02:35:18 -08:00
1 parent 4e90d6c211
commit ade0e47f48
2 files changed
+86 -15

No files matched your search

+53 -2
View File
@@ -347,7 +347,8 @@ fn same_file(a: &Placed, b: &Placed) -> bool {
/// decomposed (NFD) forms, and that is not a symbolic link, is looked up in the other form, and
/// names ignore the form when that finds the same entry. With no such entry, names ignore the form
/// on macOS, whose volumes (APFS and HFS+) look names up that way, and on Linux in a folder that
/// ignores letter case (ext4 and f2fs casefolding also ignore the form), and keep it elsewhere.
/// carries the casefold inode flag ([`casefolded`]): ext4 and f2fs casefolding also ignores the
/// form. Elsewhere, and on Linux when the flag can't be read, names keep the form.
fn ignores_normalization(folder: &std::path::Path) -> bool {
use unicode_normalization::UnicodeNormalization;
for entry in std::fs::read_dir(folder).into_iter().flatten().flatten().take(256) {
@@ -358,7 +359,31 @@ fn ignores_normalization(folder: &std::path::Path) -> bool {
return finds_same(&entry.path(), &folder.join(other));
}
}
cfg!(target_os = "macos") || (cfg!(target_os = "linux") && ignores_case(folder))
#[cfg(target_os = "linux")]
{
casefolded(folder)
}
#[cfg(not(target_os = "linux"))]
{
cfg!(target_os = "macos")
}
}
/// `FS_CASEFOLD_FL` from Linux's `include/uapi/linux/fs.h`: the folder looks names up casefolded.
#[cfg(target_os = "linux")]
const FS_CASEFOLD_FL: u32 = 0x4000_0000;
/// Whether the Linux folder `folder` carries the casefold inode flag, read with
/// `FS_IOC_GETFLAGS`. `false` when the folder can't be opened or its flags can't be read.
#[cfg(target_os = "linux")]
fn casefolded(folder: &std::path::Path) -> bool {
std::fs::File::open(folder).ok().and_then(|d| rustix::fs::ioctl_getflags(&d).ok()).is_some_and(|f| has_casefold(f.bits()))
}
/// Whether the inode flags `bits` include `FS_CASEFOLD_FL`.
#[cfg(target_os = "linux")]
fn has_casefold(bits: u32) -> bool {
bits & FS_CASEFOLD_FL != 0
}
/// The check `imageCapture` makes on the file it writes, `p`, given as written or as the real path
@@ -640,4 +665,30 @@ mod source_tests {
assert!(same_file(&target, &source));
});
}
/// In an empty Linux folder, names that differ only in their normalization form are one file
/// only when the folder is casefolded. On a folder without the flag they are distinct, and
/// nothing is written into the folder to find out.
#[cfg(target_os = "linux")]
#[test]
fn names_in_another_normalization_form_match_only_in_a_casefolded_empty_folder() {
let dir = vectorcraft_testkit::TempDir::new("capture-nfd-distinct");
let folder = std::fs::canonicalize(&*dir).unwrap();
let (linked, capture): (Placed, Placed) = ((folder.clone(), "cafe\u{301}.png".into()), (folder.clone(), "caf\u{e9}.png".into()));
assert_eq!(same_file(&capture, &linked), casefolded(&folder));
assert_eq!(std::fs::read_dir(&folder).unwrap().count(), 0);
}
/// A casefolded folder, which looks a name up in either normalization form, is recognized by
/// its inode flag, and a folder that can't be read is not taken for one.
#[cfg(target_os = "linux")]
#[test]
fn a_casefolded_folder_is_recognized_by_its_inode_flag() {
assert!(has_casefold(FS_CASEFOLD_FL));
assert!(has_casefold(FS_CASEFOLD_FL | 0x10));
assert!(!has_casefold(0x10));
assert!(!has_casefold(0));
let dir = vectorcraft_testkit::TempDir::new("capture-nfd-missing");
assert!(!casefolded(&dir.join("missing")));
}
}