Example below shows how to obtain file descriptor for directory to use it with fsync:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
int main() {
int fd;
DIR *dirp;
int error;
char path[] = "test_dir";
if ((dirp = opendir(path)) == 0) {
printf("Failed to open dir: %d\n", errno);
return -1;
}
fd = dirfd(dirp);
if (fd == -1) {
printf("Failed to open: %d\n", errno);
return -1;
}
if (fsync(fd) == -1) {
printf("fsync_range failed: %d\n", errno);
return -1;
}
return 0;
}
0 comments:
Post a Comment