maint: improve static and dynamic checking
[coreutils.git] / src / sync.c
blob07b4392b35bbf25816a76614fdeaded2b3bd4e06
1 /* sync - update the super block
2 Copyright (C) 1994-2023 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "die.h"
26 #include "error.h"
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "sync"
31 #define AUTHORS \
32 proper_name ("Jim Meyering"), \
33 proper_name ("Giuseppe Scrivano")
35 #ifndef HAVE_SYNCFS
36 # define HAVE_SYNCFS 0
37 #endif
39 enum sync_mode
41 MODE_FILE,
42 MODE_DATA,
43 MODE_FILE_SYSTEM,
44 MODE_SYNC
47 static struct option const long_options[] =
49 {"data", no_argument, nullptr, 'd'},
50 {"file-system", no_argument, nullptr, 'f'},
51 {GETOPT_HELP_OPTION_DECL},
52 {GETOPT_VERSION_OPTION_DECL},
53 {nullptr, 0, nullptr, 0}
56 void
57 usage (int status)
59 if (status != EXIT_SUCCESS)
60 emit_try_help ();
61 else
63 printf (_("Usage: %s [OPTION] [FILE]...\n"), program_name);
64 fputs (_("\
65 Synchronize cached writes to persistent storage\n\
66 \n\
67 If one or more files are specified, sync only them,\n\
68 or their containing file systems.\n\
69 \n\
70 "), stdout);
72 fputs (_("\
73 -d, --data sync only file data, no unneeded metadata\n\
74 "), stdout);
75 fputs (_("\
76 -f, --file-system sync the file systems that contain the files\n\
77 "), stdout);
79 fputs (HELP_OPTION_DESCRIPTION, stdout);
80 fputs (VERSION_OPTION_DESCRIPTION, stdout);
81 emit_ancillary_info (PROGRAM_NAME);
83 exit (status);
86 /* Sync the specified FILE, or file systems associated with FILE.
87 Return 1 on success. */
89 static bool
90 sync_arg (enum sync_mode mode, char const *file)
92 bool ret = true;
93 int open_flags = O_RDONLY | O_NONBLOCK;
94 int fd;
96 #if defined _AIX || defined __CYGWIN__
97 /* AIX 7.1, CYGWIN 2.9.0, fsync requires write access to file. */
98 if (mode == MODE_FILE)
99 open_flags = O_WRONLY | O_NONBLOCK;
100 #endif
102 /* Note O_PATH might be supported with syncfs(),
103 though as of Linux 3.18 is not. */
104 fd = open (file, open_flags);
105 if (fd < 0)
107 /* Use the O_RDONLY errno, which is significant
108 with directories for example. */
109 int rd_errno = errno;
110 if (open_flags != (O_WRONLY | O_NONBLOCK))
111 fd = open (file, O_WRONLY | O_NONBLOCK);
112 if (fd < 0)
114 error (0, rd_errno, _("error opening %s"), quoteaf (file));
115 return false;
119 /* We used O_NONBLOCK above to not hang with fifos,
120 so reset that here. */
121 int fdflags = fcntl (fd, F_GETFL);
122 if (fdflags == -1
123 || fcntl (fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
125 error (0, errno, _("couldn't reset non-blocking mode %s"),
126 quoteaf (file));
127 ret = false;
130 if (ret == true)
132 int sync_status = -1;
134 switch (mode)
136 case MODE_DATA:
137 sync_status = fdatasync (fd);
138 break;
140 case MODE_FILE:
141 sync_status = fsync (fd);
142 break;
144 #if HAVE_SYNCFS
145 case MODE_FILE_SYSTEM:
146 sync_status = syncfs (fd);
147 break;
148 #endif
150 default:
151 unreachable ();
154 if (sync_status < 0)
156 error (0, errno, _("error syncing %s"), quoteaf (file));
157 ret = false;
161 if (close (fd) < 0)
163 error (0, errno, _("failed to close %s"), quoteaf (file));
164 ret = false;
167 return ret;
171 main (int argc, char **argv)
173 int c;
174 bool args_specified;
175 bool arg_data = false, arg_file_system = false;
176 enum sync_mode mode;
177 bool ok = true;
179 initialize_main (&argc, &argv);
180 set_program_name (argv[0]);
181 setlocale (LC_ALL, "");
182 bindtextdomain (PACKAGE, LOCALEDIR);
183 textdomain (PACKAGE);
185 atexit (close_stdout);
187 while ((c = getopt_long (argc, argv, "df", long_options, nullptr))
188 != -1)
190 switch (c)
192 case 'd':
193 arg_data = true;
194 break;
196 case 'f':
197 arg_file_system = true;
198 break;
200 case_GETOPT_HELP_CHAR;
202 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
204 default:
205 usage (EXIT_FAILURE);
209 args_specified = optind < argc;
211 if (arg_data && arg_file_system)
213 die (EXIT_FAILURE, 0,
214 _("cannot specify both --data and --file-system"));
217 if (!args_specified && arg_data)
218 die (EXIT_FAILURE, 0, _("--data needs at least one argument"));
220 if (! args_specified || (arg_file_system && ! HAVE_SYNCFS))
221 mode = MODE_SYNC;
222 else if (arg_file_system)
223 mode = MODE_FILE_SYSTEM;
224 else if (! arg_data)
225 mode = MODE_FILE;
226 else
227 mode = MODE_DATA;
229 if (mode == MODE_SYNC)
230 sync ();
231 else
233 for (; optind < argc; optind++)
234 ok &= sync_arg (mode, argv[optind]);
237 return ok ? EXIT_SUCCESS : EXIT_FAILURE;