| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/sync.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* sync - update the super block | - | ||||||||||||
| 2 | Copyright (C) 1994-2018 Free Software Foundation, Inc. | - | ||||||||||||
| 3 | - | |||||||||||||
| 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. | - | ||||||||||||
| 8 | - | |||||||||||||
| 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. | - | ||||||||||||
| 13 | - | |||||||||||||
| 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/>. */ | - | ||||||||||||
| 16 | - | |||||||||||||
| 17 | /* Written by Jim Meyering */ | - | ||||||||||||
| 18 | - | |||||||||||||
| 19 | #include <config.h> | - | ||||||||||||
| 20 | #include <assert.h> | - | ||||||||||||
| 21 | #include <getopt.h> | - | ||||||||||||
| 22 | #include <stdio.h> | - | ||||||||||||
| 23 | #include <sys/types.h> | - | ||||||||||||
| 24 | - | |||||||||||||
| 25 | #include "system.h" | - | ||||||||||||
| 26 | #include "die.h" | - | ||||||||||||
| 27 | #include "error.h" | - | ||||||||||||
| 28 | - | |||||||||||||
| 29 | /* The official name of this program (e.g., no 'g' prefix). */ | - | ||||||||||||
| 30 | #define PROGRAM_NAME "sync" | - | ||||||||||||
| 31 | - | |||||||||||||
| 32 | #define AUTHORS \ | - | ||||||||||||
| 33 | proper_name ("Jim Meyering"), \ | - | ||||||||||||
| 34 | proper_name ("Giuseppe Scrivano") | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #ifndef HAVE_SYNCFS | - | ||||||||||||
| 37 | # define HAVE_SYNCFS 0 | - | ||||||||||||
| 38 | #endif | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | enum sync_mode | - | ||||||||||||
| 41 | { | - | ||||||||||||
| 42 | MODE_FILE, | - | ||||||||||||
| 43 | MODE_DATA, | - | ||||||||||||
| 44 | MODE_FILE_SYSTEM, | - | ||||||||||||
| 45 | MODE_SYNC | - | ||||||||||||
| 46 | }; | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | static struct option const long_options[] = | - | ||||||||||||
| 49 | { | - | ||||||||||||
| 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} | - | ||||||||||||
| 55 | }; | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | void | - | ||||||||||||
| 58 | usage (int status) | - | ||||||||||||
| 59 | { | - | ||||||||||||
| 60 | if (status != EXIT_SUCCESS)
| 3-7 | ||||||||||||
| 61 | emit_try_help (); executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||
| 62 | else | - | ||||||||||||
| 63 | { | - | ||||||||||||
| 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); | - | ||||||||||||
| 72 | - | |||||||||||||
| 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); | - | ||||||||||||
| 79 | - | |||||||||||||
| 80 | fputs (HELP_OPTION_DESCRIPTION, stdout); | - | ||||||||||||
| 81 | fputs (VERSION_OPTION_DESCRIPTION, stdout); | - | ||||||||||||
| 82 | emit_ancillary_info (PROGRAM_NAME); | - | ||||||||||||
| 83 | } executed 7 times by 1 test: end of blockExecuted by:
| 7 | ||||||||||||
| 84 | exit (status); executed 10 times by 1 test: exit (status);Executed by:
| 10 | ||||||||||||
| 85 | } | - | ||||||||||||
| 86 | - | |||||||||||||
| 87 | /* Sync the specified FILE, or file systems associated with FILE. | - | ||||||||||||
| 88 | Return 1 on success. */ | - | ||||||||||||
| 89 | - | |||||||||||||
| 90 | static bool | - | ||||||||||||
| 91 | sync_arg (enum sync_mode mode, char const *file) | - | ||||||||||||
| 92 | { | - | ||||||||||||
| 93 | bool ret = true; | - | ||||||||||||
| 94 | int open_flags = O_RDONLY | O_NONBLOCK; | - | ||||||||||||
| 95 | int fd; | - | ||||||||||||
| 96 | - | |||||||||||||
| 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 | - | ||||||||||||
| 102 | - | |||||||||||||
| 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)
| 2-4 | ||||||||||||
| 107 | { | - | ||||||||||||
| 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))
| 0-2 | ||||||||||||
| 112 | fd = open (file, O_WRONLY | O_NONBLOCK); executed 2 times by 1 test: fd = open (file, 01 | 04000 );Executed by:
| 2 | ||||||||||||
| 113 | if (fd < 0)
| 0-2 | ||||||||||||
| 114 | error (0, rd_errno, _("error opening %s"), quoteaf (file)); executed 2 times by 1 test: error (0, rd_errno, dcgettext (((void *)0), "error opening %s" , 5) , quotearg_style (shell_escape_always_quoting_style, file));Executed by:
| 2 | ||||||||||||
| 115 | return false; executed 2 times by 1 test: return 0 ;Executed by:
| 2 | ||||||||||||
| 116 | } | - | ||||||||||||
| 117 | - | |||||||||||||
| 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
| 0-4 | ||||||||||||
| 122 | || fcntl (fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
| 0-4 | ||||||||||||
| 123 | { | - | ||||||||||||
| 124 | error (0, errno, _("couldn't reset non-blocking mode %s"), | - | ||||||||||||
| 125 | quoteaf (file)); | - | ||||||||||||
| 126 | ret = false; | - | ||||||||||||
| 127 | } never executed: end of block | 0 | ||||||||||||
| 128 | - | |||||||||||||
| 129 | if (ret == true)
| 0-4 | ||||||||||||
| 130 | { | - | ||||||||||||
| 131 | int sync_status = -1; | - | ||||||||||||
| 132 | - | |||||||||||||
| 133 | switch (mode) | - | ||||||||||||
| 134 | { | - | ||||||||||||
| 135 | case MODE_DATA: never executed: case MODE_DATA: | 0 | ||||||||||||
| 136 | sync_status = fdatasync (fd); | - | ||||||||||||
| 137 | break; never executed: break; | 0 | ||||||||||||
| 138 | - | |||||||||||||
| 139 | case MODE_FILE: executed 4 times by 1 test: case MODE_FILE:Executed by:
| 4 | ||||||||||||
| 140 | sync_status = fsync (fd); | - | ||||||||||||
| 141 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 142 | - | |||||||||||||
| 143 | #if HAVE_SYNCFS | - | ||||||||||||
| 144 | case MODE_FILE_SYSTEM: never executed: case MODE_FILE_SYSTEM: | 0 | ||||||||||||
| 145 | sync_status = syncfs (fd); | - | ||||||||||||
| 146 | break; never executed: break; | 0 | ||||||||||||
| 147 | #endif | - | ||||||||||||
| 148 | - | |||||||||||||
| 149 | default: never executed: default: | 0 | ||||||||||||
| 150 | assert ("invalid sync_mode"); | - | ||||||||||||
| 151 | } never executed: end of block | 0 | ||||||||||||
| 152 | - | |||||||||||||
| 153 | if (sync_status < 0)
| 1-3 | ||||||||||||
| 154 | { | - | ||||||||||||
| 155 | error (0, errno, _("error syncing %s"), quoteaf (file)); | - | ||||||||||||
| 156 | ret = false; | - | ||||||||||||
| 157 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 158 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||
| 159 | - | |||||||||||||
| 160 | if (close (fd) < 0)
| 0-4 | ||||||||||||
| 161 | { | - | ||||||||||||
| 162 | error (0, errno, _("failed to close %s"), quoteaf (file)); | - | ||||||||||||
| 163 | ret = false; | - | ||||||||||||
| 164 | } never executed: end of block | 0 | ||||||||||||
| 165 | - | |||||||||||||
| 166 | return ret; executed 4 times by 1 test: return ret;Executed by:
| 4 | ||||||||||||
| 167 | } | - | ||||||||||||
| 168 | - | |||||||||||||
| 169 | int | - | ||||||||||||
| 170 | main (int argc, char **argv) | - | ||||||||||||
| 171 | { | - | ||||||||||||
| 172 | int c; | - | ||||||||||||
| 173 | bool args_specified; | - | ||||||||||||
| 174 | bool arg_data = false, arg_file_system = false; | - | ||||||||||||
| 175 | enum sync_mode mode; | - | ||||||||||||
| 176 | bool ok = true; | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | initialize_main (&argc, &argv); | - | ||||||||||||
| 179 | set_program_name (argv[0]); | - | ||||||||||||
| 180 | setlocale (LC_ALL, ""); | - | ||||||||||||
| 181 | bindtextdomain (PACKAGE, LOCALEDIR); | - | ||||||||||||
| 182 | textdomain (PACKAGE); | - | ||||||||||||
| 183 | - | |||||||||||||
| 184 | atexit (close_stdout); | - | ||||||||||||
| 185 | - | |||||||||||||
| 186 | while ((c = getopt_long (argc, argv, "df", long_options, NULL))
| 7-22 | ||||||||||||
| 187 | != -1)
| 7-22 | ||||||||||||
| 188 | { | - | ||||||||||||
| 189 | switch (c) | - | ||||||||||||
| 190 | { | - | ||||||||||||
| 191 | case 'd': executed 4 times by 1 test: case 'd':Executed by:
| 4 | ||||||||||||
| 192 | arg_data = true; | - | ||||||||||||
| 193 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||||||||
| 194 | - | |||||||||||||
| 195 | case 'f': executed 3 times by 1 test: case 'f':Executed by:
| 3 | ||||||||||||
| 196 | arg_file_system = true; | - | ||||||||||||
| 197 | break; executed 3 times by 1 test: break;Executed by:
| 3 | ||||||||||||
| 198 | - | |||||||||||||
| 199 | case_GETOPT_HELP_CHAR; never executed: break;executed 7 times by 1 test: case GETOPT_HELP_CHAR:Executed by:
| 0-7 | ||||||||||||
| 200 | - | |||||||||||||
| 201 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); executed 5 times by 1 test: exit ( 0 );Executed by:
never executed: break;executed 5 times by 1 test: case GETOPT_VERSION_CHAR:Executed by:
| 0-5 | ||||||||||||
| 202 | - | |||||||||||||
| 203 | default: executed 3 times by 1 test: default:Executed by:
| 3 | ||||||||||||
| 204 | usage (EXIT_FAILURE); | - | ||||||||||||
| 205 | } never executed: end of block | 0 | ||||||||||||
| 206 | } | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | args_specified = optind < argc; | - | ||||||||||||
| 209 | - | |||||||||||||
| 210 | if (arg_data && arg_file_system)
| 1-5 | ||||||||||||
| 211 | { | - | ||||||||||||
| 212 | die (EXIT_FAILURE, 0, | - | ||||||||||||
| 213 | _("cannot specify both --data and --file-system")); | - | ||||||||||||
| 214 | } never executed: end of block | 0 | ||||||||||||
| 215 | - | |||||||||||||
| 216 | if (!args_specified && arg_data)
| 0-5 | ||||||||||||
| 217 | die (EXIT_FAILURE, 0, _("--data needs at least one argument")); executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"--data needs at least one argument\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "--data needs at least one argument" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "--data needs at least one argument" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));Executed by:
| 1 | ||||||||||||
| 218 | - | |||||||||||||
| 219 | if (! args_specified || (arg_file_system && ! HAVE_SYNCFS))
| 0-5 | ||||||||||||
| 220 | mode = MODE_SYNC; never executed: mode = MODE_SYNC; | 0 | ||||||||||||
| 221 | else if (arg_file_system)
| 0-5 | ||||||||||||
| 222 | mode = MODE_FILE_SYSTEM; never executed: mode = MODE_FILE_SYSTEM; | 0 | ||||||||||||
| 223 | else if (! arg_data)
| 0-5 | ||||||||||||
| 224 | mode = MODE_FILE; executed 5 times by 1 test: mode = MODE_FILE;Executed by:
| 5 | ||||||||||||
| 225 | else | - | ||||||||||||
| 226 | mode = MODE_DATA; never executed: mode = MODE_DATA; | 0 | ||||||||||||
| 227 | - | |||||||||||||
| 228 | if (mode == MODE_SYNC)
| 0-5 | ||||||||||||
| 229 | sync (); never executed: sync (); | 0 | ||||||||||||
| 230 | else | - | ||||||||||||
| 231 | { | - | ||||||||||||
| 232 | for (; optind < argc; optind++)
| 5-6 | ||||||||||||
| 233 | ok &= sync_arg (mode, argv[optind]); executed 6 times by 1 test: ok &= sync_arg (mode, argv[optind]);Executed by:
| 6 | ||||||||||||
| 234 | } executed 5 times by 1 test: end of blockExecuted by:
| 5 | ||||||||||||
| 235 | - | |||||||||||||
| 236 | return ok ? EXIT_SUCCESS : EXIT_FAILURE; executed 5 times by 1 test: return ok ? 0 : 1 ;Executed by:
| 5 | ||||||||||||
| 237 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |