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