aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2015-07-21 19:50:48 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2015-07-21 19:50:48 +0200
commit35ae2ccb40924efcd0fd0c873cc9e56c58851222 (patch)
tree75f32dc79e063940ee1ea59a9d8cb1a215f1959e /coreutils
parentce193f809b32368796a3aacf2ac8ba277f4c102a (diff)
downloadbusybox-w32-35ae2ccb40924efcd0fd0c873cc9e56c58851222.tar.gz
busybox-w32-35ae2ccb40924efcd0fd0c873cc9e56c58851222.tar.bz2
busybox-w32-35ae2ccb40924efcd0fd0c873cc9e56c58851222.zip
sync: add support for -d -f FILE
Based on the patch by Ari Sundholm <ari@tuxera.com> function old new delta sync_main 20 163 +143 packed_usage 30653 30673 +20 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/0 up/down: 163/0) Total: 163 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/Config.src6
-rw-r--r--coreutils/Kbuild.src1
-rw-r--r--coreutils/sync.c78
3 files changed, 75 insertions, 10 deletions
diff --git a/coreutils/Config.src b/coreutils/Config.src
index 1ec3a0a99..02155d220 100644
--- a/coreutils/Config.src
+++ b/coreutils/Config.src
@@ -571,12 +571,6 @@ config SUM
571 help 571 help
572 checksum and count the blocks in a file 572 checksum and count the blocks in a file
573 573
574config SYNC
575 bool "sync"
576 default y
577 help
578 sync is used to flush filesystem buffers.
579
580config TAC 574config TAC
581 bool "tac" 575 bool "tac"
582 default y 576 default y
diff --git a/coreutils/Kbuild.src b/coreutils/Kbuild.src
index ec4ef7df2..4ec075ac6 100644
--- a/coreutils/Kbuild.src
+++ b/coreutils/Kbuild.src
@@ -68,7 +68,6 @@ lib-$(CONFIG_SORT) += sort.o
68lib-$(CONFIG_STAT) += stat.o 68lib-$(CONFIG_STAT) += stat.o
69lib-$(CONFIG_STTY) += stty.o 69lib-$(CONFIG_STTY) += stty.o
70lib-$(CONFIG_SUM) += sum.o 70lib-$(CONFIG_SUM) += sum.o
71lib-$(CONFIG_SYNC) += sync.o
72lib-$(CONFIG_TAC) += tac.o 71lib-$(CONFIG_TAC) += tac.o
73lib-$(CONFIG_TEE) += tee.o 72lib-$(CONFIG_TEE) += tee.o
74lib-$(CONFIG_TRUE) += true.o 73lib-$(CONFIG_TRUE) += true.o
diff --git a/coreutils/sync.c b/coreutils/sync.c
index 7d98a1e30..974e90452 100644
--- a/coreutils/sync.c
+++ b/coreutils/sync.c
@@ -3,16 +3,39 @@
3 * Mini sync implementation for busybox 3 * Mini sync implementation for busybox
4 * 4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. 5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com>
6 * 7 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */ 9 */
9 10
10/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */ 11/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
12//config:config SYNC
13//config: bool "sync"
14//config: default y
15//config: help
16//config: sync is used to flush filesystem buffers.
17//config:config FEATURE_SYNC_FANCY
18//config: bool "Enable -d and -f flags (requres syncfs(2) in libc)"
19//config: default y
20//config: depends on SYNC
21//config: help
22//config: sync -d FILE... executes fdatasync() on each FILE.
23//config: sync -f FILE... executes syncfs() on each FILE.
24
25//kbuild:lib-$(CONFIG_SYNC) += sync.o
26//applet:IF_SYNC(APPLET_NOFORK(sync, sync, BB_DIR_BIN, BB_SUID_DROP, sync))
11 27
12//usage:#define sync_trivial_usage 28//usage:#define sync_trivial_usage
13//usage: "" 29//usage: ""IF_FEATURE_SYNC_FANCY("[-df] [FILE]...")
14//usage:#define sync_full_usage "\n\n" 30//usage:#define sync_full_usage "\n\n"
31//usage: IF_NOT_FEATURE_SYNC_FANCY(
15//usage: "Write all buffered blocks to disk" 32//usage: "Write all buffered blocks to disk"
33//usage: )
34//usage: IF_FEATURE_SYNC_FANCY(
35//usage: "Write all buffered blocks (in FILEs) to disk"
36//usage: "\n -d Avoid syncing metadata"
37//usage: "\n -f Sync filesystems underlying FILEs"
38//usage: )
16 39
17#include "libbb.h" 40#include "libbb.h"
18 41
@@ -21,10 +44,59 @@
21int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 44int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
22int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) 45int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
23{ 46{
47#if !ENABLE_FEATURE_SYNC_FANCY
24 /* coreutils-6.9 compat */ 48 /* coreutils-6.9 compat */
25 bb_warn_ignoring_args(argv[1]); 49 bb_warn_ignoring_args(argv[1]);
26
27 sync(); 50 sync();
28
29 return EXIT_SUCCESS; 51 return EXIT_SUCCESS;
52#else
53 unsigned opts;
54 int ret = EXIT_SUCCESS;
55
56 enum {
57 OPT_DATASYNC = (1 << 0),
58 OPT_SYNCFS = (1 << 1),
59 };
60
61 opt_complementary = "d--f:f--d";
62 opts = getopt32(argv, "df");
63 argv += optind;
64
65 /* Handle the no-argument case. */
66 if (!argv[0])
67 sync();
68
69 while (*argv) {
70 int fd = open_or_warn(*argv, O_RDONLY);
71
72 if (fd < 0) {
73 ret = EXIT_FAILURE;
74 goto next;
75 }
76 if (opts & OPT_DATASYNC) {
77 if (fdatasync(fd))
78 goto err;
79 goto do_close;
80 }
81 if (opts & OPT_SYNCFS) {
82 /*
83 * syncfs is documented to only fail with EBADF,
84 * which can't happen here. So, no error checks.
85 */
86 syncfs(fd);
87 goto do_close;
88 }
89 if (fsync(fd)) {
90 err:
91 bb_simple_perror_msg(*argv);
92 ret = EXIT_FAILURE;
93 }
94 do_close:
95 close(fd);
96 next:
97 ++argv;
98 }
99
100 return ret;
101#endif
30} 102}