aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-05-27 00:46:38 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2016-05-27 00:46:38 +0200
commit877dedb8251be47b3614a371434081ae9b7b358b (patch)
tree70fbac15f652ff7ce9aaada1252b4b6a0a846dad
parent852e8dd734662d80aa82be802b066130af85b261 (diff)
downloadbusybox-w32-877dedb8251be47b3614a371434081ae9b7b358b.tar.gz
busybox-w32-877dedb8251be47b3614a371434081ae9b7b358b.tar.bz2
busybox-w32-877dedb8251be47b3614a371434081ae9b7b358b.zip
cp: add -u/--update and --remove-destination
Based on the patch by wdlkmpx@gmail.com function old new delta copy_file 1546 1644 +98 add_partition 1270 1362 +92 ask_and_unlink 95 133 +38 do_iproute 132 157 +25 decode_one_format 710 715 +5 cp_main 369 374 +5 ubirename_main 198 202 +4 read_package_field 232 230 -2 bb_make_directory 421 412 -9 packed_usage 30505 30476 -29 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 7/3 up/down: 267/-40) Total: 227 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/cp.c30
-rw-r--r--include/libbb.h24
-rw-r--r--libbb/copy_file.c21
3 files changed, 56 insertions, 19 deletions
diff --git a/coreutils/cp.c b/coreutils/cp.c
index 247ed0fda..2630c0d59 100644
--- a/coreutils/cp.c
+++ b/coreutils/cp.c
@@ -31,6 +31,7 @@
31//usage: "\n -f Overwrite" 31//usage: "\n -f Overwrite"
32//usage: "\n -i Prompt before overwrite" 32//usage: "\n -i Prompt before overwrite"
33//usage: "\n -l,-s Create (sym)links" 33//usage: "\n -l,-s Create (sym)links"
34//usage: "\n -u Copy only newer files"
34 35
35#include "libbb.h" 36#include "libbb.h"
36#include "libcoreutils/coreutils.h" 37#include "libcoreutils/coreutils.h"
@@ -49,12 +50,10 @@ int cp_main(int argc, char **argv)
49 int flags; 50 int flags;
50 int status; 51 int status;
51 enum { 52 enum {
52 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1), 53 FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1,
53 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
54 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
55 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
56#if ENABLE_FEATURE_CP_LONG_OPTIONS 54#if ENABLE_FEATURE_CP_LONG_OPTIONS
57 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3), 55 /*OPT_rmdest = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */
56 OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1),
58#endif 57#endif
59 }; 58 };
60 59
@@ -76,10 +75,12 @@ int cp_main(int argc, char **argv)
76 "recursive\0" No_argument "R" 75 "recursive\0" No_argument "R"
77 "symbolic-link\0" No_argument "s" 76 "symbolic-link\0" No_argument "s"
78 "verbose\0" No_argument "v" 77 "verbose\0" No_argument "v"
79 "parents\0" No_argument "\xff" 78 "update\0" No_argument "u"
79 "remove-destination\0" No_argument "\xff"
80 "parents\0" No_argument "\xfe"
80 ; 81 ;
81#endif 82#endif
82 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv"); 83 flags = getopt32(argv, FILEUTILS_CP_OPTSTR);
83 /* Options of cp from GNU coreutils 6.10: 84 /* Options of cp from GNU coreutils 6.10:
84 * -a, --archive 85 * -a, --archive
85 * -f, --force 86 * -f, --force
@@ -94,6 +95,11 @@ int cp_main(int argc, char **argv)
94 * -d same as --no-dereference --preserve=links 95 * -d same as --no-dereference --preserve=links
95 * -p same as --preserve=mode,ownership,timestamps 96 * -p same as --preserve=mode,ownership,timestamps
96 * -c same as --preserve=context 97 * -c same as --preserve=context
98 * -u, --update
99 * copy only when the SOURCE file is newer than the destination
100 * file or when the destination file is missing
101 * --remove-destination
102 * remove each existing destination file before attempting to open
97 * --parents 103 * --parents
98 * use full source file name under DIRECTORY 104 * use full source file name under DIRECTORY
99 * NOT SUPPORTED IN BBOX: 105 * NOT SUPPORTED IN BBOX:
@@ -106,8 +112,6 @@ int cp_main(int argc, char **argv)
106 * preserve attributes (default: mode,ownership,timestamps), 112 * preserve attributes (default: mode,ownership,timestamps),
107 * if possible additional attributes: security context,links,all 113 * if possible additional attributes: security context,links,all
108 * --no-preserve=ATTR_LIST 114 * --no-preserve=ATTR_LIST
109 * --remove-destination
110 * remove each existing destination file before attempting to open
111 * --sparse=WHEN 115 * --sparse=WHEN
112 * control creation of sparse files 116 * control creation of sparse files
113 * --strip-trailing-slashes 117 * --strip-trailing-slashes
@@ -118,9 +122,6 @@ int cp_main(int argc, char **argv)
118 * copy all SOURCE arguments into DIRECTORY 122 * copy all SOURCE arguments into DIRECTORY
119 * -T, --no-target-directory 123 * -T, --no-target-directory
120 * treat DEST as a normal file 124 * treat DEST as a normal file
121 * -u, --update
122 * copy only when the SOURCE file is newer than the destination
123 * file or when the destination file is missing
124 * -x, --one-file-system 125 * -x, --one-file-system
125 * stay on this file system 126 * stay on this file system
126 * -Z, --context=CONTEXT 127 * -Z, --context=CONTEXT
@@ -156,11 +157,16 @@ int cp_main(int argc, char **argv)
156 return EXIT_FAILURE; 157 return EXIT_FAILURE;
157 158
158#if ENABLE_FEATURE_CP_LONG_OPTIONS 159#if ENABLE_FEATURE_CP_LONG_OPTIONS
160 //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
161 // flags, FILEUTILS_RMDEST, OPT_parents);
159 if (flags & OPT_parents) { 162 if (flags & OPT_parents) {
160 if (!(d_flags & 2)) { 163 if (!(d_flags & 2)) {
161 bb_error_msg_and_die("with --parents, the destination must be a directory"); 164 bb_error_msg_and_die("with --parents, the destination must be a directory");
162 } 165 }
163 } 166 }
167 if (flags & FILEUTILS_RMDEST) {
168 flags |= FILEUTILS_FORCE;
169 }
164#endif 170#endif
165 171
166 /* ...if neither is a directory... */ 172 /* ...if neither is a directory... */
diff --git a/include/libbb.h b/include/libbb.h
index fd40ef74c..a21f4204a 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -351,7 +351,7 @@ extern char *strrstr(const char *haystack, const char *needle) FAST_FUNC;
351//TODO: supply a pointer to char[11] buffer (avoid statics)? 351//TODO: supply a pointer to char[11] buffer (avoid statics)?
352extern const char *bb_mode_string(mode_t mode) FAST_FUNC; 352extern const char *bb_mode_string(mode_t mode) FAST_FUNC;
353extern int is_directory(const char *name, int followLinks) FAST_FUNC; 353extern int is_directory(const char *name, int followLinks) FAST_FUNC;
354enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */ 354enum { /* cp.c, mv.c, install.c depend on these values. CAREFUL when changing them! */
355 FILEUTILS_PRESERVE_STATUS = 1 << 0, /* -p */ 355 FILEUTILS_PRESERVE_STATUS = 1 << 0, /* -p */
356 FILEUTILS_DEREFERENCE = 1 << 1, /* !-d */ 356 FILEUTILS_DEREFERENCE = 1 << 1, /* !-d */
357 FILEUTILS_RECUR = 1 << 2, /* -R */ 357 FILEUTILS_RECUR = 1 << 2, /* -R */
@@ -361,15 +361,25 @@ enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */
361 FILEUTILS_MAKE_SOFTLINK = 1 << 6, /* -s */ 361 FILEUTILS_MAKE_SOFTLINK = 1 << 6, /* -s */
362 FILEUTILS_DEREF_SOFTLINK = 1 << 7, /* -L */ 362 FILEUTILS_DEREF_SOFTLINK = 1 << 7, /* -L */
363 FILEUTILS_DEREFERENCE_L0 = 1 << 8, /* -H */ 363 FILEUTILS_DEREFERENCE_L0 = 1 << 8, /* -H */
364 /* -a = -pdR (mapped in cp.c) */
365 /* -r = -dR (mapped in cp.c) */
366 /* -P = -d (mapped in cp.c) */
367 FILEUTILS_VERBOSE = (1 << 12) * ENABLE_FEATURE_VERBOSE, /* -v */
368 FILEUTILS_UPDATE = 1 << 13, /* -u */
364#if ENABLE_SELINUX 369#if ENABLE_SELINUX
365 FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 9, /* -c */ 370 FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 14, /* -c */
366 FILEUTILS_SET_SECURITY_CONTEXT = 1 << 10,
367#endif 371#endif
368 FILEUTILS_IGNORE_CHMOD_ERR = 1 << 11, 372 FILEUTILS_RMDEST = 1 << (15 - !ENABLE_SELINUX), /* --remove-destination */
369 /* -v */ 373 /*
370 FILEUTILS_VERBOSE = (1 << 12) * ENABLE_FEATURE_VERBOSE, 374 * Hole. cp may have some bits set here,
375 * they should not affect remove_file()/copy_file()
376 */
377#if ENABLE_SELINUX
378 FILEUTILS_SET_SECURITY_CONTEXT = 1 << 30,
379#endif
380 FILEUTILS_IGNORE_CHMOD_ERR = 1 << 31,
371}; 381};
372#define FILEUTILS_CP_OPTSTR "pdRfilsLH" IF_SELINUX("c") 382#define FILEUTILS_CP_OPTSTR "pdRfilsLHarPvu" IF_SELINUX("c")
373extern int remove_file(const char *path, int flags) FAST_FUNC; 383extern int remove_file(const char *path, int flags) FAST_FUNC;
374/* NB: without FILEUTILS_RECUR in flags, it will basically "cat" 384/* NB: without FILEUTILS_RECUR in flags, it will basically "cat"
375 * the source, not copy (unless "source" is a directory). 385 * the source, not copy (unless "source" is a directory).
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index a4be875d2..23bcf2e82 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -64,6 +64,11 @@ static int ask_and_unlink(const char *dest, int flags)
64 bb_perror_msg("can't create '%s'", dest); 64 bb_perror_msg("can't create '%s'", dest);
65 return -1; /* error */ 65 return -1; /* error */
66 } 66 }
67#if ENABLE_FEATURE_CP_LONG_OPTIONS
68 if (flags & FILEUTILS_RMDEST)
69 if (flags & FILEUTILS_VERBOSE)
70 printf("removed '%s'\n", dest);
71#endif
67 return 1; /* ok (to try again) */ 72 return 1; /* ok (to try again) */
68} 73}
69 74
@@ -210,6 +215,22 @@ int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
210 goto preserve_mode_ugid_time; 215 goto preserve_mode_ugid_time;
211 } 216 }
212 217
218 if (dest_exists) {
219 if (flags & FILEUTILS_UPDATE) {
220 if (source_stat.st_mtime <= dest_stat.st_mtime) {
221 return 0; /* source file must be newer */
222 }
223 }
224#if ENABLE_FEATURE_CP_LONG_OPTIONS
225 if (flags & FILEUTILS_RMDEST) {
226 ovr = ask_and_unlink(dest, flags);
227 if (ovr <= 0)
228 return ovr;
229 dest_exists = 0;
230 }
231#endif
232 }
233
213 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { 234 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
214 int (*lf)(const char *oldpath, const char *newpath); 235 int (*lf)(const char *oldpath, const char *newpath);
215 make_links: 236 make_links: