Atlas Runtime
fsync.hpp
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2016 Hewlett Packard Enterprise Development LP
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version. This program is
8  * distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11  * for more details. You should have received a copy of the GNU Lesser
12  * General Public License along with this program. If not, see
13  * <http://www.gnu.org/licenses/>.
14  */
15 
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 
26 #define FP (void)fprintf
27 
28 static void bail(char *msg, int line) {
29  FP(stderr, "%s:%d: %s: %s\n", __FILE__, line, msg, strerror(errno));
30  exit(EXIT_FAILURE);
31 }
32 #define BAIL(msg) bail(msg, __LINE__)
33 
34 static void trim_rightmost_path_component(char *p) {
35  char *s = p + strlen(p) - 1;
36  if ('/' == *s)
37  *s-- = '\0';
38  while (s > p && '/' != *s)
39  s--;
40  *++s = '\0';
41 }
42 
43 int fsync_paranoid(const char *name) {
44  char rp[1+PATH_MAX], *file = (char *) malloc(sizeof(char)*strlen(name)+1);
45  strcpy(file, name);
46  FP(stderr, "fsync to root '%s'\n", file);
47  if (NULL == realpath(file, rp)) BAIL("realpath failed");
48  FP(stderr, " realpath '%s'\n", rp);
49  do {
50  int fd;
51  FP(stderr, " fsync-ing '%s'\n", rp);
52  if (-1 == (fd = open(rp, O_RDONLY))) BAIL("open failed");
53  if (-1 == fsync(fd)) BAIL("fsync failed");
54  if (-1 == close(fd)) BAIL("close failed");
55  trim_rightmost_path_component(rp);
56  } while (*rp);
57  FP(stderr, " done\n");
58  free(file);
59  return 0;
60 }
#define FP
Definition: fsync.hpp:26
int fsync_paranoid(const char *name)
Definition: fsync.hpp:43
#define BAIL(msg)
Definition: fsync.hpp:32