在新创建的文件夹上进行fsync?
有人能解释为什么当我在其中传递文件夹描述符时,fsync可以返回EINVAL吗?这是我的代码,非常简单:在新创建的文件夹上进行fsync?,c,linux,unix,C,Linux,Unix,有人能解释为什么当我在其中传递文件夹描述符时,fsync可以返回EINVAL吗?这是我的代码,非常简单: #include <dirent.h> /* Defines DT_* constants */ #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> #include
#include <dirent.h> /* Defines DT_* constants */
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <errno.h>
#define handle_error(msg) \
do { trace(msg); exit(0); } while (0)
#define trace printf
int createDir(const char* name) {
int r = ::mkdir( name, 0777 );
if (r != 0) {
trace("error r!=0 %d\n",errno);
}
r = open(name, O_RDONLY | O_DIRECTORY);
if (r < 0) {
trace("error create dir r <0\n");
}
return r;
}
int main(int argc, const char * argv[]) {
int r;
int dir = createDir("test");
r = fsync(dir);
trace("r = %d %d\n",r,errno);
close(dir);
return 0;
}
我正在使用linux版本。2.6.32(我记得Ubuntu 10.04)
为什么我在调用文件夹上的fsync时出错?当我在传入文件描述符的情况下调用fsync时,一切正常在我的例子中,问题是因为我在VirtualBox中安装了Ubuntu,并且我从共享文件夹运行我的程序。VirtualBox共享的vboxsf文件系统不支持文件夹上的fsync。感谢@twalberg
perror()
或strerror()
为您提供适合errno
的可读错误消息。EINVAL表示给定给函数的参数不合适。fsync()
是否有文档记录以支持目录文件描述符?我想不是。顺便说一句:您的宏实际上都不是必需的,请改用内联函数。@UlrichEckhardt:我只是在一个气喘吁吁的Debian上尝试了OP的代码,但无法重现失败fsync()
成功。O_目录
被记录为“不应在opendir(3)实现之外使用”。。。也许您应该使用opendir()
来代替…@Lobster此目录驻留在什么类型的文件系统上?可能那个特定的文件系统类型不支持目录上的fsync()
。。。
r = -1 22