Skip to content

Commit ef1e627

Browse files
committed
Retrieve file timestamp (access time, birth time, and modification
time) based on the arguments specified in the ftime command-line option.
1 parent 9cfeb65 commit ef1e627

File tree

8 files changed

+171
-118
lines changed

8 files changed

+171
-118
lines changed

src/core.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,14 @@ impl Core {
116116
.collect();
117117

118118
for path in paths {
119-
let mut meta =
120-
match Meta::from_path(&path, self.flags.dereference.0, self.flags.permission) {
121-
Ok(meta) => meta,
122-
Err(err) => {
123-
print_error!("{}: {}.", path.display(), err);
124-
exit_code.set_if_greater(ExitCode::MajorIssue);
125-
continue;
126-
}
127-
};
119+
let mut meta = match Meta::from_path(&path, self.flags.dereference.0, &self.flags) {
120+
Ok(meta) => meta,
121+
Err(err) => {
122+
print_error!("{}: {}.", path.display(), err);
123+
exit_code.set_if_greater(ExitCode::MajorIssue);
124+
continue;
125+
}
126+
};
128127

129128
let cache = if self.flags.blocks.0.contains(&Block::GitStatus) {
130129
Some(GitCache::new(&path))

src/display.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ mod tests {
504504
use crate::app::Cli;
505505
use crate::color;
506506
use crate::color::Colors;
507-
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme, PermissionFlag};
507+
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme};
508508
use crate::icon::Icons;
509509
use crate::meta::{FileType, Name};
510510
use crate::Config;
@@ -699,7 +699,7 @@ mod tests {
699699
dir.child("one.d").create_dir_all().unwrap();
700700
dir.child("one.d/two").touch().unwrap();
701701
dir.child("one.d/.hidden").touch().unwrap();
702-
let mut metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
702+
let mut metas = Meta::from_path(Path::new(dir.path()), false, &flags)
703703
.unwrap()
704704
.recurse_into(42, &flags, None)
705705
.unwrap()
@@ -732,7 +732,7 @@ mod tests {
732732
let dir = assert_fs::TempDir::new().unwrap();
733733
dir.child("dir").create_dir_all().unwrap();
734734
dir.child("dir/file").touch().unwrap();
735-
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
735+
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
736736
.unwrap()
737737
.recurse_into(42, &flags, None)
738738
.unwrap()
@@ -773,7 +773,7 @@ mod tests {
773773
let dir = assert_fs::TempDir::new().unwrap();
774774
dir.child("dir").create_dir_all().unwrap();
775775
dir.child("dir/file").touch().unwrap();
776-
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
776+
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
777777
.unwrap()
778778
.recurse_into(42, &flags, None)
779779
.unwrap()
@@ -813,7 +813,7 @@ mod tests {
813813
let dir = assert_fs::TempDir::new().unwrap();
814814
dir.child("one.d").create_dir_all().unwrap();
815815
dir.child("one.d/two").touch().unwrap();
816-
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
816+
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
817817
.unwrap()
818818
.recurse_into(42, &flags, None)
819819
.unwrap()
@@ -844,7 +844,7 @@ mod tests {
844844
let dir = assert_fs::TempDir::new().unwrap();
845845
dir.child("testdir").create_dir_all().unwrap();
846846
dir.child("test").touch().unwrap();
847-
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
847+
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
848848
.unwrap()
849849
.recurse_into(1, &flags, None)
850850
.unwrap()
@@ -878,7 +878,7 @@ mod tests {
878878

879879
let dir = assert_fs::TempDir::new().unwrap();
880880
dir.child("testdir").create_dir_all().unwrap();
881-
let metas = Meta::from_path(Path::new(dir.path()), false, PermissionFlag::Rwx)
881+
let metas = Meta::from_path(Path::new(dir.path()), false, &flags)
882882
.unwrap()
883883
.recurse_into(1, &flags, None)
884884
.unwrap()
@@ -908,11 +908,14 @@ mod tests {
908908

909909
let file_path = tmp_dir.path().join("file");
910910
std::fs::File::create(&file_path).expect("failed to create the file");
911-
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
911+
912+
let flags = Flags::default();
913+
914+
let file = Meta::from_path(&file_path, false, &flags).unwrap();
912915

913916
let dir_path = tmp_dir.path().join("dir");
914917
std::fs::create_dir(&dir_path).expect("failed to create the dir");
915-
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();
918+
let dir = Meta::from_path(&dir_path, false, &flags).unwrap();
916919

917920
assert_eq!(
918921
display_folder_path(&dir),
@@ -955,15 +958,18 @@ mod tests {
955958

956959
let file_path = tmp_dir.path().join("file");
957960
std::fs::File::create(&file_path).expect("failed to create the file");
958-
let file = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
961+
962+
let flags = Flags::default();
963+
964+
let file = Meta::from_path(&file_path, false, &flags).unwrap();
959965

960966
let dir_path = tmp_dir.path().join("dir");
961967
std::fs::create_dir(&dir_path).expect("failed to create the dir");
962-
let dir = Meta::from_path(&dir_path, false, PermissionFlag::Rwx).unwrap();
968+
let dir = Meta::from_path(&dir_path, false, &flags).unwrap();
963969

964970
let link_path = tmp_dir.path().join("link");
965971
std::os::unix::fs::symlink("dir", &link_path).unwrap();
966-
let link = Meta::from_path(&link_path, false, PermissionFlag::Rwx).unwrap();
972+
let link = Meta::from_path(&link_path, false, &flags).unwrap();
967973

968974
const YES: bool = true;
969975
const NO: bool = false;

src/icon.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Icons {
7575
#[cfg(test)]
7676
mod test {
7777
use super::{IconTheme, Icons};
78-
use crate::flags::{IconOption, IconTheme as FlagTheme, PermissionFlag};
78+
use crate::flags::{Flags, IconOption, IconTheme as FlagTheme};
7979
use crate::meta::Meta;
8080
use std::fs::File;
8181
use tempfile::tempdir;
@@ -85,7 +85,8 @@ mod test {
8585
let tmp_dir = tempdir().expect("failed to create temp dir");
8686
let file_path = tmp_dir.path().join("file.txt");
8787
File::create(&file_path).expect("failed to create file");
88-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
88+
let flags = Flags::default();
89+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
8990

9091
let icons = Icons::new(true, IconOption::Never, FlagTheme::Fancy, " ".to_string());
9192
let icon = icons.get(&meta.name);
@@ -97,7 +98,8 @@ mod test {
9798
let tmp_dir = tempdir().expect("failed to create temp dir");
9899
let file_path = tmp_dir.path().join("file.txt");
99100
File::create(&file_path).expect("failed to create file");
100-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
101+
let flags = Flags::default();
102+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
101103

102104
let icons = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string());
103105
let icon = icons.get(&meta.name);
@@ -110,7 +112,8 @@ mod test {
110112
let tmp_dir = tempdir().expect("failed to create temp dir");
111113
let file_path = tmp_dir.path().join("file.txt");
112114
File::create(&file_path).expect("failed to create file");
113-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
115+
let flags = Flags::default();
116+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
114117

115118
let icons = Icons::new(false, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
116119
let icon = icons.get(&meta.name);
@@ -122,7 +125,8 @@ mod test {
122125
let tmp_dir = tempdir().expect("failed to create temp dir");
123126
let file_path = tmp_dir.path().join("file.txt");
124127
File::create(&file_path).expect("failed to create file");
125-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
128+
let flags = Flags::default();
129+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
126130

127131
let icons = Icons::new(true, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
128132
let icon = icons.get(&meta.name);
@@ -135,7 +139,8 @@ mod test {
135139
let tmp_dir = tempdir().expect("failed to create temp dir");
136140
let file_path = tmp_dir.path().join("file");
137141
File::create(&file_path).expect("failed to create file");
138-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
142+
let flags = Flags::default();
143+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
139144

140145
let icon = Icons::new(true, IconOption::Always, FlagTheme::Fancy, " ".to_string());
141146
let icon_str = icon.get(&meta.name);
@@ -148,7 +153,8 @@ mod test {
148153
let tmp_dir = tempdir().expect("failed to create temp dir");
149154
let file_path = tmp_dir.path().join("file");
150155
File::create(&file_path).expect("failed to create file");
151-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
156+
let flags = Flags::default();
157+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
152158

153159
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
154160
let icon_str = icon.get(&meta.name);
@@ -161,7 +167,8 @@ mod test {
161167
let tmp_dir = tempdir().expect("failed to create temp dir");
162168
let file_path = tmp_dir.path().join("file");
163169
File::create(&file_path).expect("failed to create file");
164-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
170+
let flags = Flags::default();
171+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
165172

166173
let icon = Icons::new(
167174
false,
@@ -178,7 +185,8 @@ mod test {
178185
fn get_icon_default_directory() {
179186
let tmp_dir = tempdir().expect("failed to create temp dir");
180187
let file_path = tmp_dir.path();
181-
let meta = Meta::from_path(file_path, false, PermissionFlag::Rwx).unwrap();
188+
let flags = Flags::default();
189+
let meta = Meta::from_path(file_path, false, &flags).unwrap();
182190

183191
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
184192
let icon_str = icon.get(&meta.name);
@@ -190,7 +198,8 @@ mod test {
190198
fn get_icon_default_directory_unicode() {
191199
let tmp_dir = tempdir().expect("failed to create temp dir");
192200
let file_path = tmp_dir.path();
193-
let meta = Meta::from_path(file_path, false, PermissionFlag::Rwx).unwrap();
201+
let flags = Flags::default();
202+
let meta = Meta::from_path(file_path, false, &flags).unwrap();
194203

195204
let icon = Icons::new(
196205
false,
@@ -210,7 +219,8 @@ mod test {
210219
for (file_name, file_icon) in &IconTheme::get_default_icons_by_name() {
211220
let file_path = tmp_dir.path().join(file_name);
212221
File::create(&file_path).expect("failed to create file");
213-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
222+
let flags = Flags::default();
223+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
214224

215225
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
216226
let icon_str = icon.get(&meta.name);
@@ -226,7 +236,8 @@ mod test {
226236
for (ext, file_icon) in &IconTheme::get_default_icons_by_extension() {
227237
let file_path = tmp_dir.path().join(format!("file.{ext}"));
228238
File::create(&file_path).expect("failed to create file");
229-
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
239+
let flags = Flags::default();
240+
let meta = Meta::from_path(&file_path, false, &flags).unwrap();
230241

231242
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
232243
let icon_str = icon.get(&meta.name);

src/meta/date.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::locale::current_locale;
22
use crate::color::{ColoredString, Colors, Elem};
3-
use crate::flags::{DateFlag, Flags};
3+
use crate::flags::{DateFlag, FileTimeFlag, Flags};
44
use chrono::{DateTime, Duration, Local};
55
use chrono_humanize::HumanTime;
66
use std::fs::Metadata;
@@ -23,15 +23,23 @@ impl From<SystemTime> for Date {
2323
}
2424
}
2525

26-
impl From<&Metadata> for Date {
27-
fn from(meta: &Metadata) -> Self {
28-
meta.accessed()
29-
.expect("failed to retrieve modified date")
30-
.into()
31-
}
32-
}
33-
3426
impl Date {
27+
pub fn from_metadata(meta: &Metadata, flags: &Flags) -> Self {
28+
match flags.ftime {
29+
FileTimeFlag::Birth => meta
30+
.created()
31+
.expect("failed to retrieve creation date")
32+
.into(),
33+
FileTimeFlag::Access => meta
34+
.accessed()
35+
.expect("failed to retrieve access date")
36+
.into(),
37+
FileTimeFlag::Modification => meta
38+
.modified()
39+
.expect("failed to retrieve modification date")
40+
.into(),
41+
}
42+
}
3543
pub fn render(&self, colors: &Colors, flags: &Flags) -> ColoredString {
3644
let now = Local::now();
3745
#[allow(deprecated)]
@@ -130,7 +138,7 @@ mod test {
130138
assert!(success, "failed to exec touch");
131139

132140
let colors = Colors::new(ThemeOption::Default);
133-
let date = Date::from(&file_path.metadata().unwrap());
141+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
134142
let flags = Flags::default();
135143

136144
assert_eq!(
@@ -158,7 +166,7 @@ mod test {
158166
assert!(success, "failed to exec touch");
159167

160168
let colors = Colors::new(ThemeOption::Default);
161-
let date = Date::from(&file_path.metadata().unwrap());
169+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
162170
let flags = Flags::default();
163171

164172
assert_eq!(
@@ -186,7 +194,7 @@ mod test {
186194
assert!(success, "failed to exec touch");
187195

188196
let colors = Colors::new(ThemeOption::Default);
189-
let date = Date::from(&file_path.metadata().unwrap());
197+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
190198
let flags = Flags::default();
191199

192200
assert_eq!(
@@ -214,7 +222,7 @@ mod test {
214222
assert!(success, "failed to exec touch");
215223

216224
let colors = Colors::new(ThemeOption::Default);
217-
let date = Date::from(&file_path.metadata().unwrap());
225+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
218226

219227
let flags = Flags {
220228
date: DateFlag::Relative,
@@ -241,7 +249,7 @@ mod test {
241249
assert!(success, "failed to exec touch");
242250

243251
let colors = Colors::new(ThemeOption::Default);
244-
let date = Date::from(&file_path.metadata().unwrap());
252+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
245253

246254
let flags = Flags {
247255
date: DateFlag::Relative,
@@ -268,7 +276,7 @@ mod test {
268276
assert!(success, "failed to exec touch");
269277

270278
let colors = Colors::new(ThemeOption::Default);
271-
let date = Date::from(&file_path.metadata().unwrap());
279+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
272280

273281
let flags = Flags {
274282
date: DateFlag::Iso,
@@ -299,7 +307,7 @@ mod test {
299307
assert!(success, "failed to exec touch");
300308

301309
let colors = Colors::new(ThemeOption::Default);
302-
let date = Date::from(&file_path.metadata().unwrap());
310+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
303311

304312
let flags = Flags {
305313
date: DateFlag::Iso,
@@ -329,7 +337,7 @@ mod test {
329337
assert!(success, "failed to exec touch");
330338

331339
let colors = Colors::new(ThemeOption::Default);
332-
let date = Date::from(&file_path.metadata().unwrap());
340+
let date = Date::from_metadata(&file_path.metadata().unwrap(), &Flags::default());
333341

334342
let flags = Flags {
335343
date: DateFlag::Locale,

src/meta/filetype.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ impl FileType {
109109
mod test {
110110
use super::FileType;
111111
use crate::color::{Colors, ThemeOption};
112-
#[cfg(unix)]
113-
use crate::flags::PermissionFlag;
112+
use crate::flags::Flags;
114113
#[cfg(unix)]
115114
use crate::meta::permissions_or_attributes::PermissionsOrAttributes;
116115
#[cfg(unix)]
@@ -148,7 +147,8 @@ mod test {
148147
fn test_dir_type() {
149148
let tmp_dir = tempdir().expect("failed to create temp dir");
150149
#[cfg(not(windows))]
151-
let meta = crate::meta::Meta::from_path(tmp_dir.path(), false, PermissionFlag::Rwx)
150+
let flags = Flags::default();
151+
let meta = crate::meta::Meta::from_path(tmp_dir.path(), false, &flags)
152152
.expect("failed to get tempdir path");
153153
let metadata = tmp_dir.path().metadata().expect("failed to get metas");
154154

0 commit comments

Comments
 (0)