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