写入文件对我来说非常重要 O_DIRECT标志。
这是我打开文件的方式:
//Open the file
int fd;
if((fd = open(inFilepath, O_WRONLY | O_CREAT |O_SYNC |O_DIRECT,S_IRUSR|S_IWUSR))<0) {
//Error handling
return;
}
Run Code Online (Sandbox Code Playgroud)
我知道 O_DIRECT 的对齐限制。这就是我用 calloc 初始化缓冲区的原因:
char *buff = (char *) calloc((size_t) 1,sizeof(char));
if(write(fd,buff,(size_t)1)<1) {
//Error logging
free(buff);
return -1;
}
Run Code Online (Sandbox Code Playgroud)
我得到了 write: Invalid argument错误。我什至尝试使用更极端的措施,例如 memalign 和 posix_memalign,但遇到了问题(memalign 卡住了,并且 ARM 处理器缺少 posix_memalign)。
当我注释掉 O_DIRECT标志时,一切正常(但 I/O 不是直接的,这是我需要的)。
任何人都知道为什么会发生这种情况?如果O_DIRECT没有在 Android 中实现,那么它应该在 失败open(),而不是在write(); 所以我一定是做错了什么!
谢谢-LD
c java-native-interface android invalid-argument android-ndk