Added filetime crate to handle file handle times
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline failed

This commit is contained in:
Gregory Ballantine 2023-01-25 10:06:05 -05:00
parent 6da120f632
commit f3552f3af1
2 changed files with 15 additions and 0 deletions

View File

@ -10,6 +10,7 @@ authors = ["Gregory Ballantine <gballantine@bitgoblin.tech>"]
[dependencies] [dependencies]
clap = { version = "3.2", features = ['derive'] } clap = { version = "3.2", features = ['derive'] }
config = { version = "0.13", features = ['toml'] } config = { version = "0.13", features = ['toml'] }
filetime = "0.2"
log = "0.4" log = "0.4"
log4rs = "1.1" log4rs = "1.1"
toml = "0.5" toml = "0.5"

View File

@ -1,6 +1,8 @@
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use log::{error, info}; use log::{error, info};
use filetime;
use filetime::FileTime;
pub struct Repository { pub struct Repository {
pub base_dir: String, pub base_dir: String,
@ -58,6 +60,7 @@ impl Repository {
match fs::copy(&ingest_file, &archive_file) { match fs::copy(&ingest_file, &archive_file) {
Ok(_) => { Ok(_) => {
info!("Archiving video file {}.", ingest_file.to_str().unwrap()); info!("Archiving video file {}.", ingest_file.to_str().unwrap());
copy_file_times(ingest_file.to_str().unwrap(), archive_file.to_str().unwrap());
}, },
Err(e) => { Err(e) => {
error!("Error archiving file {}: {}", ingest_file.to_str().unwrap(), 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.");
}