on 52182bc help sign in
project-m36/cbits/DirectoryFsync.c
51 lines 933 B history raw
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <unistd.h>
4#include <strings.h>
5#include <errno.h>
6#include <fcntl.h>
8#ifndef _DIRECTORY_FSYNC_
9#define _DIRECTORY_FSYNC_
10/* open a directory for reading and fsync the resultant fd */
11int cDirectoryFsync(char *path)
13 int fd = 0;
14 int ret = 0;
15 struct stat fdstat = {0};
17 fd = open(path, O_RDONLY);
18 if(fd < 0)
19 {
20 return errno;
21 }
23 /* ensure that opened fd is a directory fd. Otherwise, fsync will fail (on a read-only file descriptor */
24 ret = fstat(fd,&fdstat);
25 if(ret < 0)
26 {
27 close(fd);
28 return errno;
29 }
31 if(!S_ISDIR(fdstat.st_mode))
32 {
33 close(fd);
34 return ENOTDIR;
35 }
37 /* execute the fsync */
39#if defined(__APPLE__) && defined(__MACH__) && defined(F_FULLFSYNC)
40 ret = fcntl(fd, F_FULLFSYNC);
41#else
42 ret = fsync(fd);
43#endif
44 close(fd);
45 if(ret < 0)
46 {
47 return errno;
48 }
49 return 0;
51#endif