How to work around absence of futimes() in android (NDK)?
Asked Answered
L

1

3

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.

Leaving answered 15/10, 2013 at 6:40 Comment(1)
By futime() function you mean a function that records the modification time of a file?Bunting
G
7

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.

Gatt answered 15/10, 2013 at 16:33 Comment(2)
I was not able to find any header file with futimens definition in neither standalone toolchain folder (used make-standalone-toolchain script) nor ndk folder. Used ndk-r9 (the latest available)Leaving
utimensat does not work for me. When I grep over my android-ndk-r9 folder I can neither find utimensat nor futimens. Neither is defined in sys/stat.h. Is there anything special I need to include?Foscalina

© 2022 - 2024 — McMap. All rights reserved.