1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 04/01/94";
10 #endif /* not lint */
11
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 #include <sys/mman.h>
15 #include <sys/time.h>
16
17 #include <err.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <fts.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "extern.h"
27
28 int
copy_file(entp,dne)29 copy_file(entp, dne)
30 FTSENT *entp;
31 int dne;
32 {
33 static char buf[MAXBSIZE];
34 struct stat to_stat, *fs;
35 int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
36 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
37 char *p;
38 #endif
39
40 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
41 warn("%s", entp->fts_path);
42 return (1);
43 }
44
45 fs = entp->fts_statp;
46
47 /*
48 * If the file exists and we're interactive, verify with the user.
49 * If the file DNE, set the mode to be the from file, minus setuid
50 * bits, modified by the umask; arguably wrong, but it makes copying
51 * executables work right and it's been that way forever. (The
52 * other choice is 666 or'ed with the execute bits on the from file
53 * modified by the umask.)
54 */
55 if (!dne) {
56 if (iflag) {
57 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
58 checkch = ch = getchar();
59 while (ch != '\n' && ch != EOF)
60 ch = getchar();
61 if (checkch != 'y') {
62 (void)close(from_fd);
63 return (0);
64 }
65 }
66 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
67 } else
68 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
69 fs->st_mode & ~(S_ISUID | S_ISGID));
70
71 if (to_fd == -1) {
72 warn("%s", to.p_path);
73 (void)close(from_fd);
74 return (1);;
75 }
76
77 rval = 0;
78
79 /*
80 * Mmap and write if less than 8M (the limit is so we don't totally
81 * trash memory on big files. This is really a minor hack, but it
82 * wins some CPU back.
83 */
84 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
85 if (fs->st_size <= 8 * 1048576) {
86 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
87 0, from_fd, (off_t)0)) == (char *)-1) {
88 warn("%s", entp->fts_path);
89 rval = 1;
90 } else {
91 if (write(to_fd, p, fs->st_size) != fs->st_size) {
92 warn("%s", to.p_path);
93 rval = 1;
94 }
95 /* Some systems don't unmap on close(2). */
96 if (munmap(p, fs->st_size) < 0) {
97 warn("%s", entp->fts_path);
98 rval = 1;
99 }
100 }
101 } else
102 #endif
103 {
104 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
105 wcount = write(to_fd, buf, rcount);
106 if (rcount != wcount || wcount == -1) {
107 warn("%s", to.p_path);
108 rval = 1;
109 break;
110 }
111 }
112 if (rcount < 0) {
113 warn("%s", entp->fts_path);
114 rval = 1;
115 }
116 }
117
118 /* If the copy went bad, lose the file. */
119 if (rval == 1) {
120 (void)unlink(to.p_path);
121 (void)close(from_fd);
122 (void)close(to_fd);
123 return (1);
124 }
125
126 if (pflag && setfile(fs, to_fd))
127 rval = 1;
128 /*
129 * If the source was setuid or setgid, lose the bits unless the
130 * copy is owned by the same user and group.
131 */
132 #define RETAINBITS \
133 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
134 else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid)
135 if (fstat(to_fd, &to_stat)) {
136 warn("%s", to.p_path);
137 rval = 1;
138 } else if (fs->st_gid == to_stat.st_gid &&
139 fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
140 warn("%s", to.p_path);
141 rval = 1;
142 }
143 (void)close(from_fd);
144 if (close(to_fd)) {
145 warn("%s", to.p_path);
146 rval = 1;
147 }
148 return (rval);
149 }
150
151 int
copy_link(p,exists)152 copy_link(p, exists)
153 FTSENT *p;
154 int exists;
155 {
156 int len;
157 char link[MAXPATHLEN];
158
159 if ((len = readlink(p->fts_path, link, sizeof(link))) == -1) {
160 warn("readlink: %s", p->fts_path);
161 return (1);
162 }
163 link[len] = '\0';
164 if (exists && unlink(to.p_path)) {
165 warn("unlink: %s", to.p_path);
166 return (1);
167 }
168 if (symlink(link, to.p_path)) {
169 warn("symlink: %s", link);
170 return (1);
171 }
172 return (0);
173 }
174
175 int
copy_fifo(from_stat,exists)176 copy_fifo(from_stat, exists)
177 struct stat *from_stat;
178 int exists;
179 {
180 if (exists && unlink(to.p_path)) {
181 warn("unlink: %s", to.p_path);
182 return (1);
183 }
184 if (mkfifo(to.p_path, from_stat->st_mode)) {
185 warn("mkfifo: %s", to.p_path);
186 return (1);
187 }
188 return (pflag ? setfile(from_stat, 0) : 0);
189 }
190
191 int
copy_special(from_stat,exists)192 copy_special(from_stat, exists)
193 struct stat *from_stat;
194 int exists;
195 {
196 if (exists && unlink(to.p_path)) {
197 warn("unlink: %s", to.p_path);
198 return (1);
199 }
200 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
201 warn("mknod: %s", to.p_path);
202 return (1);
203 }
204 return (pflag ? setfile(from_stat, 0) : 0);
205 }
206
207
208 int
setfile(fs,fd)209 setfile(fs, fd)
210 register struct stat *fs;
211 int fd;
212 {
213 static struct timeval tv[2];
214 int rval;
215
216 rval = 0;
217 fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
218
219 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
220 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
221 if (utimes(to.p_path, tv)) {
222 warn("utimes: %s", to.p_path);
223 rval = 1;
224 }
225 /*
226 * Changing the ownership probably won't succeed, unless we're root
227 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
228 * the mode; current BSD behavior is to remove all setuid bits on
229 * chown. If chown fails, lose setuid/setgid bits.
230 */
231 if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
232 chown(to.p_path, fs->st_uid, fs->st_gid)) {
233 if (errno != EPERM) {
234 warn("chown: %s", to.p_path);
235 rval = 1;
236 }
237 fs->st_mode &= ~(S_ISUID | S_ISGID);
238 }
239 if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
240 warn("chown: %s", to.p_path);
241 rval = 1;
242 }
243
244 if (fd ?
245 fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
246 warn("chflags: %s", to.p_path);
247 rval = 1;
248 }
249 return (rval);
250 }
251
252 void
usage()253 usage()
254 {
255 (void)fprintf(stderr, "%s\n%s\n",
256 "usage: cp [-R [-H | -L | -P] [-fip] src target",
257 " cp [-R [-H | -L | -P] [-fip] src1 ... srcN directory");
258 exit(1);
259 }
260