1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use crate::CowStr;
use std::{
borrow::Cow,
path::{Path, PathBuf},
};
#[cfg(feature = "async")]
use async_recursion::async_recursion;
#[cfg(feature = "async")]
use futures_lite::StreamExt;
#[cfg(feature = "file-type")]
use file_format::FileFormat;
#[cfg(feature = "time")]
use tai64::Tai64N;
#[cfg(feature = "time")]
use crate::DateTimeString;
/// The Metadata of all directories and files in the current directory
/// #### Example
/// ```rust
/// use dir_meta::DirMetadata;
///
/// // With feature `async` enabled using `cargo add dir-meta --features async`
/// #[cfg(feature = "async")]
/// {
/// let dir = DirMetadata::new("/path/to/directory").async_dir_metadata();
/// }
///
/// // With feature `sync` enabled using `cargo add dir-meta --features sync`
/// #[cfg(feature = "sync")]
/// {
/// let dir = DirMetadata::new("/path/to/directory").sync_dir_metadata();
/// }
/// ```
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct DirMetadata<'a> {
name: CowStr<'a>,
path: PathBuf,
directories: Vec<PathBuf>,
files: Vec<FileMetadata<'a>>,
#[cfg(feature = "extra")]
size: usize,
errors: Vec<DirError<'a>>,
}
impl<'a> DirMetadata<'_> {
/// Create a new instance of [Self]
/// but with the path as a `&str`
pub fn new(path: &'a str) -> Self {
Self::new_path_buf(path.into())
}
/// Create a new instance of [Self]
pub fn new_path_buf(path: PathBuf) -> Self {
let name = Cow::Owned(path.file_name().unwrap().to_string_lossy().to_string());
Self {
path,
name,
..Default::default()
}
}
/// Multiple files can have the same name if they are in different dirs
/// so using this method returns a [vector](Vec) of [FileMetadata]
pub fn get_file(&'a self, file_name: &'a str) -> Vec<&'a FileMetadata<'a>> {
self.files()
.iter()
.filter(|file| file.name() == file_name)
.collect()
}
/// Get a file by it's absolute path (from root)
pub fn get_file_by_path(&'a self, path: &'a str) -> Option<&'a FileMetadata<'a>> {
self.files()
.iter()
.find(|file| file.path() == Path::new(path))
}
/// Returns an error if the directory cannot be accessed
/// Read all the directories and files in the given path in async fashion
#[cfg(feature = "async")]
pub async fn async_dir_metadata(mut self) -> Result<Self, std::io::Error> {
use async_fs::read_dir;
let mut dir = read_dir(&self.path).await?;
self.async_iter_dir(&mut dir).await;
Ok(self)
}
/// Returns an error if the directory cannot be accessed
/// Read all the directories and files in the given path
#[cfg(feature = "sync")]
pub fn sync_dir_metadata(mut self) -> Result<Self, std::io::Error> {
use std::fs::read_dir;
let mut dir = read_dir(&self.path)?;
self.sync_iter_dir(&mut dir);
Ok(self)
}
/// Recursively iterate over directories inside directories
#[cfg(feature = "async")]
#[async_recursion]
pub async fn async_iter_dir(
&'a mut self,
prepared_dir: &mut async_fs::ReadDir,
) -> &'a mut Self {
use async_fs::read_dir;
let mut directories = Vec::<PathBuf>::new();
while let Some(entry_result) = prepared_dir.next().await {
match entry_result {
Err(error) => {
self.errors.push(DirError {
path: self.path.clone(),
error: error.kind(),
display: error.to_string().into(),
});
}
Ok(entry) => {
let mut is_dir = false;
match entry.file_type().await {
Ok(file_type) => is_dir = file_type.is_dir(),
Err(error) => {
let inner_path = entry.path();
self.errors.push(DirError {
path: inner_path.clone(),
error: error.kind(),
display: Cow::Owned(format!(
"Unable to check if `{}` is a directory",
inner_path.display()
)),
});
}
}
if is_dir {
directories.push(entry.path())
} else {
let mut file_meta = FileMetadata::default();
#[cfg(all(feature = "file-type", feature = "async"))]
{
let cloned_path = entry.path().clone();
let get_file_format =
blocking::unblock(move || FileFormat::from_file(cloned_path));
let format = (get_file_format.await).unwrap_or_default();
file_meta.file_format = format;
}
file_meta.name =
CowStr::Owned(entry.file_name().to_string_lossy().to_string());
file_meta.path = entry.path();
#[cfg(any(feature = "size", feature = "time", feature = "extra"))]
match entry.metadata().await {
Ok(meta) => {
#[cfg(feature = "extra")]
{
let current_file_size = meta.len() as usize;
self.size += current_file_size;
file_meta.size = current_file_size;
}
#[cfg(feature = "time")]
{
file_meta.accessed =
crate::FsUtils::maybe_time(meta.accessed().ok());
file_meta.modified =
crate::FsUtils::maybe_time(meta.modified().ok());
file_meta.created =
crate::FsUtils::maybe_time(meta.created().ok());
}
}
Err(error) => {
self.errors.push(DirError {
path: entry.path(),
error: error.kind(),
display: Cow::Owned(format!(
"Unable to access metadata of file `{}`",
entry.path().display()
)),
});
}
}
self.files.push(file_meta);
}
}
}
}
let mut dir_iter = futures_lite::stream::iter(&directories);
while let Some(path) = dir_iter.next().await {
match read_dir(path.clone()).await {
Ok(mut prepared_dir) => {
self.async_iter_dir(&mut prepared_dir).await;
}
Err(error) => self.errors.push(DirError {
path: path.to_owned(),
error: error.kind(),
display: Cow::Owned(format!(
"Unable to access metadata of file `{}`",
path.display()
)),
}),
}
}
self.directories.extend_from_slice(&directories);
self
}
/// Recursively iterate over directories inside directories
#[cfg(feature = "sync")]
pub fn sync_iter_dir(&mut self, prepared_dir: &mut std::fs::ReadDir) -> &mut Self {
let mut directories = Vec::<PathBuf>::new();
prepared_dir
.by_ref()
.for_each(|entry_result| match entry_result {
Err(error) => {
self.errors.push(DirError {
path: self.path.clone(),
error: error.kind(),
display: error.to_string().into(),
});
}
Ok(entry) => {
let mut is_dir = false;
match entry.file_type() {
Ok(file_type) => is_dir = file_type.is_dir(),
Err(error) => {
let inner_path = entry.path();
self.errors.push(DirError {
path: inner_path.clone(),
error: error.kind(),
display: Cow::Owned(format!(
"Unable to check if `{}` is a directory",
inner_path.display()
)),
});
}
}
if is_dir {
directories.push(entry.path())
} else {
let mut file_meta = FileMetadata::default();
#[cfg(all(feature = "file-type", feature = "sync"))]
{
let cloned_path = entry.path().clone();
let get_file_format = FileFormat::from_file(cloned_path);
let format = (get_file_format).unwrap_or_default();
file_meta.file_format = format;
}
file_meta.name =
CowStr::Owned(entry.file_name().to_string_lossy().to_string());
file_meta.path = entry.path();
#[cfg(any(feature = "size", feature = "time", feature = "extra"))]
match entry.metadata() {
Ok(meta) => {
#[cfg(feature = "extra")]
{
let current_file_size = meta.len() as usize;
self.size += current_file_size;
file_meta.size = current_file_size;
}
#[cfg(feature = "time")]
{
file_meta.accessed =
crate::FsUtils::maybe_time(meta.accessed().ok());
file_meta.modified =
crate::FsUtils::maybe_time(meta.modified().ok());
file_meta.created =
crate::FsUtils::maybe_time(meta.created().ok());
}
}
Err(error) => {
self.errors.push(DirError {
path: entry.path(),
error: error.kind(),
display: Cow::Owned(format!(
"Unable to access metadata of file `{}`",
entry.path().display()
)),
});
}
}
self.files.push(file_meta);
}
}
});
directories
.iter()
.for_each(|path| match std::fs::read_dir(path.clone()) {
Ok(mut prepared_dir) => {
self.sync_iter_dir(&mut prepared_dir);
}
Err(error) => self.errors.push(DirError {
path: path.to_owned(),
error: error.kind(),
display: Cow::Owned(format!(
"Unable to access metadata of file `{}`",
path.display()
)),
}),
});
self.directories.extend_from_slice(&directories);
self
}
/// Get the name of the current directory
pub fn dir_name(&self) -> &str {
self.name.as_ref()
}
/// Get the path of the current directory
pub fn dir_path(&self) -> &Path {
self.path.as_ref()
}
/// Get all the sub-directories of the current directory
pub fn directories(&self) -> &[PathBuf] {
self.directories.as_ref()
}
/// Get all the files in the current directory and all the files in it's sub-directory
pub fn files(&'a self) -> &'a [FileMetadata<'a>] {
self.files.as_ref()
}
/// Get the size of the directory including the size of all files in the sub-directories
#[cfg(feature = "extra")]
pub fn size(&self) -> usize {
self.size
}
/// Get the size of the directory including the size of all files in the sub-directories in human readable format
#[cfg(feature = "size")]
pub fn size_formatted(&self) -> String {
crate::FsUtils::size_to_bytes(self.size)
}
/// Get all the errors encountered while opening the sub-directories and files
pub fn errors(&'a self) -> &'a [DirError<'a>] {
self.errors.as_ref()
}
}
/// The file metadata like file name, file type, file size, file path etc
#[derive(Debug, PartialEq, Eq, Default, Clone)]
pub struct FileMetadata<'a> {
name: CowStr<'a>,
path: PathBuf,
#[cfg(feature = "extra")]
size: usize,
#[cfg(feature = "extra")]
read_only: bool,
#[cfg(feature = "time")]
created: Option<Tai64N>,
#[cfg(feature = "time")]
accessed: Option<Tai64N>,
#[cfg(feature = "time")]
modified: Option<Tai64N>,
#[cfg(feature = "extra")]
symlink: bool,
#[cfg(feature = "file-type")]
file_format: FileFormat,
}
impl<'a> FileMetadata<'a> {
/// Get the name of the file
pub fn name(&self) -> &str {
self.name.as_ref()
}
/// Get the path of the file
pub fn path(&self) -> &Path {
self.path.as_ref()
}
/// Get the size of the file
#[cfg(feature = "extra")]
pub fn size(&self) -> usize {
self.size
}
/// Get the size of the file in human readable format
#[cfg(feature = "size")]
pub fn formatted_size(&self) -> String {
crate::FsUtils::size_to_bytes(self.size)
}
/// Get the TAI64N timestamp when the file was last accessed
#[cfg(feature = "time")]
pub fn accessed(&self) -> Option<Tai64N> {
self.accessed
}
/// Get the TAI64N timestamp when the file was last modified
#[cfg(feature = "time")]
pub fn modified(&self) -> Option<Tai64N> {
self.modified
}
/// Get the TAI64N timestamp when the file was last created
#[cfg(feature = "time")]
pub fn created(&self) -> Option<Tai64N> {
self.created
}
/// Get the timestamp in local time in 24 hour format when the file was last accessed
#[cfg(feature = "time")]
pub fn accessed_24hr(&self) -> Option<DateTimeString<'a>> {
Some(crate::FsUtils::tai64_to_local_hrs(&self.accessed?))
}
/// Get the timestamp in local time in 12 hour format when the file was last accessed
#[cfg(feature = "time")]
pub fn accessed_am_pm(&self) -> Option<DateTimeString<'a>> {
Some(crate::FsUtils::tai64_to_local_am_pm(&self.accessed?))
}
/// Get the time passed since access of a file eg `3 sec ago`
#[cfg(feature = "time")]
pub fn accessed_humatime(&self) -> Option<String> {
crate::FsUtils::tai64_now_duration_to_humantime(&self.accessed?)
}
/// Get the timestamp in local time in 24 hour format when the file was last modified
#[cfg(feature = "time")]
pub fn modified_24hr(&self) -> Option<DateTimeString<'a>> {
Some(crate::FsUtils::tai64_to_local_hrs(&self.modified?))
}
/// Get the timestamp in local time in 12 hour format when the file was last modified
#[cfg(feature = "time")]
pub fn modified_am_pm(&self) -> Option<DateTimeString<'a>> {
Some(crate::FsUtils::tai64_to_local_am_pm(&self.modified?))
}
/// Get the time passed since modification of a file eg `3 sec ago`
#[cfg(feature = "time")]
pub fn modified_humatime(&self) -> Option<String> {
crate::FsUtils::tai64_now_duration_to_humantime(&self.modified?)
}
/// Get the timestamp in local time in 24 hour format when the file was created
#[cfg(feature = "time")]
pub fn created_24hr(&self) -> Option<DateTimeString<'a>> {
Some(crate::FsUtils::tai64_to_local_hrs(&self.created?))
}
/// Get the timestamp in local time in 12 hour format when the file was created
#[cfg(feature = "time")]
pub fn created_am_pm(&self) -> Option<DateTimeString<'a>> {
Some(crate::FsUtils::tai64_to_local_am_pm(&self.created?))
}
/// Get the time passed since file was created of a file eg `3 sec ago`
#[cfg(feature = "time")]
pub fn created_humatime(&self) -> Option<String> {
crate::FsUtils::tai64_now_duration_to_humantime(&self.created?)
}
/// Is the file read only
#[cfg(feature = "extra")]
pub fn read_only(&self) -> bool {
self.read_only
}
/// Is the file a symbolic link
#[cfg(feature = "extra")]
pub fn symlink(&self) -> bool {
self.symlink
}
/// Get the format of the current file
#[cfg(feature = "file-type")]
pub fn file_format(&self) -> &FileFormat {
&self.file_format
}
}
/// An error encountered while accessing a file or sub-directory
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct DirError<'a> {
/// The path to the sub-directory or file where the error occurred
pub path: PathBuf,
/// The kind of error that occurred based on [std::io::ErrorKind]
pub error: std::io::ErrorKind,
/// The formatted error as a [String]
pub display: CowStr<'a>,
}
#[cfg(test)]
mod sanity_checks {
#[cfg(all(feature = "async", feature = "size", feature = "extra"))]
#[test]
fn async_features() {
smol::block_on(async {
let dir = String::from(env!("CARGO_MANIFEST_DIR")) + "/src";
let outcome = crate::DirMetadata::new(&dir)
.async_dir_metadata()
.await
.unwrap();
{
#[cfg(feature = "time")]
for file in outcome.files() {
assert_ne!("", file.name());
assert_ne!(Option::None, file.accessed_24hr());
assert_ne!(Option::None, file.accessed_am_pm());
assert_ne!(Option::None, file.accessed_humatime());
assert_ne!(Option::None, file.created_24hr());
assert_ne!(Option::None, file.created_am_pm());
assert_ne!(Option::None, file.created_humatime());
assert_ne!(Option::None, file.modified_24hr());
assert_ne!(Option::None, file.modified_am_pm());
assert_ne!(Option::None, file.modified_humatime());
assert_ne!(String::default(), file.formatted_size());
}
}
})
}
#[cfg(all(feature = "sync", feature = "size", feature = "extra"))]
#[test]
fn sync_features() {
use file_format::FileFormat;
let dir = String::from(env!("CARGO_MANIFEST_DIR")) + "/src";
let outcome = crate::DirMetadata::new(&dir).sync_dir_metadata().unwrap();
{
#[cfg(feature = "time")]
for file in outcome.files() {
assert_ne!("", file.name());
assert_ne!(Option::None, file.accessed_24hr());
assert_ne!(Option::None, file.accessed_am_pm());
assert_ne!(Option::None, file.accessed_humatime());
assert_ne!(Option::None, file.created_24hr());
assert_ne!(Option::None, file.created_am_pm());
assert_ne!(Option::None, file.created_humatime());
assert_ne!(Option::None, file.modified_24hr());
assert_ne!(Option::None, file.modified_am_pm());
assert_ne!(Option::None, file.modified_humatime());
assert_ne!(String::default(), file.formatted_size());
}
}
#[cfg(feature = "extra")]
{
assert!(outcome.size() > 0usize);
}
#[cfg(feature = "file-type")]
{
let path = dir.clone() + "/lib.rs";
let file = outcome.get_file_by_path(&path);
assert!(file.is_some());
assert_eq!(file.unwrap().file_format(), &FileFormat::PlainText);
}
}
}