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
perror()orstrerror()give you a human-readable error message appropriate forerrno.fsync()documented to support directory filedescriptors? I guess not. BTW: None of your macros are actually necessary, use inline functions instead.fsync()succeeded.O_DIRECTORYis documented as "should not be used outside of the implementation of opendir(3)"... Perhaps you should be usingopendir()instead...fsync()on directories...