1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92#define FORCE_FLAGS
93#define FOR_cp
94#include "toys.h"
95
96GLOBALS(
97 union {
98
99 struct {
100 char *g, *o, *m, *t;
101 } i;
102
103 struct {
104 char *t, *preserve;
105 } c;
106 };
107
108 char *destname;
109 struct stat top;
110 int (*callback)(struct dirtree *try);
111 uid_t uid;
112 gid_t gid;
113 int pflags;
114)
115
116struct cp_preserve {
117 char *name;
118} static const cp_preserve[] = TAGGED_ARRAY(CP,
119 {"mode"}, {"ownership"}, {"timestamps"}, {"context"}, {"xattr"},
120);
121
122
123
124static int cp_node(struct dirtree *try)
125{
126 int fdout = -1, cfd = try->parent ? try->parent->extra : AT_FDCWD,
127 save = DIRTREE_SAVE*(CFG_MV && toys.which->name[0] == 'm'), rc = 0,
128 tfd = dirtree_parentfd(try);
129 unsigned flags = toys.optflags;
130 char *s = 0, *catch = try->parent ? try->name : TT.destname, *err = "%s";
131 struct stat cst;
132
133 if (!dirtree_notdotdot(try)) return 0;
134
135
136 if (S_ISDIR(try->st.st_mode) && try->again) {
137 fdout = try->extra;
138 err = 0;
139
140
141 if (try->child) {
142 save = 0;
143 llist_traverse(try->child, free);
144 }
145 } else {
146
147 if (S_ISLNK(try->st.st_mode) && (flags & FLAG_d)) flags |= FLAG_r;
148
149
150
151 if ((same_file(&TT.top, &try->st) && (catch = TT.destname))
152 || (!fstatat(cfd, catch, &cst, 0) && same_file(&cst, &try->st)))
153 {
154 error_msg("'%s' is '%s'", catch, err = dirtree_path(try, 0));
155 free(err);
156
157 return save;
158 }
159
160
161 if (!faccessat(cfd, catch, F_OK, 0) && !S_ISDIR(cst.st_mode)) {
162 if (S_ISDIR(try->st.st_mode))
163 error_msg("dir at '%s'", s = dirtree_path(try, 0));
164 else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0))
165 error_msg("unlink '%s'", catch);
166 else if (flags & FLAG_i) {
167 fprintf(stderr, "%s: overwrite '%s'", toys.which->name,
168 s = dirtree_path(try, 0));
169 if (yesno(0)) rc++;
170 } else if (!((flags&FLAG_u) && nanodiff(&try->st.st_mtim, &cst.st_mtim)>0)
171 && !(flags & FLAG_n)) rc++;
172 free(s);
173 if (!rc) return save;
174 }
175
176 if (flags & FLAG_v) {
177 printf("%s '%s'\n", toys.which->name, s = dirtree_path(try, 0));
178 free(s);
179 }
180
181
182 do {
183
184
185
186
187
188 if (S_ISDIR(try->st.st_mode)) {
189 struct stat st2;
190
191 if (!(flags & (FLAG_a|FLAG_r))) {
192 err = "Skipped dir '%s'";
193 catch = try->name;
194 break;
195 }
196
197
198
199
200
201
202
203
204 if (!mkdirat(cfd, catch, try->st.st_mode | 0200) || errno == EEXIST)
205 if (-1 != (try->extra = openat(cfd, catch, O_NOFOLLOW)))
206 if (!fstat(try->extra, &st2) && S_ISDIR(st2.st_mode))
207 return DIRTREE_COMEAGAIN | (DIRTREE_SYMFOLLOW*!!FLAG(L));
208
209
210
211 } else if (flags & FLAG_l) {
212 if (!linkat(tfd, try->name, cfd, catch, 0)) err = 0;
213
214
215
216
217 } else if (flags & FLAG_s) {
218 char *s;
219 struct dirtree *or;
220 int dotdots = 0;
221
222 s = dirtree_path(try, 0);
223 for (or = try; or->parent; or = or->parent) dotdots++;
224
225 if (*or->name == '/') dotdots = 0;
226 if (dotdots) {
227 char *s2 = xmprintf("%*c%s", 3*dotdots, ' ', s);
228 free(s);
229 s = s2;
230 while(dotdots--) {
231 memcpy(s2, "../", 3);
232 s2 += 3;
233 }
234 }
235 if (!symlinkat(s, cfd, catch)) {
236 err = 0;
237 fdout = AT_FDCWD;
238 }
239 free(s);
240
241
242 } else if (!S_ISREG(try->st.st_mode)
243 && (try->parent || (flags & (FLAG_a|FLAG_P|FLAG_r))))
244 {
245
246 if (S_ISLNK(try->st.st_mode)
247 ? readlinkat0(tfd, try->name, toybuf, sizeof(toybuf)) &&
248 (!unlinkat(cfd, catch, 0) || ENOENT == errno) &&
249 !symlinkat(toybuf, cfd, catch)
250 : !mknodat(cfd, catch, try->st.st_mode, try->st.st_rdev))
251 {
252 err = 0;
253 fdout = AT_FDCWD;
254 }
255
256
257 } else {
258 int fdin, ii;
259
260 fdin = openat(tfd, try->name, O_RDONLY);
261 if (fdin < 0) {
262 catch = try->name;
263 break;
264 }
265
266 if (S_ISLNK(try->st.st_mode)) fstat(fdin, &try->st);
267 fdout = openat(cfd, catch, O_RDWR|O_CREAT|O_TRUNC, try->st.st_mode);
268 if (fdout >= 0) {
269 xsendfile(fdin, fdout);
270 err = 0;
271 }
272
273
274 if (TT.pflags&(_CP_xattr|_CP_context)) {
275 ssize_t listlen = xattr_flist(fdin, 0, 0), len;
276 char *name, *value, *list;
277
278 if (listlen>0) {
279 list = xmalloc(listlen);
280 xattr_flist(fdin, list, listlen);
281 list[listlen-1] = 0;
282 for (name = list; name-list < listlen; name += strlen(name)+1) {
283
284 ii = strncmp(name, "security.", 9) ? _CP_xattr : _CP_context;
285 if (!(TT.pflags&ii)) continue;
286 if ((len = xattr_fget(fdin, name, 0, 0))>0) {
287 value = xmalloc(len);
288 if (len == xattr_fget(fdin, name, value, len))
289 if (xattr_fset(fdout, name, value, len, 0))
290 perror_msg("%s setxattr(%s=%s)", catch, name, value);
291 free(value);
292 }
293 }
294 free(list);
295 }
296 }
297
298 close(fdin);
299 }
300 } while (err && (flags & (FLAG_f|FLAG_n)) && !unlinkat(cfd, catch, 0));
301 }
302
303
304 if (fdout != -1) {
305
306
307
308 if (TT.pflags & _CP_ownership) {
309
310
311
312 if (fdout == AT_FDCWD)
313 rc = fchownat(cfd, catch, try->st.st_uid, try->st.st_gid,
314 AT_SYMLINK_NOFOLLOW);
315 else rc = fchown(fdout, try->st.st_uid, try->st.st_gid);
316 if (rc && !geteuid()) {
317 char *pp;
318
319 perror_msg("chown '%s'", pp = dirtree_path(try, 0));
320 free(pp);
321 }
322 }
323
324
325 if (TT.pflags & _CP_timestamps) {
326 struct timespec times[] = {try->st.st_atim, try->st.st_mtim};
327
328 if (fdout == AT_FDCWD) utimensat(cfd, catch, times, AT_SYMLINK_NOFOLLOW);
329 else futimens(fdout, times);
330 }
331
332
333 if (fdout != AT_FDCWD) {
334 if (TT.pflags & _CP_mode) fchmod(fdout, try->st.st_mode);
335 xclose(fdout);
336 }
337
338 if (save)
339 if (unlinkat(tfd, try->name, S_ISDIR(try->st.st_mode) ? AT_REMOVEDIR :0))
340 err = "%s";
341 }
342
343 if (err) {
344 if (catch == try->name) {
345 s = dirtree_path(try, 0);
346 while (try->parent) try = try->parent;
347 catch = xmprintf("%s%s", TT.destname, s+strlen(try->name));
348 free(s);
349 s = catch;
350 } else s = 0;
351 perror_msg(err, catch);
352 free(s);
353 }
354 return 0;
355}
356
357void cp_main(void)
358{
359 char *tt = *toys.which->name == 'i' ? TT.i.t : TT.c.t,
360 *destname = tt ? : toys.optargs[--toys.optc];
361 int i, destdir = !stat(destname, &TT.top);
362
363 if (!toys.optc) error_exit("Needs 2 arguments");
364 if (!destdir && errno==ENOENT && FLAG(D)) {
365 if (tt && mkpathat(AT_FDCWD, tt, 0777, MKPATHAT_MAKE|MKPATHAT_MKLAST))
366 perror_exit("-t '%s'", tt);
367 destdir = 1;
368 } else {
369 destdir = destdir && S_ISDIR(TT.top.st_mode);
370 if (!destdir && (toys.optc>1 || FLAG(D) || tt))
371 error_exit("'%s' not directory", destname);
372 }
373
374 if (FLAG(T)) {
375 if (toys.optc>1) help_exit("Max 2 arguments");
376 if (destdir) error_exit("'%s' is a directory", destname);
377 }
378
379 if (FLAG(a)||FLAG(p)) TT.pflags = _CP_mode|_CP_ownership|_CP_timestamps;
380
381
382 if (FLAG(preserve)) {
383 char *pre = xstrdup(TT.c.preserve ? TT.c.preserve : "mot"), *s;
384
385 if (comma_remove(pre, "all")) TT.pflags = ~0;
386 for (i=0; i<ARRAY_LEN(cp_preserve); i++)
387 while (comma_remove(pre, cp_preserve[i].name)) TT.pflags |= 1<<i;
388 if (*pre) {
389
390
391 for (s = pre; *s; s++) {
392 for (i=0; i<ARRAY_LEN(cp_preserve); i++)
393 if (*s == *cp_preserve[i].name) break;
394 if (i == ARRAY_LEN(cp_preserve)) {
395 if (*s == 'a') TT.pflags = ~0;
396 else break;
397 } else TT.pflags |= 1<<i;
398 }
399
400 if (*s) error_exit("bad --preserve=%s", pre);
401 }
402 free(pre);
403 }
404 if (TT.pflags & _CP_mode) umask(0);
405 if (!TT.callback) TT.callback = cp_node;
406
407
408 for (i=0; i<toys.optc; i++) {
409 char *src = toys.optargs[i], *trail;
410 int send = 1;
411
412 if (!(trail = strrchr(src, '/')) || trail[1]) trail = 0;
413 else while (trail>src && *trail=='/') *trail-- = 0;
414
415 if (destdir) {
416 char *s = FLAG(D) ? src : getbasename(src);
417
418 TT.destname = xmprintf("%s/%s", destname, s);
419 if (FLAG(D)) {
420 if (!(s = fileunderdir(TT.destname, destname))) {
421 error_msg("%s not under %s", TT.destname, destname);
422 continue;
423 }
424
425 free(s);
426 mkpath(TT.destname);
427 }
428 } else TT.destname = destname;
429
430
431 errno = EXDEV;
432 if (CFG_MV && toys.which->name[0] == 'm') {
433 int force = FLAG(f), no_clobber = FLAG(n);
434
435 if (!force || no_clobber) {
436 struct stat st;
437 int exists = !stat(TT.destname, &st);
438
439
440
441
442 if (exists && (FLAG(i) || (!(st.st_mode & 0222) && isatty(0)))) {
443 fprintf(stderr, "%s: overwrite '%s'", toys.which->name, TT.destname);
444 if (!yesno(0)) send = 0;
445 else unlink(TT.destname);
446 }
447
448 if (exists && no_clobber) send = 0;
449 }
450 if (send) send = rename(src, TT.destname);
451 if (trail) trail[1] = '/';
452 }
453
454
455 if (send) {
456 if (errno!=EXDEV || dirtree_flagread(src, DIRTREE_SHUTUP+
457 DIRTREE_SYMFOLLOW*!!(FLAG(H)||FLAG(L)), TT.callback))
458 perror_msg("bad '%s'", src);
459 }
460 if (destdir) free(TT.destname);
461 }
462}
463
464void mv_main(void)
465{
466 toys.optflags |= FLAG_d|FLAG_p|FLAG_r;
467
468 cp_main();
469}
470
471
472
473static inline int cp_flag_F(void) { return FLAG_F; };
474static inline int cp_flag_p(void) { return FLAG_p; };
475static inline int cp_flag_v(void) { return FLAG_v; };
476
477
478#define FOR_install
479#include <generated/flags.h>
480
481static int install_node(struct dirtree *try)
482{
483 try->st.st_mode = TT.i.m ? string_to_mode(TT.i.m, try->st.st_mode) : 0755;
484 if (TT.i.g) try->st.st_gid = TT.gid;
485 if (TT.i.o) try->st.st_uid = TT.uid;
486
487
488 cp_node(try);
489
490
491 if (FLAG(s) && xrun((char *[]){"strip", "-p", TT.destname, 0}))
492 toys.exitval = 1;
493
494 return 0;
495}
496
497void install_main(void)
498{
499 char **ss;
500
501 TT.uid = TT.i.o ? xgetuid(TT.i.o) : -1;
502 TT.gid = TT.i.g ? xgetgid(TT.i.g) : -1;
503
504 if (FLAG(d)) {
505 for (ss = toys.optargs; *ss; ss++) {
506 if (FLAG(v)) printf("%s\n", *ss);
507 if (mkpathat(AT_FDCWD, *ss, 0777, MKPATHAT_MKLAST | MKPATHAT_MAKE))
508 perror_msg_raw(*ss);
509 if (FLAG(g)||FLAG(o))
510 if (lchown(*ss, TT.uid, TT.gid)) perror_msg("chown '%s'", *ss);
511 }
512
513 return;
514 }
515
516 if (FLAG(D)) {
517 char *destname = FLAG(t) ? TT.i.t : (TT.destname = toys.optargs[toys.optc-1]);
518 if (mkpathat(AT_FDCWD, destname, 0777, MKPATHAT_MAKE | (FLAG(t) ? MKPATHAT_MKLAST : 0)))
519 perror_exit("-D '%s'", destname);
520 if (toys.optc == !FLAG(t)) return;
521 }
522
523
524 toys.optflags = cp_flag_F() + cp_flag_v()*!!FLAG(v)
525 + cp_flag_p()*!!(FLAG(p)|FLAG(o)|FLAG(g));
526
527 TT.callback = install_node;
528 cp_main();
529}
530