如何解决android(NDK)中futimes()的缺失?
我有大项目需要futimes
或futimens
功能。不幸的是,在android ndk include文件夹的头文件中没有这样的函数。是否有解决方法(使用现有函数的存根或简单代码片段)?对于futimes
功能如何解决android(NDK)中futimes()的缺失?
文档可以发现here。
futimes(3)
是非POSIX函数,它接受一个struct timeval
(秒,微秒)。 POSIX版本是futimens(3)
,它需要struct timespec
(秒,纳秒)。后者在仿生libc中可用。
更新:恐怕我有一点点提前了。代码是checked into AOSP但尚未提供。
但是,如果您查看代码,则futimens(fd, times)
实施为utimensat(fd, NULL, times, 0)
,其中utimensat()
是看起来在NDK中定义的Linux系统调用。所以你应该能够根据系统调用提供你自己的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);
}
那些添加到您的项目,包括utimensat.h头,你应该是好去。用NDK r9b进行测试。
(这应该有适当的ifdef(例如#ifndef HAVE_UTIMENSAT
)包裹所以当NDK赶上你可以禁用它。)
更新: AOSP变化here。
我无法在独立工具链文件夹(使用'make-standalone-toolchain'脚本)或ndk文件夹中找到任何带'futimens'定义的头文件。使用ndk-r9(最新版本) – 4ntoine
哎呦。答案已更新。 – fadden
'utimensat'不适合我。当我在我的android-ndk-r9文件夹中进行grep时,我找不到'utimensat'和'futimens'。 'sys/stat.h'中没有定义。我需要包括什么特别的东西? – codingFriend1
通过'futime()'函数你的意思是一个函数,记录文件的修改时间? – Kerry