aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-04-04 08:12:58 +0100
committerRon Yorston <rmy@pobox.com>2019-04-04 08:12:58 +0100
commitf17dd059cbab0a83c677d78475ef1fdf8f5ab59a (patch)
treeb2221c7a9d4ab616ab85e28010903850728c57a5 /coreutils
parentb990323902ab8c16d84a45e3ed5b319874320d64 (diff)
parenta3ce161363380899ae45716c70714cfcc93a7755 (diff)
downloadbusybox-w32-f17dd059cbab0a83c677d78475ef1fdf8f5ab59a.tar.gz
busybox-w32-f17dd059cbab0a83c677d78475ef1fdf8f5ab59a.tar.bz2
busybox-w32-f17dd059cbab0a83c677d78475ef1fdf8f5ab59a.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/fsync.c60
-rw-r--r--coreutils/sync.c102
2 files changed, 70 insertions, 92 deletions
diff --git a/coreutils/fsync.c b/coreutils/fsync.c
deleted file mode 100644
index c7cba9f61..000000000
--- a/coreutils/fsync.c
+++ /dev/null
@@ -1,60 +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:IF_FSYNC(APPLET_NOFORK(fsync, fsync, BB_DIR_BIN, BB_SUID_DROP, fsync))
16
17//kbuild:lib-$(CONFIG_FSYNC) += fsync.o
18
19//usage:#define fsync_trivial_usage
20//usage: "[-d] FILE..."
21//usage:#define fsync_full_usage "\n\n"
22//usage: "Write files' buffered blocks to disk\n"
23//usage: "\n -d Avoid syncing metadata"
24
25#include "libbb.h"
26#ifndef O_NOATIME
27# define O_NOATIME 0
28#endif
29
30/* This is a NOFORK applet. Be very careful! */
31
32int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
33int fsync_main(int argc UNUSED_PARAM, char **argv)
34{
35 int status;
36 int opts;
37
38 opts = getopt32(argv, "d"); /* fdatasync */
39 argv += optind;
40 if (!*argv) {
41 bb_show_usage();
42 }
43
44 status = EXIT_SUCCESS;
45 do {
46 int fd = open_or_warn(*argv, O_NOATIME | O_NOCTTY | O_RDONLY);
47
48 if (fd == -1) {
49 status = EXIT_FAILURE;
50 continue;
51 }
52 if ((opts ? fdatasync(fd) : fsync(fd))) {
53 //status = EXIT_FAILURE; - do we want this?
54 bb_simple_perror_msg(*argv);
55 }
56 close(fd);
57 } while (*++argv);
58
59 return status;
60}
diff --git a/coreutils/sync.c b/coreutils/sync.c
index b93476aee..ea328a54c 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
@@ -42,61 +43,98 @@
42 43
43/* This is a NOFORK applet. Be very careful! */ 44/* This is a NOFORK applet. Be very careful! */
44 45
45int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 46#if ENABLE_FEATURE_SYNC_FANCY || ENABLE_FSYNC
46int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) 47static int sync_common(int opts, char **argv)
47{ 48{
48#if !ENABLE_FEATURE_SYNC_FANCY 49 int ret;
49 /* coreutils-6.9 compat */
50 bb_warn_ignoring_args(argv[1]);
51 sync();
52 return EXIT_SUCCESS;
53#else
54 unsigned opts;
55 int ret = EXIT_SUCCESS;
56
57 enum { 50 enum {
58 OPT_DATASYNC = (1 << 0), 51 OPT_DATASYNC = (1 << 0),
59 OPT_SYNCFS = (1 << 1), 52 OPT_SYNCFS = (1 << 1),
60 }; 53 };
61 54
62 opts = getopt32(argv, "^" "df" "\0" "d--f:f--d"); 55 ret = EXIT_SUCCESS;
63 argv += optind; 56 do {
64 57 /* GNU "sync FILE" uses O_NONBLOCK open */
65 /* Handle the no-argument case. */ 58 int fd = open_or_warn(*argv, /*O_NOATIME |*/ O_NOCTTY | O_RDONLY | O_NONBLOCK);
66 if (!argv[0]) 59 /* open(NOATIME) can only be used by owner or root, don't use NOATIME here */
67 sync();
68
69 while (*argv) {
70 int fd = open_or_warn(*argv, O_RDONLY);
71 60
72 if (fd < 0) { 61 if (fd < 0) {
73 ret = EXIT_FAILURE; 62 ret = EXIT_FAILURE;
74 goto next; 63 goto next;
75 } 64 }
76 if (opts & OPT_DATASYNC) { 65# if ENABLE_FEATURE_SYNC_FANCY
77 if (fdatasync(fd))
78 goto err;
79 goto do_close;
80 }
81 if (opts & OPT_SYNCFS) { 66 if (opts & OPT_SYNCFS) {
82 /* 67 /*
83 * syncfs is documented to only fail with EBADF, 68 * syncfs is documented to only fail with EBADF,
84 * which can't happen here. So, no error checks. 69 * which can't happen here. So, no error checks.
85 */ 70 */
86 syncfs(fd); 71 syncfs(fd);
87 goto do_close; 72 } else
88 } 73# endif
89 if (fsync(fd)) { 74 if (((opts & OPT_DATASYNC) ? fdatasync(fd) : fsync(fd)) != 0) {
90 err:
91 bb_simple_perror_msg(*argv); 75 bb_simple_perror_msg(*argv);
92 ret = EXIT_FAILURE; 76 ret = EXIT_FAILURE;
93 } 77 }
94 do_close:
95 close(fd); 78 close(fd);
96 next: 79 next:
97 ++argv; 80 argv++;
98 } 81 } while (*argv);
99 82
100 return ret; 83 return ret;
84}
85#endif
86
87#if ENABLE_SYNC
88int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
89int 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}
101#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
133int fsync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
134int 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);
102} 139}
140#endif