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