4nt*_*ine 2 c android android-ndk bionic
我有需要futimes
或futimens
功能的大型项目.不幸的是,android ndk include文件夹中的头文件中没有这样的函数.是否有解决方法(使用现有函数的存根或简单代码片段)?
futimes
可以在此处找到该功能的文档.
futimes(3)
是一个非POSIX函数,需要一个struct timeval
(秒,微秒).POSIX版本futimens(3)
需要struct timespec
(秒,纳秒).后者可用于仿生libc.
更新:我担心自己会领先一步.代码已检入AOSP但尚未提供.
但是......如果你看一下代码,那就futimens(fd, times)
实现了utimensat(fd, NULL, times, 0)
,那里utimensat()
的Linux系统调用似乎确实在NDK中定义了.因此,您应该能够futimens()
基于系统调用提供自己的实现.
更新:它成为仿生而不是NDK.以下是如何自己动手:
// ----- 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);
}
Run Code Online (Sandbox Code Playgroud)
将它们添加到您的项目中,包括utimensat.h标题,您应该很高兴.用NDK r9b测试.
(这应该用适当的ifdef(例如#ifndef HAVE_UTIMENSAT
)包装,这样你可以在NDK赶上时禁用它.)
更新: AOSP 在此更改.