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