build: update gnulib submodule to latest
[coreutils.git] / src / sync.c
blob5e1dbb8c61eb64658da60603f04a899f4f3adda1
1 /* sync - update the super block
2 Copyright (C) 1994-2015 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering */
19 #include <config.h>
20 #include <assert.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "error.h"
27 #include "quote.h"
29 /* The official name of this program (e.g., no 'g' prefix). */
30 #define PROGRAM_NAME "sync"
32 #define AUTHORS \
33 proper_name ("Jim Meyering"), \
34 proper_name ("Giuseppe Scrivano")
36 #ifndef HAVE_SYNCFS
37 # define HAVE_SYNCFS 0
38 #endif
40 enum sync_mode
42 MODE_FILE,
43 MODE_DATA,
44 MODE_FILE_SYSTEM,
45 MODE_SYNC
48 static struct option const long_options[] =
50 {"data", no_argument, NULL, 'd'},
51 {"file-system", no_argument, NULL, 'f'},
52 {GETOPT_HELP_OPTION_DECL},
53 {GETOPT_VERSION_OPTION_DECL},
54 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != EXIT_SUCCESS)
61 emit_try_help ();
62 else
64 printf (_("Usage: %s [OPTION] [FILE]...\n"), program_name);
65 fputs (_("\
66 Synchronize cached writes to persistent storage\n\
67 \n\
68 If one or more files are specified, sync only them,\n\
69 or their containing file systems.\n\
70 \n\
71 "), stdout);
73 fputs (_("\
74 -d, --data sync only file data, no unneeded metadata\n\
75 "), stdout);
76 fputs (_("\
77 -f, --file-system sync the file systems that contain the files\n\
78 "), stdout);
80 fputs (HELP_OPTION_DESCRIPTION, stdout);
81 fputs (VERSION_OPTION_DESCRIPTION, stdout);
82 emit_ancillary_info (PROGRAM_NAME);
84 exit (status);
87 /* Sync the specified FILE, or file systems associated with FILE.
88 Return 1 on success. */
90 static bool
91 sync_arg (enum sync_mode mode, char const *file)
93 bool ret = true;
94 int open_flags = O_RDONLY | O_NONBLOCK;
95 int fd;
97 #ifdef _AIX
98 /* AIX 7.1 fsync requires write access to file. */
99 if (mode == MODE_FILE)
100 open_flags = O_WRONLY | O_NONBLOCK;
101 #endif
103 /* Note O_PATH might be supported with syncfs(),
104 though as of Linux 3.18 is not. */
105 fd = open (file, open_flags);
106 if (fd < 0)
108 /* Use the O_RDONLY errno, which is significant
109 with directories for example. */
110 int rd_errno = errno;
111 if (open_flags != (O_WRONLY | O_NONBLOCK))
112 fd = open (file, O_WRONLY | O_NONBLOCK);
113 if (fd < 0)
114 error (0, rd_errno, _("error opening %s"), quote (file));
115 return false;
118 /* We used O_NONBLOCK above to not hang with fifos,
119 so reset that here. */
120 int fdflags = fcntl (fd, F_GETFL);
121 if (fdflags == -1
122 || fcntl (fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
124 error (0, errno, _("couldn't reset non-blocking mode %s"), quote (file));
125 ret = false;
128 if (ret == true)
130 int sync_status = -1;
132 switch (mode)
134 case MODE_DATA:
135 sync_status = fdatasync (fd);
136 break;
138 case MODE_FILE:
139 sync_status = fsync (fd);
140 break;
142 #if HAVE_SYNCFS
143 case MODE_FILE_SYSTEM:
144 sync_status = syncfs (fd);
145 break;
146 #endif
148 default:
149 assert ("invalid sync_mode");
152 if (sync_status < 0)
154 error (0, errno, _("error syncing %s"), quote (file));
155 ret = false;
159 if (close (fd) < 0)
161 error (0, errno, _("failed to close %s"), quote (file));
162 ret = false;
165 return ret;
169 main (int argc, char **argv)
171 int c;
172 bool args_specified;
173 bool arg_data = false, arg_file_system = false;
174 enum sync_mode mode;
175 bool ok = true;
177 initialize_main (&argc, &argv);
178 set_program_name (argv[0]);
179 setlocale (LC_ALL, "");
180 bindtextdomain (PACKAGE, LOCALEDIR);
181 textdomain (PACKAGE);
183 atexit (close_stdout);
185 while ((c = getopt_long (argc, argv, "df", long_options, NULL))
186 != -1)
188 switch (c)
190 case 'd':
191 arg_data = true;
192 break;
194 case 'f':
195 arg_file_system = true;
196 break;
198 case_GETOPT_HELP_CHAR;
200 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
202 default:
203 usage (EXIT_FAILURE);
207 args_specified = optind < argc;
209 if (arg_data && arg_file_system)
211 error (EXIT_FAILURE, 0,
212 _("cannot specify both --data and --file-system"));
215 if (!args_specified && arg_data)
216 error (EXIT_FAILURE, 0, _("--data needs at least one argument"));
218 if (! args_specified || (arg_file_system && ! HAVE_SYNCFS))
219 mode = MODE_SYNC;
220 else if (arg_file_system)
221 mode = MODE_FILE_SYSTEM;
222 else if (! arg_data)
223 mode = MODE_FILE;
224 else
225 mode = MODE_DATA;
227 if (mode == MODE_SYNC)
228 sync ();
229 else
231 for (; optind < argc; optind++)
232 ok &= sync_arg (mode, argv[optind]);
235 return ok ? EXIT_SUCCESS : EXIT_FAILURE;