Debian Codegraph @

debian/rust-vendor/filetime/src/unix/mod.rs

raw
1 use crate::FileTime;
2 use libc::{time_t, timespec, UTIME_OMIT};
3 use std::fs;
4 use std::io;
5 use std::os::unix::prelude::*;
6 use std::path::Path;
7
8 cfg_if::cfg_if! {
9 if #[cfg(target_os = "linux")] {
10 mod utimes;
11 mod linux;
12 pub use self::linux::*;
13 } else if #[cfg(target_os = "android")] {
14 mod android;
15 pub use self::android::*;
16 } else if #[cfg(target_os = "macos")] {
17 mod utimes;
18 mod macos;
19 pub use self::macos::*;
20 } else if #[cfg(any(target_os = "aix",
21 target_os = "nto",
22 target_os = "solaris",
23 target_os = "illumos",
24 target_os = "emscripten",
25 target_os = "freebsd",
26 target_os = "netbsd",
27 target_os = "openbsd",
28 target_os = "haiku"))] {
29 mod utimensat;
30 pub use self::utimensat::*;
31 } else {
32 mod utimes;
33 pub use self::utimes::*;
34 }
35 }
36
37 #[allow(dead_code)]
38 fn to_timespec(ft: &Option<FileTime>) -> timespec {
39 let mut ts: timespec = unsafe { std::mem::zeroed() };
40 if let &Some(ft) = ft {
41 ts.tv_sec = ft.seconds() as time_t;
42 ts.tv_nsec = ft.nanoseconds() as _;
43 } else {
44 ts.tv_sec = 0;
45 ts.tv_nsec = UTIME_OMIT as _;
46 }
47
48 ts
49 }
50
51 pub fn from_last_modification_time(meta: &fs::Metadata) -> FileTime {
52 FileTime {
53 seconds: meta.mtime(),
54 nanos: meta.mtime_nsec() as u32,
55 }
56 }
57
58 pub fn from_last_access_time(meta: &fs::Metadata) -> FileTime {
59 FileTime {
60 seconds: meta.atime(),
61 nanos: meta.atime_nsec() as u32,
62 }
63 }
64
65 pub fn from_creation_time(meta: &fs::Metadata) -> Option<FileTime> {
66 meta.created().map(|i| i.into()).ok()
67 }
68
69 pub fn open(path: &Path) -> io::Result<fs::File> {
70 // Try a read-only open, but if that fails try a write-only open, and if
71 // that fails then give up.
72 fs::File::open(path).or_else(|_| fs::OpenOptions::new().write(true).open(path))
73 }