Mercurial > hg > toybox
comparison toys/cp.c @ 263:7c53152a483b
Make cp pass most of its test suite. Still need to add symlink support.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 21 Feb 2008 04:44:42 -0600 |
parents | 70f36d9c5387 |
children | 784bc9b0d6df |
comparison
equal
deleted
inserted
replaced
262:70f36d9c5387 | 263:7c53152a483b |
---|---|
5 * Copyright 2008 Rob Landley <rob@landley.net> | 5 * Copyright 2008 Rob Landley <rob@landley.net> |
6 * | 6 * |
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/cp.html | 7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/cp.html |
8 * | 8 * |
9 * "R+ra+d+p+r" | 9 * "R+ra+d+p+r" |
10 USE_HELLO(NEWTOY(hello, "<2rR+rdpa+d+p+rHLPif", TOYFLAG_BIN|TOYFLAG_UMASK)) | 10 USE_CP(NEWTOY(cp, "<2rR+rdpa+d+p+rHLPif", TOYFLAG_BIN)) |
11 | 11 |
12 config CP | 12 config CP |
13 bool "cp" | 13 bool "cp" |
14 default n | 14 default n |
15 help | 15 help |
38 #define FLAG_r 512 | 38 #define FLAG_r 512 |
39 | 39 |
40 DEFINE_GLOBALS( | 40 DEFINE_GLOBALS( |
41 char *destname; | 41 char *destname; |
42 int destisdir; | 42 int destisdir; |
43 int destisnew; | |
43 ) | 44 ) |
44 | 45 |
45 #define TT this.cp | 46 #define TT this.cp |
46 | 47 |
47 // Copy an individual file or directory to target. | 48 // Copy an individual file or directory to target. |
48 | 49 |
49 void cp_file(char *src, struct stat *srcst, int topdir, int again) | 50 void cp_file(char *src, struct stat *srcst, int topdir) |
50 { | 51 { |
51 char *s = NULL; | 52 char *s = NULL; |
52 int mode = (toys.optflags & FLAG_p) ? 0700 : 0777; | 53 int fdout; |
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 | 54 |
71 // Trim path from name if necessary. | 55 // Trim path from name if necessary. |
56 | |
57 | |
72 if (topdir) s = strrchr(src, '/'); | 58 if (topdir) s = strrchr(src, '/'); |
73 if (!s) s=src; | 59 if (!s) s=src; |
74 | 60 |
75 // Determine location to create new file/directory at. | 61 // Determine location to create new file/directory at. |
76 if (TT.destisdir) s = xmsprintf(toybuf, "%s/%s", TT.destname, s); | 62 |
63 if (TT.destisdir || !topdir) s = xmsprintf("%s/%s", TT.destname, s); | |
77 else s = xstrdup(TT.destname); | 64 else s = xstrdup(TT.destname); |
78 | 65 |
79 // Copy directory or file to destination. | 66 // Copy directory or file to destination. |
67 | |
80 if (S_ISDIR(srcst->st_mode)) { | 68 if (S_ISDIR(srcst->st_mode)) { |
81 if (mkdir(s, mode)) perror_exit("mkdir '%s'", s); | 69 struct stat st2; |
70 | |
71 // Always make directory writeable to us, so we can create files in it. | |
72 // | |
73 // Yes, there's a race window between mkdir() and open() so it's | |
74 // possible that -p can be made to chown a directory other than the one | |
75 // we created. The closest we can do to closing this is make sure | |
76 // that what we open _is_ a directory rather than something else. | |
77 | |
78 if (mkdir(s, srcst->st_mode | 0200) || 0>(fdout=open(s, 0)) | |
79 || fstat(fdout, &st2) || !S_ISDIR(st2.st_mode)) | |
80 { | |
81 perror_exit("mkdir '%s'", s); | |
82 } | |
82 } else { | 83 } else { |
83 int fdin, fdout; | 84 int fdin, i; |
85 | |
84 fdin = xopen(src, O_RDONLY); | 86 fdin = xopen(src, O_RDONLY); |
85 fdout = xcreate(s, O_CREAT|O_TRUNC, mode); | 87 for (i=2 ; i; i--) { |
88 fdout = open(s, O_RDWR|O_CREAT|O_TRUNC, srcst->st_mode); | |
89 if (fdout>=0 || !(toys.optflags & FLAG_f)) break; | |
90 unlink(s); | |
91 } | |
92 if (fdout<0) perror_exit("%s", s); | |
86 xsendfile(fdin, fdout); | 93 xsendfile(fdin, fdout); |
87 close(fdin); | 94 close(fdin); |
88 xclose(fdout); | |
89 } | 95 } |
96 | |
97 // Inability to set these isn't fatal, some require root access. | |
98 // Can't do fchmod() etc here because -p works on mkdir, too. | |
99 | |
100 if (toys.optflags & FLAG_p) { | |
101 int mask = umask(0); | |
102 struct utimbuf ut; | |
103 | |
104 fchown(fdout,srcst->st_uid, srcst->st_gid); | |
105 ut.actime = srcst->st_atime; | |
106 ut.modtime = srcst->st_mtime; | |
107 utime(s, &ut); | |
108 umask(mask); | |
109 } | |
110 xclose(fdout); | |
111 free(s); | |
90 } | 112 } |
91 | 113 |
92 // Callback from dirtree_read() for each file/directory under a source dir. | 114 // Callback from dirtree_read() for each file/directory under a source dir. |
93 | 115 |
94 int cp_node(struct dirtree *node, int after) | 116 int cp_node(char *path, struct dirtree *node) |
95 { | 117 { |
96 cp_file(node->name, &(node->st), 0, after); | 118 char *s = path+strlen(path); |
119 struct dirtree *n = node; | |
120 | |
121 // Find appropriate chunk of path for destination. | |
122 | |
123 for (;;) { | |
124 if (*(--s) == '/') { | |
125 if (!n->parent) break; | |
126 n = n->parent; | |
127 } | |
128 } | |
129 s++; | |
130 | |
131 cp_file(s, &(node->st), 0); | |
97 return 0; | 132 return 0; |
98 } | 133 } |
99 | 134 |
100 void cp_main(void) | 135 void cp_main(void) |
101 { | 136 { |
105 // Grab target argument. (Guaranteed to be there due to "<2" above.) | 140 // Grab target argument. (Guaranteed to be there due to "<2" above.) |
106 | 141 |
107 TT.destname = toys.optargs[--toys.optc]; | 142 TT.destname = toys.optargs[--toys.optc]; |
108 | 143 |
109 // If destination doesn't exist, are we ok with that? | 144 // If destination doesn't exist, are we ok with that? |
145 | |
110 if (stat(TT.destname, &st)) { | 146 if (stat(TT.destname, &st)) { |
111 if (toys.optc>1) goto error_notdir; | 147 if (toys.optc>1) goto error_notdir; |
148 TT.destisnew++; | |
112 | 149 |
113 // If destination exists... | 150 // If destination exists... |
151 | |
114 } else { | 152 } else { |
115 if (S_ISDIR(st.st_mode)) TT.destisdir++; | 153 if (S_ISDIR(st.st_mode)) TT.destisdir++; |
116 else if (toys.optc > 1) goto error_notdir; | 154 else if (toys.optc > 1) goto error_notdir; |
117 } | 155 } |
118 | 156 |
119 // Handle sources | 157 // Handle sources |
158 | |
120 for (i=0; i<toys.optc; i++) { | 159 for (i=0; i<toys.optc; i++) { |
121 char *src = toys.optargs[i]; | 160 char *src = toys.optargs[i]; |
122 | 161 |
123 // Skip nonexistent sources... | 162 // Skip nonexistent sources, or src==dest. |
124 if (!((toys.optflags & FLAG_d) ? lstat(src, &st) : stat(src, &st))) { | 163 |
164 if (!strcmp(src, TT.destname)) continue; | |
165 if ((toys.optflags & FLAG_d) ? lstat(src, &st) : stat(src, &st)) | |
166 { | |
125 perror_msg("'%s'", src); | 167 perror_msg("'%s'", src); |
126 toys.exitval = 1; | 168 toys.exitval = 1; |
127 continue; | 169 continue; |
128 } | 170 } |
129 | 171 |
130 // Copy directory or file. | 172 // Copy directory or file. |
173 | |
131 if (S_ISDIR(st.st_mode)) { | 174 if (S_ISDIR(st.st_mode)) { |
132 if (toys.optflags & FLAG_r) { | 175 if (toys.optflags & FLAG_r) { |
133 cp_file(src, &st, 1, 0); | 176 cp_file(src, &st, 1); |
134 dirtree_read(src, NULL, cp_node); | 177 strncpy(toybuf, src, sizeof(toybuf)-1); |
135 cp_file(src, &st, 1, 1); | 178 toybuf[sizeof(toybuf)-1]=0; |
179 dirtree_read(toybuf, NULL, cp_node); | |
136 } else error_msg("Skipped dir '%s'", src); | 180 } else error_msg("Skipped dir '%s'", src); |
137 } else { | 181 } else cp_file(src, &st, 1); |
138 cp_file(src, &st, 1, 0); | |
139 cp_file(src, &st, 1, 1); | |
140 } | |
141 } | 182 } |
183 | |
142 return; | 184 return; |
143 | 185 |
144 error_notdir: | 186 error_notdir: |
145 error_exit("'%s' isn't a directory", TT.destname); | 187 error_exit("'%s' isn't a directory", TT.destname); |
146 } | 188 } |