/rust/registry/src/index.crates.io-6f17d22bba15001f/filetime-0.2.25/src/unix/utimes.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::FileTime; |
2 | | use std::ffi::CString; |
3 | | use std::fs; |
4 | | use std::io; |
5 | | use std::os::unix::prelude::*; |
6 | | use std::path::Path; |
7 | | |
8 | | #[allow(dead_code)] |
9 | 0 | pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { |
10 | 0 | set_times(p, Some(atime), Some(mtime), false) |
11 | 0 | } |
12 | | |
13 | | #[allow(dead_code)] |
14 | 0 | pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> { |
15 | 0 | set_times(p, None, Some(mtime), false) |
16 | 0 | } |
17 | | |
18 | | #[allow(dead_code)] |
19 | 0 | pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> { |
20 | 0 | set_times(p, Some(atime), None, false) |
21 | 0 | } |
22 | | |
23 | | #[cfg(not(target_env = "uclibc"))] |
24 | | #[allow(dead_code)] |
25 | 0 | pub fn set_file_handle_times( |
26 | 0 | f: &fs::File, |
27 | 0 | atime: Option<FileTime>, |
28 | 0 | mtime: Option<FileTime>, |
29 | 0 | ) -> io::Result<()> { |
30 | 0 | let (atime, mtime) = match get_times(atime, mtime, || f.metadata())? { |
31 | 0 | Some(pair) => pair, |
32 | 0 | None => return Ok(()), |
33 | | }; |
34 | 0 | let times = [to_timeval(&atime), to_timeval(&mtime)]; |
35 | 0 | let rc = unsafe { libc::futimes(f.as_raw_fd(), times.as_ptr()) }; |
36 | 0 | return if rc == 0 { |
37 | 0 | Ok(()) |
38 | | } else { |
39 | 0 | Err(io::Error::last_os_error()) |
40 | | }; |
41 | 0 | } |
42 | | |
43 | | #[cfg(target_env = "uclibc")] |
44 | | #[allow(dead_code)] |
45 | | pub fn set_file_handle_times( |
46 | | f: &fs::File, |
47 | | atime: Option<FileTime>, |
48 | | mtime: Option<FileTime>, |
49 | | ) -> io::Result<()> { |
50 | | let (atime, mtime) = match get_times(atime, mtime, || f.metadata())? { |
51 | | Some(pair) => pair, |
52 | | None => return Ok(()), |
53 | | }; |
54 | | let times = [to_timespec(&atime), to_timespec(&mtime)]; |
55 | | let rc = unsafe { libc::futimens(f.as_raw_fd(), times.as_ptr()) }; |
56 | | return if rc == 0 { |
57 | | Ok(()) |
58 | | } else { |
59 | | Err(io::Error::last_os_error()) |
60 | | }; |
61 | | } |
62 | | |
63 | 0 | fn get_times( |
64 | 0 | atime: Option<FileTime>, |
65 | 0 | mtime: Option<FileTime>, |
66 | 0 | current: impl FnOnce() -> io::Result<fs::Metadata>, |
67 | 0 | ) -> io::Result<Option<(FileTime, FileTime)>> { |
68 | 0 | let pair = match (atime, mtime) { |
69 | 0 | (Some(a), Some(b)) => (a, b), |
70 | 0 | (None, None) => return Ok(None), |
71 | 0 | (Some(a), None) => { |
72 | 0 | let meta = current()?; |
73 | 0 | (a, FileTime::from_last_modification_time(&meta)) |
74 | | } |
75 | 0 | (None, Some(b)) => { |
76 | 0 | let meta = current()?; |
77 | 0 | (FileTime::from_last_access_time(&meta), b) |
78 | | } |
79 | | }; |
80 | 0 | Ok(Some(pair)) |
81 | 0 | } Unexecuted instantiation: filetime::imp::utimes::get_times::<filetime::imp::utimes::set_file_handle_times::{closure#0}> Unexecuted instantiation: filetime::imp::utimes::get_times::<filetime::imp::utimes::set_times::{closure#0}> |
82 | | |
83 | | #[allow(dead_code)] |
84 | 0 | pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { |
85 | 0 | set_times(p, Some(atime), Some(mtime), true) |
86 | 0 | } |
87 | | |
88 | 0 | pub fn set_times( |
89 | 0 | p: &Path, |
90 | 0 | atime: Option<FileTime>, |
91 | 0 | mtime: Option<FileTime>, |
92 | 0 | symlink: bool, |
93 | 0 | ) -> io::Result<()> { |
94 | 0 | let (atime, mtime) = match get_times(atime, mtime, || p.metadata())? { |
95 | 0 | Some(pair) => pair, |
96 | 0 | None => return Ok(()), |
97 | | }; |
98 | 0 | let p = CString::new(p.as_os_str().as_bytes())?; |
99 | 0 | let times = [to_timeval(&atime), to_timeval(&mtime)]; |
100 | 0 | let rc = unsafe { |
101 | 0 | if symlink { |
102 | 0 | libc::lutimes(p.as_ptr(), times.as_ptr()) |
103 | | } else { |
104 | 0 | libc::utimes(p.as_ptr(), times.as_ptr()) |
105 | | } |
106 | | }; |
107 | 0 | return if rc == 0 { |
108 | 0 | Ok(()) |
109 | | } else { |
110 | 0 | Err(io::Error::last_os_error()) |
111 | | }; |
112 | 0 | } |
113 | | |
114 | 0 | fn to_timeval(ft: &FileTime) -> libc::timeval { |
115 | 0 | libc::timeval { |
116 | 0 | tv_sec: ft.seconds() as libc::time_t, |
117 | 0 | tv_usec: (ft.nanoseconds() / 1000) as libc::suseconds_t, |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | #[cfg(target_env = "uclibc")] |
122 | | fn to_timespec(ft: &FileTime) -> libc::timespec { |
123 | | libc::timespec { |
124 | | tv_sec: ft.seconds() as libc::time_t, |
125 | | #[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))] |
126 | | tv_nsec: (ft.nanoseconds()) as i64, |
127 | | #[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))] |
128 | | tv_nsec: (ft.nanoseconds()) as libc::c_long, |
129 | | } |
130 | | } |