Mercurial > hg > toybox
comparison toys/posix/cp.c @ 1455:70f5188b4848 draft
Add cp -F to force delete of existing destination files, and make install command use that.
-F is equivalent to the gnu/dammit flag --we-think-long-options-without-short-options-are-a-good-idea
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 01 Sep 2014 12:47:32 -0500 |
parents | 7a3afbc7fc8b |
children | 2f9bc9495144 |
comparison
equal
deleted
inserted
replaced
1454:ab91ee85afbd | 1455:70f5188b4848 |
---|---|
5 * Posix says "cp -Rf dir file" shouldn't delete file, but our -f does. | 5 * Posix says "cp -Rf dir file" shouldn't delete file, but our -f does. |
6 | 6 |
7 // This is subtle: MV options must be in same order (right to left) as CP | 7 // This is subtle: MV options must be in same order (right to left) as CP |
8 // for FLAG_X macros to work out right. | 8 // for FLAG_X macros to work out right. |
9 | 9 |
10 USE_CP(NEWTOY(cp, "<2RHLPp"USE_CP_MORE("rdaslvn")"fi[-HLPd]"USE_CP_MORE("[-ni]"), TOYFLAG_BIN)) | 10 USE_CP(NEWTOY(cp, "<2RHLPp"USE_CP_MORE("rdaslvnF")"fi[-HLPd]"USE_CP_MORE("[-ni]"), TOYFLAG_BIN)) |
11 USE_CP_MV(OLDTOY(mv, cp, "<2"USE_CP_MORE("vn")"fi"USE_CP_MORE("[-ni]"), TOYFLAG_BIN)) | 11 USE_CP_MV(OLDTOY(mv, cp, "<2"USE_CP_MORE("vnF")"fi"USE_CP_MORE("[-ni]"), TOYFLAG_BIN)) |
12 USE_INSTALL(NEWTOY(install, "<1cdDpsvm:o:g:", TOYFLAG_USR|TOYFLAG_BIN)) | 12 USE_INSTALL(NEWTOY(install, "<1cdDpsvm:o:g:", TOYFLAG_USR|TOYFLAG_BIN)) |
13 * | 13 * |
14 | 14 |
15 config CP | 15 config CP |
16 bool "cp" | 16 bool "cp" |
19 usage: cp [-fipRHLP] SOURCE... DEST | 19 usage: cp [-fipRHLP] SOURCE... DEST |
20 | 20 |
21 Copy files from SOURCE to DEST. If more than one SOURCE, DEST must | 21 Copy files from SOURCE to DEST. If more than one SOURCE, DEST must |
22 be a directory. | 22 be a directory. |
23 | 23 |
24 -f force copy by deleting destination file | 24 -f delete destination files we can't write to |
25 -F delete any existing destination file first (breaks hardlinks) | |
25 -i interactive, prompt before overwriting existing DEST | 26 -i interactive, prompt before overwriting existing DEST |
26 -p preserve timestamps, ownership, and permissions | 27 -p preserve timestamps, ownership, and permissions |
27 -R recurse into subdirectories (DEST must be a directory) | 28 -R recurse into subdirectories (DEST must be a directory) |
28 -H Follow symlinks listed on command line | 29 -H Follow symlinks listed on command line |
29 -L Follow all symlinks | 30 -L Follow all symlinks |
139 char *s; | 140 char *s; |
140 | 141 |
141 if (S_ISDIR(try->st.st_dev)) { | 142 if (S_ISDIR(try->st.st_dev)) { |
142 error_msg("dir at '%s'", s = dirtree_path(try, 0)); | 143 error_msg("dir at '%s'", s = dirtree_path(try, 0)); |
143 free(s); | 144 free(s); |
145 return 0; | |
146 } else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0)) { | |
147 error_msg("unlink '%s'", catch); | |
144 return 0; | 148 return 0; |
145 } else if (flags & FLAG_n) return 0; | 149 } else if (flags & FLAG_n) return 0; |
146 else if (flags & FLAG_i) { | 150 else if (flags & FLAG_i) { |
147 fprintf(stderr, "cp: overwrite '%s'", s = dirtree_path(try, 0)); | 151 fprintf(stderr, "cp: overwrite '%s'", s = dirtree_path(try, 0)); |
148 free(s); | 152 free(s); |
363 if (toys.optc == 1) return; | 367 if (toys.optc == 1) return; |
364 } | 368 } |
365 if (toys.optc < 2) error_exit("needs 2 args"); | 369 if (toys.optc < 2) error_exit("needs 2 args"); |
366 | 370 |
367 // Translate flags from install to cp | 371 // Translate flags from install to cp |
368 toys.optflags = 4; // Force cp's FLAG_n | 372 toys.optflags = 4; // Force cp's FLAG_F |
369 if (flags & FLAG_v) toys.optflags |= 8; // cp's FLAG_v | 373 if (flags & FLAG_v) toys.optflags |= 8; // cp's FLAG_v |
370 if (flags & (FLAG_p|FLAG_o|FLAG_g)) toys.optflags |= 512; // cp's FLAG_p | 374 if (flags & (FLAG_p|FLAG_o|FLAG_g)) toys.optflags |= 512; // cp's FLAG_p |
371 | 375 |
372 if (TT.user) TT.uid = xgetpwnam(TT.user)->pw_uid; | 376 if (TT.user) TT.uid = xgetpwnam(TT.user)->pw_uid; |
373 if (TT.group) TT.gid = xgetgrnam(TT.group)->gr_gid; | 377 if (TT.group) TT.gid = xgetgrnam(TT.group)->gr_gid; |