Skip to main content

relibc/header/utime/
mod.rs

1//! `utime.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/utime.h.html>.
4//!
5//! The `utime.h` header was marked obsolescent in the Open Group Base
6//! Specifications Issue 7, and removed in Issue 8.
7
8use crate::{
9    c_str::CStr,
10    error::ResultExt,
11    header::time::timespec,
12    platform::{
13        Pal, Sys,
14        types::{c_char, c_int, time_t},
15    },
16};
17
18/// See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/utime.h.html>.
19///
20/// A structure representing both access and modification times in seconds.
21///
22/// Times are measured in seconds since the Epoch.
23#[deprecated]
24#[repr(C)]
25#[derive(Clone)]
26pub struct utimbuf {
27    /// Access time.
28    pub actime: time_t,
29    /// Modification time.
30    pub modtime: time_t,
31}
32
33/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/utime.html>.
34///
35/// Sets the access and modification times of the file named by the `path`
36/// argument.
37///
38/// Upon success, returns `0`. Upon failure, returns `-1`, sets errno to
39/// indicate the error, and the file times shall not be affected.
40///
41/// # Deprecated
42/// Marked obsolete in issue 7, removed in issue 8.
43///
44/// Should use `utimensat()` instead for greater accuracy because `utimebuf`
45/// uses `time_t` which represents whole seconds only.
46#[deprecated]
47#[expect(deprecated, reason = "utimbuf struct")]
48#[unsafe(no_mangle)]
49pub unsafe extern "C" fn utime(path: *const c_char, times: *const utimbuf) -> c_int {
50    let filename_cstr = unsafe { CStr::from_ptr(path) };
51    let times_ref = unsafe { &*times };
52    let times_spec = [
53        timespec {
54            tv_sec: times_ref.actime,
55            tv_nsec: 0,
56        },
57        timespec {
58            tv_sec: times_ref.modtime,
59            tv_nsec: 0,
60        },
61    ];
62    unsafe { Sys::utimens(filename_cstr, times_spec.as_ptr()) }
63        .map(|()| 0)
64        .or_minus_one_errno()
65}