diff options
| author | Alejandro Soto <alejandro@34project.org> | 2022-01-03 08:16:28 -0600 |
|---|---|---|
| committer | Alejandro Soto <alejandro@34project.org> | 2022-01-03 08:16:28 -0600 |
| commit | 988ce8bbc2a33f6a90adecb7547a88cfbf7acaf2 (patch) | |
| tree | 7477c5b1671c37b60b30abae86e6b4c659ba8b50 | |
| parent | b64ce0506671f72a5142c0e82673331dc687b9e2 (diff) | |
Implement Timestamp::new()
| -rw-r--r-- | src/fuse/io.rs | 9 | ||||
| -rw-r--r-- | src/lib.rs | 29 |
2 files changed, 29 insertions, 9 deletions
diff --git a/src/fuse/io.rs b/src/fuse/io.rs index f9fa908..7124fda 100644 --- a/src/fuse/io.rs +++ b/src/fuse/io.rs @@ -8,9 +8,8 @@ use std::{ ops::{ControlFlow, FromResidual, Try}, }; -use crate::{proto, FuseResult, Ino, Timestamp, Ttl}; - use super::{Done, Operation, Reply, Request}; +use crate::{proto, FuseResult, Ino, Timestamp, Ttl}; #[doc(no_inline)] pub use nix::{ @@ -293,9 +292,9 @@ impl Attrs { #[must_use] pub fn times(self, access: Timestamp, modify: Timestamp, change: Timestamp) -> Self { Attrs(proto::Attrs { - atime: access.seconds, - mtime: modify.seconds, - ctime: change.seconds, + atime: access.seconds as _, + mtime: modify.seconds as _, + ctime: change.seconds as _, atimensec: access.nanoseconds, mtimensec: modify.nanoseconds, ctimensec: change.nanoseconds, @@ -33,7 +33,7 @@ pub struct Ttl { #[derive(Copy, Clone, Default, Eq, PartialEq)] pub struct Timestamp { - seconds: u64, + seconds: i64, nanoseconds: u32, } @@ -98,12 +98,33 @@ impl Ttl { } } +impl Timestamp { + pub fn new(seconds: i64, nanoseconds: u32) -> Self { + Timestamp { + seconds, + nanoseconds, + } + } +} + impl From<SystemTime> for Timestamp { fn from(time: SystemTime) -> Self { - let duration = time.duration_since(UNIX_EPOCH).unwrap(); + let (seconds, nanoseconds) = match time.duration_since(UNIX_EPOCH) { + Ok(duration) => { + let secs = duration.as_secs().try_into().unwrap(); + (secs, duration.subsec_nanos()) + } + + Err(before_epoch) => { + let duration = before_epoch.duration(); + let secs = -i64::try_from(duration.as_secs()).unwrap(); + (secs, duration.subsec_nanos()) + } + }; + Timestamp { - seconds: duration.as_secs(), - nanoseconds: duration.subsec_nanos(), + seconds, + nanoseconds, } } } |
