Skip to content

Commit 444b315

Browse files
committed
Adding support for uclibc's funtimens in set_file_handle_times
1 parent bfee019 commit 444b315

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/unix/utimes.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> {
2020
set_times(p, Some(atime), None, false)
2121
}
2222

23+
#[cfg(not(target_env = "uclibc"))]
2324
#[allow(dead_code)]
2425
pub fn set_file_handle_times(
2526
f: &fs::File,
@@ -39,6 +40,26 @@ pub fn set_file_handle_times(
3940
};
4041
}
4142

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+
4263
fn get_times(
4364
atime: Option<FileTime>,
4465
mtime: Option<FileTime>,
@@ -96,3 +117,10 @@ fn to_timeval(ft: &FileTime) -> libc::timeval {
96117
tv_usec: (ft.nanoseconds() / 1000) as libc::suseconds_t,
97118
}
98119
}
120+
121+
fn to_timespec(ft: &FileTime) -> libc::timespec {
122+
libc::timespec {
123+
tv_sec: ft.seconds() as libc::time_t,
124+
tv_nsec: (ft.nanoseconds()) as libc::suseconds_t,
125+
}
126+
}

0 commit comments

Comments
 (0)