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