- Project::open canonicalises the root (no \?\ prefix on Windows where safe); typed errors for missing and non-directory paths. - Lazy children(dir): dirs first, case-insensitive; kind, ignored flag and role (vendor/ library, node_modules/ excluded, .git hidden). - Ignore rules via the ignore crate: .gitignore, .ignore, .git/info/exclude, global git excludes, with git precedence. - Watcher: std-only polling (100 ms, watched dirs only) with create/modify/remove/rename events and a Failed event on root loss. notify is CC0-1.0 and fails cargo deny; owner decision pending. - read_text with a 32 MiB cap, UTF-8 + BOM, typed InvalidUtf8 and UnsupportedEncoding; write_atomic via temp file + rename, keeping permissions and refusing read-only files. - 28 tests on temp dirs (30 on Unix: permission and symlink write tests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
5.8 KiB
Rust
193 lines
5.8 KiB
Rust
//! Reading with limits and decoding; atomic writes.
|
|
|
|
#![allow(
|
|
clippy::unwrap_used,
|
|
clippy::expect_used,
|
|
clippy::panic,
|
|
clippy::indexing_slicing
|
|
)]
|
|
|
|
mod common;
|
|
|
|
use std::fs;
|
|
|
|
use common::tree;
|
|
use lutra_vfs::{VfsError, read_bytes, read_text, read_text_with_limit, write_atomic, write_text};
|
|
|
|
#[test]
|
|
fn reads_utf8_with_and_without_bom() {
|
|
let dir = tree(&[("plain.php", "<?php echo 'é';")]);
|
|
let plain = read_text(&dir.path().join("plain.php")).unwrap();
|
|
assert_eq!(plain.text, "<?php echo 'é';");
|
|
assert!(!plain.bom);
|
|
let bom_path = dir.path().join("bom.php");
|
|
fs::write(&bom_path, b"\xEF\xBB\xBF<?php").unwrap();
|
|
let bom = read_text(&bom_path).unwrap();
|
|
assert_eq!(bom.text, "<?php");
|
|
assert!(bom.bom);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_is_a_typed_error() {
|
|
let dir = tree(&[]);
|
|
let path = dir.path().join("latin1.txt");
|
|
fs::write(&path, b"caf\xE9").unwrap();
|
|
assert_eq!(
|
|
read_text(&path),
|
|
Err(VfsError::InvalidUtf8 {
|
|
path: path.clone(),
|
|
valid_up_to: 3
|
|
})
|
|
);
|
|
fs::write(&path, b"\xEF\xBB\xBFab\xFF").unwrap();
|
|
assert!(matches!(
|
|
read_text(&path),
|
|
Err(VfsError::InvalidUtf8 { valid_up_to: 5, .. })
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn utf16_is_reported_as_unsupported() {
|
|
let dir = tree(&[]);
|
|
let path = dir.path().join("wide.txt");
|
|
fs::write(&path, b"\xFF\xFEa\x00").unwrap();
|
|
assert!(matches!(
|
|
read_text(&path),
|
|
Err(VfsError::UnsupportedEncoding {
|
|
encoding: "UTF-16LE",
|
|
..
|
|
})
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn reads_are_capped() {
|
|
let dir = tree(&[("big.txt", "0123456789")]);
|
|
let path = dir.path().join("big.txt");
|
|
assert_eq!(read_text_with_limit(&path, 10).unwrap().text, "0123456789");
|
|
assert_eq!(
|
|
read_text_with_limit(&path, 9),
|
|
Err(VfsError::TooLarge {
|
|
path: path.clone(),
|
|
size: 10,
|
|
limit: 9
|
|
})
|
|
);
|
|
assert!(matches!(
|
|
read_bytes(&dir.path().join("missing"), 10),
|
|
Err(VfsError::NotFound { .. })
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn reading_a_directory_fails_cleanly() {
|
|
let dir = tree(&[("sub/", "")]);
|
|
assert!(read_text(&dir.path().join("sub")).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn atomic_write_replaces_contents_and_leaves_no_temp_files() {
|
|
let dir = tree(&[("a.php", "old contents that are longer")]);
|
|
let path = dir.path().join("a.php");
|
|
write_atomic(&path, b"new").unwrap();
|
|
assert_eq!(fs::read(&path).unwrap(), b"new");
|
|
write_text(&dir.path().join("created.php"), "x", true).unwrap();
|
|
assert_eq!(
|
|
fs::read(dir.path().join("created.php")).unwrap(),
|
|
b"\xEF\xBB\xBFx"
|
|
);
|
|
let mut names: Vec<_> = fs::read_dir(dir.path())
|
|
.unwrap()
|
|
.map(|e| e.unwrap().file_name().into_string().unwrap())
|
|
.collect();
|
|
names.sort();
|
|
assert_eq!(names, ["a.php", "created.php"]);
|
|
}
|
|
|
|
#[test]
|
|
fn atomic_write_refuses_read_only_files_and_directories() {
|
|
let dir = tree(&[("locked.php", "keep"), ("sub/", "")]);
|
|
let path = dir.path().join("locked.php");
|
|
let mut perms = fs::metadata(&path).unwrap().permissions();
|
|
perms.set_readonly(true);
|
|
fs::set_permissions(&path, perms.clone()).unwrap();
|
|
assert!(matches!(
|
|
write_atomic(&path, b"lost"),
|
|
Err(VfsError::ReadOnly { .. })
|
|
));
|
|
assert_eq!(fs::read(&path).unwrap(), b"keep");
|
|
#[allow(clippy::permissions_set_readonly_false)]
|
|
perms.set_readonly(false);
|
|
fs::set_permissions(&path, perms).unwrap();
|
|
assert!(matches!(
|
|
write_atomic(&dir.path().join("sub"), b"x"),
|
|
Err(VfsError::NotAFile { .. })
|
|
));
|
|
assert!(matches!(
|
|
write_atomic(&dir.path().join("missing").join("a.php"), b"x"),
|
|
Err(VfsError::NotFound { .. })
|
|
));
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn atomic_write_keeps_permissions() {
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let dir = tree(&[("run.sh", "#!/bin/sh")]);
|
|
let path = dir.path().join("run.sh");
|
|
fs::set_permissions(&path, fs::Permissions::from_mode(0o751)).unwrap();
|
|
write_atomic(&path, b"#!/bin/sh\necho hi").unwrap();
|
|
let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777;
|
|
assert_eq!(mode, 0o751);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn atomic_write_through_a_symlink_keeps_the_link() {
|
|
let dir = tree(&[("real.php", "old")]);
|
|
let link = dir.path().join("link.php");
|
|
std::os::unix::fs::symlink(dir.path().join("real.php"), &link).unwrap();
|
|
write_atomic(&link, b"new").unwrap();
|
|
assert!(
|
|
fs::symlink_metadata(&link)
|
|
.unwrap()
|
|
.file_type()
|
|
.is_symlink()
|
|
);
|
|
assert_eq!(fs::read(dir.path().join("real.php")).unwrap(), b"new");
|
|
}
|
|
|
|
/// Decoding random bytes never panics, and valid UTF-8 round-trips with and
|
|
/// without a BOM. A small deterministic generator stands in for proptest.
|
|
#[test]
|
|
fn decode_never_panics_on_random_bytes() {
|
|
let mut state: u64 = 0x9E37_79B9_7F4A_7C15;
|
|
let mut next = move || {
|
|
state ^= state << 13;
|
|
state ^= state >> 7;
|
|
state ^= state << 17;
|
|
state
|
|
};
|
|
let path = std::path::Path::new("random.bin");
|
|
for _ in 0..5_000 {
|
|
let len = (next() % 64) as usize;
|
|
let mut bytes: Vec<u8> = (0..len).map(|_| next() as u8).collect();
|
|
if next() % 4 == 0 {
|
|
bytes.splice(0..0, [0xEF, 0xBB, 0xBF]);
|
|
}
|
|
match lutra_vfs::decode(path, bytes.clone()) {
|
|
Ok(file) => {
|
|
let mut again = if file.bom {
|
|
vec![0xEF, 0xBB, 0xBF]
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
again.extend_from_slice(file.text.as_bytes());
|
|
assert_eq!(again, bytes);
|
|
}
|
|
Err(VfsError::InvalidUtf8 { valid_up_to, .. }) => assert!(valid_up_to < bytes.len()),
|
|
Err(VfsError::UnsupportedEncoding { .. }) => {}
|
|
Err(other) => panic!("unexpected error {other:?}"),
|
|
}
|
|
}
|
|
}
|