Skip to content

Commit

Permalink
Introduce fn set_timestamp(), fn set_creation_timestamp() to override…
Browse files Browse the repository at this point in the history
… timestamps
  • Loading branch information
sftse committed Apr 18, 2024
1 parent 220e231 commit 21bfcba
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,10 +958,45 @@ impl<F: Read + Write + Seek> CompoundFile<F> {
/// Sets the modified time for the object at the given path to now. Has no
/// effect when called on the root storage.
pub fn touch<P: AsRef<Path>>(&mut self, path: P) -> io::Result<()> {
self.touch_with_path(path.as_ref())
self.set_timestamp(path, std::time::SystemTime::now())
}

fn touch_with_path(&mut self, path: &Path) -> io::Result<()> {
/// Sets the modified time for the object at the given path.
pub fn set_timestamp<P: AsRef<Path>>(
&mut self,
path: P,
ts: std::time::SystemTime,
) -> io::Result<()> {
self.set_timestamp_with_path(
path.as_ref(),
Timestamp::from_system_time(ts),
)
}

/// Sets the created time for the object at the given path.
pub fn set_creation_timestamp<P: AsRef<Path>>(
&mut self,
path: P,
ts: std::time::SystemTime,
) -> io::Result<()> {
let names = internal::path::name_chain_from_path(path.as_ref())?;
let path = internal::path::path_from_name_chain(&names);
let stream_id = match self.stream_id_for_name_chain(&names) {
Some(stream_id) => stream_id,
None => not_found!("No such object: {:?}", path),
};
let mut minialloc = self.minialloc_mut();
minialloc.with_dir_entry_mut(stream_id, |dir_entry| {
dir_entry.creation_time = Timestamp::from_system_time(ts);
})?;
Ok(())
}

fn set_timestamp_with_path(
&mut self,
path: &Path,
ts: Timestamp,
) -> io::Result<()> {
let names = internal::path::name_chain_from_path(path)?;
let path = internal::path::path_from_name_chain(&names);
let stream_id = match self.stream_id_for_name_chain(&names) {
Expand All @@ -975,7 +1010,7 @@ impl<F: Read + Write + Seek> CompoundFile<F> {
ObjType::Root
);
minialloc.with_dir_entry_mut(stream_id, |dir_entry| {
dir_entry.modified_time = Timestamp::now();
dir_entry.modified_time = ts;
})?;
}
Ok(())
Expand Down Expand Up @@ -1053,6 +1088,12 @@ mod tests {
let mut stream = cfb.create_stream("/foo/bar").unwrap();
stream.write_all(b"data").unwrap();
drop(stream);

let entries: Vec<_> = cfb.walk().collect();
for entr in entries {
cfb.set_timestamp(entr.path(), ts).unwrap();
cfb.set_creation_timestamp(entr.path(), ts).unwrap();
}
cfb.flush().unwrap();
buf
}
Expand Down

0 comments on commit 21bfcba

Please sign in to comment.