Annotation of src/usr.bin/xinstall/xinstall.c, Revision 1.69
1.69 ! schwarze 1: /* $OpenBSD: xinstall.c,v 1.68 2019/02/08 12:53:44 schwarze Exp $ */
1.2 deraadt 2: /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1987, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.34 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
1.58 deraadt 33: #include <sys/param.h> /* MAXBSIZE */
1.1 deraadt 34: #include <sys/wait.h>
35: #include <sys/mman.h>
36: #include <sys/stat.h>
37:
38: #include <ctype.h>
1.4 millert 39: #include <err.h>
1.1 deraadt 40: #include <errno.h>
41: #include <fcntl.h>
42: #include <grp.h>
43: #include <paths.h>
44: #include <pwd.h>
45: #include <stdio.h>
46: #include <stdlib.h>
47: #include <string.h>
48: #include <unistd.h>
1.58 deraadt 49: #include <limits.h>
1.4 millert 50: #include <utime.h>
1.60 jasper 51: #include <libgen.h>
1.1 deraadt 52:
53: #include "pathnames.h"
54:
1.58 deraadt 55: #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
56:
1.19 millert 57: #define DIRECTORY 0x01 /* Tell install it's a directory. */
58: #define SETFLAGS 0x02 /* Tell install to set flags. */
1.64 millert 59: #define USEFSYNC 0x04 /* Tell install to use fsync(2). */
1.19 millert 60: #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
61: #define BACKUP_SUFFIX ".old"
62:
1.69 ! schwarze 63: int dobackup, docompare, dodest, dodir, dopreserve, dostrip, safecopy;
1.1 deraadt 64: int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
1.58 deraadt 65: char pathbuf[PATH_MAX], tempfile[PATH_MAX];
1.19 millert 66: char *suffix = BACKUP_SUFFIX;
1.67 millert 67: uid_t uid = (uid_t)-1;
68: gid_t gid = (gid_t)-1;
1.1 deraadt 69:
1.31 millert 70: void copy(int, char *, int, char *, off_t, int);
1.59 guenther 71: int compare(int, const char *, off_t, int, const char *, off_t);
1.31 millert 72: void install(char *, char *, u_long, u_int);
1.62 jasper 73: void install_dir(char *, int);
1.31 millert 74: void strip(char *);
75: void usage(void);
1.69 ! schwarze 76: int create_newfile(char *, struct stat *);
1.31 millert 77: int create_tempfile(char *, char *, size_t);
78: int file_write(int, char *, size_t, int *, int *, int);
1.33 tedu 79: void file_flush(int, int);
1.1 deraadt 80:
81: int
1.35 deraadt 82: main(int argc, char *argv[])
1.1 deraadt 83: {
84: struct stat from_sb, to_sb;
1.41 otto 85: void *set;
1.23 mickey 86: u_int32_t fset;
1.1 deraadt 87: u_int iflags;
88: int ch, no_target;
89: char *flags, *to_name, *group = NULL, *owner = NULL;
1.67 millert 90: const char *errstr;
1.1 deraadt 91:
92: iflags = 0;
1.64 millert 93: while ((ch = getopt(argc, argv, "B:bCcDdFf:g:m:o:pSs")) != -1)
1.56 okan 94: switch(ch) {
1.4 millert 95: case 'C':
96: docompare = 1;
97: break;
1.19 millert 98: case 'B':
99: suffix = optarg;
100: /* fall through; -B implies -b */
101: case 'b':
102: dobackup = 1;
103: break;
1.1 deraadt 104: case 'c':
1.4 millert 105: /* For backwards compatibility. */
1.1 deraadt 106: break;
1.64 millert 107: case 'F':
108: iflags |= USEFSYNC;
109: break;
1.1 deraadt 110: case 'f':
111: flags = optarg;
1.23 mickey 112: if (strtofflags(&flags, &fset, NULL))
1.63 millert 113: errx(1, "%s: invalid flag", flags);
1.1 deraadt 114: iflags |= SETFLAGS;
115: break;
116: case 'g':
117: group = optarg;
118: break;
119: case 'm':
120: if (!(set = setmode(optarg)))
1.63 millert 121: errx(1, "%s: invalid file mode", optarg);
1.1 deraadt 122: mode = getmode(set, 0);
1.16 deraadt 123: free(set);
1.1 deraadt 124: break;
125: case 'o':
126: owner = optarg;
127: break;
1.4 millert 128: case 'p':
129: docompare = dopreserve = 1;
130: break;
131: case 'S':
1.69 ! schwarze 132: safecopy = 1;
1.4 millert 133: break;
1.1 deraadt 134: case 's':
135: dostrip = 1;
136: break;
1.60 jasper 137: case 'D':
138: dodest = 1;
139: break;
1.1 deraadt 140: case 'd':
141: dodir = 1;
142: break;
143: case '?':
144: default:
145: usage();
146: }
147: argc -= optind;
148: argv += optind;
149:
1.4 millert 150: /* some options make no sense when creating directories */
1.69 ! schwarze 151: if ((safecopy || docompare || dostrip) && dodir)
1.1 deraadt 152: usage();
153:
154: /* must have at least two arguments, except when creating directories */
155: if (argc < 2 && !dodir)
156: usage();
157:
1.69 ! schwarze 158: /* need to make a temp copy so we can compare stripped version */
! 159: if (docompare && dostrip)
! 160: safecopy = 1;
! 161:
1.1 deraadt 162: /* get group and owner id's */
1.67 millert 163: if (group != NULL && gid_from_group(group, &gid) == -1) {
164: gid = strtonum(group, 0, GID_MAX, &errstr);
165: if (errstr != NULL)
166: errx(1, "unknown group %s", group);
167: }
168: if (owner != NULL && uid_from_user(owner, &uid) == -1) {
169: uid = strtonum(owner, 0, UID_MAX, &errstr);
170: if (errstr != NULL)
171: errx(1, "unknown user %s", owner);
172: }
1.1 deraadt 173:
174: if (dodir) {
175: for (; *argv != NULL; ++argv)
1.62 jasper 176: install_dir(*argv, mode);
1.63 millert 177: exit(0);
1.1 deraadt 178: /* NOTREACHED */
179: }
180:
1.60 jasper 181: if (dodest) {
182: char *dest = dirname(argv[argc - 1]);
183: if (dest == NULL)
1.63 millert 184: errx(1, "cannot determine dirname");
1.62 jasper 185: /*
186: * When -D is passed, do not chmod the directory with the mode set for
187: * the target file. If more restrictive permissions are required then
188: * '-d -m' ought to be used instead.
189: */
190: install_dir(dest, 0755);
1.60 jasper 191: }
192:
1.1 deraadt 193: no_target = stat(to_name = argv[argc - 1], &to_sb);
194: if (!no_target && S_ISDIR(to_sb.st_mode)) {
195: for (; *argv != to_name; ++argv)
196: install(*argv, to_name, fset, iflags | DIRECTORY);
1.63 millert 197: exit(0);
1.4 millert 198: /* NOTREACHED */
1.1 deraadt 199: }
200:
201: /* can't do file1 file2 directory/file */
202: if (argc != 2)
1.63 millert 203: errx(1, "Target: %s", argv[argc-1]);
1.1 deraadt 204:
205: if (!no_target) {
206: if (stat(*argv, &from_sb))
1.63 millert 207: err(1, "%s", *argv);
1.1 deraadt 208: if (!S_ISREG(to_sb.st_mode))
1.63 millert 209: errc(1, EFTYPE, "%s", to_name);
1.1 deraadt 210: if (to_sb.st_dev == from_sb.st_dev &&
211: to_sb.st_ino == from_sb.st_ino)
1.63 millert 212: errx(1, "%s and %s are the same file", *argv, to_name);
1.1 deraadt 213: }
214: install(*argv, to_name, fset, iflags);
1.63 millert 215: exit(0);
1.4 millert 216: /* NOTREACHED */
1.1 deraadt 217: }
218:
219: /*
220: * install --
221: * build a path name and install the file
222: */
223: void
1.35 deraadt 224: install(char *from_name, char *to_name, u_long fset, u_int flags)
1.1 deraadt 225: {
226: struct stat from_sb, to_sb;
1.59 guenther 227: struct timespec ts[2];
1.4 millert 228: int devnull, from_fd, to_fd, serrno, files_match = 0;
1.1 deraadt 229: char *p;
230:
1.4 millert 231: (void)memset((void *)&from_sb, 0, sizeof(from_sb));
232: (void)memset((void *)&to_sb, 0, sizeof(to_sb));
233:
1.1 deraadt 234: /* If try to install NULL file to a directory, fails. */
235: if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
236: if (stat(from_name, &from_sb))
1.63 millert 237: err(1, "%s", from_name);
1.1 deraadt 238: if (!S_ISREG(from_sb.st_mode))
1.63 millert 239: errc(1, EFTYPE, "%s", from_name);
1.1 deraadt 240: /* Build the target path. */
241: if (flags & DIRECTORY) {
242: (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
243: to_name,
1.11 millert 244: (p = strrchr(from_name, '/')) ? ++p : from_name);
1.1 deraadt 245: to_name = pathbuf;
246: }
247: devnull = 0;
248: } else {
249: devnull = 1;
250: }
251:
1.4 millert 252: if (stat(to_name, &to_sb) == 0) {
253: /* Only compare against regular files. */
254: if (docompare && !S_ISREG(to_sb.st_mode)) {
255: docompare = 0;
1.57 guenther 256: warnc(EFTYPE, "%s", to_name);
1.4 millert 257: }
258: } else if (docompare) {
259: /* File does not exist so silently ignore compare flag. */
260: docompare = 0;
261: }
262:
1.52 millert 263: if (!devnull) {
264: if ((from_fd = open(from_name, O_RDONLY, 0)) < 0)
1.63 millert 265: err(1, "%s", from_name);
1.52 millert 266: }
267:
1.69 ! schwarze 268: if (safecopy) {
! 269: to_fd = create_tempfile(to_name, tempfile, sizeof(tempfile));
! 270: if (to_fd < 0)
! 271: err(1, "%s", tempfile);
! 272: } else if (docompare && !dostrip) {
! 273: if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
! 274: err(1, "%s", to_name);
! 275: } else {
! 276: if ((to_fd = create_newfile(to_name, &to_sb)) < 0)
! 277: err(1, "%s", to_name);
! 278: }
1.4 millert 279:
1.69 ! schwarze 280: if (!devnull) {
! 281: if (docompare && !safecopy) {
! 282: files_match = !(compare(from_fd, from_name,
! 283: from_sb.st_size, to_fd,
! 284: to_name, to_sb.st_size));
! 285:
! 286: /* Truncate "to" file for copy unless we match */
! 287: if (!files_match) {
! 288: (void)close(to_fd);
! 289: if ((to_fd = create_newfile(to_name, &to_sb)) < 0)
! 290: err(1, "%s", to_name);
! 291: }
! 292: }
! 293: if (!files_match)
! 294: copy(from_fd, from_name, to_fd,
! 295: safecopy ? tempfile : to_name, from_sb.st_size,
! 296: ((off_t)from_sb.st_blocks * S_BLKSIZE < from_sb.st_size));
! 297: }
1.2 deraadt 298:
299: if (dostrip) {
1.69 ! schwarze 300: strip(safecopy ? tempfile : to_name);
1.2 deraadt 301:
302: /*
303: * Re-open our fd on the target, in case we used a strip
304: * that does not work in-place -- like gnu binutils strip.
305: */
306: close(to_fd);
1.69 ! schwarze 307: if ((to_fd = open(safecopy ? tempfile : to_name, O_RDONLY,
! 308: 0)) < 0)
1.63 millert 309: err(1, "stripping %s", to_name);
1.4 millert 310: }
311:
312: /*
313: * Compare the (possibly stripped) temp file to the target.
314: */
1.69 ! schwarze 315: if (safecopy && docompare) {
1.4 millert 316: int temp_fd = to_fd;
317: struct stat temp_sb;
318:
319: /* Re-open to_fd using the real target name. */
320: if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
1.63 millert 321: err(1, "%s", to_name);
1.4 millert 322:
323: if (fstat(temp_fd, &temp_sb)) {
324: serrno = errno;
325: (void)unlink(tempfile);
1.63 millert 326: errc(1, serrno, "%s", tempfile);
1.4 millert 327: }
328:
1.59 guenther 329: if (compare(temp_fd, tempfile, temp_sb.st_size, to_fd,
330: to_name, to_sb.st_size) == 0) {
1.4 millert 331: /*
332: * If target has more than one link we need to
333: * replace it in order to snap the extra links.
334: * Need to preserve target file times, though.
335: */
336: if (to_sb.st_nlink != 1) {
1.59 guenther 337: ts[0] = to_sb.st_atim;
338: ts[1] = to_sb.st_mtim;
339: futimens(temp_fd, ts);
1.4 millert 340: } else {
341: files_match = 1;
342: (void)unlink(tempfile);
343: }
344: }
1.48 phessler 345: (void)close(to_fd);
346: to_fd = temp_fd;
1.4 millert 347: }
348:
349: /*
1.30 millert 350: * Preserve the timestamp of the source file if necessary.
1.4 millert 351: */
352: if (dopreserve && !files_match) {
1.59 guenther 353: ts[0] = from_sb.st_atim;
354: ts[1] = from_sb.st_mtim;
355: futimens(to_fd, ts);
1.2 deraadt 356: }
357:
1.1 deraadt 358: /*
359: * Set owner, group, mode for target; do the chown first,
360: * chown may lose the setuid bits.
361: */
1.48 phessler 362: if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
363: fchown(to_fd, uid, gid)) {
1.1 deraadt 364: serrno = errno;
1.69 ! schwarze 365: (void)unlink(safecopy ? tempfile : to_name);
! 366: errx(1, "%s: chown/chgrp: %s",
! 367: safecopy ? tempfile : to_name, strerror(serrno));
1.1 deraadt 368: }
369: if (fchmod(to_fd, mode)) {
370: serrno = errno;
1.69 ! schwarze 371: (void)unlink(safecopy ? tempfile : to_name);
! 372: errx(1, "%s: chmod: %s", safecopy ? tempfile : to_name,
! 373: strerror(serrno));
1.1 deraadt 374: }
375:
376: /*
377: * If provided a set of flags, set them, otherwise, preserve the
378: * flags, except for the dump flag.
379: */
380: if (fchflags(to_fd,
381: flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
1.12 millert 382: if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
1.69 ! schwarze 383: warnx("%s: chflags: %s",
! 384: safecopy ? tempfile :to_name, strerror(errno));
1.1 deraadt 385: }
386:
1.64 millert 387: if (flags & USEFSYNC)
388: fsync(to_fd);
1.1 deraadt 389: (void)close(to_fd);
1.18 millert 390: if (!devnull)
391: (void)close(from_fd);
1.48 phessler 392:
393: /*
1.69 ! schwarze 394: * Move the new file into place if doing a safe copy
! 395: * and the files are different (or just not compared).
1.48 phessler 396: */
1.69 ! schwarze 397: if (safecopy && !files_match) {
1.48 phessler 398: /* Try to turn off the immutable bits. */
399: if (to_sb.st_flags & (NOCHANGEBITS))
400: (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
401: if (dobackup) {
1.58 deraadt 402: char backup[PATH_MAX];
403: (void)snprintf(backup, PATH_MAX, "%s%s", to_name,
1.48 phessler 404: suffix);
405: /* It is ok for the target file not to exist. */
406: if (rename(to_name, backup) < 0 && errno != ENOENT) {
407: serrno = errno;
408: unlink(tempfile);
1.63 millert 409: errx(1, "rename: %s to %s: %s", to_name,
1.48 phessler 410: backup, strerror(serrno));
411: }
412: }
413: if (rename(tempfile, to_name) < 0 ) {
414: serrno = errno;
415: unlink(tempfile);
1.63 millert 416: errx(1, "rename: %s to %s: %s", tempfile,
1.48 phessler 417: to_name, strerror(serrno));
418: }
419: }
1.1 deraadt 420: }
421:
422: /*
423: * copy --
424: * copy from one file to another
425: */
426: void
1.35 deraadt 427: copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size,
428: int sparse)
1.1 deraadt 429: {
1.29 mpech 430: ssize_t nr, nw;
1.1 deraadt 431: int serrno;
432: char *p, buf[MAXBSIZE];
433:
1.51 millert 434: if (size == 0)
435: return;
436:
1.7 millert 437: /* Rewind file descriptors. */
438: if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1.63 millert 439: err(1, "lseek: %s", from_name);
1.7 millert 440: if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
1.63 millert 441: err(1, "lseek: %s", to_name);
1.7 millert 442:
1.1 deraadt 443: /*
444: * Mmap and write if less than 8M (the limit is so we don't totally
445: * trash memory on big files. This is really a minor hack, but it
1.17 millert 446: * wins some CPU back. Sparse files need special treatment.
1.1 deraadt 447: */
1.51 millert 448: if (!sparse && size <= 8 * 1048576) {
1.50 espie 449: size_t siz;
1.17 millert 450:
1.19 millert 451: if ((p = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE,
1.38 grange 452: from_fd, (off_t)0)) == MAP_FAILED) {
1.6 millert 453: serrno = errno;
454: (void)unlink(to_name);
1.63 millert 455: errc(1, serrno, "%s", from_name);
1.6 millert 456: }
1.50 espie 457: madvise(p, size, MADV_SEQUENTIAL);
1.3 deraadt 458: siz = (size_t)size;
1.6 millert 459: if ((nw = write(to_fd, p, siz)) != siz) {
460: serrno = errno;
461: (void)unlink(to_name);
1.63 millert 462: errx(1, "%s: %s",
1.6 millert 463: to_name, strerror(nw > 0 ? EIO : serrno));
464: }
1.4 millert 465: (void) munmap(p, (size_t)size);
1.1 deraadt 466: } else {
1.17 millert 467: int sz, rem, isem = 1;
468: struct stat sb;
469:
470: /*
471: * Pass the blocksize of the file being written to the write
472: * routine. if the size is zero, use the default S_BLKSIZE.
473: */
474: if (fstat(to_fd, &sb) != 0 || sb.st_blksize == 0)
475: sz = S_BLKSIZE;
476: else
477: sz = sb.st_blksize;
1.20 millert 478: rem = sz;
1.17 millert 479:
480: while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
481: if (sparse)
482: nw = file_write(to_fd, buf, nr, &rem, &isem, sz);
483: else
484: nw = write(to_fd, buf, nr);
485: if (nw != nr) {
1.1 deraadt 486: serrno = errno;
487: (void)unlink(to_name);
1.63 millert 488: errx(1, "%s: %s",
1.1 deraadt 489: to_name, strerror(nw > 0 ? EIO : serrno));
490: }
1.17 millert 491: }
1.33 tedu 492: if (sparse)
493: file_flush(to_fd, isem);
1.1 deraadt 494: if (nr != 0) {
495: serrno = errno;
496: (void)unlink(to_name);
1.63 millert 497: errc(1, serrno, "%s", from_name);
1.1 deraadt 498: }
499: }
500: }
501:
502: /*
1.4 millert 503: * compare --
504: * compare two files; non-zero means files differ
505: */
506: int
1.59 guenther 507: compare(int from_fd, const char *from_name, off_t from_len, int to_fd,
508: const char *to_name, off_t to_len)
1.4 millert 509: {
510: caddr_t p1, p2;
1.59 guenther 511: size_t length;
512: off_t from_off, to_off, remainder;
1.4 millert 513: int dfound;
514:
1.50 espie 515: if (from_len == 0 && from_len == to_len)
516: return (0);
517:
1.4 millert 518: if (from_len != to_len)
1.50 espie 519: return (1);
1.4 millert 520:
521: /*
522: * Compare the two files being careful not to mmap
523: * more than 8M at a time.
524: */
1.21 millert 525: from_off = to_off = (off_t)0;
1.4 millert 526: remainder = from_len;
527: do {
1.58 deraadt 528: length = MINIMUM(remainder, 8 * 1048576);
1.4 millert 529: remainder -= length;
530:
1.22 mickey 531: if ((p1 = mmap(NULL, length, PROT_READ, MAP_PRIVATE,
1.38 grange 532: from_fd, from_off)) == MAP_FAILED)
1.63 millert 533: err(1, "%s", from_name);
1.22 mickey 534: if ((p2 = mmap(NULL, length, PROT_READ, MAP_PRIVATE,
1.38 grange 535: to_fd, to_off)) == MAP_FAILED)
1.63 millert 536: err(1, "%s", to_name);
1.39 mickey 537: if (length) {
538: madvise(p1, length, MADV_SEQUENTIAL);
539: madvise(p2, length, MADV_SEQUENTIAL);
540: }
1.4 millert 541:
542: dfound = memcmp(p1, p2, length);
543:
544: (void) munmap(p1, length);
545: (void) munmap(p2, length);
1.21 millert 546:
547: from_off += length;
548: to_off += length;
1.4 millert 549:
1.7 millert 550: } while (!dfound && remainder > 0);
1.4 millert 551:
552: return(dfound);
553: }
554:
555: /*
1.1 deraadt 556: * strip --
557: * use strip(1) to strip the target file
558: */
559: void
1.35 deraadt 560: strip(char *to_name)
1.1 deraadt 561: {
562: int serrno, status;
1.26 millert 563: char * volatile path_strip;
1.66 deraadt 564: pid_t pid;
1.18 millert 565:
1.13 millert 566: if (issetugid() || (path_strip = getenv("STRIP")) == NULL)
567: path_strip = _PATH_STRIP;
1.1 deraadt 568:
1.66 deraadt 569: switch ((pid = vfork())) {
1.1 deraadt 570: case -1:
571: serrno = errno;
572: (void)unlink(to_name);
1.63 millert 573: errc(1, serrno, "forks");
1.1 deraadt 574: case 0:
1.44 moritz 575: execl(path_strip, "strip", "--", to_name, (char *)NULL);
1.13 millert 576: warn("%s", path_strip);
1.63 millert 577: _exit(1);
1.1 deraadt 578: default:
1.66 deraadt 579: while (waitpid(pid, &status, 0) == -1) {
580: if (errno != EINTR)
581: break;
582: }
583: if (!WIFEXITED(status))
1.1 deraadt 584: (void)unlink(to_name);
585: }
586: }
587:
588: /*
589: * install_dir --
1.42 jsg 590: * build directory hierarchy
1.1 deraadt 591: */
592: void
1.62 jasper 593: install_dir(char *path, int mode)
1.1 deraadt 594: {
1.29 mpech 595: char *p;
1.8 imp 596: struct stat sb;
597: int ch;
598:
599: for (p = path;; ++p)
600: if (!*p || (p != path && *p == '/')) {
601: ch = *p;
602: *p = '\0';
1.54 naddy 603: if (mkdir(path, 0777)) {
604: int mkdir_errno = errno;
605: if (stat(path, &sb)) {
606: /* Not there; use mkdir()s errno */
1.63 millert 607: errc(1, mkdir_errno, "%s",
1.57 guenther 608: path);
1.54 naddy 609: /* NOTREACHED */
610: }
611: if (!S_ISDIR(sb.st_mode)) {
612: /* Is there, but isn't a directory */
1.63 millert 613: errc(1, ENOTDIR, "%s", path);
1.1 deraadt 614: /* NOTREACHED */
1.8 imp 615: }
616: }
617: if (!(*p = ch))
1.1 deraadt 618: break;
1.8 imp 619: }
1.1 deraadt 620:
1.4 millert 621: if (((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid)) ||
1.8 imp 622: chmod(path, mode)) {
623: warn("%s", path);
624: }
1.1 deraadt 625: }
626:
627: /*
628: * usage --
629: * print a usage message and die
630: */
631: void
1.35 deraadt 632: usage(void)
1.1 deraadt 633: {
634: (void)fprintf(stderr, "\
1.65 jmc 635: usage: install [-bCcDdFpSs] [-B suffix] [-f flags] [-g group] [-m mode] [-o owner]\n source ... target ...\n");
1.63 millert 636: exit(1);
1.4 millert 637: /* NOTREACHED */
638: }
639:
640: /*
641: * create_tempfile --
642: * create a temporary file based on path and open it
643: */
644: int
1.35 deraadt 645: create_tempfile(char *path, char *temp, size_t tsize)
1.4 millert 646: {
647: char *p;
648:
1.43 millert 649: strlcpy(temp, path, tsize);
650: if ((p = strrchr(temp, '/')) != NULL)
1.4 millert 651: p++;
652: else
653: p = temp;
1.43 millert 654: *p = '\0';
655: strlcat(p, "INS@XXXXXXXXXX", tsize);
1.4 millert 656:
657: return(mkstemp(temp));
1.69 ! schwarze 658: }
! 659:
! 660: /*
! 661: * create_newfile --
! 662: * create a new file, overwriting an existing one if necessary
! 663: */
! 664: int
! 665: create_newfile(char *path, struct stat *sbp)
! 666: {
! 667: char backup[PATH_MAX];
! 668:
! 669: /*
! 670: * Unlink now... avoid ETXTBSY errors later. Try and turn
! 671: * off the append/immutable bits -- if we fail, go ahead,
! 672: * it might work.
! 673: */
! 674: if (sbp->st_flags & (NOCHANGEBITS))
! 675: (void)chflags(path, sbp->st_flags & ~(NOCHANGEBITS));
! 676:
! 677: if (dobackup) {
! 678: (void)snprintf(backup, PATH_MAX, "%s%s", path, suffix);
! 679: /* It is ok for the target file not to exist. */
! 680: if (rename(path, backup) < 0 && errno != ENOENT)
! 681: err(1, "rename: %s to %s (errno %d)", path, backup, errno);
! 682: } else {
! 683: if (unlink(path) < 0 && errno != ENOENT)
! 684: err(1, "%s", path);
! 685: }
! 686:
! 687: return(open(path, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR));
1.17 millert 688: }
689:
690: /*
691: * file_write()
692: * Write/copy a file (during copy or archive extract). This routine knows
693: * how to copy files with lseek holes in it. (Which are read as file
694: * blocks containing all 0's but do not have any file blocks associated
695: * with the data). Typical examples of these are files created by dbm
696: * variants (.pag files). While the file size of these files are huge, the
697: * actual storage is quite small (the files are sparse). The problem is
698: * the holes read as all zeros so are probably stored on the archive that
699: * way (there is no way to determine if the file block is really a hole,
700: * we only know that a file block of all zero's can be a hole).
701: * At this writing, no major archive format knows how to archive files
702: * with holes. However, on extraction (or during copy, -rw) we have to
703: * deal with these files. Without detecting the holes, the files can
704: * consume a lot of file space if just written to disk. This replacement
705: * for write when passed the basic allocation size of a file system block,
706: * uses lseek whenever it detects the input data is all 0 within that
707: * file block. In more detail, the strategy is as follows:
708: * While the input is all zero keep doing an lseek. Keep track of when we
1.45 krw 709: * pass over file block boundaries. Only write when we hit a non zero
1.17 millert 710: * input. once we have written a file block, we continue to write it to
711: * the end (we stop looking at the input). When we reach the start of the
712: * next file block, start checking for zero blocks again. Working on file
1.45 krw 713: * block boundaries significantly reduces the overhead when copying files
1.17 millert 714: * that are NOT very sparse. This overhead (when compared to a write) is
715: * almost below the measurement resolution on many systems. Without it,
716: * files with holes cannot be safely copied. It does has a side effect as
717: * it can put holes into files that did not have them before, but that is
718: * not a problem since the file contents are unchanged (in fact it saves
719: * file space). (Except on paging files for diskless clients. But since we
720: * cannot determine one of those file from here, we ignore them). If this
721: * ever ends up on a system where CTG files are supported and the holes
722: * are not desired, just do a conditional test in those routines that
723: * call file_write() and have it call write() instead. BEFORE CLOSING THE
724: * FILE, make sure to call file_flush() when the last write finishes with
725: * an empty block. A lot of file systems will not create an lseek hole at
726: * the end. In this case we drop a single 0 at the end to force the
727: * trailing 0's in the file.
728: * ---Parameters---
729: * rem: how many bytes left in this file system block
730: * isempt: have we written to the file block yet (is it empty)
731: * sz: basic file block allocation size
732: * cnt: number of bytes on this write
733: * str: buffer to write
734: * Return:
735: * number of bytes written, -1 on write (or lseek) error.
736: */
737:
738: int
1.35 deraadt 739: file_write(int fd, char *str, size_t cnt, int *rem, int *isempt, int sz)
1.17 millert 740: {
1.29 mpech 741: char *pt;
742: char *end;
743: size_t wcnt;
744: char *st = str;
1.17 millert 745:
746: /*
747: * while we have data to process
748: */
749: while (cnt) {
750: if (!*rem) {
751: /*
752: * We are now at the start of file system block again
753: * (or what we think one is...). start looking for
754: * empty blocks again
755: */
756: *isempt = 1;
757: *rem = sz;
758: }
759:
760: /*
761: * only examine up to the end of the current file block or
762: * remaining characters to write, whatever is smaller
763: */
1.58 deraadt 764: wcnt = MINIMUM(cnt, *rem);
1.17 millert 765: cnt -= wcnt;
766: *rem -= wcnt;
767: if (*isempt) {
768: /*
769: * have not written to this block yet, so we keep
770: * looking for zero's
771: */
772: pt = st;
773: end = st + wcnt;
774:
775: /*
776: * look for a zero filled buffer
777: */
778: while ((pt < end) && (*pt == '\0'))
779: ++pt;
780:
781: if (pt == end) {
782: /*
783: * skip, buf is empty so far
784: */
785: if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
786: warn("lseek");
787: return(-1);
788: }
789: st = pt;
790: continue;
791: }
792: /*
793: * drat, the buf is not zero filled
794: */
795: *isempt = 0;
796: }
797:
798: /*
799: * have non-zero data in this file system block, have to write
800: */
801: if (write(fd, st, wcnt) != wcnt) {
802: warn("write");
803: return(-1);
804: }
805: st += wcnt;
806: }
807: return(st - str);
1.33 tedu 808: }
809:
810: /*
811: * file_flush()
812: * when the last file block in a file is zero, many file systems will not
813: * let us create a hole at the end. To get the last block with zeros, we
814: * write the last BYTE with a zero (back up one byte and write a zero).
815: */
816: void
817: file_flush(int fd, int isempt)
818: {
819: static char blnk[] = "\0";
820:
821: /*
822: * silly test, but make sure we are only called when the last block is
823: * filled with all zeros.
824: */
825: if (!isempt)
826: return;
827:
828: /*
829: * move back one byte and write a zero
830: */
831: if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
832: warn("Failed seek on file");
833: return;
834: }
835:
836: if (write(fd, blnk, 1) < 0)
837: warn("Failed write to file");
838: return;
1.1 deraadt 839: }