Home | History | Annotate | Line # | Download | only in cp
utils.c revision 1.19
      1 /*	$NetBSD: utils.c,v 1.19 2001/08/30 04:45:56 chs 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.19 2001/08/30 04:45:56 chs Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <sys/mman.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(file, fs)
     63 	const char * file;
     64 	struct stat * fs;
     65 {
     66     static struct timeval tv[2];
     67 
     68     TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
     69     TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
     70 
     71     if (lutimes(file, tv)) {
     72 	warn("lutimes: %s", file);
     73 	return (1);
     74     }
     75     return (0);
     76 }
     77 
     78 int
     79 copy_file(entp, dne)
     80 	FTSENT *entp;
     81 	int dne;
     82 {
     83 	static char buf[MAXBSIZE];
     84 	struct stat to_stat, *fs;
     85 	int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
     86 	char *p;
     87 
     88 	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
     89 		warn("%s", entp->fts_path);
     90 		return (1);
     91 	}
     92 
     93 	fs = entp->fts_statp;
     94 
     95 	/*
     96 	 * If the file exists and we're interactive, verify with the user.
     97 	 * If the file DNE, set the mode to be the from file, minus setuid
     98 	 * bits, modified by the umask; arguably wrong, but it makes copying
     99 	 * executables work right and it's been that way forever.  (The
    100 	 * other choice is 666 or'ed with the execute bits on the from file
    101 	 * modified by the umask.)
    102 	 */
    103 	if (!dne) {
    104 		if (iflag) {
    105 			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
    106 			checkch = ch = getchar();
    107 			while (ch != '\n' && ch != EOF)
    108 				ch = getchar();
    109 			if (checkch != 'y' && checkch != 'Y') {
    110 				(void)close(from_fd);
    111 				return (0);
    112 			}
    113 		}
    114 		/* overwrite existing destination file name */
    115 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
    116 	} else
    117 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
    118 		    fs->st_mode & ~(S_ISUID | S_ISGID));
    119 
    120 	if (to_fd == -1 && fflag) {
    121 		/*
    122 		 * attempt to remove existing destination file name and
    123 		 * create a new file
    124 		 */
    125 		(void)unlink(to.p_path);
    126 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
    127 			     fs->st_mode & ~(S_ISUID | S_ISGID));
    128 	}
    129 
    130 	if (to_fd == -1) {
    131 		warn("%s", to.p_path);
    132 		(void)close(from_fd);
    133 		return (1);;
    134 	}
    135 
    136 	rval = 0;
    137 
    138 	/*
    139 	 * There's no reason to do anything other than close the file
    140 	 * now if it's empty, so let's not bother.
    141 	 */
    142 
    143 	if (fs->st_size > 0) {
    144 
    145 		/*
    146 		 * Mmap and write if less than 8M (the limit is so
    147 		 * we don't totally trash memory on big files).
    148 		 * This is really a minor hack, but it wins some CPU back.
    149 		 */
    150 
    151 		if (fs->st_size <= 8 * 1048576) {
    152 			p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
    153 			    MAP_FILE|MAP_SHARED, from_fd, (off_t)0);
    154 			if (p == MAP_FAILED) {
    155 				goto mmap_failed;
    156 			} else {
    157 				(void) madvise(p, (size_t)fs->st_size,
    158 				     MADV_SEQUENTIAL);
    159 				if (write(to_fd, p, fs->st_size) !=
    160 				    fs->st_size) {
    161 					warn("%s", to.p_path);
    162 					rval = 1;
    163 				}
    164 				if (munmap(p, fs->st_size) < 0) {
    165 					warn("%s", entp->fts_path);
    166 					rval = 1;
    167 				}
    168 			}
    169 		} else {
    170 mmap_failed:
    171 			while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
    172 				wcount = write(to_fd, buf, rcount);
    173 				if (rcount != wcount || wcount == -1) {
    174 					warn("%s", to.p_path);
    175 					rval = 1;
    176 					break;
    177 				}
    178 			}
    179 			if (rcount < 0) {
    180 				warn("%s", entp->fts_path);
    181 				rval = 1;
    182 			}
    183 		}
    184 	}
    185 
    186 	if (rval == 1) {
    187 		(void)close(from_fd);
    188 		(void)close(to_fd);
    189 		return (1);
    190 	}
    191 
    192 	if (pflag && setfile(fs, to_fd))
    193 		rval = 1;
    194 	/*
    195 	 * If the source was setuid or setgid, lose the bits unless the
    196 	 * copy is owned by the same user and group.
    197 	 */
    198 #define	RETAINBITS \
    199 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
    200 	else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
    201 		if (fstat(to_fd, &to_stat)) {
    202 			warn("%s", to.p_path);
    203 			rval = 1;
    204 		} else if (fs->st_gid == to_stat.st_gid &&
    205 		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
    206 			warn("%s", to.p_path);
    207 			rval = 1;
    208 		}
    209 	}
    210 	(void)close(from_fd);
    211 	if (close(to_fd)) {
    212 		warn("%s", to.p_path);
    213 		rval = 1;
    214 	}
    215 	/* set the mod/access times now after close of the fd */
    216 	if (pflag && set_utimes(to.p_path, fs)) {
    217 	    rval = 1;
    218 	}
    219 	return (rval);
    220 }
    221 
    222 int
    223 copy_link(p, exists)
    224 	FTSENT *p;
    225 	int exists;
    226 {
    227 	int len;
    228 	char target[MAXPATHLEN];
    229 
    230 	if ((len = readlink(p->fts_path, target, sizeof(target))) == -1) {
    231 		warn("readlink: %s", p->fts_path);
    232 		return (1);
    233 	}
    234 	target[len] = '\0';
    235 	if (exists && unlink(to.p_path)) {
    236 		warn("unlink: %s", to.p_path);
    237 		return (1);
    238 	}
    239 	if (symlink(target, to.p_path)) {
    240 		warn("symlink: %s", target);
    241 		return (1);
    242 	}
    243 	return (pflag ? setfile(p->fts_statp, 0) : 0);
    244 }
    245 
    246 int
    247 copy_fifo(from_stat, exists)
    248 	struct stat *from_stat;
    249 	int exists;
    250 {
    251 	if (exists && unlink(to.p_path)) {
    252 		warn("unlink: %s", to.p_path);
    253 		return (1);
    254 	}
    255 	if (mkfifo(to.p_path, from_stat->st_mode)) {
    256 		warn("mkfifo: %s", to.p_path);
    257 		return (1);
    258 	}
    259 	return (pflag ? setfile(from_stat, 0) : 0);
    260 }
    261 
    262 int
    263 copy_special(from_stat, exists)
    264 	struct stat *from_stat;
    265 	int exists;
    266 {
    267 	if (exists && unlink(to.p_path)) {
    268 		warn("unlink: %s", to.p_path);
    269 		return (1);
    270 	}
    271 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
    272 		warn("mknod: %s", to.p_path);
    273 		return (1);
    274 	}
    275 	return (pflag ? setfile(from_stat, 0) : 0);
    276 }
    277 
    278 
    279 /*
    280  * Function: setfile
    281  *
    282  * Purpose:
    283  *   Set the owner/group/permissions for the "to" file to the information
    284  *   in the stat structure.  If fd is zero, also call set_utimes() to set
    285  *   the mod/access times.  If fd is non-zero, the caller must do a utimes
    286  *   itself after close(fd).
    287  */
    288 int
    289 setfile(fs, fd)
    290 	struct stat *fs;
    291 	int fd;
    292 {
    293 	int rval, islink;
    294 
    295 	rval = 0;
    296 	islink = S_ISLNK(fs->st_mode);
    297 	fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
    298 
    299 	/*
    300 	 * Changing the ownership probably won't succeed, unless we're root
    301 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
    302 	 * the mode; current BSD behavior is to remove all setuid bits on
    303 	 * chown.  If chown fails, lose setuid/setgid bits.
    304 	 */
    305 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
    306 	    lchown(to.p_path, fs->st_uid, fs->st_gid)) {
    307 		if (errno != EPERM) {
    308 			warn("chown: %s", to.p_path);
    309 			rval = 1;
    310 		}
    311 		fs->st_mode &= ~(S_ISUID | S_ISGID);
    312 	}
    313 	if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
    314 		warn("chmod: %s", to.p_path);
    315 		rval = 1;
    316 	}
    317 
    318 	if (!islink) {
    319 		/*
    320 		 * XXX
    321 		 * NFS doesn't support chflags; ignore errors unless
    322 		 * there's reason to believe we're losing bits.
    323 		 * (Note, this still won't be right if the server
    324 		 * supports flags and we were trying to *remove* flags
    325 		 * on a file that we copied, i.e., that we didn't create.)
    326 		 */
    327 		errno = 0;
    328 		if (fd ? fchflags(fd, fs->st_flags) :
    329 		    chflags(to.p_path, fs->st_flags))
    330 			if (errno != EOPNOTSUPP || fs->st_flags != 0) {
    331 				warn("chflags: %s", to.p_path);
    332 				rval = 1;
    333 			}
    334 	}
    335 	/* if fd is non-zero, caller must call set_utimes() after close() */
    336 	if (fd == 0 && set_utimes(to.p_path, fs))
    337 	    rval = 1;
    338 	return (rval);
    339 }
    340 
    341 void
    342 usage()
    343 {
    344 	(void)fprintf(stderr, "%s\n%s\n",
    345 	    "usage: cp [-R [-H | -L | -P]] [-f | -i] [-p] src target",
    346 	    "       cp [-R [-H | -L | -P]] [-f | -i] [-p] src1 ... srcN directory");
    347 	exit(1);
    348 	/* NOTREACHED */
    349 }
    350