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