【问题标题】:How to work around absence of futimes() in android (NDK)?如何解决 android (NDK) 中没有 futimes() 的问题?
【发布时间】:2013-10-15 06:40:32
【问题描述】:

我有一个需要futimesfutimens 函数的大型项目。不幸的是,android ndk包含文件夹的头文件中没有这样的功能。是否有解决方法(存根或使用现有函数的简单代码 sn-p)?

futimes 函数的文档可以在 here 找到。

【问题讨论】:

  • futime()函数是指记录文件修改时间的函数?

标签: android c android-ndk bionic


【解决方案1】:

futimes(3) 是一个非 POSIX 函数,它采用 struct timeval(秒,微秒)。 POSIX 版本是futimens(3),它需要struct timespec(秒,纳秒)。后者在仿生库中可用。

更新:恐怕我有点超前了。代码是checked into 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);
}

将这些添加到您的项目中,包括 utimensat.h 标头,您应该一切顺利。使用 NDK r9b 测试。

(这应该用适当的 ifdefs 包装(例如#ifndef HAVE_UTIMENSAT),以便您可以在 NDK 赶上时禁用它。)

更新: AOSP 更改 here

【讨论】:

  • 我在独立工具链文件夹(使用 make-standalone-toolchain 脚本)和 ndk 文件夹中都找不到任何带有 futimens 定义的头文件。使用 ndk-r9(最新可用)
  • utimensat 对我不起作用。当我 grep 遍历我的 android-ndk-r9 文件夹时,我既找不到 utimensat 也找不到 futimenssys/stat.h 中都没有定义。有什么特别的我需要包括的吗?
猜你喜欢
  • 2011-03-16
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 2012-01-24
相关资源
最近更新 更多