Mercurial > hg > toybox
annotate toys/posix/cp.c @ 784:d8b2f7706f82
Teach cp to do mknod.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 09 Jan 2013 05:16:48 -0600 |
parents | 7bbb49149bb6 |
children | 50441fee583d |
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 * |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
5 * TODO: sHLP |
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 |
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
|
7 USE_CP(NEWTOY(cp, "<2"USE_CP_MORE("rdavsl")"RHLPfip", TOYFLAG_BIN)) |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 config CP |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
10 bool "cp" |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
11 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
|
12 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
|
13 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
|
14 |
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
|
15 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
|
16 be a directory. |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 |
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
|
18 -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
|
19 -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
|
20 -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
|
21 -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
|
22 -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
|
23 -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
|
24 -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
|
25 |
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
|
26 config CP_MORE |
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
|
27 bool "cp -rdavsl options" |
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 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
|
29 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
|
30 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
|
31 usage: cp [-rdavsl] |
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
|
32 |
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
|
33 -r synonym for -R |
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 -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
|
35 -a same as -dpr |
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 -l hard link 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
|
37 -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
|
38 -v verbose |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 */ |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 |
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
|
41 #define FOR_cp |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 #include "toys.h" |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 |
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
|
44 // TODO: PLHlsd |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 |
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
|
46 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
|
47 char *destname; |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
48 struct stat top; |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 ) |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
51 // 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
|
52 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
53 int cp_node(struct dirtree *try) |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
54 { |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
55 int fdout, cfd = try->parent ? try->parent->extra : AT_FDCWD, |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
56 tfd = dirtree_parentfd(try); |
784 | 57 unsigned flags = toys.optflags; |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
58 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
|
59 struct stat cst; |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
61 if (!dirtree_notdotdot(try)) return 0; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
62 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
63 // 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
|
64 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
|
65 fdout = try->extra; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
66 err = 0; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
67 goto dashp; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
68 } |
497 | 69 |
784 | 70 // -d is only the same as -r for symlinks, not for directories |
71 if (S_ISLNK(try->st.st_mode) & (flags & FLAG_d)) flags |= FLAG_r; | |
72 | |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
73 // Detect recursive copies via repeated top node (cp -R .. .) or |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
74 // identical source/target (fun with hardlinks). |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
75 if ((TT.top.st_dev == try->st.st_dev && TT.top.st_ino == try->st.st_ino |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
76 && (catch = TT.destname)) |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
77 || (!fstatat(cfd, catch, &cst, 0) && cst.st_dev == try->st.st_dev |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
78 && cst.st_ino == try->st.st_ino)) |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
79 { |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
80 char *s = dirtree_path(try, 0); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
81 error_msg("'%s' is '%s'", catch, s); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
82 free(s); |
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 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 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
87 // Handle -i and -v |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
88 |
784 | 89 if ((flags & FLAG_i) && !faccessat(cfd, catch, R_OK, 0) |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
90 && !yesno("cp: overwrite", 1)) return 0; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
91 |
784 | 92 if (flags & FLAG_v) { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
93 char *s = dirtree_path(try, 0); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
94 printf("cp '%s'\n", s); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
95 free(s); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
96 } |
282 | 97 |
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
|
98 // Copy directory or file to destination. |
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
|
99 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
100 if (S_ISDIR(try->st.st_mode)) { |
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
|
101 struct stat st2; |
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
|
102 |
784 | 103 if (!(flags & (FLAG_a|FLAG_r|FLAG_R))) { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
104 err = "Skipped dir '%s'"; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
105 catch = try->name; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
106 |
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
|
107 // Always make directory writeable to us, so we can create files in it. |
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
|
108 // |
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
|
109 // Yes, there's a race window between mkdir() and open() so it's |
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
|
110 // possible that -p can be made to chown a directory other than the one |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
111 // we created. The closest we can do to closing this is make sure |
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
|
112 // that what we open _is_ a directory rather than something else. |
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
|
113 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
114 } else if ((mkdirat(cfd, catch, try->st.st_mode | 0200) && errno != EEXIST) |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
115 || 0>(try->extra = openat(cfd, catch, 0)) || fstat(try->extra, &st2) |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
116 || !S_ISDIR(st2.st_mode)); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
117 else return DIRTREE_COMEAGAIN; |
784 | 118 } else if (flags & FLAG_l) { |
119 if (!linkat(tfd, try->name, cfd, catch, 0)) err = 0; | |
120 } else if ((try->parent || (flags & (FLAG_a|FLAG_r))) | |
121 && !S_ISREG(try->st.st_mode)) | |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
122 { |
784 | 123 if (S_ISLNK(try->st.st_mode)) { |
124 int i = readlinkat(tfd, try->name, toybuf, sizeof(toybuf)); | |
125 if (i > 0 && i < sizeof(toybuf) && !symlinkat(toybuf, cfd, catch)) | |
126 err = 0; | |
127 } else if (!mknodat(cfd, catch, try->st.st_mode, try->st.st_dev)) err = 0; | |
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
|
128 } else { |
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
|
129 int fdin, i; |
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
|
130 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
131 fdin = openat(tfd, try->name, O_RDONLY); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
132 if (fdin < 0) catch = try->name; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
133 else { |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
134 for (i=2 ; i; i--) { |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
135 fdout = openat(cfd, catch, O_RDWR|O_CREAT|O_TRUNC, try->st.st_mode); |
784 | 136 if (fdout>=0 || !(flags & FLAG_f)) break; |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
137 unlinkat(cfd, catch, 0); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
138 } |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
139 if (fdout >= 0) { |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
140 xsendfile(fdin, fdout); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
141 err = 0; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
142 } |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
143 close(fdin); |
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
|
144 } |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
145 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
146 dashp: |
784 | 147 if (!(flags & FLAG_l) && (flags & (FLAG_a|FLAG_p))) { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
148 struct timespec times[2]; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
149 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
150 // 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
|
151 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
152 fchown(fdout, try->st.st_uid, try->st.st_gid); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
153 times[0] = try->st.st_atim; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
154 times[1] = try->st.st_mtim; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
155 futimens(fdout, times); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
156 fchmod(fdout, try->st.st_mode); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
157 } |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
158 |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
159 xclose(fdout); |
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
|
160 } |
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
|
161 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
162 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
|
163 return 0; |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 } |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 void cp_main(void) |
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
168 char *destname = toys.optargs[--toys.optc]; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
169 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
|
170 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
171 if (toys.optc>1 && !destdir) error_exit("'%s' not directory", destname); |
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
|
172 |
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
|
173 // 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
|
174 |
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
|
175 for (i=0; i<toys.optc; i++) { |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
176 struct dirtree *new; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
177 char *src = toys.optargs[i]; |
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
|
178 |
783
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
179 // Skip nonexistent sources |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
180 if (!(new = dirtree_add_node(0, src, !(toys.optflags & (FLAG_d|FLAG_a))))) |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
181 perror_msg("bad '%s'", src); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
182 else { |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
183 if (destdir) TT.destname = xmsprintf("%s/%s", destname, basename(src)); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
184 else TT.destname = destname; |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
185 dirtree_handle_callback(new, cp_node); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
186 if (destdir) free(TT.destname); |
7bbb49149bb6
Adapt cp to updated dirtree code.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
187 } |
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
|
188 } |
262
70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
189 } |