From 71893aafeb1e8badd787408f350a90cd15ed0285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Tue, 11 Oct 2022 21:22:45 -0300 Subject: [PATCH] Refactor the way we set last modified times Last modified time is a piece of metadata that is available when decompressing an archive --- Cargo.lock | 16 +++++++++++++ Cargo.toml | 3 ++- src/archive/zip.rs | 58 +++++++++++----------------------------------- 3 files changed, 31 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e893c6..a41d737 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -393,6 +393,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" + [[package]] name = "jobserver" version = "0.1.25" @@ -521,6 +527,7 @@ dependencies = [ "clap", "clap_complete", "clap_mangen", + "filetime", "flate2", "fs-err", "ignore", @@ -913,10 +920,18 @@ version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d634a985c4d4238ec39cacaed2e7ae552fbd3c476b552c1deac3021b7d7eaf0c" dependencies = [ + "itoa", "libc", "num_threads", + "time-macros", ] +[[package]] +name = "time-macros" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792" + [[package]] name = "unicode-ident" version = "1.0.5" @@ -1075,6 +1090,7 @@ dependencies = [ "crc32fast", "crossbeam-utils", "flate2", + "time", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b792859..701d461 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,11 +25,12 @@ once_cell = "1.15.0" snap = "1.0.5" tar = "0.4.38" xz2 = "0.1.7" -zip = { version = "0.6.2", default-features = false } +zip = { version = "0.6.2", default-features = false, features = ["time"] } zstd = { version = "0.11.2", default-features = false } tempfile = "3.3.0" ignore = "0.4.18" indicatif = "0.17.1" +filetime = "0.2.17" [target.'cfg(unix)'.dependencies] time = { version = "0.3.15", default-features = false } diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 77b9684..b3e329b 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -10,6 +10,7 @@ use std::{ thread, }; +use filetime::{set_file_mtime, FileTime}; use fs_err as fs; use zip::{self, read::ZipFile, ZipArchive}; @@ -72,13 +73,12 @@ where let mut output_file = fs::File::create(file_path)?; io::copy(&mut file, &mut output_file)?; - #[cfg(unix)] - set_last_modified_time(&output_file, &file)?; + set_last_modified_time(&file, file_path)?; } } #[cfg(unix)] - __unix_set_permissions(&file_path, &file)?; + unix_set_permissions(&file_path, &file)?; unpacked_files.push(file_path); } @@ -228,55 +228,23 @@ fn display_zip_comment_if_exists(file: &ZipFile) { } } -#[cfg(unix)] -/// Attempts to convert a [`zip::DateTime`] to a [`libc::timespec`]. -fn convert_zip_date_time(date_time: zip::DateTime) -> Option { - use time::{Date, Month, PrimitiveDateTime, Time}; +fn set_last_modified_time(zip_file: &ZipFile, path: &Path) -> crate::Result<()> { + let modification_time_in_seconds = zip_file + .last_modified() + .to_time() + .expect("Zip archive contains a file with broken 'last modified time'") + .unix_timestamp(); - // Safety: time::Month is repr(u8) and goes from 1 to 12 - let month: Month = unsafe { std::mem::transmute(date_time.month()) }; + // Zip does not support nanoseconds, so we can assume zero here + let modification_time = FileTime::from_unix_time(modification_time_in_seconds, 0); - let date = Date::from_calendar_date(date_time.year() as _, month, date_time.day()).ok()?; - - let time = Time::from_hms(date_time.hour(), date_time.minute(), date_time.second()).ok()?; - - let date_time = PrimitiveDateTime::new(date, time); - let timestamp = date_time.assume_utc().unix_timestamp(); - - Some(libc::timespec { - tv_sec: timestamp, - tv_nsec: 0, - }) -} - -#[cfg(unix)] -fn set_last_modified_time(file: &fs::File, zip_file: &ZipFile) -> crate::Result<()> { - use std::os::unix::prelude::AsRawFd; - - use libc::UTIME_NOW; - - let now = libc::timespec { - tv_sec: 0, - tv_nsec: UTIME_NOW, - }; - - let last_modified = zip_file.last_modified(); - let last_modified = convert_zip_date_time(last_modified).unwrap_or(now); - - // The first value is the last accessed time, which we'll set as being right now. - // The second value is the last modified time, which we'll copy over from the zip archive - let times = [now, last_modified]; - - let output_fd = file.as_raw_fd(); - - // TODO: check for -1 - unsafe { libc::futimens(output_fd, × as *const _) }; + set_file_mtime(path, modification_time)?; Ok(()) } #[cfg(unix)] -fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> { +fn unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> { use std::fs::Permissions; if let Some(mode) = file.unix_mode() {