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