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