Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the dependency filetime optional #277

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ contents are never required to be entirely resident in memory all at once.
"""

[dependencies]
filetime = "0.2.8"
filetime = { version = "0.2.8", optional = true }

[dev-dependencies]
tempfile = "3"

[target."cfg(unix)".dependencies]
xattr = { version = "0.2", optional = true }
libc = "0.2"
libc = { version = "0.2", optional = true }

[features]
default = ["xattr"]
default = ["builder", "filetime", "xattr"]
builder = ["libc"]

[[test]]
name = "entry"
required-features = ["builder"]

[[example]]
name = "write"
required-features = ["builder"]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ fn main() {
}
```

# Cargo features

- **builder** (enabled by default): provides tools for creating tarballs:
`Builder` and `Header::{set_metadata, set_metadata_in_mode}`. Depends on
libc on unix, and methods touching `std::fs` are unimplemented on wasm.
- **filetime** (enabled by default): write file times when unpacking a tarball.
- **xattr** (enabled by defeult): write xattrs when unpacking a tarball on unix.

# License

This project is licensed under either of
Expand Down
2 changes: 2 additions & 0 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::io::{self, Error, ErrorKind, SeekFrom};
use std::marker;
use std::path::{Component, Path, PathBuf};

#[cfg(feature = "filetime")]
use filetime::{self, FileTime};

use crate::archive::ArchiveInner;
Expand Down Expand Up @@ -617,6 +618,7 @@ impl<'a> EntryFields<'a> {
)
})?;

#[cfg(feature = "filetime")]
if self.preserve_mtime {
if let Ok(mtime) = self.header.mtime() {
// For some more information on this see the comments in
Expand Down
76 changes: 44 additions & 32 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::os::windows::prelude::*;

use std::borrow::Cow;
use std::fmt;
#[cfg(feature = "builder")]
use std::fs;
use std::io;
use std::iter;
Expand Down Expand Up @@ -279,12 +280,14 @@ impl Header {
/// This is useful for initializing a `Header` from the OS's metadata from a
/// file. By default, this will use `HeaderMode::Complete` to include all
/// metadata.
#[cfg(feature = "builder")]
pub fn set_metadata(&mut self, meta: &fs::Metadata) {
self.fill_from(meta, HeaderMode::Complete);
}

/// Sets only the metadata relevant to the given HeaderMode in this header
/// from the metadata argument provided.
#[cfg(feature = "builder")]
pub fn set_metadata_in_mode(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
self.fill_from(meta, mode);
}
Expand Down Expand Up @@ -714,6 +717,7 @@ impl Header {
.fold(0, |a, b| a + (*b as u32))
}

#[cfg(feature = "builder")]
fn fill_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
self.fill_platform_from(meta, mode);
// Set size of directories to zero
Expand All @@ -732,13 +736,7 @@ impl Header {
}
}

#[cfg(target_arch = "wasm32")]
#[allow(unused_variables)]
fn fill_platform_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
unimplemented!();
}

#[cfg(unix)]
#[cfg(all(unix, feature = "builder"))]
fn fill_platform_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
match mode {
HeaderMode::Complete => {
Expand Down Expand Up @@ -797,39 +795,53 @@ impl Header {
}
}

#[cfg(windows)]
#[cfg(all(not(unix), feature = "builder"))]
#[allow(unused_variables)]
fn fill_platform_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
// There's no concept of a file mode on Windows, so do a best approximation here.
match mode {
// Fallback implementation: approximate modes, and get as good an mtime as we can.
let mtime = match mode {
#[cfg(windows)]
HeaderMode::Complete => {
self.set_uid(0);
self.set_gid(0);
// The dates listed in tarballs are always seconds relative to
// January 1, 1970. On Windows, however, the timestamps are returned as
// dates relative to January 1, 1601 (in 100ns intervals), so we need to
// add in some offset for those dates.
let mtime = (meta.last_write_time() / (1_000_000_000 / 100)) - 11644473600;
self.set_mtime(mtime);
let fs_mode = {
const FILE_ATTRIBUTE_READONLY: u32 = 0x00000001;
let readonly = meta.file_attributes() & FILE_ATTRIBUTE_READONLY;
match (meta.is_dir(), readonly != 0) {
(true, false) => 0o755,
(true, true) => 0o555,
(false, false) => 0o644,
(false, true) => 0o444,
}
};
self.set_mode(fs_mode);
(meta.last_write_time() / (1_000_000_000 / 100)) - 11644473600
}
HeaderMode::Deterministic => {
self.set_uid(0);
self.set_gid(0);
self.set_mtime(123456789); // see above in unix
let fs_mode = if meta.is_dir() { 0o755 } else { 0o644 };
self.set_mode(fs_mode);
#[cfg(not(windows))]
HeaderMode::Complete => {
meta.modified()
.ok()
.and_then(|time| time.duration_since(std::time::SystemTime::UNIX_EPOCH).ok())
.map(|duration| duration.as_secs())
.unwrap_or(1) // avoiding zero mtimes, see above in unix
}
}
HeaderMode::Deterministic => 123456789, // see above in unix
};
let fs_mode = match mode {
#[cfg(windows)]
HeaderMode::Complete => {
const FILE_ATTRIBUTE_READONLY: u32 = 0x00000001;
let readonly = meta.file_attributes() & FILE_ATTRIBUTE_READONLY;
match (meta.is_dir(), readonly != 0) {
(true, false) => 0o755,
(true, true) => 0o555,
(false, false) => 0o644,
(false, true) => 0o444,
}
}
_ => {
if meta.is_dir() {
0o755
} else {
0o644
}
}
};
self.set_uid(0);
self.set_gid(0);
self.set_mtime(mtime);
self.set_mode(fs_mode);

let ft = meta.file_type();
self.set_entry_type(if ft.is_dir() {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use std::io::{Error, ErrorKind};

pub use crate::archive::{Archive, Entries};
#[cfg(feature = "builder")]
pub use crate::builder::Builder;
pub use crate::entry::{Entry, Unpacked};
pub use crate::entry_type::EntryType;
Expand All @@ -32,6 +33,7 @@ pub use crate::header::{GnuHeader, GnuSparseHeader, Header, HeaderMode, OldHeade
pub use crate::pax::{PaxExtension, PaxExtensions};

mod archive;
#[cfg(feature = "builder")]
mod builder;
mod entry;
mod entry_type;
Expand Down
Loading