Question

I have large project which needs futimes or futimens function. Unfortunately there are no such functions in header files in android ndk include folder. Is there a work-around (stub or simple code snippet using existing functions)?

Documentation for the futimes function can be found here.

Was it helpful?

Solution

futimes(3) is a non-POSIX function that takes a struct timeval (seconds, microseconds). The POSIX version is futimens(3), which takes a struct timespec (seconds, nanoseconds). The latter is available in bionic libc.

Update: I'm afraid I got a little ahead of myself. The code is checked into AOSP but isn't available yet.

However... if you look at the code, futimens(fd, times) is implemented as utimensat(fd, NULL, times, 0), where utimensat() is a Linux system call that does appear to be defined in the NDK. So you should be able to provide your own implementation of futimens() based on the syscall.

Update: It made it into bionic but not the NDK. Here's how to roll your own:

// ----- utimensat.h -----
#include <sys/stat.h>
#ifdef __cplusplus
extern "C" {
#endif
int utimensat(int dirfd, const char *pathname,
        const struct timespec times[2], int flags);
int futimens(int fd, const struct timespec times[2]);
#ifdef __cplusplus
}
#endif

// ----- utimensat.c -----
#include <sys/syscall.h>
#include "utimensat.h"
int utimensat(int dirfd, const char *pathname,
        const struct timespec times[2], int flags) {
    return syscall(__NR_utimensat, dirfd, pathname, times, flags);
}
int futimens(int fd, const struct timespec times[2]) {
    return utimensat(fd, NULL, times, 0);
}

Add those to your project, include the utimensat.h header, and you should be good to go. Tested with NDK r9b.

(This should be wrapped with appropriate ifdefs (e.g. #ifndef HAVE_UTIMENSAT) so you can disable it when the NDK catches up.)

Update: AOSP change here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top