utils.c revision 1.1 1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/mman.h>
41 #include <sys/time.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <fts.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "extern.h"
53
54 int
55 copy_file(entp, dne)
56 FTSENT *entp;
57 int dne;
58 {
59 static char buf[MAXBSIZE];
60 struct stat to_stat, *fs;
61 int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
62 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
63 char *p;
64 #endif
65
66 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
67 warn("%s", entp->fts_path);
68 return (1);
69 }
70
71 fs = entp->fts_statp;
72
73 /*
74 * If the file exists and we're interactive, verify with the user.
75 * If the file DNE, set the mode to be the from file, minus setuid
76 * bits, modified by the umask; arguably wrong, but it makes copying
77 * executables work right and it's been that way forever. (The
78 * other choice is 666 or'ed with the execute bits on the from file
79 * modified by the umask.)
80 */
81 if (!dne) {
82 if (iflag) {
83 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
84 checkch = ch = getchar();
85 while (ch != '\n' && ch != EOF)
86 ch = getchar();
87 if (checkch != 'y') {
88 (void)close(from_fd);
89 return (0);
90 }
91 }
92 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
93 } else
94 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
95 fs->st_mode & ~(S_ISUID | S_ISGID));
96
97 if (to_fd == -1) {
98 warn("%s", to.p_path);
99 (void)close(from_fd);
100 return (1);;
101 }
102
103 rval = 0;
104
105 /*
106 * Mmap and write if less than 8M (the limit is so we don't totally
107 * trash memory on big files. This is really a minor hack, but it
108 * wins some CPU back.
109 */
110 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
111 if (fs->st_size <= 8 * 1048576) {
112 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
113 0, from_fd, (off_t)0)) == (char *)-1) {
114 warn("%s", entp->fts_path);
115 rval = 1;
116 } else {
117 if (write(to_fd, p, fs->st_size) != fs->st_size) {
118 warn("%s", to.p_path);
119 rval = 1;
120 }
121 /* Some systems don't unmap on close(2). */
122 if (munmap(p, fs->st_size) < 0) {
123 warn("%s", entp->fts_path);
124 rval = 1;
125 }
126 }
127 } else
128 #endif
129 {
130 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
131 wcount = write(to_fd, buf, rcount);
132 if (rcount != wcount || wcount == -1) {
133 warn("%s", to.p_path);
134 rval = 1;
135 break;
136 }
137 }
138 if (rcount < 0) {
139 warn("%s", entp->fts_path);
140 rval = 1;
141 }
142 }
143
144 /* If the copy went bad, lose the file. */
145 if (rval == 1) {
146 (void)unlink(to.p_path);
147 (void)close(from_fd);
148 (void)close(to_fd);
149 return (1);
150 }
151
152 if (pflag && setfile(fs, to_fd))
153 rval = 1;
154 /*
155 * If the source was setuid or setgid, lose the bits unless the
156 * copy is owned by the same user and group.
157 */
158 #define RETAINBITS \
159 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
160 else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid)
161 if (fstat(to_fd, &to_stat)) {
162 warn("%s", to.p_path);
163 rval = 1;
164 } else if (fs->st_gid == to_stat.st_gid &&
165 fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
166 warn("%s", to.p_path);
167 rval = 1;
168 }
169 (void)close(from_fd);
170 if (close(to_fd)) {
171 warn("%s", to.p_path);
172 rval = 1;
173 }
174 return (rval);
175 }
176
177 int
178 copy_link(p, exists)
179 FTSENT *p;
180 int exists;
181 {
182 int len;
183 char link[MAXPATHLEN];
184
185 if ((len = readlink(p->fts_path, link, sizeof(link))) == -1) {
186 warn("readlink: %s", p->fts_path);
187 return (1);
188 }
189 link[len] = '\0';
190 if (exists && unlink(to.p_path)) {
191 warn("unlink: %s", to.p_path);
192 return (1);
193 }
194 if (symlink(link, to.p_path)) {
195 warn("symlink: %s", link);
196 return (1);
197 }
198 return (0);
199 }
200
201 int
202 copy_fifo(from_stat, exists)
203 struct stat *from_stat;
204 int exists;
205 {
206 if (exists && unlink(to.p_path)) {
207 warn("unlink: %s", to.p_path);
208 return (1);
209 }
210 if (mkfifo(to.p_path, from_stat->st_mode)) {
211 warn("mkfifo: %s", to.p_path);
212 return (1);
213 }
214 return (pflag ? setfile(from_stat, 0) : 0);
215 }
216
217 int
218 copy_special(from_stat, exists)
219 struct stat *from_stat;
220 int exists;
221 {
222 if (exists && unlink(to.p_path)) {
223 warn("unlink: %s", to.p_path);
224 return (1);
225 }
226 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
227 warn("mknod: %s", to.p_path);
228 return (1);
229 }
230 return (pflag ? setfile(from_stat, 0) : 0);
231 }
232
233
234 int
235 setfile(fs, fd)
236 register struct stat *fs;
237 int fd;
238 {
239 static struct timeval tv[2];
240 int rval;
241
242 rval = 0;
243 fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
244
245 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
246 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
247 if (utimes(to.p_path, tv)) {
248 warn("utimes: %s", to.p_path);
249 rval = 1;
250 }
251 /*
252 * Changing the ownership probably won't succeed, unless we're root
253 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
254 * the mode; current BSD behavior is to remove all setuid bits on
255 * chown. If chown fails, lose setuid/setgid bits.
256 */
257 if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
258 chown(to.p_path, fs->st_uid, fs->st_gid)) {
259 if (errno != EPERM) {
260 warn("chown: %s", to.p_path);
261 rval = 1;
262 }
263 fs->st_mode &= ~(S_ISUID | S_ISGID);
264 }
265 if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
266 warn("chown: %s", to.p_path);
267 rval = 1;
268 }
269
270 if (fd ?
271 fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
272 warn("chflags: %s", to.p_path);
273 rval = 1;
274 }
275 return (rval);
276 }
277
278 void
279 usage()
280 {
281 (void)fprintf(stderr, "%s\n%s\n",
282 "usage: cp [-R [-H | -L | -P] [-fip] src target",
283 " cp [-R [-H | -L | -P] [-fip] src1 ... srcN directory");
284 exit(1);
285 }
286