Mercurial > hg > toybox
comparison toys/cp.c @ 262:70f36d9c5387
Add first pass at cp, totally untested, unlikely to work yet. :)
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 20 Feb 2008 01:47:56 -0600 |
parents | |
children | 7c53152a483b |
comparison
equal
deleted
inserted
replaced
261:ae693c7bf2f7 | 262:70f36d9c5387 |
---|---|
1 /* vi: set sw=4 ts=4: | |
2 * | |
3 * cp.c - Copy files. | |
4 * | |
5 * Copyright 2008 Rob Landley <rob@landley.net> | |
6 * | |
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/cp.html | |
8 * | |
9 * "R+ra+d+p+r" | |
10 USE_HELLO(NEWTOY(hello, "<2rR+rdpa+d+p+rHLPif", TOYFLAG_BIN|TOYFLAG_UMASK)) | |
11 | |
12 config CP | |
13 bool "cp" | |
14 default n | |
15 help | |
16 usage: cp -f SOURCE... DEST | |
17 | |
18 Copy files from SOURCE to DEST. If more than one SOURCE, DEST must | |
19 be a directory. | |
20 | |
21 -f force copy by deleting destination file | |
22 -i interactive, prompt before overwriting existing DEST | |
23 -p preserve timestamps, ownership, and permissions | |
24 -r recurse into subdirectories (DEST must be a directory) | |
25 */ | |
26 | |
27 #include "toys.h" | |
28 | |
29 #define FLAG_f 1 | |
30 #define FLAG_i 2 | |
31 #define FLAG_P 4 | |
32 #define FLAG_L 8 | |
33 #define FLAG_H 16 | |
34 #define FLAG_a 32 | |
35 #define FLAG_p 64 | |
36 #define FLAG_d 128 | |
37 #define FLAG_R 256 | |
38 #define FLAG_r 512 | |
39 | |
40 DEFINE_GLOBALS( | |
41 char *destname; | |
42 int destisdir; | |
43 ) | |
44 | |
45 #define TT this.cp | |
46 | |
47 // Copy an individual file or directory to target. | |
48 | |
49 void cp_file(char *src, struct stat *srcst, int topdir, int again) | |
50 { | |
51 char *s = NULL; | |
52 int mode = (toys.optflags & FLAG_p) ? 0700 : 0777; | |
53 | |
54 // The second time we're called, chmod data. We can't do this on | |
55 // the first pass because we may copy files into a read-only directory. | |
56 if (again) { | |
57 if (toys.optflags & FLAG_p) { | |
58 struct utimbuf ut; | |
59 | |
60 // Inability to set these isn't fatal, some require root access. | |
61 // Can't do fchmod() etc here because -p works on mkdir, too. | |
62 chown(s, srcst->st_uid, srcst->st_gid); | |
63 chmod(s, srcst->st_mode); | |
64 ut.actime = srcst->st_atime; | |
65 ut.modtime = srcst->st_mtime; | |
66 utime(s, &ut); | |
67 } | |
68 return; | |
69 } | |
70 | |
71 // Trim path from name if necessary. | |
72 if (topdir) s = strrchr(src, '/'); | |
73 if (!s) s=src; | |
74 | |
75 // Determine location to create new file/directory at. | |
76 if (TT.destisdir) s = xmsprintf(toybuf, "%s/%s", TT.destname, s); | |
77 else s = xstrdup(TT.destname); | |
78 | |
79 // Copy directory or file to destination. | |
80 if (S_ISDIR(srcst->st_mode)) { | |
81 if (mkdir(s, mode)) perror_exit("mkdir '%s'", s); | |
82 } else { | |
83 int fdin, fdout; | |
84 fdin = xopen(src, O_RDONLY); | |
85 fdout = xcreate(s, O_CREAT|O_TRUNC, mode); | |
86 xsendfile(fdin, fdout); | |
87 close(fdin); | |
88 xclose(fdout); | |
89 } | |
90 } | |
91 | |
92 // Callback from dirtree_read() for each file/directory under a source dir. | |
93 | |
94 int cp_node(struct dirtree *node, int after) | |
95 { | |
96 cp_file(node->name, &(node->st), 0, after); | |
97 return 0; | |
98 } | |
99 | |
100 void cp_main(void) | |
101 { | |
102 struct stat st; | |
103 int i; | |
104 | |
105 // Grab target argument. (Guaranteed to be there due to "<2" above.) | |
106 | |
107 TT.destname = toys.optargs[--toys.optc]; | |
108 | |
109 // If destination doesn't exist, are we ok with that? | |
110 if (stat(TT.destname, &st)) { | |
111 if (toys.optc>1) goto error_notdir; | |
112 | |
113 // If destination exists... | |
114 } else { | |
115 if (S_ISDIR(st.st_mode)) TT.destisdir++; | |
116 else if (toys.optc > 1) goto error_notdir; | |
117 } | |
118 | |
119 // Handle sources | |
120 for (i=0; i<toys.optc; i++) { | |
121 char *src = toys.optargs[i]; | |
122 | |
123 // Skip nonexistent sources... | |
124 if (!((toys.optflags & FLAG_d) ? lstat(src, &st) : stat(src, &st))) { | |
125 perror_msg("'%s'", src); | |
126 toys.exitval = 1; | |
127 continue; | |
128 } | |
129 | |
130 // Copy directory or file. | |
131 if (S_ISDIR(st.st_mode)) { | |
132 if (toys.optflags & FLAG_r) { | |
133 cp_file(src, &st, 1, 0); | |
134 dirtree_read(src, NULL, cp_node); | |
135 cp_file(src, &st, 1, 1); | |
136 } else error_msg("Skipped dir '%s'", src); | |
137 } else { | |
138 cp_file(src, &st, 1, 0); | |
139 cp_file(src, &st, 1, 1); | |
140 } | |
141 } | |
142 return; | |
143 | |
144 error_notdir: | |
145 error_exit("'%s' isn't a directory", TT.destname); | |
146 } |