diff options
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/fsync.c | 62 | ||||
| -rw-r--r-- | coreutils/sync.c | 80 |
2 files changed, 61 insertions, 81 deletions
diff --git a/coreutils/fsync.c b/coreutils/fsync.c deleted file mode 100644 index 216cbcf41..000000000 --- a/coreutils/fsync.c +++ /dev/null | |||
| @@ -1,62 +0,0 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * Mini fsync implementation for busybox | ||
| 4 | * | ||
| 5 | * Copyright (C) 2008 Nokia Corporation. All rights reserved. | ||
| 6 | * | ||
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
| 8 | */ | ||
| 9 | //config:config FSYNC | ||
| 10 | //config: bool "fsync (3.6 kb)" | ||
| 11 | //config: default y | ||
| 12 | //config: help | ||
| 13 | //config: fsync is used to flush file-related cached blocks to disk. | ||
| 14 | |||
| 15 | // APPLET_NOFORK:name main location suid_type help | ||
| 16 | //applet:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync)) | ||
| 17 | |||
| 18 | //kbuild:lib-$(CONFIG_FSYNC) += fsync.o | ||
| 19 | |||
| 20 | //usage:#define fsync_trivial_usage | ||
| 21 | //usage: "[-d] FILE..." | ||
| 22 | //usage:#define fsync_full_usage "\n\n" | ||
| 23 | //usage: "Write all buffered blocks in FILEs to disk\n" | ||
| 24 | //usage: "\n -d Avoid syncing metadata" | ||
| 25 | |||
| 26 | #include "libbb.h" | ||
| 27 | |||
| 28 | /* This is a NOFORK applet. Be very careful! */ | ||
| 29 | |||
| 30 | int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
| 31 | int fsync_main(int argc UNUSED_PARAM, char **argv) | ||
| 32 | { | ||
| 33 | int ret; | ||
| 34 | int opts; | ||
| 35 | |||
| 36 | opts = getopt32(argv, "d"); /* fdatasync */ | ||
| 37 | argv += optind; | ||
| 38 | if (!*argv) { | ||
| 39 | bb_show_usage(); | ||
| 40 | } | ||
| 41 | |||
| 42 | ret = EXIT_SUCCESS; | ||
| 43 | do { | ||
| 44 | /* GNU "sync FILE" uses O_NONBLOCK open */ | ||
| 45 | int fd = open_or_warn(*argv, /*O_NOATIME |*/ O_NOCTTY | O_RDONLY | O_NONBLOCK); | ||
| 46 | /* open(NOATIME) can only be used by owner or root, don't use NOATIME here */ | ||
| 47 | |||
| 48 | if (fd < 0) { | ||
| 49 | ret = EXIT_FAILURE; | ||
| 50 | goto next; | ||
| 51 | } | ||
| 52 | if ((opts ? fdatasync(fd) : fsync(fd)) != 0) { | ||
| 53 | bb_simple_perror_msg(*argv); | ||
| 54 | ret = EXIT_FAILURE; | ||
| 55 | } | ||
| 56 | close(fd); | ||
| 57 | next: | ||
| 58 | argv++; | ||
| 59 | } while (*argv); | ||
| 60 | |||
| 61 | return ret; | ||
| 62 | } | ||
diff --git a/coreutils/sync.c b/coreutils/sync.c index e60e553a3..ea328a54c 100644 --- a/coreutils/sync.c +++ b/coreutils/sync.c | |||
| @@ -43,32 +43,17 @@ | |||
| 43 | 43 | ||
| 44 | /* This is a NOFORK applet. Be very careful! */ | 44 | /* This is a NOFORK applet. Be very careful! */ |
| 45 | 45 | ||
| 46 | int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 46 | #if ENABLE_FEATURE_SYNC_FANCY || ENABLE_FSYNC |
| 47 | int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | 47 | static int sync_common(int opts, char **argv) |
| 48 | { | 48 | { |
| 49 | #if !ENABLE_FEATURE_SYNC_FANCY | ||
| 50 | /* coreutils-6.9 compat */ | ||
| 51 | bb_warn_ignoring_args(argv[1]); | ||
| 52 | sync(); | ||
| 53 | return EXIT_SUCCESS; | ||
| 54 | #else | ||
| 55 | unsigned opts; | ||
| 56 | int ret; | 49 | int ret; |
| 57 | |||
| 58 | enum { | 50 | enum { |
| 59 | OPT_DATASYNC = (1 << 0), | 51 | OPT_DATASYNC = (1 << 0), |
| 60 | OPT_SYNCFS = (1 << 1), | 52 | OPT_SYNCFS = (1 << 1), |
| 61 | }; | 53 | }; |
| 62 | 54 | ||
| 63 | opts = getopt32(argv, "^" "df" "\0" "d--f:f--d"); | ||
| 64 | argv += optind; | ||
| 65 | |||
| 66 | /* Handle the no-argument case. */ | ||
| 67 | if (!argv[0]) | ||
| 68 | sync(); | ||
| 69 | |||
| 70 | ret = EXIT_SUCCESS; | 55 | ret = EXIT_SUCCESS; |
| 71 | while (*argv) { | 56 | do { |
| 72 | /* GNU "sync FILE" uses O_NONBLOCK open */ | 57 | /* GNU "sync FILE" uses O_NONBLOCK open */ |
| 73 | int fd = open_or_warn(*argv, /*O_NOATIME |*/ O_NOCTTY | O_RDONLY | O_NONBLOCK); | 58 | int fd = open_or_warn(*argv, /*O_NOATIME |*/ O_NOCTTY | O_RDONLY | O_NONBLOCK); |
| 74 | /* open(NOATIME) can only be used by owner or root, don't use NOATIME here */ | 59 | /* open(NOATIME) can only be used by owner or root, don't use NOATIME here */ |
| @@ -77,6 +62,7 @@ int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | |||
| 77 | ret = EXIT_FAILURE; | 62 | ret = EXIT_FAILURE; |
| 78 | goto next; | 63 | goto next; |
| 79 | } | 64 | } |
| 65 | # if ENABLE_FEATURE_SYNC_FANCY | ||
| 80 | if (opts & OPT_SYNCFS) { | 66 | if (opts & OPT_SYNCFS) { |
| 81 | /* | 67 | /* |
| 82 | * syncfs is documented to only fail with EBADF, | 68 | * syncfs is documented to only fail with EBADF, |
| @@ -84,6 +70,7 @@ int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | |||
| 84 | */ | 70 | */ |
| 85 | syncfs(fd); | 71 | syncfs(fd); |
| 86 | } else | 72 | } else |
| 73 | # endif | ||
| 87 | if (((opts & OPT_DATASYNC) ? fdatasync(fd) : fsync(fd)) != 0) { | 74 | if (((opts & OPT_DATASYNC) ? fdatasync(fd) : fsync(fd)) != 0) { |
| 88 | bb_simple_perror_msg(*argv); | 75 | bb_simple_perror_msg(*argv); |
| 89 | ret = EXIT_FAILURE; | 76 | ret = EXIT_FAILURE; |
| @@ -91,8 +78,63 @@ int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | |||
| 91 | close(fd); | 78 | close(fd); |
| 92 | next: | 79 | next: |
| 93 | argv++; | 80 | argv++; |
| 94 | } | 81 | } while (*argv); |
| 95 | 82 | ||
| 96 | return ret; | 83 | return ret; |
| 84 | } | ||
| 85 | #endif | ||
| 86 | |||
| 87 | #if ENABLE_SYNC | ||
| 88 | int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
| 89 | int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | ||
| 90 | { | ||
| 91 | # if !ENABLE_FEATURE_SYNC_FANCY | ||
| 92 | /* coreutils-6.9 compat */ | ||
| 93 | bb_warn_ignoring_args(argv[1]); | ||
| 94 | sync(); | ||
| 95 | return EXIT_SUCCESS; | ||
| 96 | # else | ||
| 97 | unsigned opts = getopt32(argv, "^" "df" "\0" "d--f:f--d"); | ||
| 98 | argv += optind; | ||
| 99 | if (!argv[0]) { | ||
| 100 | sync(); | ||
| 101 | return EXIT_SUCCESS; | ||
| 102 | } | ||
| 103 | return sync_common(opts, argv); | ||
| 104 | # endif | ||
| 105 | } | ||
| 97 | #endif | 106 | #endif |
| 107 | |||
| 108 | /* | ||
| 109 | * Mini fsync implementation for busybox | ||
| 110 | * | ||
| 111 | * Copyright (C) 2008 Nokia Corporation. All rights reserved. | ||
| 112 | * | ||
| 113 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
| 114 | */ | ||
| 115 | //config:config FSYNC | ||
| 116 | //config: bool "fsync (3.6 kb)" | ||
| 117 | //config: default y | ||
| 118 | //config: help | ||
| 119 | //config: fsync is used to flush file-related cached blocks to disk. | ||
| 120 | |||
| 121 | // APPLET_NOFORK:name main location suid_type help | ||
| 122 | //applet:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync)) | ||
| 123 | |||
| 124 | //kbuild:lib-$(CONFIG_FSYNC) += sync.o | ||
| 125 | |||
| 126 | //usage:#define fsync_trivial_usage | ||
| 127 | //usage: "[-d] FILE..." | ||
| 128 | //usage:#define fsync_full_usage "\n\n" | ||
| 129 | //usage: "Write all buffered blocks in FILEs to disk\n" | ||
| 130 | //usage: "\n -d Avoid syncing metadata" | ||
| 131 | |||
| 132 | #if ENABLE_FSYNC | ||
| 133 | int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
| 134 | int fsync_main(int argc UNUSED_PARAM, char **argv) | ||
| 135 | { | ||
| 136 | int opts = getopt32(argv, "^" "d" "\0" "-1"/*min 1 arg*/); | ||
| 137 | argv += optind; | ||
| 138 | return sync_common(opts, argv); | ||
| 98 | } | 139 | } |
| 140 | #endif | ||
