Mercurial > hg > toybox
annotate toys/posix/cp.c @ 1388:c4f5f82adce6 draft
Implement -HL for cp.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 14 Jul 2014 05:38:27 -0500 |
parents | 37ea9dff9c27 |
children | ffc7f606ce5b |
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 | 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 |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
7 // This is subtle: MV options must be in same order (right to left) as CP |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
8 // for FLAG_X macros to work out right. |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
9 |
1388 | 10 USE_CP(NEWTOY(cp, "<2RHLPp"USE_CP_MORE("rdaslvn")"fi[-HLPd]"USE_CP_MORE("[-ni]"), TOYFLAG_BIN)) |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
11 USE_CP_MV(OLDTOY(mv, cp, "<2"USE_CP_MORE("vn")"fi"USE_CP_MORE("[-ni]"), TOYFLAG_BIN)) |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 config CP |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
14 bool "cp" |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 |
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
|
19 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
|
20 be a directory. |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 |
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
|
22 -f force copy by deleting destination file |
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
|
23 -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
|
24 -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
|
25 -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
|
26 -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
|
27 -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
|
28 -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
|
29 |
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
|
30 config CP_MORE |
1196 | 31 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
|
32 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
|
33 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
|
34 help |
1196 | 35 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
|
36 |
785 | 37 -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
|
38 -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
|
39 -l hard link instead of copy |
785 | 40 -n no clobber (don't overwrite DEST) |
41 -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
|
42 -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
|
43 -v verbose |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
44 |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
45 config CP_MV |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
46 bool "mv" |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
47 default y |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
48 depends on CP |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
49 help |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
50 usage: mv [-fi] SOURCE... DEST" |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
51 |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
52 -f force copy by deleting destination file |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
53 -i interactive, prompt before overwriting existing DEST |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
54 |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
55 config CP_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
|
56 bool |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
57 default y |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
58 depends on CP_MV && CP_MORE |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
59 help |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
60 usage: mv [-vn] |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
61 |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
62 -v verbose |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
63 -n no clobber (don't overwrite DEST) |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 */ |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 |
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
|
66 #define FOR_cp |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 #include "toys.h" |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 |
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
|
69 GLOBALS( |
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
|
70 char *destname; |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
71 struct stat top; |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
72 ) |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
73 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
74 // 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
|
75 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
76 int cp_node(struct dirtree *try) |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
77 { |
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
|
78 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
|
79 tfd = dirtree_parentfd(try); |
784 | 80 unsigned flags = toys.optflags; |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
81 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
|
82 struct stat cst; |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
84 if (!dirtree_notdotdot(try)) return 0; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
85 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
86 // If returning from COMEAGAIN, jump straight to -p logic at end. |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
87 if (S_ISDIR(try->st.st_mode) && try->data == -1) { |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
88 fdout = try->extra; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
89 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
|
90 } 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
|
91 |
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
|
92 // -d is only the same as -r for symlinks, not for directories |
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
|
93 if (S_ISLNK(try->st.st_mode) & (flags & FLAG_d)) flags |= 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
|
94 |
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
|
95 // 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
|
96 // 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
|
97 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
|
98 && (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
|
99 || (!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
|
100 && 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
|
101 { |
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
|
102 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
|
103 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
|
104 |
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
|
105 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
|
106 } |
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
|
107 |
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
|
108 // Handle -inv |
497 | 109 |
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
|
110 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
|
111 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
|
112 |
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
|
113 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
|
114 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
|
115 free(s); |
802 | 116 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
|
117 } 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
|
118 else if (flags & FLAG_i) { |
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
|
119 fprintf(stderr, "cp: overwrite '%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
|
120 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
|
121 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
|
122 } |
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
|
123 } |
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
|
124 |
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 (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
|
126 char *s = 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
|
127 printf("cp '%s'\n", 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
|
128 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
|
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 |
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 // 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
|
132 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
|
133 |
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
|
134 // 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
|
135 |
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
|
136 // 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
|
137 |
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
|
138 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
|
139 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
|
140 |
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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 } |
784 | 146 |
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
|
147 // 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
|
148 // |
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
|
149 // 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
|
150 // 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
|
151 // 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
|
152 // 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
|
153 |
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
|
154 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
|
155 if (-1 != (try->extra = openat(cfd, catch, O_NOFOLLOW))) |
1388 | 156 if (!fstat(try->extra, &st2) && S_ISDIR(st2.st_mode)) |
157 return DIRTREE_COMEAGAIN | |
158 | (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
|
159 |
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 // 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
|
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 } 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
|
163 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
|
164 |
802 | 165 // Copy tree as symlinks. For non-absolute paths this involves |
166 // appending the right number of .. entries as you go down the tree. | |
167 | |
168 } else if (flags & FLAG_s) { | |
169 char *s; | |
170 struct dirtree *or; | |
171 int dotdots = 0; | |
172 | |
173 s = dirtree_path(try, 0); | |
174 for (or = try; or->parent; or = or->parent) dotdots++; | |
175 | |
176 if (*or->name == '/') dotdots = 0; | |
177 if (dotdots) { | |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
985
diff
changeset
|
178 char *s2 = xmprintf("% *c%s", 3*dotdots, ' ', s); |
802 | 179 free(s); |
180 s = s2; | |
181 while(dotdots--) { | |
182 memcpy(s2, "../", 3); | |
183 s2 += 3; | |
184 } | |
185 } | |
186 if (!symlinkat(s, cfd, catch)) { | |
187 err = 0; | |
188 fdout = AT_FDCWD; | |
189 } | |
190 free(s); | |
191 | |
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
|
192 // 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
|
193 } 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
|
194 && (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
|
195 { |
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
|
196 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
|
197 |
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
|
198 // 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
|
199 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
|
200 ? (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
|
201 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
|
202 : !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
|
203 { |
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
|
204 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
|
205 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
|
206 } |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
207 |
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
|
208 // 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
|
209 } 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
|
210 int fdin; |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
211 |
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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 } 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 } |
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
|
222 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
|
223 } |
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 } |
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
|
225 } 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
|
226 } |
282 | 227 |
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
|
228 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
|
229 if (flags & (FLAG_a|FLAG_p)) { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
230 struct timespec times[2]; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
231 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
232 // 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
|
233 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
234 times[0] = try->st.st_atim; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
235 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
|
236 |
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
|
237 // If we can't get a filehandle to the actual object, use racy functions |
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 if (fdout == AT_FDCWD) { |
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
|
239 fchownat(cfd, catch, try->st.st_uid, try->st.st_gid, |
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
|
240 AT_SYMLINK_NOFOLLOW); |
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
|
241 utimensat(cfd, catch, times, AT_SYMLINK_NOFOLLOW); |
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
|
242 // permission bits already correct for mknod, don't apply to symlink |
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
|
243 } 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
|
244 fchown(fdout, try->st.st_uid, try->st.st_gid); |
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
|
245 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
|
246 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
|
247 } |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
248 } |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
249 |
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
|
250 if (fdout != AT_FDCWD) xclose(fdout); |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
251 |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
252 if (toys.which->name[0] == 'm') |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
253 if (unlinkat(tfd, try->name, S_ISDIR(try->st.st_mode) ? AT_REMOVEDIR : 0)) |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
254 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
|
255 } |
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
|
256 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
257 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
|
258 return 0; |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
259 } |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
260 |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
261 void cp_main(void) |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
262 { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
263 char *destname = toys.optargs[--toys.optc]; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
264 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
|
265 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
266 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
|
267 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
|
268 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
|
269 |
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
|
270 // 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
|
271 |
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
|
272 for (i=0; i<toys.optc; i++) { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
273 struct dirtree *new; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
274 char *src = toys.optargs[i]; |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
275 int rc = 1; |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
276 |
1183
0752b2d58909
Rename xmsprintf() to just xmprintf().
Rob Landley <rob@landley.net>
parents:
985
diff
changeset
|
277 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
|
278 else TT.destname = destname; |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
279 |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
280 errno = EXDEV; |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
281 if (toys.which->name[0] == 'm') rc = rename(src, TT.destname); |
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
|
282 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
283 // Skip nonexistent sources |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
284 if (rc) { |
1388 | 285 int symfollow = toys.optflags & (FLAG_H|FLAG_L); |
286 | |
287 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
|
288 perror_msg("bad '%s'", src); |
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
289 else dirtree_handle_callback(new, cp_node); |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
290 } |
932
58b5263d63bf
Implement mv as an extension of cp.
Rob Landley <rob@landley.net>
parents:
802
diff
changeset
|
291 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
|
292 } |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
293 } |