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