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