4

Can anyone explain why fsync can return EINVAL when I pass folder descriptor in it? There is my code, it is pretty simple:

#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;
}

it gives me this output:

r = -1 22

I am using linux ver. 2.6.32 (Ubuntu 10.04 as I remember)

So why I got error when I call fsync on folder? When I call fsync with file descriptor passed in, everything is fine

13
  • perror() or strerror() give you a human-readable error message appropriate for errno.
    – user3185968
    Commented May 14, 2015 at 8:46
  • EINVAL means that a parameter given to a function is not suitable. Is fsync() documented to support directory filedescriptors? I guess not. BTW: None of your macros are actually necessary, use inline functions instead. Commented May 14, 2015 at 9:03
  • 2
    @UlrichEckhardt: I just tried the OP's code on a Wheezy-Debian and could not reproduce the failure. fsync() succeeded.
    – alk
    Commented May 14, 2015 at 9:17
  • 1
    O_DIRECTORY is documented as "should not be used outside of the implementation of opendir(3)"... Perhaps you should be using opendir() instead...
    – twalberg
    Commented May 14, 2015 at 17:49
  • 2
    @Lobster What type of file system does this directory reside on? Perhaps that particular file system type does not support fsync() on directories...
    – twalberg
    Commented May 15, 2015 at 13:43

1 Answer 1

0

In my case problem was because I have Ubuntu installed in VirtualBox and I run my program from shared folder. VirtualBox shared had vboxsf file system that doesn't support fsync on folder. Thanks for @twalberg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.