mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Saving: a leftover temp file is recognised only with a file part a save can write (review)
A save's temporary file keeps at most the first 64 characters of the document's name, and older versions put one dot in front of them. A name with a longer part before the process id and counter was still recognised as a leftover, so the recovery sweep could delete someone else's file a day later. Only a part a save can write is recognised now, counted in characters as the save cuts it.
This commit is contained in:
2 files changed
+35
-1
No files matched your search
@@ -485,6 +485,9 @@ fn resolve_symlink(path: &Path) -> io::Result<PathBuf> {
|
||||
/// What every [`temp_name`] ends with.
|
||||
const TEMP_SUFFIX: &str = ".pcsave.tmp";
|
||||
|
||||
/// How many characters of the file name a [`temp_name`] keeps.
|
||||
const TEMP_STEM_CHARS: usize = 64;
|
||||
|
||||
/// A unique temporary file name to write beside `file` before renaming over it:
|
||||
/// `{file}.{pid}-{n}.pcsave.tmp`.
|
||||
///
|
||||
@@ -500,7 +503,7 @@ pub fn temp_name(file: &str) -> String {
|
||||
static COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
// Keep the name short: a long file name plus the suffix must stay under NAME_MAX.
|
||||
let stem: String = file.chars().take(64).collect();
|
||||
let stem: String = file.chars().take(TEMP_STEM_CHARS).collect();
|
||||
format!("{stem}.{}-{n}{TEMP_SUFFIX}", std::process::id())
|
||||
}
|
||||
|
||||
@@ -511,6 +514,12 @@ pub fn temp_owner(name: &str) -> Option<u32> {
|
||||
let rest = name.strip_suffix(TEMP_SUFFIX)?;
|
||||
let (stem, ids) = rest.rsplit_once('.')?;
|
||||
let (pid, n) = ids.split_once('-')?;
|
||||
// Only a file part `temp_name` writes: at most `TEMP_STEM_CHARS` characters, or one more when
|
||||
// the first is the dot older versions put in front of them.
|
||||
let chars = stem.chars().count();
|
||||
if chars > TEMP_STEM_CHARS && !(chars == TEMP_STEM_CHARS + 1 && stem.starts_with('.')) {
|
||||
return None;
|
||||
}
|
||||
let stem = stem.strip_prefix('.').unwrap_or(stem);
|
||||
// Only numbers `temp_name` writes: plain decimal digits with no leading zero (a lone "0" is
|
||||
// fine), a `u32` pid and the `u64` counter it counts with. Any other name is someone else's
|
||||
|
||||
@@ -740,3 +740,28 @@ fn temp_owner_reads_both_name_forms_and_nothing_else() {
|
||||
assert_eq!(temp_owner(other), None, "{other}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A recovery folder can hold someone else's file that only looks like a temp name. `temp_name`
|
||||
/// keeps at most 64 characters (not bytes) of the file name, and older versions put one dot in
|
||||
/// front of them, so a longer file part is not a leftover of a save and the sweep must keep it.
|
||||
#[test]
|
||||
fn temp_owner_takes_only_a_file_part_temp_name_writes() {
|
||||
let pid = Some(std::process::id());
|
||||
for file in ["a".repeat(200), "🚀".repeat(70), format!(".{}", "a".repeat(100))] {
|
||||
assert_eq!(temp_owner(&temp_name(&file)), pid, "{file}");
|
||||
}
|
||||
let today = |stem: &str| format!("{stem}.42-7.pcsave.tmp");
|
||||
let older = |stem: &str| format!(".{stem}.42-7.pcsave.tmp");
|
||||
for c in ["a", "é", "🚀"] {
|
||||
let (at, over) = (c.repeat(64), c.repeat(65));
|
||||
assert_eq!(temp_owner(&today(&at)), Some(42), "64 characters: {c}");
|
||||
assert_eq!(temp_owner(&older(&at)), Some(42), "64 characters after the old dot: {c}");
|
||||
assert_eq!(temp_owner(&today(&over)), None, "65 characters: {c}");
|
||||
assert_eq!(temp_owner(&older(&over)), None, "65 characters after the old dot: {c}");
|
||||
// A dot file's own dot counts in its 64 characters; the old form adds one more dot.
|
||||
let dotted = |k: usize| format!(".{}", c.repeat(k));
|
||||
assert_eq!(temp_owner(&today(&dotted(63))), Some(42), "a dot file today: {c}");
|
||||
assert_eq!(temp_owner(&older(&dotted(63))), Some(42), "a dot file in the old form: {c}");
|
||||
assert_eq!(temp_owner(&older(&dotted(64))), None, "a dot file one character over: {c}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user