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 * @(#)utils.c 8.3 (Berkeley) 4/1/94 34 * $FreeBSD: src/bin/cp/utils.c,v 1.27.2.6 2002/08/10 13:20:19 johan Exp $ 35 * $DragonFly: src/bin/cp/utils.c,v 1.7 2004/10/22 22:34:10 dillon Exp $ 36 */ 37 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 #include <sys/time.h> 41 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 42 #include <sys/mman.h> 43 #endif 44 45 #include <err.h> 46 #include <errno.h> 47 #include <fcntl.h> 48 #include <fts.h> 49 #include <limits.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <sysexits.h> 53 #include <unistd.h> 54 55 #include "extern.h" 56 57 #define YESNO "(y/n [n]) " 58 59 int 60 copy_file(FTSENT *entp, int dne) 61 { 62 static char buf[MAXBSIZE]; 63 struct stat *fs; 64 int ch, checkch, from_fd, rcount, rval, to_fd, wcount, wresid; 65 char *bufp; 66 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 67 char *p; 68 #endif 69 70 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) { 71 warn("%s", entp->fts_path); 72 return (1); 73 } 74 75 fs = entp->fts_statp; 76 77 /* 78 * If the file exists and we're interactive, verify with the user. 79 * If the file DNE, set the mode to be the from file, minus setuid 80 * bits, modified by the umask; arguably wrong, but it makes copying 81 * executables work right and it's been that way forever. (The 82 * other choice is 666 or'ed with the execute bits on the from file 83 * modified by the umask.) 84 */ 85 if (!dne) { 86 if (nflag) { 87 if (vflag) 88 printf("%s not overwritten\n", to.p_path); 89 return (0); 90 } else if (iflag) { 91 fprintf(stderr, "overwrite %s? %s", 92 to.p_path, YESNO); 93 checkch = ch = getchar(); 94 while (ch != '\n' && ch != EOF) 95 ch = getchar(); 96 if (checkch != 'y' && checkch != 'Y') { 97 close(from_fd); 98 fprintf(stderr, "not overwritten\n"); 99 return (1); 100 } 101 } 102 103 if (fflag) { 104 /* remove existing destination file name, 105 * create a new file */ 106 unlink(to.p_path); 107 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, 108 fs->st_mode & ~(S_ISUID | S_ISGID)); 109 } else 110 /* overwrite existing destination file name */ 111 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0); 112 } else 113 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, 114 fs->st_mode & ~(S_ISUID | S_ISGID)); 115 116 if (to_fd == -1) { 117 warn("%s", to.p_path); 118 close(from_fd); 119 return (1);; 120 } 121 122 rval = 0; 123 124 /* 125 * Mmap and write if less than 8M (the limit is so we don't totally 126 * trash memory on big files. This is really a minor hack, but it 127 * wins some CPU back. 128 */ 129 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 130 if (S_ISREG(fs->st_mode) && fs->st_size <= 8 * 1048576) { 131 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ, 132 MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) { 133 warn("%s", entp->fts_path); 134 rval = 1; 135 } else { 136 for (bufp = p, wresid = fs->st_size; ; 137 bufp += wcount, wresid -= wcount) { 138 wcount = write(to_fd, bufp, wresid); 139 if (wcount >= wresid || wcount <= 0) 140 break; 141 } 142 if (wcount != wresid) { 143 warn("%s", to.p_path); 144 rval = 1; 145 } 146 /* Some systems don't unmap on close(2). */ 147 if (munmap(p, fs->st_size) < 0) { 148 warn("%s", entp->fts_path); 149 rval = 1; 150 } 151 } 152 } else 153 #endif 154 { 155 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { 156 for (bufp = buf, wresid = rcount; ; 157 bufp += wcount, wresid -= wcount) { 158 wcount = write(to_fd, bufp, wresid); 159 if (wcount >= wresid || wcount <= 0) 160 break; 161 } 162 if (wcount != wresid) { 163 warn("%s", to.p_path); 164 rval = 1; 165 break; 166 } 167 } 168 if (rcount < 0) { 169 warn("%s", entp->fts_path); 170 rval = 1; 171 } 172 } 173 174 /* 175 * Don't remove the target even after an error. The target might 176 * not be a regular file, or its attributes might be important, 177 * or its contents might be irreplaceable. It would only be safe 178 * to remove it if we created it and its length is 0. 179 */ 180 181 if (pflag && setfile(fs, to_fd)) 182 rval = 1; 183 close(from_fd); 184 if (close(to_fd)) { 185 warn("%s", to.p_path); 186 rval = 1; 187 } 188 return (rval); 189 } 190 191 int 192 copy_link(FTSENT *p, int exists) 193 { 194 int len; 195 char linkname[PATH_MAX]; 196 197 if ((len = readlink(p->fts_path, linkname, sizeof(linkname) - 1)) == -1) { 198 warn("readlink: %s", p->fts_path); 199 return (1); 200 } 201 linkname[len] = '\0'; 202 if (exists && unlink(to.p_path)) { 203 warn("unlink: %s", to.p_path); 204 return (1); 205 } 206 if (symlink(linkname, to.p_path)) { 207 warn("symlink: %s", linkname); 208 return (1); 209 } 210 return (pflag ? setfile(p->fts_statp, -1) : 0); 211 } 212 213 int 214 copy_fifo(struct stat *from_stat, int exists) 215 { 216 if (exists && unlink(to.p_path)) { 217 warn("unlink: %s", to.p_path); 218 return (1); 219 } 220 if (mkfifo(to.p_path, from_stat->st_mode)) { 221 warn("mkfifo: %s", to.p_path); 222 return (1); 223 } 224 return (pflag ? setfile(from_stat, -1) : 0); 225 } 226 227 int 228 copy_special(struct stat *from_stat, int exists) 229 { 230 if (exists && unlink(to.p_path)) { 231 warn("unlink: %s", to.p_path); 232 return (1); 233 } 234 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) { 235 warn("mknod: %s", to.p_path); 236 return (1); 237 } 238 return (pflag ? setfile(from_stat, -1) : 0); 239 } 240 241 int 242 setfile(struct stat *fs, int fd) 243 { 244 static struct timeval tv[2]; 245 struct stat ts; 246 int rval, gotstat, islink, fdval; 247 248 rval = 0; 249 fdval = fd != -1; 250 islink = !fdval && S_ISLNK(fs->st_mode); 251 fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX | 252 S_IRWXU | S_IRWXG | S_IRWXO; 253 254 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec); 255 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec); 256 if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) { 257 warn("%sutimes: %s", islink ? "l" : "", to.p_path); 258 rval = 1; 259 } 260 if (fdval ? fstat(fd, &ts) : 261 (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts))) { 262 gotstat = 0; 263 } else { 264 gotstat = 1; 265 ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX | 266 S_IRWXU | S_IRWXG | S_IRWXO; 267 } 268 /* 269 * Changing the ownership probably won't succeed, unless we're root 270 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting 271 * the mode; current BSD behavior is to remove all setuid bits on 272 * chown. If chown fails, lose setuid/setgid bits. 273 */ 274 if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid) 275 if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) : 276 (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) : 277 chown(to.p_path, fs->st_uid, fs->st_gid))) { 278 if (errno != EPERM) { 279 warn("chown: %s", to.p_path); 280 rval = 1; 281 } 282 fs->st_mode &= ~(S_ISUID | S_ISGID); 283 } 284 285 if (!gotstat || fs->st_mode != ts.st_mode) 286 if (fdval ? fchmod(fd, fs->st_mode) : 287 (islink ? lchmod(to.p_path, fs->st_mode) : 288 chmod(to.p_path, fs->st_mode))) { 289 warn("chmod: %s", to.p_path); 290 rval = 1; 291 } 292 293 if (!gotstat || fs->st_flags != ts.st_flags) 294 if (fdval ? 295 fchflags(fd, fs->st_flags) : 296 (islink ? (errno = ENOSYS) : 297 chflags(to.p_path, fs->st_flags))) { 298 warn("chflags: %s", to.p_path); 299 rval = 1; 300 } 301 302 return (rval); 303 } 304 305 void 306 usage(void) 307 { 308 309 fprintf(stderr, "%s\n%s\n", 310 "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src target", 311 " cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src1 ... srcN directory"); 312 exit(EX_USAGE); 313 } 314