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 */
23 #include <sys/types.h>
29 /* The official name of this program (e.g., no 'g' prefix). */
30 #define PROGRAM_NAME "sync"
33 proper_name ("Jim Meyering"), \
34 proper_name ("Giuseppe Scrivano")
37 # define HAVE_SYNCFS 0
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
},
60 if (status
!= EXIT_SUCCESS
)
64 printf (_("Usage: %s [OPTION] [FILE]...\n"), program_name
);
66 Synchronize cached writes to persistent storage\n\
68 If one or more files are specified, sync only them,\n\
69 or their containing file systems.\n\
74 -d, --data sync only file data, no unneeded metadata\n\
77 -f, --file-system sync the file systems that contain the files\n\
80 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
81 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
82 emit_ancillary_info (PROGRAM_NAME
);
87 /* Sync the specified FILE, or file systems associated with FILE.
88 Return 1 on success. */
91 sync_arg (enum sync_mode mode
, char const *file
)
94 int open_flags
= O_RDONLY
| O_NONBLOCK
;
97 #if defined _AIX || defined __CYGWIN__
98 /* AIX 7.1, CYGWIN 2.9.0, fsync requires write access to file. */
99 if (mode
== MODE_FILE
)
100 open_flags
= O_WRONLY
| O_NONBLOCK
;
103 /* Note O_PATH might be supported with syncfs(),
104 though as of Linux 3.18 is not. */
105 fd
= open (file
, open_flags
);
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
);
115 error (0, rd_errno
, _("error opening %s"), quoteaf (file
));
120 /* We used O_NONBLOCK above to not hang with fifos,
121 so reset that here. */
122 int fdflags
= fcntl (fd
, F_GETFL
);
124 || fcntl (fd
, F_SETFL
, fdflags
& ~O_NONBLOCK
) < 0)
126 error (0, errno
, _("couldn't reset non-blocking mode %s"),
133 int sync_status
= -1;
138 sync_status
= fdatasync (fd
);
142 sync_status
= fsync (fd
);
146 case MODE_FILE_SYSTEM
:
147 sync_status
= syncfs (fd
);
152 assert ("invalid sync_mode");
157 error (0, errno
, _("error syncing %s"), quoteaf (file
));
164 error (0, errno
, _("failed to close %s"), quoteaf (file
));
172 main (int argc
, char **argv
)
176 bool arg_data
= false, arg_file_system
= false;
180 initialize_main (&argc
, &argv
);
181 set_program_name (argv
[0]);
182 setlocale (LC_ALL
, "");
183 bindtextdomain (PACKAGE
, LOCALEDIR
);
184 textdomain (PACKAGE
);
186 atexit (close_stdout
);
188 while ((c
= getopt_long (argc
, argv
, "df", long_options
, NULL
))
198 arg_file_system
= true;
201 case_GETOPT_HELP_CHAR
;
203 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
206 usage (EXIT_FAILURE
);
210 args_specified
= optind
< argc
;
212 if (arg_data
&& arg_file_system
)
214 die (EXIT_FAILURE
, 0,
215 _("cannot specify both --data and --file-system"));
218 if (!args_specified
&& arg_data
)
219 die (EXIT_FAILURE
, 0, _("--data needs at least one argument"));
221 if (! args_specified
|| (arg_file_system
&& ! HAVE_SYNCFS
))
223 else if (arg_file_system
)
224 mode
= MODE_FILE_SYSTEM
;
230 if (mode
== MODE_SYNC
)
234 for (; optind
< argc
; optind
++)
235 ok
&= sync_arg (mode
, argv
[optind
]);
238 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;