rlogger 0.2.0

Bounded, ordered local file logging with per-instance context and rotation
Documentation
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Versioned hourly storage contract shared with the local file manager.
#[cfg(unix)]
use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
use std::{
    fs::{self, File, OpenOptions},
    io::{self, Read, Write},
    path::{Path, PathBuf},
};

/// A directory capability pinned to the inode opened at construction time.
/// Descendants are resolved relative to this descriptor, never through the
/// original root pathname again.
pub struct RootDir {
    dir: File,
}

impl RootDir {
    pub fn open(path: &Path) -> io::Result<Self> {
        #[cfg(unix)]
        {
            let dir = OpenOptions::new()
                .read(true)
                .custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC)
                .open(path)?;
            Ok(Self { dir })
        }
        #[cfg(not(unix))]
        {
            let _ = path;
            Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "confined operations unsupported",
            ))
        }
    }

    pub fn metadata(&self) -> io::Result<fs::Metadata> {
        self.dir.metadata()
    }

    pub fn file(&self) -> &File {
        &self.dir
    }

    #[cfg(unix)]
    fn parent_fd(&self, relative: &Path, create: bool) -> io::Result<(File, std::ffi::CString)> {
        parent_fd_from(self.dir.try_clone()?, relative, create)
    }

    pub fn open_directory(&self, relative: &Path) -> io::Result<File> {
        #[cfg(unix)]
        {
            use std::os::fd::{AsRawFd, FromRawFd};
            if relative.as_os_str().is_empty() {
                let fd = unsafe {
                    libc::openat(
                        self.dir.as_raw_fd(),
                        c".".as_ptr(),
                        libc::O_RDONLY | libc::O_DIRECTORY | libc::O_CLOEXEC,
                    )
                };
                if fd < 0 {
                    return Err(io::Error::last_os_error());
                }
                return Ok(unsafe { File::from_raw_fd(fd) });
            }
            let (parent, name) = self.parent_fd(relative, false)?;
            let fd = unsafe {
                libc::openat(
                    parent.as_raw_fd(),
                    name.as_ptr(),
                    libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
                )
            };
            if fd < 0 {
                return Err(io::Error::last_os_error());
            }
            Ok(unsafe { File::from_raw_fd(fd) })
        }
        #[cfg(not(unix))]
        {
            let _ = relative;
            Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "directory unsupported",
            ))
        }
    }

    pub fn open_node(&self, relative: &Path) -> io::Result<File> {
        #[cfg(unix)]
        {
            use std::os::fd::{AsRawFd, FromRawFd};
            if relative.as_os_str().is_empty() {
                return self.dir.try_clone();
            }
            let (parent, name) = self.parent_fd(relative, false)?;
            let fd = unsafe {
                libc::openat(
                    parent.as_raw_fd(),
                    name.as_ptr(),
                    libc::O_RDONLY | libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC,
                )
            };
            if fd < 0 {
                return Err(io::Error::last_os_error());
            }
            Ok(unsafe { File::from_raw_fd(fd) })
        }
        #[cfg(not(unix))]
        {
            let _ = relative;
            Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "node unsupported",
            ))
        }
    }

    pub fn open_file(&self, relative: &Path) -> io::Result<File> {
        let file = self.open_node(relative)?;
        if !file.metadata()?.is_file() {
            return Err(io::Error::other("not a regular file"));
        }
        Ok(file)
    }

    #[cfg(unix)]
    pub fn create_directory(&self, relative: &Path) -> io::Result<()> {
        use std::os::fd::AsRawFd;
        let (parent, name) = self.parent_fd(relative, false)?;
        if unsafe { libc::mkdirat(parent.as_raw_fd(), name.as_ptr(), 0o700) } == 0 {
            Ok(())
        } else {
            let error = io::Error::last_os_error();
            if error.kind() == io::ErrorKind::AlreadyExists {
                self.open_directory(relative).map(|_| ())
            } else {
                Err(error)
            }
        }
    }

    #[cfg(unix)]
    pub fn create_new(&self, relative: &Path) -> io::Result<File> {
        use std::os::fd::{AsRawFd, FromRawFd};
        let (parent, name) = self.parent_fd(relative, false)?;
        let fd = unsafe {
            libc::openat(
                parent.as_raw_fd(),
                name.as_ptr(),
                libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOFOLLOW | libc::O_CLOEXEC,
                0o600,
            )
        };
        if fd < 0 {
            Err(io::Error::last_os_error())
        } else {
            Ok(unsafe { File::from_raw_fd(fd) })
        }
    }

    #[cfg(unix)]
    pub fn replace(&self, source: &Path, target: &Path) -> io::Result<()> {
        use std::{
            ffi::CString,
            os::{fd::AsRawFd, unix::ffi::OsStrExt},
        };
        if source.parent() != target.parent() {
            return Err(io::Error::other("replacement must stay in its directory"));
        }
        self.open_file(source)?;
        let (parent, old) = self.parent_fd(source, false)?;
        let new = CString::new(
            target
                .file_name()
                .ok_or_else(|| io::Error::other("missing filename"))?
                .as_bytes(),
        )?;
        if unsafe {
            libc::renameat(
                parent.as_raw_fd(),
                old.as_ptr(),
                parent.as_raw_fd(),
                new.as_ptr(),
            )
        } == 0
        {
            Ok(())
        } else {
            Err(io::Error::last_os_error())
        }
    }

    #[cfg(unix)]
    pub fn read_directory(&self, relative: &Path) -> io::Result<DirectoryEntries> {
        DirectoryEntries::new(self.open_directory(relative)?)
    }

    /// Callers must hold the management gate and require private parent directories.
    /// POSIX unlinkat cannot atomically compare the final name's inode with a handle;
    /// non-cooperating processes of the same trusted account can still race a rename.
    pub fn remove(
        &self,
        relative: &Path,
        directory: bool,
        expected: Option<&str>,
    ) -> io::Result<()> {
        #[cfg(unix)]
        {
            use std::os::fd::AsRawFd;
            let (parent, name) = self.parent_fd(relative, false)?;
            let target = self.open_node(relative)?;
            let metadata = target.metadata()?;
            if metadata.is_dir() != directory || expected.is_some_and(|t| token(&metadata) != t) {
                return Err(io::Error::other("file changed"));
            }
            let flags = if directory { libc::AT_REMOVEDIR } else { 0 };
            if unsafe { libc::unlinkat(parent.as_raw_fd(), name.as_ptr(), flags) } == 0 {
                Ok(())
            } else {
                Err(io::Error::last_os_error())
            }
        }
        #[cfg(not(unix))]
        {
            let _ = (relative, directory, expected);
            Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "remove unsupported",
            ))
        }
    }

    pub fn rename(&self, relative: &Path, target: &Path) -> io::Result<()> {
        #[cfg(unix)]
        {
            use std::os::fd::AsRawFd;
            if relative.parent() != target.parent() {
                return Err(io::Error::other("publication must stay in its directory"));
            }
            let _source = self.open_file(relative)?;
            let (parent, old) = self.parent_fd(relative, false)?;
            let (_, new) = self.parent_fd(target, false)?;
            #[cfg(target_os = "macos")]
            let rc = unsafe {
                libc::renameatx_np(
                    parent.as_raw_fd(),
                    old.as_ptr(),
                    parent.as_raw_fd(),
                    new.as_ptr(),
                    libc::RENAME_EXCL,
                )
            };
            #[cfg(target_os = "linux")]
            let rc = unsafe {
                libc::renameat2(
                    parent.as_raw_fd(),
                    old.as_ptr(),
                    parent.as_raw_fd(),
                    new.as_ptr(),
                    libc::RENAME_NOREPLACE,
                )
            };
            #[cfg(not(any(target_os = "macos", target_os = "linux")))]
            return Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "rename unsupported",
            ));
            #[cfg(any(target_os = "macos", target_os = "linux"))]
            if rc == 0 {
                Ok(())
            } else {
                Err(io::Error::last_os_error())
            }
        }
        #[cfg(not(unix))]
        {
            let _ = (relative, target);
            Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "rename unsupported",
            ))
        }
    }
}

#[cfg(unix)]
pub struct DirectoryEntries {
    dir: *mut libc::DIR,
}

#[cfg(unix)]
impl DirectoryEntries {
    fn new(file: File) -> io::Result<Self> {
        use std::os::fd::IntoRawFd;
        let fd = file.into_raw_fd();
        let dir = unsafe { libc::fdopendir(fd) };
        if dir.is_null() {
            let error = io::Error::last_os_error();
            unsafe { libc::close(fd) };
            return Err(error);
        }
        Ok(Self { dir })
    }
}

#[cfg(unix)]
impl Iterator for DirectoryEntries {
    type Item = io::Result<std::ffi::OsString>;

    fn next(&mut self) -> Option<Self::Item> {
        use std::{ffi::CStr, os::unix::ffi::OsStringExt};
        loop {
            #[cfg(target_os = "macos")]
            unsafe {
                *libc::__error() = 0
            };
            #[cfg(target_os = "linux")]
            unsafe {
                *libc::__errno_location() = 0
            };
            let entry = unsafe { libc::readdir(self.dir) };
            if entry.is_null() {
                let error = io::Error::last_os_error();
                return (error.raw_os_error() != Some(0)).then_some(Err(error));
            }
            let name = unsafe { CStr::from_ptr((*entry).d_name.as_ptr()) }.to_bytes();
            if name != b"." && name != b".." {
                return Some(Ok(std::ffi::OsString::from_vec(name.to_vec())));
            }
        }
    }
}

#[cfg(unix)]
impl Drop for DirectoryEntries {
    fn drop(&mut self) {
        unsafe { libc::closedir(self.dir) };
    }
}
pub const MARKER: &str = "RLOGGER storage 2\n";
pub const TECH: &str = ".rlogger";

pub fn hourly_layout(path: &Path) -> bool {
    let parts = path.components().collect::<Vec<_>>();
    if parts.len() != 3
        || parts
            .iter()
            .any(|p| !matches!(p, std::path::Component::Normal(_)))
    {
        return false;
    }
    let Some(day) = parts[0].as_os_str().to_str() else {
        return false;
    };
    chrono::NaiveDate::parse_from_str(day, "%Y-%m-%d")
        .is_ok_and(|d| d.format("%Y-%m-%d").to_string() == day)
        && parts[1]
            .as_os_str()
            .to_str()
            .is_some_and(crate::model::valid_name)
}

#[cfg(unix)]
fn parent_fd(root: &Path, relative: &Path, create: bool) -> io::Result<(File, std::ffi::CString)> {
    parent_fd_from(RootDir::open(root)?.dir, relative, create)
}

#[cfg(unix)]
fn parent_fd_from(
    mut dir: File,
    relative: &Path,
    create: bool,
) -> io::Result<(File, std::ffi::CString)> {
    use std::{
        ffi::CString,
        os::{
            fd::{AsRawFd, FromRawFd},
            unix::ffi::OsStrExt,
        },
    };
    let parts = relative.components().collect::<Vec<_>>();
    if parts.is_empty()
        || parts
            .iter()
            .any(|p| !matches!(p, std::path::Component::Normal(_)))
    {
        return Err(io::Error::other("unsafe relative path"));
    }
    for part in &parts[..parts.len() - 1] {
        let name = CString::new(part.as_os_str().as_bytes())?;
        if create && unsafe { libc::mkdirat(dir.as_raw_fd(), name.as_ptr(), 0o755) } != 0 {
            let e = io::Error::last_os_error();
            if e.kind() != io::ErrorKind::AlreadyExists {
                return Err(e);
            }
        }
        let fd = unsafe {
            libc::openat(
                dir.as_raw_fd(),
                name.as_ptr(),
                libc::O_RDONLY | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
            )
        };
        if fd < 0 {
            return Err(io::Error::last_os_error());
        }
        dir = unsafe { File::from_raw_fd(fd) };
    }
    Ok((
        dir,
        CString::new(parts.last().unwrap().as_os_str().as_bytes())?,
    ))
}
pub fn open_beneath(root: &Path, relative: &Path, create: bool) -> io::Result<File> {
    #[cfg(unix)]
    {
        use std::os::fd::{AsRawFd, FromRawFd};
        let (dir, name) = parent_fd(root, relative, create)?;
        let flags = libc::O_NOFOLLOW
            | libc::O_NONBLOCK
            | libc::O_CLOEXEC
            | if create {
                libc::O_RDWR | libc::O_CREAT | libc::O_EXCL
            } else {
                libc::O_RDONLY
            };
        let fd = unsafe { libc::openat(dir.as_raw_fd(), name.as_ptr(), flags, 0o600) };
        if fd < 0 {
            return Err(io::Error::last_os_error());
        }
        let file = unsafe { File::from_raw_fd(fd) };
        if !file.metadata()?.is_file() {
            return Err(io::Error::other("not a regular file"));
        }
        Ok(file)
    }
    #[cfg(not(unix))]
    {
        let _ = (root, relative, create);
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "confined operations unsupported",
        ))
    }
}
pub fn remove_beneath(
    root: &Path,
    relative: &Path,
    directory: bool,
    expected: Option<&str>,
) -> io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::fd::{AsRawFd, FromRawFd};
        let (dir, name) = parent_fd(root, relative, false)?;
        let fd = unsafe {
            libc::openat(
                dir.as_raw_fd(),
                name.as_ptr(),
                libc::O_RDONLY
                    | libc::O_NOFOLLOW
                    | libc::O_NONBLOCK
                    | libc::O_CLOEXEC
                    | if directory { libc::O_DIRECTORY } else { 0 },
            )
        };
        if fd < 0 {
            return Err(io::Error::last_os_error());
        }
        let f = unsafe { File::from_raw_fd(fd) };
        let metadata = f.metadata()?;
        if expected.is_some_and(|t| token(&metadata) != t) {
            return Err(io::Error::other("file changed"));
        }
        if unsafe {
            libc::unlinkat(
                dir.as_raw_fd(),
                name.as_ptr(),
                if directory { libc::AT_REMOVEDIR } else { 0 },
            )
        } == 0
        {
            Ok(())
        } else {
            Err(io::Error::last_os_error())
        }
    }
    #[cfg(not(unix))]
    {
        let _ = (root, relative, directory, expected);
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "confined operations unsupported",
        ))
    }
}
pub fn rename_beneath(root: &Path, relative: &Path, target: &Path) -> io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::fd::AsRawFd;
        if relative.parent() != target.parent() {
            return Err(io::Error::other("publication must stay in its directory"));
        }
        // Publication is reserved for regular files, never a symlink source.
        let _source = open_beneath(root, relative, false)?;
        let (dir, old) = parent_fd(root, relative, false)?;
        let (_, new) = parent_fd(root, target, false)?;
        #[cfg(target_os = "macos")]
        let rc = unsafe {
            libc::renameatx_np(
                dir.as_raw_fd(),
                old.as_ptr(),
                dir.as_raw_fd(),
                new.as_ptr(),
                libc::RENAME_EXCL,
            )
        };
        #[cfg(target_os = "linux")]
        let rc = unsafe {
            libc::renameat2(
                dir.as_raw_fd(),
                old.as_ptr(),
                dir.as_raw_fd(),
                new.as_ptr(),
                libc::RENAME_NOREPLACE,
            )
        };
        #[cfg(not(any(target_os = "macos", target_os = "linux")))]
        return Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "atomic rename unsupported",
        ));
        #[cfg(any(target_os = "macos", target_os = "linux"))]
        if rc == 0 {
            Ok(())
        } else {
            Err(io::Error::last_os_error())
        }
    }
    #[cfg(not(unix))]
    {
        let _ = (root, relative, target);
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "confined operations unsupported",
        ))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileState {
    Active,
    Closed,
    Recovered,
}
#[derive(Debug, Clone)]
pub struct LogName {
    pub run_id: String,
    pub hour_start: i64,
    pub state: FileState,
}
impl LogName {
    pub fn parse(name: &str) -> Option<Self> {
        let (base, state) = if let Some(s) = name.strip_suffix(".active.log") {
            (s, FileState::Active)
        } else if let Some(s) = name.strip_suffix(".recovered.log") {
            (s, FileState::Recovered)
        } else {
            (name.strip_suffix(".log")?, FileState::Closed)
        };
        let (base, segment) = base.rsplit_once("-s")?;
        if segment.parse::<u64>().ok()? == 0 {
            return None;
        }
        let (base, hour) = base.rsplit_once("-h")?;
        let hour_start = hour.parse::<i64>().ok()?;
        hour_start.checked_add(3600)?;
        let mut parts = base.rsplitn(5, '-');
        let counter = parts.next()?;
        let pid = parts.next()?;
        let stamp = parts.next()?;
        let hh = parts.next()?;
        let destination = parts.next()?;
        if ![counter, pid, stamp]
            .iter()
            .all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit()))
            || hh.len() != 2
            || hh.parse::<u8>().ok()? > 23
            || !crate::model::valid_name(destination)
        {
            return None;
        }
        Some(Self {
            run_id: format!("{stamp}-{pid}-{counter}"),
            hour_start,
            state,
        })
    }
    pub fn ended(&self, now: i64) -> bool {
        self.hour_start
            .checked_add(3600)
            .is_some_and(|end| end <= now)
    }
}

/// Open regular files without following a final symlink. Caller validates parents.
pub fn open_file(path: &Path, write: bool, create: bool) -> io::Result<File> {
    let mut o = OpenOptions::new();
    o.read(true).write(write).create_new(create);
    #[cfg(unix)]
    o.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK);
    let f = o.open(path)?;
    if !f.metadata()?.is_file() {
        return Err(io::Error::other("not a regular file"));
    }
    Ok(f)
}
pub fn checked_directory(path: &Path) -> io::Result<()> {
    let m = fs::symlink_metadata(path)?;
    if !m.is_dir() || m.file_type().is_symlink() {
        return Err(io::Error::other("unsafe directory"));
    }
    Ok(())
}
pub fn recognized(root: &Path) -> bool {
    (|| -> io::Result<bool> {
        checked_directory(root)?;
        checked_directory(&root.join(TECH))?;
        let mut s = String::new();
        open_beneath(root, &Path::new(TECH).join("format"), false)?
            .take(128)
            .read_to_string(&mut s)?;
        Ok(s == MARKER)
    })()
    .unwrap_or(false)
}
pub fn initialize(root: &Path) -> io::Result<()> {
    fs::create_dir_all(root)?;
    checked_directory(root)?;
    match fs::create_dir(root.join(TECH)) {
        Ok(()) => {}
        Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
        Err(e) => return Err(e),
    }
    checked_directory(&root.join(TECH))?;
    // The gate is permanent: unlinking a lock would split cooperating processes.
    let gate_path = Path::new(TECH).join("gate");
    let gate = match open_beneath(root, &gate_path, true) {
        Ok(f) => f,
        Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
            open_beneath(root, &gate_path, false)?
        }
        Err(e) => return Err(e),
    };
    gate.lock()?;
    let marker = Path::new(TECH).join("format");
    match open_beneath(root, &marker, true) {
        Ok(mut f) => f.write_all(MARKER.as_bytes())?,
        Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
        Err(e) => return Err(e),
    }
    if !recognized(root) {
        return Err(io::Error::other("unsupported RLOGGER storage"));
    }
    Ok(())
}
pub fn gate(root: &Path) -> io::Result<File> {
    if !recognized(root) {
        return Err(io::Error::other("unrecognized RLOGGER root"));
    }
    let file = open_beneath(root, &Path::new(TECH).join("gate"), false)?;
    file.lock()?;
    Ok(file)
}
pub fn lease_path(root: &Path, run: &str) -> PathBuf {
    root.join(TECH).join(format!("{run}.lock"))
}
pub fn token(meta: &fs::Metadata) -> String {
    #[cfg(unix)]
    {
        format!(
            "{}-{}-{}-{}-{}-{}-{}",
            meta.dev(),
            meta.ino(),
            meta.len(),
            meta.mtime(),
            meta.mtime_nsec(),
            meta.ctime(),
            meta.ctime_nsec()
        )
    }
    #[cfg(not(unix))]
    {
        format!("{}-{:?}", meta.len(), meta.modified().ok())
    }
}
pub fn same_file(a: &fs::Metadata, b: &fs::Metadata) -> bool {
    #[cfg(unix)]
    {
        a.dev() == b.dev() && a.ino() == b.ino()
    }
    #[cfg(not(unix))]
    {
        a.created().ok() == b.created().ok()
    }
}