File: vzfsync.c

package info (click to toggle)
vzctl 4.8-1%2Bdeb8u2
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 4,000 kB
  • ctags: 3,612
  • sloc: ansic: 23,382; sh: 16,273; makefile: 549
file content (118 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download | duplicates (4)
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
/*
 *  Copyright (C) 2014, Parallels, Inc. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>

static int usage(int rc)
{
	FILE *out = (rc == 0)? stdout : stderr;

	fprintf(out,
"Usage: vzfsync {-s|-d} [-n] file [file ...]\n"
"       -s, --sync		do fsync()\n"
"       -d, --datasync		do fdatasync()\n"
"       -n, --dontneed		do fadvise(DONTNEED)\n"
	);

	return rc;
}

int main(int argc, char *argv[])
{
	int i;
	int fails = 0;
	int sync = 0, datasync = 0, dontneed = 0;
	struct option longopt[] = {
		{ "help",	no_argument, 0, 'h' },
		{ "sync",	no_argument, 0, 's' },
		{ "datasync",	no_argument, 0, 'd' },
		{ "dontneed",	no_argument, 0, 'n' },
		{}
	};

	while ((i = getopt_long(argc, argv, "hsdn", longopt, NULL)) != EOF) {
		switch (i) {
		   case 'h':
			return usage(0);
			break;
		   case 's':
			sync = 1;
			break;
		   case 'd':
			datasync = 1;
			break;
		   case 'n':
			dontneed = 1;
			break;
		   default:
			return usage(1);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc < 1)
		return usage(0);

	if (sync == 1 && datasync == 1) {
		fprintf(stderr, "Options -s and -d can't be used together\n");
		return usage(1);
	}
	if (sync == 0 && datasync == 0 && dontneed == 0) {
		fprintf(stderr, "No options given\n");
		return usage(1);
	}

	/* Process files one by one */
	for (; argc > 0; argc--, argv++) {
		const char *name = argv[0];
		int fd;

		fd = open(name, O_RDONLY|O_DIRECT);
		if (fd < 0) {
			fprintf(stderr, "Can't open %s: %m\n", name);
			fails++;
			continue;
		}

		if (sync && fsync(fd) < 0) {
			fprintf(stderr, "Error fsync(%s): %m\n", name);
			fails++;
		}
		if (datasync && fdatasync(fd) < 0) {
			fprintf(stderr, "Error fdatasync(%s): %m\n", name);
			fails++;
		}
		if (dontneed && posix_fadvise(fd, 0, 0,
					POSIX_FADV_DONTNEED) < 0) {
			fprintf(stderr, "Error fadvise(%s): %m\n", name);
			fails++;
		}

		if (close(fd) < 0) {
			fprintf(stderr, "Error close(%s): %m\n", name);
			fails++;
		}
	}

	return (fails == 0) ? 0 : 2;
}