Skip to content

Commit

Permalink
Only reset directories if we modify the contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Emann committed Apr 16, 2024
1 parent 5c5e530 commit d792560
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions crates/applesauce/src/threads/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct Context {
// Fields are dropped in top-down order, so ensure we update the parent's times before
// dropping the operation (which will notify that the operation is done if this is the last
// file).
_parent_reset: Option<Arc<times::Resetter>>,
parent_resetter: Option<Arc<times::Resetter>>,
operation: Arc<OperationContext>,
path: PathBuf,
progress: Box<dyn progress::Task + Send + Sync>,
Expand Down Expand Up @@ -205,7 +205,7 @@ impl BackgroundThreads {
path,
progress: inner_progress,
orig_metadata: metadata,
_parent_reset: dir_reset,
parent_resetter: dir_reset,
orig_times: saved_times,
}),
})
Expand Down
6 changes: 6 additions & 0 deletions crates/applesauce/src/threads/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ impl Handler {
let _entered = tracing::debug_span!("rename tmp file").entered();
tmp_file.persist(&item.context.path)?
};
if let Some(resetter) = &item.context.parent_resetter {
resetter.activate();
}
if let Err(e) = times::reset_times(&new_file, &item.context.orig_times) {
tracing::error!("Unable to reset times: {e}");
}
Expand Down Expand Up @@ -184,6 +187,9 @@ impl Handler {
)?;

let new_file = tmp_file.persist(&item.context.path)?;
if let Some(resetter) = &item.context.parent_resetter {
resetter.activate();
}
if let Err(e) = times::reset_times(&new_file, &item.context.orig_times) {
tracing::error!("Unable to reset times: {e}");
}
Expand Down
15 changes: 14 additions & 1 deletion crates/applesauce/src/times.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::mem::MaybeUninit;
use std::os::fd::AsRawFd;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::sync::atomic::AtomicBool;
use std::{io, mem, ptr};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -200,10 +201,14 @@ pub fn reset_times<F: GetSet + std::fmt::Debug + ?Sized>(f: &F, saved: &Saved) -
f.reset_times(saved)
}

/// Reset the times of a file/dir
///
/// By default, will do nothing on drop, unless `activate` is called at least once
#[derive(Debug)]
pub struct Resetter {
dir_path: CString,
saved_times: Saved,
activated: AtomicBool,
}

impl Resetter {
Expand All @@ -212,12 +217,20 @@ impl Resetter {
Ok(Self {
dir_path,
saved_times,
activated: AtomicBool::new(false),
})
}

pub fn activate(&self) {
self.activated
.store(true, std::sync::atomic::Ordering::Relaxed);
}
}

impl Drop for Resetter {
fn drop(&mut self) {
let _ = times::reset_times(self.dir_path.as_c_str(), &self.saved_times);
if self.activated.load(std::sync::atomic::Ordering::Relaxed) {
let _ = times::reset_times(self.dir_path.as_c_str(), &self.saved_times);
}
}
}

0 comments on commit d792560

Please sign in to comment.