diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2019-04-02 14:39:56 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2019-04-02 14:39:56 +0200 |
| commit | 4f74bb6059b54df31b9a6cdaf844112a7003b0ff (patch) | |
| tree | eb4ed2d1463644e60fd8cc77704ff8546f951c25 /coreutils | |
| parent | e48559eae3ba10e4b4f4c2ce726c31fd038a37ba (diff) | |
| download | busybox-w32-4f74bb6059b54df31b9a6cdaf844112a7003b0ff.tar.gz busybox-w32-4f74bb6059b54df31b9a6cdaf844112a7003b0ff.tar.bz2 busybox-w32-4f74bb6059b54df31b9a6cdaf844112a7003b0ff.zip | |
fsync,sync: make them similar
sync: add O_NOCTTY
fsync: drop O_NOATIME, add O_NONBLOCK, set exitcode to 1 if fsync() fails,
update --help message to be similar to sync.
both: reformat code to minimize "diff -u sync.c fsync.c":
in particular, they use same open() flags now
function old new delta
fsync_main 126 130 +4
packed_usage 33316 33317 +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 5/0) Total: 5 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/fsync.c | 30 | ||||
| -rw-r--r-- | coreutils/sync.c | 22 |
2 files changed, 25 insertions, 27 deletions
diff --git a/coreutils/fsync.c b/coreutils/fsync.c index c7cba9f61..216cbcf41 100644 --- a/coreutils/fsync.c +++ b/coreutils/fsync.c | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | //config: help | 12 | //config: help |
| 13 | //config: fsync is used to flush file-related cached blocks to disk. | 13 | //config: fsync is used to flush file-related cached blocks to disk. |
| 14 | 14 | ||
| 15 | // APPLET_NOFORK:name main location suid_type help | ||
| 15 | //applet:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync)) | 16 | //applet:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync)) |
| 16 | 17 | ||
| 17 | //kbuild:lib-$(CONFIG_FSYNC) += fsync.o | 18 | //kbuild:lib-$(CONFIG_FSYNC) += fsync.o |
| @@ -19,20 +20,17 @@ | |||
| 19 | //usage:#define fsync_trivial_usage | 20 | //usage:#define fsync_trivial_usage |
| 20 | //usage: "[-d] FILE..." | 21 | //usage: "[-d] FILE..." |
| 21 | //usage:#define fsync_full_usage "\n\n" | 22 | //usage:#define fsync_full_usage "\n\n" |
| 22 | //usage: "Write files' buffered blocks to disk\n" | 23 | //usage: "Write all buffered blocks in FILEs to disk\n" |
| 23 | //usage: "\n -d Avoid syncing metadata" | 24 | //usage: "\n -d Avoid syncing metadata" |
| 24 | 25 | ||
| 25 | #include "libbb.h" | 26 | #include "libbb.h" |
| 26 | #ifndef O_NOATIME | ||
| 27 | # define O_NOATIME 0 | ||
| 28 | #endif | ||
| 29 | 27 | ||
| 30 | /* This is a NOFORK applet. Be very careful! */ | 28 | /* This is a NOFORK applet. Be very careful! */ |
| 31 | 29 | ||
| 32 | int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 30 | int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 33 | int fsync_main(int argc UNUSED_PARAM, char **argv) | 31 | int fsync_main(int argc UNUSED_PARAM, char **argv) |
| 34 | { | 32 | { |
| 35 | int status; | 33 | int ret; |
| 36 | int opts; | 34 | int opts; |
| 37 | 35 | ||
| 38 | opts = getopt32(argv, "d"); /* fdatasync */ | 36 | opts = getopt32(argv, "d"); /* fdatasync */ |
| @@ -41,20 +39,24 @@ int fsync_main(int argc UNUSED_PARAM, char **argv) | |||
| 41 | bb_show_usage(); | 39 | bb_show_usage(); |
| 42 | } | 40 | } |
| 43 | 41 | ||
| 44 | status = EXIT_SUCCESS; | 42 | ret = EXIT_SUCCESS; |
| 45 | do { | 43 | do { |
| 46 | int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY); | 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 | 47 | ||
| 48 | if (fd == -1) { | 48 | if (fd < 0) { |
| 49 | status = EXIT_FAILURE; | 49 | ret = EXIT_FAILURE; |
| 50 | continue; | 50 | goto next; |
| 51 | } | 51 | } |
| 52 | if ((opts ? fdatasync(fd) : fsync(fd))) { | 52 | if ((opts ? fdatasync(fd) : fsync(fd)) != 0) { |
| 53 | //status = EXIT_FAILURE; - do we want this? | ||
| 54 | bb_simple_perror_msg(*argv); | 53 | bb_simple_perror_msg(*argv); |
| 54 | ret = EXIT_FAILURE; | ||
| 55 | } | 55 | } |
| 56 | close(fd); | 56 | close(fd); |
| 57 | } while (*++argv); | 57 | next: |
| 58 | argv++; | ||
| 59 | } while (*argv); | ||
| 58 | 60 | ||
| 59 | return status; | 61 | return ret; |
| 60 | } | 62 | } |
diff --git a/coreutils/sync.c b/coreutils/sync.c index b93476aee..e60e553a3 100644 --- a/coreutils/sync.c +++ b/coreutils/sync.c | |||
| @@ -20,6 +20,7 @@ | |||
| 20 | //config: sync -d FILE... executes fdatasync() on each FILE. | 20 | //config: sync -d FILE... executes fdatasync() on each FILE. |
| 21 | //config: sync -f FILE... executes syncfs() on each FILE. | 21 | //config: sync -f FILE... executes syncfs() on each FILE. |
| 22 | 22 | ||
| 23 | // APPLET_NOFORK:name main location suid_type help | ||
| 23 | //applet:IF_SYNC(APPLET_NOFORK(sync, sync, BB_DIR_BIN, BB_SUID_DROP, sync)) | 24 | //applet:IF_SYNC(APPLET_NOFORK(sync, sync, BB_DIR_BIN, BB_SUID_DROP, sync)) |
| 24 | 25 | ||
| 25 | //kbuild:lib-$(CONFIG_SYNC) += sync.o | 26 | //kbuild:lib-$(CONFIG_SYNC) += sync.o |
| @@ -52,7 +53,7 @@ int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | |||
| 52 | return EXIT_SUCCESS; | 53 | return EXIT_SUCCESS; |
| 53 | #else | 54 | #else |
| 54 | unsigned opts; | 55 | unsigned opts; |
| 55 | int ret = EXIT_SUCCESS; | 56 | int ret; |
| 56 | 57 | ||
| 57 | enum { | 58 | enum { |
| 58 | OPT_DATASYNC = (1 << 0), | 59 | OPT_DATASYNC = (1 << 0), |
| @@ -66,35 +67,30 @@ int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | |||
| 66 | if (!argv[0]) | 67 | if (!argv[0]) |
| 67 | sync(); | 68 | sync(); |
| 68 | 69 | ||
| 70 | ret = EXIT_SUCCESS; | ||
| 69 | while (*argv) { | 71 | while (*argv) { |
| 70 | int fd = open_or_warn(*argv, O_RDONLY); | 72 | /* GNU "sync FILE" uses O_NONBLOCK open */ |
| 73 | 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 */ | ||
| 71 | 75 | ||
| 72 | if (fd < 0) { | 76 | if (fd < 0) { |
| 73 | ret = EXIT_FAILURE; | 77 | ret = EXIT_FAILURE; |
| 74 | goto next; | 78 | goto next; |
| 75 | } | 79 | } |
| 76 | if (opts & OPT_DATASYNC) { | ||
| 77 | if (fdatasync(fd)) | ||
| 78 | goto err; | ||
| 79 | goto do_close; | ||
| 80 | } | ||
| 81 | if (opts & OPT_SYNCFS) { | 80 | if (opts & OPT_SYNCFS) { |
| 82 | /* | 81 | /* |
| 83 | * syncfs is documented to only fail with EBADF, | 82 | * syncfs is documented to only fail with EBADF, |
| 84 | * which can't happen here. So, no error checks. | 83 | * which can't happen here. So, no error checks. |
| 85 | */ | 84 | */ |
| 86 | syncfs(fd); | 85 | syncfs(fd); |
| 87 | goto do_close; | 86 | } else |
| 88 | } | 87 | if (((opts & OPT_DATASYNC) ? fdatasync(fd) : fsync(fd)) != 0) { |
| 89 | if (fsync(fd)) { | ||
| 90 | err: | ||
| 91 | bb_simple_perror_msg(*argv); | 88 | bb_simple_perror_msg(*argv); |
| 92 | ret = EXIT_FAILURE; | 89 | ret = EXIT_FAILURE; |
| 93 | } | 90 | } |
| 94 | do_close: | ||
| 95 | close(fd); | 91 | close(fd); |
| 96 | next: | 92 | next: |
| 97 | ++argv; | 93 | argv++; |
| 98 | } | 94 | } |
| 99 | 95 | ||
| 100 | return ret; | 96 | return ret; |
