on request_timeout help sign in
project-m36/cbits/DirectoryFsync.c
48 lines 886 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 return errno;
28 }
30 if(!S_ISDIR(fdstat.st_mode))
31 {
32 return ENOTDIR;
33 }
35 /* execute the fsync */
37#if defined(__APPLE__) && defined(__MACH__) && defined(F_FULLFSYNC)
38 ret = fcntl(fd, F_FULLFSYNC);
39#else
40 ret = fsync(fd);
41#endif
42 if(ret < 0)
43 {
44 return errno;
45 }
46 return 0;
48#endif