Skip to content

Commit d99dd1c

Browse files
committed
Update file timestamps to reflect real systemtime
This commit retains the use of a stateless tick in the model but allows for the filesystem to report times in systemtime. This resolves a problem where all times were previously reported by the filesystem as being UNIX_EPOCH even though the model kept track of time. Signed-off-by: Brian L. Troutwine <[email protected]>
1 parent 35a65a9 commit d99dd1c

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

lading/src/bin/logrotate_fs.rs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ use tracing_subscriber::{fmt::format::FmtSpan, util::SubscriberInitExt};
1111
// use lading_payload::block;
1212
use nix::libc::{self, ENOENT};
1313
use serde::Deserialize;
14-
use std::{
15-
ffi::OsStr,
16-
num::NonZeroU32,
17-
path::PathBuf,
18-
time::{Duration, UNIX_EPOCH},
19-
};
14+
use std::{ffi::OsStr, num::NonZeroU32, path::PathBuf, time::Duration};
2015

2116
// fn default_config_path() -> String {
2217
// "/etc/lading/logrotate_fs.yaml".to_string()
@@ -83,6 +78,7 @@ const TTL: Duration = Duration::from_secs(1); // Attribute cache timeout
8378
struct LogrotateFS {
8479
state: model::State,
8580
start_time: std::time::Instant,
81+
start_time_system: std::time::SystemTime,
8682
}
8783

8884
impl LogrotateFS {
@@ -95,33 +91,46 @@ impl LogrotateFS {
9591
fn getattr_helper(&mut self, tick: model::Tick, inode: usize) -> Option<FileAttr> {
9692
let nlink = self.state.nlink(inode) as u32;
9793

98-
self.state.getattr(tick, inode).map(|attr| FileAttr {
99-
ino: attr.inode as u64,
100-
size: attr.size,
101-
blocks: (attr.size + 511) / 512,
102-
// TODO these times should reflect those in the model, will need to
103-
// be translated from the tick to systemtime. Implies we'll need to
104-
// adjust up from start_time, knowing that a tick is one second
105-
// wide.
106-
atime: UNIX_EPOCH,
107-
mtime: UNIX_EPOCH,
108-
ctime: UNIX_EPOCH,
109-
crtime: UNIX_EPOCH,
110-
kind: match attr.kind {
111-
model::NodeType::File => fuser::FileType::RegularFile,
112-
model::NodeType::Directory => fuser::FileType::Directory,
113-
},
114-
perm: if matches!(attr.kind, model::NodeType::Directory) {
115-
0o755
116-
} else {
117-
0o644
118-
},
119-
nlink,
120-
uid: unsafe { libc::getuid() },
121-
gid: unsafe { libc::getgid() },
122-
rdev: 0,
123-
blksize: 512,
124-
flags: 0,
94+
self.state.getattr(tick, inode).map(|attr| {
95+
// Convert ticks to durations
96+
let access_duration = Duration::from_secs(attr.access_tick);
97+
let modified_duration = Duration::from_secs(attr.modified_tick);
98+
let status_duration = Duration::from_secs(attr.status_tick);
99+
100+
// Calculate SystemTime instances
101+
let atime = self.start_time_system + access_duration;
102+
let mtime = self.start_time_system + modified_duration;
103+
let ctime = self.start_time_system + status_duration;
104+
let crtime = self.start_time_system; // Assume creation time is when the filesystem started
105+
106+
FileAttr {
107+
ino: attr.inode as u64,
108+
size: attr.size,
109+
blocks: (attr.size + 511) / 512,
110+
// TODO these times should reflect those in the model, will need to
111+
// be translated from the tick to systemtime. Implies we'll need to
112+
// adjust up from start_time, knowing that a tick is one second
113+
// wide.
114+
atime,
115+
mtime,
116+
ctime,
117+
crtime,
118+
kind: match attr.kind {
119+
model::NodeType::File => fuser::FileType::RegularFile,
120+
model::NodeType::Directory => fuser::FileType::Directory,
121+
},
122+
perm: if matches!(attr.kind, model::NodeType::Directory) {
123+
0o755
124+
} else {
125+
0o644
126+
},
127+
nlink,
128+
uid: unsafe { libc::getuid() },
129+
gid: unsafe { libc::getgid() },
130+
rdev: 0,
131+
blksize: 512,
132+
flags: 0,
133+
}
125134
})
126135
}
127136
}
@@ -297,6 +306,7 @@ fn main() -> Result<(), Error> {
297306
let fs = LogrotateFS {
298307
state,
299308
start_time: std::time::Instant::now(),
309+
start_time_system: std::time::SystemTime::now(),
300310
};
301311

302312
// Mount the filesystem

lading/src/generator/file_gen/model.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ pub struct NodeAttributes {
168168
pub kind: NodeType,
169169
/// The size in bytes.
170170
pub size: u64,
171+
/// The last access time in ticks.
172+
pub access_tick: Tick,
173+
/// The last modified time in ticks.
174+
pub modified_tick: Tick,
175+
/// The last status change time in ticks.
176+
pub status_tick: Tick,
171177
}
172178

173179
/// Describe whether the Node is a File or Directory.
@@ -298,11 +304,17 @@ impl State {
298304
inode,
299305
kind: NodeType::File,
300306
size: file.bytes_written,
307+
access_tick: file.access_tick,
308+
modified_tick: file.modified_tick,
309+
status_tick: file.status_tick,
301310
},
302311
Node::Directory { .. } => NodeAttributes {
303312
inode,
304313
kind: NodeType::Directory,
305314
size: 0,
315+
access_tick: self.now,
316+
modified_tick: self.now,
317+
status_tick: self.now,
306318
},
307319
})
308320
}

0 commit comments

Comments
 (0)