pcu.git  about / heads / tags
page cache utilities for Linux
blob ebf039fb1a3de1a0e5e7f3b765e8d00d19c22c20 2643 bytes (raw)
$ git show HEAD:fsync.c	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 
#include "compat-util.h"
#include <dirent.h>
#include <libgen.h>
#include <dlfcn.h>

#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
static int have_fdatasync = 1;
#else
static int have_fdatasync;
#endif

/* TODO: sync_file_range() if on Linux */

static int usage(const char * argv0)
{
	fprintf(stderr, "Usage: %s [-d] [-D] FILE...\n", argv0);
	return 1;
}

#define FN_NOT_FOUND ((void *)(1))

static int fs_sync(const char *path)
{
	int rc = 0;
#if defined(__linux__) && defined(RTLD_NEXT)
	static int (*syncfs_fn)(int);

	if (syncfs_fn == NULL) {
		syncfs_fn = dlsym(RTLD_DEFAULT, "syncfs");
		if (syncfs_fn == NULL || dlerror())
			syncfs_fn = FN_NOT_FOUND;
	}
	if (syncfs_fn != NULL && syncfs_fn != FN_NOT_FOUND) {
		int fd = open(path, O_RDONLY|O_NOATIME);

		if (fd >= 0) {
			int err;
			rc = syncfs_fn(fd);
			err = errno;
			close(fd);

			/*
			 * if glibc has syncfs(2) but we're running an
			 * old kernel, fall back to sync(2) below
			 */
			if (err != ENOSYS)
				return rc;
			rc = 0;
		}
	}
#endif /* ! __linux__ */
	sync();
	return rc;
}

static int do_sync(const char *path, int data_only, int directory)
{
	int fd;
	const char *errfunc = "";

	if ((fd = open(path, O_RDWR|O_NOATIME)) < 0) {
		if (errno == EISDIR) {
			directory = 1;
			goto sync_dir;
		}
		errfunc = "open";
		goto err;
	}

	if (data_only && have_fdatasync) {
		if (fdatasync(fd) < 0) {
			errfunc = "fdatasync";
			goto err;
		}
	} else {
		if (fsync(fd) < 0) {
			errfunc = "fsync";
			goto err;
		}
	}

	if (close(fd) < 0) {
		errfunc = "close";
		goto err;
	}

sync_dir:
	if (directory) {
		char *dpath;
		DIR *dir;

		if (!(dpath = strdup(path))){
			errfunc = "strdup";
			goto err;
		}
		if (!(dir = opendir(dirname(dpath)))) {
			errfunc = "opendir";
			goto err;
		}
		if (fsync(dirfd(dir)) < 0) {
			errfunc = "(directory) fsync";
			goto err;
		}
		if (closedir(dir) < 0) {
			errfunc = "closedir";
			goto err;
		}
		free(dpath);
	}

	return 0;
err:
	fprintf(stderr, "%s: %s(): %s\n", path, errfunc, strerror(errno));
	return -1;
}

int main(int argc, char * const argv[])
{
	int data_only = 0;
	int fs = 0;
	int directory = 0;
	int opt;
	int argi = 1;

	while ((opt = getopt(argc, argv, "dDf")) != -1) {
		++argi;
		switch(opt) {
		case 'd':
			data_only = 1;
			break;
		case 'D':
			directory = 1;
			break;
		case 'f':
			fs = 1;
			break;
		default:
			return usage(argv[0]);
		}
	}

	if (argi >= argc)
		return usage(argv[0]);

	for (; argi < argc; ++argi) {
		if (fs) {
			if (fs_sync(argv[argi]) < 0)
				return 1;
		} else {
			if (do_sync(argv[argi], data_only, directory) < 0)
				return 1;
		}
	}

	return 0;
}

git clone https://yhbt.net/pcu.git