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

debug HFS on other systems #37

Merged
merged 4 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[Documentation](https://docs.rs/filetime)

A helper library for inspecting the various timestamps of files in Rust. This
A helper library for inspecting and seting the various timestamps of files in Rust. This
library takes into account cross-platform differences in terms of where the
timestamps are located, what they are called, and how to convert them into a
platform-independent representation.
Expand All @@ -16,6 +16,12 @@ platform-independent representation.
filetime = "0.2"
```

# Advantages over using `std::fs::Metadata`

This library includes the ability to set this data, which std dose not.

This library, when built with debug_assertions and with the variable `FILETIME_EMULATE_SECOND_ONLY_SYSTOM` set, will return all times rounded down to the second. This emulates the behavior of some file systems, mostly [HFS](https://en.wikipedia.org/wiki/HFS_Plus), allowing debugging on other hardware.

# License

This project is licensed under either of
Expand All @@ -30,5 +36,5 @@ at your option.
### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be
for inclusion in Filetime by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#[macro_use]
extern crate cfg_if;

use std::env;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. this line can come out.

use std::fmt;
use std::fs;
use std::io;
Expand Down Expand Up @@ -75,6 +76,14 @@ impl FileTime {
FileTime { seconds: 0, nanos: 0 }
}

fn emulate_second_only_systom(self) -> FileTime {
if cfg!(debug_assertions) && env::var_os("FILETIME_EMULATE_SECOND_ONLY_SYSTOM").is_some() {
FileTime {seconds: self.seconds, nanos: 0}
} else {
self
}
}

/// Creates a new instance of `FileTime` with a number of seconds and
/// nanoseconds relative to the Unix epoch, 1970-01-01T00:00:00Z.
///
Expand All @@ -89,7 +98,7 @@ impl FileTime {
FileTime {
seconds: seconds + if cfg!(windows) {11644473600} else {0},
nanos,
}
}.emulate_second_only_systom()
}

/// Creates a new timestamp from the last modification time listed in the
Expand All @@ -98,7 +107,7 @@ impl FileTime {
/// The returned value corresponds to the `mtime` field of `stat` on Unix
/// platforms and the `ftLastWriteTime` field on Windows platforms.
pub fn from_last_modification_time(meta: &fs::Metadata) -> FileTime {
imp::from_last_modification_time(meta)
imp::from_last_modification_time(meta).emulate_second_only_systom()
}

/// Creates a new timestamp from the last access time listed in the
Expand All @@ -107,7 +116,7 @@ impl FileTime {
/// The returned value corresponds to the `atime` field of `stat` on Unix
/// platforms and the `ftLastAccessTime` field on Windows platforms.
pub fn from_last_access_time(meta: &fs::Metadata) -> FileTime {
imp::from_last_access_time(meta)
imp::from_last_access_time(meta).emulate_second_only_systom()
}

/// Creates a new timestamp from the creation time listed in the specified
Expand All @@ -118,7 +127,7 @@ impl FileTime {
/// that not all Unix platforms have this field available and may return
/// `None` in some circumstances.
pub fn from_creation_time(meta: &fs::Metadata) -> Option<FileTime> {
imp::from_creation_time(meta)
imp::from_creation_time(meta).map(|x| x.emulate_second_only_systom())
}

/// Creates a new timestamp from the given SystemTime.
Expand Down Expand Up @@ -149,7 +158,7 @@ impl FileTime {
seconds: -1 * until_epoch.as_secs() as i64 + sec_offset,
nanos
}
})
}).emulate_second_only_systom()
}

/// Returns the whole number of seconds represented by this timestamp.
Expand Down