From 7d9ce47b799ecca7747b40ab765c39d4e9984e6b Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Fri, 28 May 2021 23:20:11 -0700 Subject: [PATCH] convert-all-flts: Use source name as part of new name --- convert-all-flts/src/main.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/convert-all-flts/src/main.rs b/convert-all-flts/src/main.rs index e2eb035..2f5acf4 100644 --- a/convert-all-flts/src/main.rs +++ b/convert-all-flts/src/main.rs @@ -101,19 +101,21 @@ fn rename_flt(to_rename: &Path) -> Result { const MOVED_SUFFIX: &str = ".moved"; -// _TOCTOU: The Function_, but let's assume nothing's making a bunch of FLT files -// in the exact same second. fn timestamp_name(to_rename: &Path) -> PathBuf { use std::os::windows::fs::MetadataExt; let now = Local::now(); + // Try to avoid stomping other files by using to_rename as part of the output. + // Avoids cases (like copying several at once) where several were created + // in the same second and get moved on top of each other. match fs::metadata(to_rename).map(|meta| windows_timestamp(meta.creation_time())) { Ok(Some(ct)) => { let local = ct.with_timezone(now.offset()); PathBuf::from(format!( - "{}.flt{}", + "{}_{}.flt{}", local.format("%Y-%m-%d_%H-%M-%S"), + to_rename.file_stem().unwrap().to_string_lossy(), MOVED_SUFFIX )) } @@ -123,8 +125,9 @@ fn timestamp_name(to_rename: &Path) -> PathBuf { to_rename.display() ); PathBuf::from(format!( - "{}.flt{}", - now.format("%Y-%m-%d_%H-%M-%S"), + "{}_{}.flt{}", + now.format("%Y-%m-%d_%H-%M-%S_{}"), + to_rename.file_stem().unwrap().to_string_lossy(), MOVED_SUFFIX )) }