Annotation of src/usr.bin/xinstall/xinstall.c, Revision 1.51
1.51 ! millert 1: /* $OpenBSD: xinstall.c,v 1.50 2012/04/11 09:27:42 espie 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:
33: #include <sys/param.h>
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.4 millert 49: #include <sysexits.h>
50: #include <utime.h>
1.1 deraadt 51:
52: #include "pathnames.h"
53:
1.19 millert 54: #define DIRECTORY 0x01 /* Tell install it's a directory. */
55: #define SETFLAGS 0x02 /* Tell install to set flags. */
56: #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
57: #define BACKUP_SUFFIX ".old"
58:
1.1 deraadt 59: struct passwd *pp;
60: struct group *gp;
1.19 millert 61: int dobackup, docompare, dodir, dopreserve, dostrip, safecopy;
1.1 deraadt 62: int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
1.4 millert 63: char pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
1.19 millert 64: char *suffix = BACKUP_SUFFIX;
1.1 deraadt 65: uid_t uid;
66: gid_t gid;
67:
1.31 millert 68: void copy(int, char *, int, char *, off_t, int);
69: int compare(int, const char *, size_t, int, const char *, size_t);
70: void install(char *, char *, u_long, u_int);
71: void install_dir(char *);
72: void strip(char *);
73: void usage(void);
74: int create_newfile(char *, struct stat *);
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;
88:
89: iflags = 0;
1.47 jsg 90: while ((ch = getopt(argc, argv, "B:bCcdf:g:m:o:pSs")) != -1)
1.1 deraadt 91: switch((char)ch) {
1.4 millert 92: case 'C':
93: docompare = 1;
94: break;
1.19 millert 95: case 'B':
96: suffix = optarg;
97: /* fall through; -B implies -b */
98: case 'b':
99: dobackup = 1;
100: break;
1.1 deraadt 101: case 'c':
1.4 millert 102: /* For backwards compatibility. */
1.1 deraadt 103: break;
104: case 'f':
105: flags = optarg;
1.23 mickey 106: if (strtofflags(&flags, &fset, NULL))
1.4 millert 107: errx(EX_USAGE, "%s: invalid flag", flags);
1.1 deraadt 108: iflags |= SETFLAGS;
109: break;
110: case 'g':
111: group = optarg;
112: break;
113: case 'm':
114: if (!(set = setmode(optarg)))
1.4 millert 115: errx(EX_USAGE, "%s: invalid file mode", optarg);
1.1 deraadt 116: mode = getmode(set, 0);
1.16 deraadt 117: free(set);
1.1 deraadt 118: break;
119: case 'o':
120: owner = optarg;
121: break;
1.4 millert 122: case 'p':
123: docompare = dopreserve = 1;
124: break;
125: case 'S':
126: safecopy = 1;
127: break;
1.1 deraadt 128: case 's':
129: dostrip = 1;
130: break;
131: case 'd':
132: dodir = 1;
133: break;
134: case '?':
135: default:
136: usage();
137: }
138: argc -= optind;
139: argv += optind;
140:
1.4 millert 141: /* some options make no sense when creating directories */
142: if ((safecopy || docompare || dostrip) && dodir)
1.1 deraadt 143: usage();
144:
145: /* must have at least two arguments, except when creating directories */
146: if (argc < 2 && !dodir)
147: usage();
148:
1.4 millert 149: /* need to make a temp copy so we can compare stripped version */
150: if (docompare && dostrip)
151: safecopy = 1;
152:
1.1 deraadt 153: /* get group and owner id's */
154: if (group && !(gp = getgrnam(group)) && !isdigit(*group))
1.4 millert 155: errx(EX_NOUSER, "unknown group %s", group);
156: gid = (group) ? ((gp) ? gp->gr_gid : (gid_t)strtoul(group, NULL, 10)) : (gid_t)-1;
1.1 deraadt 157: if (owner && !(pp = getpwnam(owner)) && !isdigit(*owner))
1.4 millert 158: errx(EX_NOUSER, "unknown user %s", owner);
159: uid = (owner) ? ((pp) ? pp->pw_uid : (uid_t)strtoul(owner, NULL, 10)) : (uid_t)-1;
1.1 deraadt 160:
161: if (dodir) {
162: for (; *argv != NULL; ++argv)
163: install_dir(*argv);
1.4 millert 164: exit(EX_OK);
1.1 deraadt 165: /* NOTREACHED */
166: }
167:
168: no_target = stat(to_name = argv[argc - 1], &to_sb);
169: if (!no_target && S_ISDIR(to_sb.st_mode)) {
170: for (; *argv != to_name; ++argv)
171: install(*argv, to_name, fset, iflags | DIRECTORY);
1.4 millert 172: exit(EX_OK);
173: /* NOTREACHED */
1.1 deraadt 174: }
175:
176: /* can't do file1 file2 directory/file */
177: if (argc != 2)
1.32 millert 178: errx(EX_OSERR, "Target: %s", argv[argc-1]);
1.1 deraadt 179:
180: if (!no_target) {
181: if (stat(*argv, &from_sb))
1.4 millert 182: err(EX_OSERR, "%s", *argv);
1.1 deraadt 183: if (!S_ISREG(to_sb.st_mode))
1.4 millert 184: errx(EX_OSERR, "%s: %s", to_name, strerror(EFTYPE));
1.1 deraadt 185: if (to_sb.st_dev == from_sb.st_dev &&
186: to_sb.st_ino == from_sb.st_ino)
1.4 millert 187: errx(EX_USAGE, "%s and %s are the same file", *argv, to_name);
1.1 deraadt 188: }
189: install(*argv, to_name, fset, iflags);
1.4 millert 190: exit(EX_OK);
191: /* NOTREACHED */
1.1 deraadt 192: }
193:
194: /*
195: * install --
196: * build a path name and install the file
197: */
198: void
1.35 deraadt 199: install(char *from_name, char *to_name, u_long fset, u_int flags)
1.1 deraadt 200: {
201: struct stat from_sb, to_sb;
1.4 millert 202: struct utimbuf utb;
203: int devnull, from_fd, to_fd, serrno, files_match = 0;
1.1 deraadt 204: char *p;
205:
1.4 millert 206: (void)memset((void *)&from_sb, 0, sizeof(from_sb));
207: (void)memset((void *)&to_sb, 0, sizeof(to_sb));
208:
1.1 deraadt 209: /* If try to install NULL file to a directory, fails. */
210: if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
211: if (stat(from_name, &from_sb))
1.4 millert 212: err(EX_OSERR, "%s", from_name);
1.1 deraadt 213: if (!S_ISREG(from_sb.st_mode))
1.4 millert 214: errx(EX_OSERR, "%s: %s", from_name, strerror(EFTYPE));
1.1 deraadt 215: /* Build the target path. */
216: if (flags & DIRECTORY) {
217: (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
218: to_name,
1.11 millert 219: (p = strrchr(from_name, '/')) ? ++p : from_name);
1.1 deraadt 220: to_name = pathbuf;
221: }
222: devnull = 0;
223: } else {
224: devnull = 1;
225: }
226:
1.4 millert 227: if (stat(to_name, &to_sb) == 0) {
228: /* Only compare against regular files. */
229: if (docompare && !S_ISREG(to_sb.st_mode)) {
230: docompare = 0;
231: warnx("%s: %s", to_name, strerror(EFTYPE));
232: }
233: } else if (docompare) {
234: /* File does not exist so silently ignore compare flag. */
235: docompare = 0;
236: }
237:
238: if (safecopy) {
239: to_fd = create_tempfile(to_name, tempfile, sizeof(tempfile));
240: if (to_fd < 0)
241: err(EX_OSERR, "%s", tempfile);
242: } else if (docompare && !dostrip) {
243: if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
244: err(EX_OSERR, "%s", to_name);
245: } else {
246: if ((to_fd = create_newfile(to_name, &to_sb)) < 0)
247: err(EX_OSERR, "%s", to_name);
248: }
249:
1.1 deraadt 250: if (!devnull) {
251: if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
1.4 millert 252: serrno = errno;
253: (void)unlink(safecopy ? tempfile : to_name);
254: errx(EX_OSERR, "%s: %s", from_name, strerror(serrno));
255: }
256:
257: if (docompare && !safecopy) {
258: files_match = !(compare(from_fd, from_name,
259: (size_t)from_sb.st_size, to_fd,
260: to_name, (size_t)to_sb.st_size));
261:
262: /* Truncate "to" file for copy unless we match */
263: if (!files_match) {
264: (void)close(to_fd);
265: if ((to_fd = create_newfile(to_name, &to_sb)) < 0)
266: err(EX_OSERR, "%s", to_name);
267: }
1.1 deraadt 268: }
1.4 millert 269: if (!files_match)
270: copy(from_fd, from_name, to_fd,
1.17 millert 271: safecopy ? tempfile : to_name, from_sb.st_size,
272: ((off_t)from_sb.st_blocks * S_BLKSIZE < from_sb.st_size));
1.1 deraadt 273: }
1.2 deraadt 274:
275: if (dostrip) {
1.4 millert 276: strip(safecopy ? tempfile : to_name);
1.2 deraadt 277:
278: /*
279: * Re-open our fd on the target, in case we used a strip
280: * that does not work in-place -- like gnu binutils strip.
281: */
282: close(to_fd);
1.4 millert 283: if ((to_fd = open(safecopy ? tempfile : to_name, O_RDONLY,
284: 0)) < 0)
285: err(EX_OSERR, "stripping %s", to_name);
286: }
287:
288: /*
289: * Compare the (possibly stripped) temp file to the target.
290: */
291: if (safecopy && docompare) {
292: int temp_fd = to_fd;
293: struct stat temp_sb;
294:
295: /* Re-open to_fd using the real target name. */
296: if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
297: err(EX_OSERR, "%s", to_name);
298:
299: if (fstat(temp_fd, &temp_sb)) {
300: serrno = errno;
301: (void)unlink(tempfile);
302: errx(EX_OSERR, "%s: %s", tempfile, strerror(serrno));
303: }
304:
305: if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
306: to_name, (size_t)to_sb.st_size) == 0) {
307: /*
308: * If target has more than one link we need to
309: * replace it in order to snap the extra links.
310: * Need to preserve target file times, though.
311: */
312: if (to_sb.st_nlink != 1) {
313: utb.actime = to_sb.st_atime;
314: utb.modtime = to_sb.st_mtime;
315: (void)utime(tempfile, &utb);
316: } else {
317: files_match = 1;
318: (void)unlink(tempfile);
319: }
320: }
1.48 phessler 321: (void)close(to_fd);
322: to_fd = temp_fd;
1.4 millert 323: }
324:
325: /*
1.30 millert 326: * Preserve the timestamp of the source file if necessary.
1.4 millert 327: */
328: if (dopreserve && !files_match) {
329: utb.actime = from_sb.st_atime;
330: utb.modtime = from_sb.st_mtime;
1.48 phessler 331: (void)utime(safecopy ? tempfile : to_name, &utb);
1.2 deraadt 332: }
333:
1.1 deraadt 334: /*
335: * Set owner, group, mode for target; do the chown first,
336: * chown may lose the setuid bits.
337: */
1.48 phessler 338: if ((gid != (gid_t)-1 || uid != (uid_t)-1) &&
339: fchown(to_fd, uid, gid)) {
1.1 deraadt 340: serrno = errno;
1.48 phessler 341: (void)unlink(safecopy ? tempfile : to_name);
342: errx(EX_OSERR, "%s: chown/chgrp: %s",
343: safecopy ? tempfile : to_name, strerror(serrno));
1.1 deraadt 344: }
345: if (fchmod(to_fd, mode)) {
346: serrno = errno;
1.48 phessler 347: (void)unlink(safecopy ? tempfile : to_name);
348: errx(EX_OSERR, "%s: chmod: %s", safecopy ? tempfile : to_name,
349: strerror(serrno));
1.1 deraadt 350: }
351:
352: /*
353: * If provided a set of flags, set them, otherwise, preserve the
354: * flags, except for the dump flag.
355: */
356: if (fchflags(to_fd,
357: flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
1.12 millert 358: if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
1.48 phessler 359: warnx("%s: chflags: %s",
360: safecopy ? tempfile :to_name, strerror(errno));
1.1 deraadt 361: }
362:
363: (void)close(to_fd);
1.18 millert 364: if (!devnull)
365: (void)close(from_fd);
1.48 phessler 366:
367: /*
368: * Move the new file into place if doing a safe copy
369: * and the files are different (or just not compared).
370: */
371: if (safecopy && !files_match) {
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) {
376: char backup[MAXPATHLEN];
377: (void)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
378: suffix);
379: /* It is ok for the target file not to exist. */
380: if (rename(to_name, backup) < 0 && errno != ENOENT) {
381: serrno = errno;
382: unlink(tempfile);
383: errx(EX_OSERR, "rename: %s to %s: %s", to_name,
384: backup, strerror(serrno));
385: }
386: }
387: if (rename(tempfile, to_name) < 0 ) {
388: serrno = errno;
389: unlink(tempfile);
390: errx(EX_OSERR, "rename: %s to %s: %s", tempfile,
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)
413: err(EX_OSERR, "lseek: %s", from_name);
414: if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
415: err(EX_OSERR, "lseek: %s", to_name);
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);
429: errx(EX_OSERR, "%s: %s", from_name, strerror(serrno));
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);
436: errx(EX_OSERR, "%s: %s",
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.4 millert 462: errx(EX_OSERR, "%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.4 millert 471: errx(EX_OSERR, "%s: %s", from_name, strerror(serrno));
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.35 deraadt 481: compare(int from_fd, const char *from_name, size_t from_len, int to_fd,
482: const char *to_name, size_t to_len)
1.4 millert 483: {
484: caddr_t p1, p2;
1.21 millert 485: size_t length, remainder;
486: off_t from_off, to_off;
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 {
502: length = MIN(remainder, 8 * 1048576);
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.4 millert 507: err(EX_OSERR, "%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.4 millert 510: err(EX_OSERR, "%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.18 millert 538:
1.13 millert 539: if (issetugid() || (path_strip = getenv("STRIP")) == NULL)
540: path_strip = _PATH_STRIP;
1.1 deraadt 541:
542: switch (vfork()) {
543: case -1:
544: serrno = errno;
545: (void)unlink(to_name);
1.4 millert 546: errx(EX_TEMPFAIL, "forks: %s", strerror(serrno));
1.1 deraadt 547: case 0:
1.44 moritz 548: execl(path_strip, "strip", "--", to_name, (char *)NULL);
1.13 millert 549: warn("%s", path_strip);
1.9 deraadt 550: _exit(EX_OSERR);
1.1 deraadt 551: default:
1.14 millert 552: if (wait(&status) == -1 || !WIFEXITED(status))
1.1 deraadt 553: (void)unlink(to_name);
554: }
555: }
556:
557: /*
558: * install_dir --
1.42 jsg 559: * build directory hierarchy
1.1 deraadt 560: */
561: void
1.35 deraadt 562: install_dir(char *path)
1.1 deraadt 563: {
1.29 mpech 564: char *p;
1.8 imp 565: struct stat sb;
566: int ch;
567:
568: for (p = path;; ++p)
569: if (!*p || (p != path && *p == '/')) {
570: ch = *p;
571: *p = '\0';
572: if (stat(path, &sb)) {
573: if (errno != ENOENT || mkdir(path, 0777) < 0) {
1.4 millert 574: err(EX_OSERR, "%s", path);
1.1 deraadt 575: /* NOTREACHED */
1.8 imp 576: }
577: }
578: if (!(*p = ch))
1.1 deraadt 579: break;
1.8 imp 580: }
1.1 deraadt 581:
1.4 millert 582: if (((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid)) ||
1.8 imp 583: chmod(path, mode)) {
584: warn("%s", path);
585: }
1.1 deraadt 586: }
587:
588: /*
589: * usage --
590: * print a usage message and die
591: */
592: void
1.35 deraadt 593: usage(void)
1.1 deraadt 594: {
595: (void)fprintf(stderr, "\
1.46 sobrado 596: usage: install [-bCcdpSs] [-B suffix] [-f flags] [-g group] [-m mode] [-o owner]\n source ... target ...\n");
1.4 millert 597: exit(EX_USAGE);
598: /* NOTREACHED */
599: }
600:
601: /*
602: * create_tempfile --
603: * create a temporary file based on path and open it
604: */
605: int
1.35 deraadt 606: create_tempfile(char *path, char *temp, size_t tsize)
1.4 millert 607: {
608: char *p;
609:
1.43 millert 610: strlcpy(temp, path, tsize);
611: if ((p = strrchr(temp, '/')) != NULL)
1.4 millert 612: p++;
613: else
614: p = temp;
1.43 millert 615: *p = '\0';
616: strlcat(p, "INS@XXXXXXXXXX", tsize);
1.4 millert 617:
618: return(mkstemp(temp));
619: }
620:
621: /*
622: * create_newfile --
1.30 millert 623: * create a new file, overwriting an existing one if necessary
1.4 millert 624: */
625: int
1.35 deraadt 626: create_newfile(char *path, struct stat *sbp)
1.4 millert 627: {
1.19 millert 628: char backup[MAXPATHLEN];
629:
1.4 millert 630: /*
631: * Unlink now... avoid ETXTBSY errors later. Try and turn
632: * off the append/immutable bits -- if we fail, go ahead,
633: * it might work.
634: */
635: if (sbp->st_flags & (NOCHANGEBITS))
636: (void)chflags(path, sbp->st_flags & ~(NOCHANGEBITS));
637:
1.19 millert 638: if (dobackup) {
639: (void)snprintf(backup, MAXPATHLEN, "%s%s", path, suffix);
1.28 heko 640: /* It is ok for the target file not to exist. */
641: if (rename(path, backup) < 0 && errno != ENOENT)
642: err(EX_OSERR, "rename: %s to %s (errno %d)", path, backup, errno);
1.19 millert 643: } else
644: (void)unlink(path);
1.4 millert 645:
646: return(open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
1.17 millert 647: }
648:
649: /*
650: * file_write()
651: * Write/copy a file (during copy or archive extract). This routine knows
652: * how to copy files with lseek holes in it. (Which are read as file
653: * blocks containing all 0's but do not have any file blocks associated
654: * with the data). Typical examples of these are files created by dbm
655: * variants (.pag files). While the file size of these files are huge, the
656: * actual storage is quite small (the files are sparse). The problem is
657: * the holes read as all zeros so are probably stored on the archive that
658: * way (there is no way to determine if the file block is really a hole,
659: * we only know that a file block of all zero's can be a hole).
660: * At this writing, no major archive format knows how to archive files
661: * with holes. However, on extraction (or during copy, -rw) we have to
662: * deal with these files. Without detecting the holes, the files can
663: * consume a lot of file space if just written to disk. This replacement
664: * for write when passed the basic allocation size of a file system block,
665: * uses lseek whenever it detects the input data is all 0 within that
666: * file block. In more detail, the strategy is as follows:
667: * While the input is all zero keep doing an lseek. Keep track of when we
1.45 krw 668: * pass over file block boundaries. Only write when we hit a non zero
1.17 millert 669: * input. once we have written a file block, we continue to write it to
670: * the end (we stop looking at the input). When we reach the start of the
671: * next file block, start checking for zero blocks again. Working on file
1.45 krw 672: * block boundaries significantly reduces the overhead when copying files
1.17 millert 673: * that are NOT very sparse. This overhead (when compared to a write) is
674: * almost below the measurement resolution on many systems. Without it,
675: * files with holes cannot be safely copied. It does has a side effect as
676: * it can put holes into files that did not have them before, but that is
677: * not a problem since the file contents are unchanged (in fact it saves
678: * file space). (Except on paging files for diskless clients. But since we
679: * cannot determine one of those file from here, we ignore them). If this
680: * ever ends up on a system where CTG files are supported and the holes
681: * are not desired, just do a conditional test in those routines that
682: * call file_write() and have it call write() instead. BEFORE CLOSING THE
683: * FILE, make sure to call file_flush() when the last write finishes with
684: * an empty block. A lot of file systems will not create an lseek hole at
685: * the end. In this case we drop a single 0 at the end to force the
686: * trailing 0's in the file.
687: * ---Parameters---
688: * rem: how many bytes left in this file system block
689: * isempt: have we written to the file block yet (is it empty)
690: * sz: basic file block allocation size
691: * cnt: number of bytes on this write
692: * str: buffer to write
693: * Return:
694: * number of bytes written, -1 on write (or lseek) error.
695: */
696:
697: int
1.35 deraadt 698: file_write(int fd, char *str, size_t cnt, int *rem, int *isempt, int sz)
1.17 millert 699: {
1.29 mpech 700: char *pt;
701: char *end;
702: size_t wcnt;
703: char *st = str;
1.17 millert 704:
705: /*
706: * while we have data to process
707: */
708: while (cnt) {
709: if (!*rem) {
710: /*
711: * We are now at the start of file system block again
712: * (or what we think one is...). start looking for
713: * empty blocks again
714: */
715: *isempt = 1;
716: *rem = sz;
717: }
718:
719: /*
720: * only examine up to the end of the current file block or
721: * remaining characters to write, whatever is smaller
722: */
723: wcnt = MIN(cnt, *rem);
724: cnt -= wcnt;
725: *rem -= wcnt;
726: if (*isempt) {
727: /*
728: * have not written to this block yet, so we keep
729: * looking for zero's
730: */
731: pt = st;
732: end = st + wcnt;
733:
734: /*
735: * look for a zero filled buffer
736: */
737: while ((pt < end) && (*pt == '\0'))
738: ++pt;
739:
740: if (pt == end) {
741: /*
742: * skip, buf is empty so far
743: */
744: if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
745: warn("lseek");
746: return(-1);
747: }
748: st = pt;
749: continue;
750: }
751: /*
752: * drat, the buf is not zero filled
753: */
754: *isempt = 0;
755: }
756:
757: /*
758: * have non-zero data in this file system block, have to write
759: */
760: if (write(fd, st, wcnt) != wcnt) {
761: warn("write");
762: return(-1);
763: }
764: st += wcnt;
765: }
766: return(st - str);
1.33 tedu 767: }
768:
769: /*
770: * file_flush()
771: * when the last file block in a file is zero, many file systems will not
772: * let us create a hole at the end. To get the last block with zeros, we
773: * write the last BYTE with a zero (back up one byte and write a zero).
774: */
775: void
776: file_flush(int fd, int isempt)
777: {
778: static char blnk[] = "\0";
779:
780: /*
781: * silly test, but make sure we are only called when the last block is
782: * filled with all zeros.
783: */
784: if (!isempt)
785: return;
786:
787: /*
788: * move back one byte and write a zero
789: */
790: if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
791: warn("Failed seek on file");
792: return;
793: }
794:
795: if (write(fd, blnk, 1) < 0)
796: warn("Failed write to file");
797: return;
1.1 deraadt 798: }