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 */
22 #include <sys/types.h>
28 /* The official name of this program (e.g., no 'g' prefix). */
29 #define PROGRAM_NAME "sync"
32 proper_name ("Jim Meyering"), \
33 proper_name ("Giuseppe Scrivano")
36 # define HAVE_SYNCFS 0
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}
59 if (status
!= EXIT_SUCCESS
)
63 printf (_("Usage: %s [OPTION] [FILE]...\n"), program_name
);
65 Synchronize cached writes to persistent storage\n\
67 If one or more files are specified, sync only them,\n\
68 or their containing file systems.\n\
73 -d, --data sync only file data, no unneeded metadata\n\
76 -f, --file-system sync the file systems that contain the files\n\
79 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
80 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
81 emit_ancillary_info (PROGRAM_NAME
);
86 /* Sync the specified FILE, or file systems associated with FILE.
87 Return 1 on success. */
90 sync_arg (enum sync_mode mode
, char const *file
)
93 int open_flags
= O_RDONLY
| O_NONBLOCK
;
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
;
102 /* Note O_PATH might be supported with syncfs(),
103 though as of Linux 3.18 is not. */
104 fd
= open (file
, open_flags
);
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
);
114 error (0, rd_errno
, _("error opening %s"), quoteaf (file
));
119 /* We used O_NONBLOCK above to not hang with fifos,
120 so reset that here. */
121 int fdflags
= fcntl (fd
, F_GETFL
);
123 || fcntl (fd
, F_SETFL
, fdflags
& ~O_NONBLOCK
) < 0)
125 error (0, errno
, _("couldn't reset non-blocking mode %s"),
132 int sync_status
= -1;
137 sync_status
= fdatasync (fd
);
141 sync_status
= fsync (fd
);
145 case MODE_FILE_SYSTEM
:
146 sync_status
= syncfs (fd
);
156 error (0, errno
, _("error syncing %s"), quoteaf (file
));
163 error (0, errno
, _("failed to close %s"), quoteaf (file
));
171 main (int argc
, char **argv
)
175 bool arg_data
= false, arg_file_system
= false;
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))
197 arg_file_system
= true;
200 case_GETOPT_HELP_CHAR
;
202 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
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
))
222 else if (arg_file_system
)
223 mode
= MODE_FILE_SYSTEM
;
229 if (mode
== MODE_SYNC
)
233 for (; optind
< argc
; optind
++)
234 ok
&= sync_arg (mode
, argv
[optind
]);
237 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;