Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在新创建的文件夹上进行fsync?_C_Linux_Unix - Fatal编程技术网

在新创建的文件夹上进行fsync?

在新创建的文件夹上进行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

有人能解释为什么当我在其中传递文件夹描述符时,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 <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