From f3552f3af1f4e7cc929819f8e9c2e162e7a9ab76 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 25 Jan 2023 10:06:05 -0500 Subject: [PATCH] Added filetime crate to handle file handle times --- Cargo.toml | 1 + src/transcoder/repository.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index f243215..122214a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ authors = ["Gregory Ballantine "] [dependencies] clap = { version = "3.2", features = ['derive'] } config = { version = "0.13", features = ['toml'] } +filetime = "0.2" log = "0.4" log4rs = "1.1" toml = "0.5" diff --git a/src/transcoder/repository.rs b/src/transcoder/repository.rs index f04a243..9b0c325 100644 --- a/src/transcoder/repository.rs +++ b/src/transcoder/repository.rs @@ -1,6 +1,8 @@ use std::fs; use std::path::Path; use log::{error, info}; +use filetime; +use filetime::FileTime; pub struct Repository { pub base_dir: String, @@ -58,6 +60,7 @@ impl Repository { match fs::copy(&ingest_file, &archive_file) { Ok(_) => { info!("Archiving video file {}.", ingest_file.to_str().unwrap()); + copy_file_times(ingest_file.to_str().unwrap(), archive_file.to_str().unwrap()); }, Err(e) => { error!("Error archiving file {}: {}", ingest_file.to_str().unwrap(), e); @@ -89,3 +92,14 @@ fn create_directory(path: &str) { } } } + +fn copy_file_times(old_file: &str, new_file: &str) { + // grab old file metadata + let old_metadata = fs::metadata(old_file).unwrap(); + let mtime = FileTime::from_last_modification_time(&old_metadata); + let atime = FileTime::from_last_access_time(&old_metadata); + + // set metadata on the new file + let new_path = Path::new(new_file); + filetime::set_file_times(new_path, atime, mtime).expect("Failed to set atime and mtime."); +}