The Linux Programming Interface

(nextflipdebug5) #1
File Attributes 289

With futimes(), the file is specified via an open file descriptor, fd.
With lutimes(), the file is specified via a pathname, with the difference from
utimes() that if the pathname refers to a symbolic link, then the link is not derefer-
enced; instead, the timestamps of the link itself are changed.
The futimes() function is supported since glibc 2.3. The lutimes() function is
supported since glibc 2.6.

15.2.2 Changing File Timestamps with utimensat() and futimens()........................


The utimensat() system call (supported since kernel 2.6.22) and the futimens() library
function (supported since glibc 2.6) provide extended functionality for setting a
file’s last access and last modification timestamps. Among the advantages of these
interfaces are the following:

z We can set timestamps with nanosecond precision. This improves on the
microsecond precision provided by utimes().
z It is possible to set the timestamps independently (i.e., one at a time). As shown
earlier, to change just one of the timestamps using the older interfaces, we
must first call stat() to retrieve the value of the other timestamp, and then spec-
ify the retrieved value along with the timestamp whose value we want to
change. (This could lead to a race condition if another process performed an
operation that updated the timestamp between these two steps.)
z We can independently set either of the timestamps to the current time. To
change just one timestamp to the current time with the older interfaces, we
need to employ a call to stat() to retrieve the setting of the timestamp whose
value we wish to leave unchanged, and a call to gettimeofday() to obtain the cur-
rent time.

These interfaces are not specified in SUSv3, but are included in SUSv4.
The utimensat() system call updates the timestamps of the file specified by
pathname to the values specified in the array times.

#include <sys/time.h>

int futimes(int fd, const struct timeval tv[2]);
int lutimes(const char *pathname, const struct timeval tv[2]);
Both return 0 on success, or –1 on error

#define _XOPEN_SOURCE 700 /* Or define _POSIX_C_SOURCE >= 200809 */
#include <sys/stat.h>

int utimensat(int dirfd, const char *pathname,
const struct timespec times[2], int flags);
Returns 0 on success, or –1 on error
Free download pdf