annotate toys/posix/cp.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents b11f536bac74
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* Copyright 2008 Rob Landley <rob@landley.net>
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
3 * See http://opengroup.org/onlinepubs/9699919799/utilities/cp.html
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
1388
c4f5f82adce6 Implement -HL for cp.
Rob Landley <rob@landley.net>
parents: 1196
diff changeset
5 * Posix says "cp -Rf dir file" shouldn't delete file, but our -f does.
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
6
1746
b11f536bac74 install -D bugfix from David Halls.
Rob Landley <rob@landley.net>
parents: 1731
diff changeset
7 // options shared between mv/cp must be in same order (right to left)
b11f536bac74 install -D bugfix from David Halls.
Rob Landley <rob@landley.net>
parents: 1731
diff changeset
8 // for FLAG macros to work out right in shared infrastructure.
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
9
1699
dee053b7c775 Add --remove-destination longopt for -F because the host version doesn't have a shortopt for it.
Rob Landley <rob@landley.net>
parents: 1633
diff changeset
10 USE_CP(NEWTOY(cp, "<2RHLPp"USE_CP_MORE("rdaslvnF(remove-destination)")"fi[-HLP"USE_CP_MORE("d")"]"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
1615
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
11 USE_MV(NEWTOY(mv, "<2"USE_CP_MORE("vnF")"fi"USE_CP_MORE("[-ni]"), TOYFLAG_BIN))
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
12 USE_INSTALL(NEWTOY(install, "<1cdDpsvm:o:g:", TOYFLAG_USR|TOYFLAG_BIN))
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
13
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
14 config CP
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
15 bool "cp"
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
16 default y
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
17 help
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
18 usage: cp [-fipRHLP] SOURCE... DEST
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
19
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
20 Copy files from SOURCE to DEST. If more than one SOURCE, DEST must
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
21 be a directory.
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
22
1455
70f5188b4848 Add cp -F to force delete of existing destination files, and make install command use that.
Rob Landley <rob@landley.net>
parents: 1438
diff changeset
23 -f delete destination files we can't write to
1699
dee053b7c775 Add --remove-destination longopt for -F because the host version doesn't have a shortopt for it.
Rob Landley <rob@landley.net>
parents: 1633
diff changeset
24 -F delete any existing destination file first (--remove-destination)
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
25 -i interactive, prompt before overwriting existing DEST
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
26 -p preserve timestamps, ownership, and permissions
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
27 -R recurse into subdirectories (DEST must be a directory)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
28 -H Follow symlinks listed on command line
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
29 -L Follow all symlinks
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
30 -P Do not follow symlinks [default]
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
31
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
32 config CP_MORE
1196
37ea9dff9c27 Tweak help text.
Rob Landley <rob@landley.net>
parents: 1183
diff changeset
33 bool "cp -adlnrsv options"
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
34 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
35 depends on CP
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
36 help
1196
37ea9dff9c27 Tweak help text.
Rob Landley <rob@landley.net>
parents: 1183
diff changeset
37 usage: cp [-adlnrsv]
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
38
785
50441fee583d Teach cp to do -n.
Rob Landley <rob@landley.net>
parents: 784
diff changeset
39 -a same as -dpr
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
40 -d don't dereference symlinks
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
41 -l hard link instead of copy
785
50441fee583d Teach cp to do -n.
Rob Landley <rob@landley.net>
parents: 784
diff changeset
42 -n no clobber (don't overwrite DEST)
50441fee583d Teach cp to do -n.
Rob Landley <rob@landley.net>
parents: 784
diff changeset
43 -r synonym for -R
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
44 -s symlink instead of copy
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
45 -v verbose
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
46
1615
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
47 config MV
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
48 bool "mv"
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
49 default y
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
50 depends on CP
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
51 help
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
52 usage: mv [-fi] SOURCE... DEST"
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
53
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
54 -f force copy by deleting destination file
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
55 -i interactive, prompt before overwriting existing DEST
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
56
1615
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
57 config MV_MORE
985
32644e4439bd Need to specify bool for CP_MV_MORE config symbol to avoid warnings.
Rob Landley <rob@landley.net>
parents: 932
diff changeset
58 bool
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
59 default y
1615
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
60 depends on MV && CP_MORE
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
61 help
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
62 usage: mv [-vn]
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
63
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
64 -v verbose
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
65 -n no clobber (don't overwrite DEST)
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
66
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
67 config INSTALL
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
68 bool "install"
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
69 default y
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
70 depends on CP && CP_MORE
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
71 help
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
72 usage: install [-dDpsv] [-o USER] [-g GROUP] [-m MODE] [SOURCE...] DEST
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
73
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
74 Copy files and set attributes.
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
75
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
76 -d Act like mkdir -p
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
77 -D Create leading directories for DEST
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
78 -g Make copy belong to GROUP
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
79 -m Set permissions to MODE
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
80 -o Make copy belong to USER
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
81 -p Preserve timestamps
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
82 -s Call "strip -p"
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
83 -v Verbose
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
84 */
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
85
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
86 #define FOR_cp
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
87 #include "toys.h"
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
88
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
89 GLOBALS(
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
90 // install's options
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
91 char *group;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
92 char *user;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
93 char *mode;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
94
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
95 char *destname;
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
96 struct stat top;
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
97 int (*callback)(struct dirtree *try);
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
98 uid_t uid;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
99 gid_t gid;
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
100 )
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
101
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
102 // Callback from dirtree_read() for each file/directory under a source dir.
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
103
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
104 int cp_node(struct dirtree *try)
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
105 {
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
106 int fdout = -1, cfd = try->parent ? try->parent->extra : AT_FDCWD,
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
107 tfd = dirtree_parentfd(try);
784
d8b2f7706f82 Teach cp to do mknod.
Rob Landley <rob@landley.net>
parents: 783
diff changeset
108 unsigned flags = toys.optflags;
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
109 char *catch = try->parent ? try->name : TT.destname, *err = "%s";
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
110 struct stat cst;
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
111
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
112 if (!dirtree_notdotdot(try)) return 0;
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
113
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
114 // If returning from COMEAGAIN, jump straight to -p logic at end.
1404
ffc7f606ce5b Move DIRTREE_COMEAGAIN second callback up to when the filehandle is still open, and add dir->again variable to distinguish second call instead of checking for -1 filehandle.
Rob Landley <rob@landley.net>
parents: 1388
diff changeset
115 if (S_ISDIR(try->st.st_mode) && try->again) {
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
116 fdout = try->extra;
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
117 err = 0;
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
118 } else {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
119
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
120 // -d is only the same as -r for symlinks, not for directories
1518
4bfbd8b96f66 Various bugfixes (mostly resource leaks) from Ashwini Sharma's static analysis, plus occasional tweak by me while reviewing them.
Rob Landley <rob@landley.net>
parents: 1472
diff changeset
121 if (S_ISLNK(try->st.st_mode) && (flags & FLAG_d)) flags |= FLAG_r;
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
122
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
123 // Detect recursive copies via repeated top node (cp -R .. .) or
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
124 // identical source/target (fun with hardlinks).
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
125 if ((TT.top.st_dev == try->st.st_dev && TT.top.st_ino == try->st.st_ino
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
126 && (catch = TT.destname))
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
127 || (!fstatat(cfd, catch, &cst, 0) && cst.st_dev == try->st.st_dev
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
128 && cst.st_ino == try->st.st_ino))
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
129 {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
130 error_msg("'%s' is '%s'", catch, err = dirtree_path(try, 0));
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
131 free(err);
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
132
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
133 return 0;
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
134 }
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
135
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
136 // Handle -inv
497
da73bb464ce8 Implemented -i for cp
Bryce Fricke
parents: 435
diff changeset
137
798
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
138 if (!faccessat(cfd, catch, F_OK, 0) && !S_ISDIR(cst.st_mode)) {
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
139 char *s;
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
140
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
141 if (S_ISDIR(try->st.st_dev)) {
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
142 error_msg("dir at '%s'", s = dirtree_path(try, 0));
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
143 free(s);
802
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
144 return 0;
1455
70f5188b4848 Add cp -F to force delete of existing destination files, and make install command use that.
Rob Landley <rob@landley.net>
parents: 1438
diff changeset
145 } else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0)) {
70f5188b4848 Add cp -F to force delete of existing destination files, and make install command use that.
Rob Landley <rob@landley.net>
parents: 1438
diff changeset
146 error_msg("unlink '%s'", catch);
70f5188b4848 Add cp -F to force delete of existing destination files, and make install command use that.
Rob Landley <rob@landley.net>
parents: 1438
diff changeset
147 return 0;
798
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
148 } else if (flags & FLAG_n) return 0;
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
149 else if (flags & FLAG_i) {
1521
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
150 fprintf(stderr, "%s: overwrite '%s'", toys.which->name,
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
151 s = dirtree_path(try, 0));
798
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
152 free(s);
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
153 if (!yesno("", 1)) return 0;
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
154 }
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
155 }
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
156
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
157 if (flags & FLAG_v) {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
158 char *s = dirtree_path(try, 0);
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
159 printf("%s '%s'\n", toys.which->name, s);
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
160 free(s);
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
161 }
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
162
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
163 // Loop for -f retry after unlink
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
164 do {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
165
790
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
166 // directory, hardlink, symlink, mknod (char, block, fifo, socket), file
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
167
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
168 // Copy directory
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
169
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
170 if (S_ISDIR(try->st.st_mode)) {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
171 struct stat st2;
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
172
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
173 if (!(flags & (FLAG_a|FLAG_r|FLAG_R))) {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
174 err = "Skipped dir '%s'";
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
175 catch = try->name;
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
176 break;
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
177 }
784
d8b2f7706f82 Teach cp to do mknod.
Rob Landley <rob@landley.net>
parents: 783
diff changeset
178
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
179 // Always make directory writeable to us, so we can create files in it.
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
180 //
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
181 // Yes, there's a race window between mkdir() and open() so it's
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
182 // possible that -p can be made to chown a directory other than the one
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
183 // we created. The closest we can do to closing this is make sure
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
184 // that what we open _is_ a directory rather than something else.
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
185
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
186 if (!mkdirat(cfd, catch, try->st.st_mode | 0200) || errno == EEXIST)
790
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
187 if (-1 != (try->extra = openat(cfd, catch, O_NOFOLLOW)))
1388
c4f5f82adce6 Implement -HL for cp.
Rob Landley <rob@landley.net>
parents: 1196
diff changeset
188 if (!fstat(try->extra, &st2) && S_ISDIR(st2.st_mode))
c4f5f82adce6 Implement -HL for cp.
Rob Landley <rob@landley.net>
parents: 1196
diff changeset
189 return DIRTREE_COMEAGAIN
c4f5f82adce6 Implement -HL for cp.
Rob Landley <rob@landley.net>
parents: 1196
diff changeset
190 | (DIRTREE_SYMFOLLOW*!!(toys.optflags&FLAG_L));
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
191
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
192 // Hardlink
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
193
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
194 } else if (flags & FLAG_l) {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
195 if (!linkat(tfd, try->name, cfd, catch, 0)) err = 0;
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
196
802
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
197 // Copy tree as symlinks. For non-absolute paths this involves
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
198 // appending the right number of .. entries as you go down the tree.
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
199
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
200 } else if (flags & FLAG_s) {
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
201 char *s;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
202 struct dirtree *or;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
203 int dotdots = 0;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
204
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
205 s = dirtree_path(try, 0);
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
206 for (or = try; or->parent; or = or->parent) dotdots++;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
207
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
208 if (*or->name == '/') dotdots = 0;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
209 if (dotdots) {
1714
0c8ef714cf03 Patches from Elliott Hughes to fix various printf() format strings.
Rob Landley <rob@landley.net>
parents: 1699
diff changeset
210 char *s2 = xmprintf("%*c%s", 3*dotdots, ' ', s);
802
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
211 free(s);
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
212 s = s2;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
213 while(dotdots--) {
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
214 memcpy(s2, "../", 3);
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
215 s2 += 3;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
216 }
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
217 }
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
218 if (!symlinkat(s, cfd, catch)) {
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
219 err = 0;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
220 fdout = AT_FDCWD;
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
221 }
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
222 free(s);
aad12ce05aae Implement cp -s option.
Rob Landley <rob@landley.net>
parents: 798
diff changeset
223
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
224 // Do something _other_ than copy contents of a file?
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
225 } else if (!S_ISREG(try->st.st_mode)
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
226 && (try->parent || (flags & (FLAG_a|FLAG_r))))
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
227 {
793
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
228 int i;
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
229
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
230 // make symlink, or make block/char/fifo/socket
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
231 if (S_ISLNK(try->st.st_mode)
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
232 ? (0 < (i = readlinkat(tfd, try->name, toybuf, sizeof(toybuf))) &&
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
233 sizeof(toybuf) > i && !symlinkat(toybuf, cfd, catch))
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
234 : !mknodat(cfd, catch, try->st.st_mode, try->st.st_rdev))
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
235 {
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
236 err = 0;
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
237 fdout = AT_FDCWD;
790
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
238 }
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
239
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
240 // Copy contents of file.
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
241 } else {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
242 int fdin;
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
243
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
244 fdin = openat(tfd, try->name, O_RDONLY);
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
245 if (fdin < 0) {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
246 catch = try->name;
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
247 break;
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
248 } else {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
249 fdout = openat(cfd, catch, O_RDWR|O_CREAT|O_TRUNC, try->st.st_mode);
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
250 if (fdout >= 0) {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
251 xsendfile(fdin, fdout);
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
252 err = 0;
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
253 }
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
254 close(fdin);
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
255 }
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
256 }
798
16bcabb8cf97 Fix -in behavior: descend into existing directory without prompting, show full path in error messages, actually overwrite when answering yes to -i.
Rob Landley <rob@landley.net>
parents: 793
diff changeset
257 } while (err && (flags & (FLAG_f|FLAG_n)) && !unlinkat(cfd, catch, 0));
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
258 }
282
a08f1affe016 Add -v to cp.
Rob Landley <rob@landley.net>
parents: 272
diff changeset
259
786
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
260 if (fdout != -1) {
5bc258a4c750 Update -p and -f to apply properly to various conditions. Still some bugs to squeeze out but this gets the infrastructure mostly right (and does away with the remaining gotos).
Rob Landley <rob@landley.net>
parents: 785
diff changeset
261 if (flags & (FLAG_a|FLAG_p)) {
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
262 struct timespec times[2];
1571
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
263 int rc;
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
264
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
265 // Inability to set these isn't fatal, some require root access.
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
266
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
267 times[0] = try->st.st_atim;
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
268 times[1] = try->st.st_mtim;
790
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
269
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
270 // If we can't get a filehandle to the actual object, use racy functions
1571
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
271 if (fdout == AT_FDCWD)
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
272 rc = fchownat(cfd, catch, try->st.st_uid, try->st.st_gid,
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
273 AT_SYMLINK_NOFOLLOW);
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
274 else rc = fchown(fdout, try->st.st_uid, try->st.st_gid);
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
275 if (rc) {
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
276 char *pp;
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
277
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
278 perror_msg("chown '%s'", pp = dirtree_path(try, 0));
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
279 free(pp);
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
280 }
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
281
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
282 // permission bits already correct for mknod and don't apply to symlink
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
283 if (fdout == AT_FDCWD) utimensat(cfd, catch, times, AT_SYMLINK_NOFOLLOW);
e85e5f3b87c2 As long as Android's going to require fortify, fixup the warnings it generates.
Rob Landley <rob@landley.net>
parents: 1522
diff changeset
284 else {
793
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
285 futimens(fdout, times);
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
286 fchmod(fdout, try->st.st_mode);
790
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
287 }
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
288 }
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
289
790
6aa6efdd5883 Make "sudo cp -rp /dev/null blah" work. Still not happy with it, fchmodat(AT_SYMLINK_NOFOLLOW) doesn't work (there's a glibc bug open for this. It's really a missing kernel syscall, but glibc fails without ever making any syscall if you feed it that flag, which isn't helpful).
Rob Landley <rob@landley.net>
parents: 786
diff changeset
290 if (fdout != AT_FDCWD) xclose(fdout);
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
291
1615
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
292 if (CFG_MV && toys.which->name[0] == 'm')
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
293 if (unlinkat(tfd, try->name, S_ISDIR(try->st.st_mode) ? AT_REMOVEDIR :0))
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
294 err = "%s";
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
295 }
263
7c53152a483b Make cp pass most of its test suite. Still need to add symlink support.
Rob Landley <rob@landley.net>
parents: 262
diff changeset
296
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
297 if (err) perror_msg(err, catch);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
298 return 0;
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
299 }
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
300
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
301 void cp_main(void)
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
302 {
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
303 char *destname = toys.optargs[--toys.optc];
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
304 int i, destdir = !stat(destname, &TT.top) && S_ISDIR(TT.top.st_mode);
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
305
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
306 if (toys.optc>1 && !destdir) error_exit("'%s' not directory", destname);
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
307 if (toys.which->name[0] == 'm') toys.optflags |= FLAG_d|FLAG_p|FLAG_R;
793
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
308 if (toys.optflags & (FLAG_a|FLAG_p)) umask(0);
f8f5ddb6b69a Adjust umask(0) for cp -p so mknod doesn't have to try to fchmodat() without the unsupported symlink nofollow flag.
Rob Landley <rob@landley.net>
parents: 790
diff changeset
309
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
310 if (!TT.callback) TT.callback = cp_node;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
311
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
312 // Loop through sources
263
7c53152a483b Make cp pass most of its test suite. Still need to add symlink support.
Rob Landley <rob@landley.net>
parents: 262
diff changeset
313
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
314 for (i=0; i<toys.optc; i++) {
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
315 struct dirtree *new;
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
316 char *src = toys.optargs[i];
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
317 int rc = 1;
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
318
1183
0752b2d58909 Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents: 985
diff changeset
319 if (destdir) TT.destname = xmprintf("%s/%s", destname, basename(src));
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
320 else TT.destname = destname;
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
321
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
322 errno = EXDEV;
1615
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
323 if (CFG_MV && toys.which->name[0] == 'm') {
1521
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
324 if (!(toys.optflags & FLAG_f)) {
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
325 struct stat st;
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
326
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
327 // Technically "is writeable" is more complicated (022 is not writeable
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
328 // by the owner, just everybody _else_) but I don't care.
1522
fc2200f927af Fix last night's mv commit (use dest, not source).
Rob Landley <rob@landley.net>
parents: 1521
diff changeset
329 if (!stat(TT.destname, &st)
1521
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
330 && ((toys.optflags & FLAG_i) || !(st.st_mode & 0222)))
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
331 {
1522
fc2200f927af Fix last night's mv commit (use dest, not source).
Rob Landley <rob@landley.net>
parents: 1521
diff changeset
332 fprintf(stderr, "%s: overwrite '%s'", toys.which->name, TT.destname);
1521
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
333 if (!yesno("", 1)) rc = 0;
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
334 else unlink(src);
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
335 }
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
336 }
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
337
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
338 if (rc) rc = rename(src, TT.destname);
a8d9bf6ca8bd Implement mv -f and -i.
Rob Landley <rob@landley.net>
parents: 1518
diff changeset
339 }
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
340
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
341 // Skip nonexistent sources
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
342 if (rc) {
1388
c4f5f82adce6 Implement -HL for cp.
Rob Landley <rob@landley.net>
parents: 1196
diff changeset
343 int symfollow = toys.optflags & (FLAG_H|FLAG_L);
c4f5f82adce6 Implement -HL for cp.
Rob Landley <rob@landley.net>
parents: 1196
diff changeset
344
c4f5f82adce6 Implement -HL for cp.
Rob Landley <rob@landley.net>
parents: 1196
diff changeset
345 if (errno != EXDEV || !(new = dirtree_add_node(0, src, symfollow)))
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
346 perror_msg("bad '%s'", src);
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
347 else dirtree_handle_callback(new, TT.callback);
783
7bbb49149bb6 Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
348 }
932
58b5263d63bf Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents: 802
diff changeset
349 if (destdir) free(TT.destname);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
350 }
262
70f36d9c5387 Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff changeset
351 }
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
352
1615
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
353 void mv_main(void)
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
354 {
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
355 cp_main();
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
356 }
95b6ae6498fd Decouple cp and mv so mv uses its own --help text.
Rob Landley <rob@landley.net>
parents: 1571
diff changeset
357
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
358 #define CLEANUP_cp
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
359 #define FOR_install
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
360 #include <generated/flags.h>
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
361
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
362 static int install_node(struct dirtree *try)
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
363 {
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
364 if (TT.mode) try->st.st_mode = string_to_mode(TT.mode, try->st.st_mode);
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
365 if (TT.group) try->st.st_gid = TT.gid;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
366 if (TT.user) try->st.st_uid = TT.uid;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
367
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
368 // Always returns 0 because no -r
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
369 cp_node(try);
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
370
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
371 // No -r so always one level deep, so destname as set by cp_node() is correct
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
372 if (toys.optflags & FLAG_s)
1472
2f9bc9495144 Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents: 1455
diff changeset
373 if (xrun((char *[]){"strip", "-p", TT.destname, 0})) toys.exitval = 1;
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
374
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
375 return 0;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
376 }
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
377
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
378 void install_main(void)
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
379 {
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
380 char **ss;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
381 int flags = toys.optflags;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
382
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
383 if (flags & FLAG_d) {
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
384 for (ss = toys.optargs; *ss; ss++) {
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
385 if (mkpathat(AT_FDCWD, *ss, 0777, 3)) perror_msg("%s", *ss);
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
386 if (flags & FLAG_v) printf("%s\n", *ss);
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
387 }
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
388
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
389 return;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
390 }
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
391
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
392 if (toys.optflags & FLAG_D) {
1746
b11f536bac74 install -D bugfix from David Halls.
Rob Landley <rob@landley.net>
parents: 1731
diff changeset
393 TT.destname = toys.optargs[toys.optc-1];
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
394 if (mkpathat(AT_FDCWD, TT.destname, 0, 2))
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
395 perror_exit("-D '%s'", TT.destname);
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
396 if (toys.optc == 1) return;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
397 }
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
398 if (toys.optc < 2) error_exit("needs 2 args");
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
399
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
400 // Translate flags from install to cp
1455
70f5188b4848 Add cp -F to force delete of existing destination files, and make install command use that.
Rob Landley <rob@landley.net>
parents: 1438
diff changeset
401 toys.optflags = 4; // Force cp's FLAG_F
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
402 if (flags & FLAG_v) toys.optflags |= 8; // cp's FLAG_v
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
403 if (flags & (FLAG_p|FLAG_o|FLAG_g)) toys.optflags |= 512; // cp's FLAG_p
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
404
1731
5a00bc5e1c0f Make install support numeric uid/gids (reported by Kylie McClain).
Rob Landley <rob@landley.net>
parents: 1714
diff changeset
405 if (TT.user) TT.uid = xgetpwnamid(TT.user)->pw_uid;
5a00bc5e1c0f Make install support numeric uid/gids (reported by Kylie McClain).
Rob Landley <rob@landley.net>
parents: 1714
diff changeset
406 if (TT.group) TT.gid = xgetgrnamid(TT.group)->gr_gid;
1438
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
407
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
408 TT.callback = install_node;
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
409 cp_main();
7a3afbc7fc8b Add install to cp/mv.
Rob Landley <rob@landley.net>
parents: 1404
diff changeset
410 }