on bda2cfc help sign in
project-m36/ProjectM36/DirectoryFsync.c
44 lines 769 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 */
36 ret = fsync(fd);
37 if(ret < 0)
38 {
39 return errno;
40 }
42 return 0;
44#endif