xinstall.c revision 1.60 1 /* $NetBSD: xinstall.c,v 1.60 2001/11/22 23:27:38 dillo Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
45 #else
46 __RCSID("$NetBSD: xinstall.c,v 1.60 2001/11/22 23:27:38 dillo Exp $");
47 #endif
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>
56 #include <err.h>
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>
66 #include <vis.h>
67
68 #include "pathnames.h"
69 #include "stat_flags.h"
70
71 #define STRIP_ARGS_MAX 32
72 #define BACKUP_SUFFIX ".old"
73
74 int dobackup, docopy, dodir, dostrip, dolink, dopreserve, dorename,
75 dounpriv;
76 int numberedbackup;
77 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
78 char pathbuf[MAXPATHLEN];
79 uid_t uid;
80 gid_t gid;
81 char *group, *owner, *fflags, *tags;
82 FILE *metafp;
83 char *metafile;
84 u_long fileflags;
85 char *stripArgs;
86 char *suffix = BACKUP_SUFFIX;
87
88 #define LN_ABSOLUTE 0x01
89 #define LN_RELATIVE 0x02
90 #define LN_HARD 0x04
91 #define LN_SYMBOLIC 0x08
92 #define LN_MIXED 0x10
93
94 #define DIRECTORY 0x01 /* Tell install it's a directory. */
95 #define SETFLAGS 0x02 /* Tell install to set flags. */
96 #define HASUID 0x04 /* Tell install the uid was given */
97 #define HASGID 0x08 /* Tell install the gid was given */
98
99 void backup(const char *);
100 void copy(int, char *, int, char *, off_t);
101 void install(char *, char *, u_int);
102 void install_dir(char *, u_int);
103 int main(int, char *[]);
104 void makelink(char *, char *);
105 int parseid(char *, id_t *);
106 void strip(char *);
107 void metadata_log(const char *, const char *, struct timeval *, const char *);
108 void usage(void);
109
110 int
111 main(int argc, char *argv[])
112 {
113 struct stat from_sb, to_sb;
114 void *set;
115 u_int iflags;
116 int ch, no_target;
117 char *p, *to_name;
118
119 setprogname(argv[0]);
120
121 iflags = 0;
122 while ((ch = getopt(argc, argv, "cbB:df:g:l:m:M:o:prsS:T:U")) != -1)
123 switch((char)ch) {
124 case 'B':
125 suffix = optarg;
126 numberedbackup = 0;
127 {
128 /* Check if given suffix really generates
129 different suffixes - catch e.g. ".%" */
130 char suffix_expanded0[FILENAME_MAX],
131 suffix_expanded1[FILENAME_MAX];
132 (void)snprintf(suffix_expanded0, FILENAME_MAX,
133 suffix, 0);
134 (void)snprintf(suffix_expanded1, FILENAME_MAX,
135 suffix, 1);
136 if (strcmp(suffix_expanded0, suffix_expanded1)
137 != 0)
138 numberedbackup = 1;
139 }
140 /* fall through; -B implies -b */
141 case 'b':
142 dobackup = 1;
143 break;
144 case 'c':
145 docopy = 1;
146 break;
147 case 'd':
148 dodir = 1;
149 break;
150 case 'f':
151 fflags = optarg;
152 break;
153 case 'g':
154 group = optarg;
155 break;
156 case 'l':
157 for (p = optarg; *p; p++)
158 switch (*p) {
159 case 's':
160 dolink &= ~(LN_HARD|LN_MIXED);
161 dolink |= LN_SYMBOLIC;
162 break;
163 case 'h':
164 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
165 dolink |= LN_HARD;
166 break;
167 case 'm':
168 dolink &= ~(LN_SYMBOLIC|LN_HARD);
169 dolink |= LN_MIXED;
170 break;
171 case 'a':
172 dolink &= ~LN_RELATIVE;
173 dolink |= LN_ABSOLUTE;
174 break;
175 case 'r':
176 dolink &= ~LN_ABSOLUTE;
177 dolink |= LN_RELATIVE;
178 break;
179 default:
180 errx(1, "%c: invalid link type", *p);
181 break;
182 }
183 break;
184 case 'm':
185 if (!(set = setmode(optarg)))
186 errx(1, "%s: invalid file mode", optarg);
187 mode = getmode(set, 0);
188 free(set);
189 break;
190 case 'M':
191 metafile = optarg;
192 break;
193 case 'o':
194 owner = optarg;
195 break;
196 case 'p':
197 dopreserve = 1;
198 break;
199 case 'r':
200 dorename = 1;
201 break;
202 case 'S':
203 stripArgs = strdup(optarg);
204 if (stripArgs == NULL)
205 errx(1, "%s", strerror(ENOMEM));
206 /* fall through; -S implies -s */
207 case 's':
208 dostrip = 1;
209 break;
210 case 'T':
211 tags = optarg;
212 break;
213 case 'U':
214 dounpriv = 1;
215 break;
216 case '?':
217 default:
218 usage();
219 }
220 argc -= optind;
221 argv += optind;
222
223 /* strip and link options make no sense when creating directories */
224 if ((dostrip || dolink) && dodir)
225 usage();
226
227 /* strip and flags make no sense with links */
228 if ((dostrip || fflags) && dolink)
229 usage();
230
231 /* must have at least two arguments, except when creating directories */
232 if (argc < 2 && !dodir)
233 usage();
234
235 /* get group and owner id's */
236 if (group && !dounpriv) {
237 struct group *gp;
238
239 if ((gp = getgrnam(group)) != NULL)
240 gid = gp->gr_gid;
241 else if (! parseid(group, &gid))
242 errx(1, "unknown group %s", group);
243 iflags |= HASGID;
244 }
245 if (owner && !dounpriv) {
246 struct passwd *pp;
247
248 if ((pp = getpwnam(owner)) != NULL)
249 uid = pp->pw_uid;
250 else if (! parseid(owner, &uid))
251 errx(1, "unknown user %s", owner);
252 iflags |= HASUID;
253 }
254
255 if (fflags && !dounpriv) {
256 if (string_to_flags(&fflags, &fileflags, NULL))
257 errx(1, "%s: invalid flag", fflags);
258 iflags |= SETFLAGS;
259 }
260
261 if (metafile) {
262 if ((metafp = fopen(metafile, "a")) == NULL)
263 warn("open %s", metafile);
264 }
265
266 if (dodir) {
267 for (; *argv != NULL; ++argv)
268 install_dir(*argv, iflags);
269 exit (0);
270 }
271
272 no_target = stat(to_name = argv[argc - 1], &to_sb);
273 if (!no_target && S_ISDIR(to_sb.st_mode)) {
274 for (; *argv != to_name; ++argv)
275 install(*argv, to_name, iflags | DIRECTORY);
276 exit(0);
277 }
278
279 /* can't do file1 file2 directory/file */
280 if (argc != 2)
281 usage();
282
283 if (!no_target) {
284 if (stat(*argv, &from_sb))
285 err(1, "%s", *argv);
286 if (!S_ISREG(to_sb.st_mode))
287 errx(1, "%s: not a regular file", to_name);
288 if (!dolink && to_sb.st_dev == from_sb.st_dev &&
289 to_sb.st_ino == from_sb.st_ino)
290 errx(1, "%s and %s are the same file", *argv, to_name);
291 /*
292 * Unlink now... avoid ETXTBSY errors later. Try and turn
293 * off the append/immutable bits -- if we fail, go ahead,
294 * it might work.
295 */
296 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
297 if (to_sb.st_flags & NOCHANGEBITS)
298 (void)chflags(to_name,
299 to_sb.st_flags & ~(NOCHANGEBITS));
300 if (dobackup)
301 backup(to_name);
302 else if (!dorename)
303 (void)unlink(to_name);
304 }
305 install(*argv, to_name, iflags);
306 exit(0);
307 }
308
309 /*
310 * parseid --
311 * parse uid or gid from arg into id, returning non-zero if successful
312 */
313 int
314 parseid(char *name, id_t *id)
315 {
316 char *ep;
317
318 errno = 0;
319 *id = (id_t)strtoul(name, &ep, 10);
320 if (errno || *ep != '\0')
321 return (0);
322 return (1);
323 }
324
325 /*
326 * makelink --
327 * make a link from source to destination
328 */
329 void
330 makelink(char *from_name, char *to_name)
331 {
332 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
333
334 /* Try hard links first */
335 if (dolink & (LN_HARD|LN_MIXED)) {
336 if (link(from_name, to_name) == -1) {
337 if ((dolink & LN_HARD) || errno != EXDEV)
338 err(1, "link %s -> %s", from_name, to_name);
339 }
340 else {
341 metadata_log(to_name, "hlink", NULL, from_name);
342 return;
343 }
344 }
345
346 /* Symbolic links */
347 if (dolink & LN_ABSOLUTE) {
348 /* Convert source path to absolute */
349 if (realpath(from_name, src) == NULL)
350 err(1, "%s", src);
351 if (symlink(src, to_name) == -1)
352 err(1, "symlink %s -> %s", src, to_name);
353 metadata_log(to_name, "link", NULL, src);
354 return;
355 }
356
357 if (dolink & LN_RELATIVE) {
358 char *s, *d;
359
360 /* Resolve pathnames */
361 if (realpath(from_name, src) == NULL)
362 err(1, "%s", src);
363 if (realpath(to_name, dst) == NULL)
364 err(1, "%s", dst);
365
366 /* trim common path components */
367 for (s = src, d = dst; *s == *d; s++, d++)
368 continue;
369 while (*s != '/')
370 s--, d--;
371
372 /* count the number of directories we need to backtrack */
373 for (++d, lnk[0] = '\0'; *d; d++)
374 if (*d == '/')
375 (void) strcat(lnk, "../");
376
377 (void) strcat(lnk, ++s);
378
379 if (symlink(lnk, dst) == -1)
380 err(1, "symlink %s -> %s", lnk, dst);
381 metadata_log(dst, "link", NULL, lnk);
382 return;
383 }
384
385 /*
386 * If absolute or relative was not specified,
387 * try the names the user provided
388 */
389 if (symlink(from_name, to_name) == -1)
390 err(1, "symlink %s -> %s", from_name, to_name);
391 metadata_log(to_name, "link", NULL, from_name);
392 }
393
394 /*
395 * install --
396 * build a path name and install the file
397 */
398 void
399 install(char *from_name, char *to_name, u_int flags)
400 {
401 struct stat from_sb, to_sb;
402 struct timeval tv[2];
403 int devnull, from_fd, to_fd, serrno, tmpmode;
404 char *p, tmpl[MAXPATHLEN], *oto_name;
405
406 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
407 if (!dolink) {
408 if (stat(from_name, &from_sb))
409 err(1, "%s", from_name);
410 if (!S_ISREG(from_sb.st_mode))
411 errx(1, "%s: not a regular file", from_name);
412 }
413 /* Build the target path. */
414 if (flags & DIRECTORY) {
415 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
416 to_name,
417 (p = strrchr(from_name, '/')) ? ++p : from_name);
418 to_name = pathbuf;
419 }
420 devnull = 0;
421 } else {
422 from_sb.st_flags = 0; /* XXX */
423 devnull = 1;
424 }
425
426 /*
427 * Unlink now... avoid ETXTBSY errors later. Try and turn
428 * off the append/immutable bits -- if we fail, go ahead,
429 * it might work.
430 */
431 if (stat(to_name, &to_sb) == 0 &&
432 to_sb.st_flags & (NOCHANGEBITS))
433 (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
434 if (dorename) {
435 char *ptr, c, *dir;
436
437 if ((ptr = strrchr(to_name, '/')) != NULL) {
438 c = *++ptr;
439 *ptr = '\0';
440 dir = to_name;
441 } else {
442 c = '\0'; /* pacify gcc */
443 dir = tmpl;
444 *dir = '\0';
445 }
446 (void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
447 if (ptr)
448 *ptr = c;
449 oto_name = to_name;
450 to_name = tmpl;
451
452 } else {
453 oto_name = NULL; /* pacify gcc */
454 if (dobackup)
455 backup(to_name);
456 else
457 (void)unlink(to_name);
458 }
459
460 if (dolink) {
461 makelink(from_name, to_name);
462 return;
463 }
464
465 /* Create target. */
466 if (dorename) {
467 if ((to_fd = mkstemp(to_name)) == -1)
468 err(1, "%s", to_name);
469 } else {
470 if ((to_fd = open(to_name,
471 O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
472 err(1, "%s", to_name);
473 }
474 if (!devnull) {
475 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
476 (void)unlink(to_name);
477 err(1, "%s", from_name);
478 }
479 copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
480 (void)close(from_fd);
481 }
482
483 if (dostrip) {
484 strip(to_name);
485
486 /*
487 * Re-open our fd on the target, in case we used a strip
488 * that does not work in-place -- like gnu binutils strip.
489 */
490 close(to_fd);
491 if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
492 err(1, "stripping %s", to_name);
493 }
494
495 /*
496 * Set owner, group, mode for target; do the chown first,
497 * chown may lose the setuid bits.
498 */
499 if (!dounpriv &&
500 (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
501 serrno = errno;
502 (void)unlink(to_name);
503 errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
504 }
505 tmpmode = mode;
506 if (dounpriv)
507 tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
508 if (fchmod(to_fd, tmpmode) == -1) {
509 serrno = errno;
510 (void)unlink(to_name);
511 errx(1, "%s: chmod: %s", to_name, strerror(serrno));
512 }
513
514 /*
515 * Preserve the date of the source file.
516 */
517 if (dopreserve) {
518 #ifdef BSD4_4
519 TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
520 TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
521 #else
522 tv[0].tv_sec = from_sb.st_atime;
523 tv[0].tv_usec = 0;
524 tv[1].tv_sec = from_sb.st_mtime;
525 tv[1].tv_usec = 0;
526 #endif
527 if (!dounpriv && futimes(to_fd, tv) == -1)
528 warn("%s: futimes", to_name);
529 }
530
531 (void)close(to_fd);
532
533 if (dorename) {
534 if (rename(to_name, oto_name) == -1)
535 err(1, "%s: rename", to_name);
536 to_name = oto_name;
537 }
538
539 if (!docopy && !devnull && unlink(from_name))
540 err(1, "%s", from_name);
541
542 /*
543 * If provided a set of flags, set them, otherwise, preserve the
544 * flags, except for the dump flag.
545 */
546 if (!dounpriv && chflags(to_name,
547 flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
548 {
549 if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
550 warn("%s: chflags", to_name);
551 }
552
553 metadata_log(to_name, "file", tv, NULL);
554 }
555
556 /*
557 * copy --
558 * copy from one file to another
559 */
560 void
561 copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
562 {
563 ssize_t nr, nw;
564 int serrno;
565 char *p;
566 char buf[MAXBSIZE];
567
568 /*
569 * There's no reason to do anything other than close the file
570 * now if it's empty, so let's not bother.
571 */
572 if (size > 0) {
573
574 /*
575 * Mmap and write if less than 8M (the limit is so we
576 * don't totally trash memory on big files). This is
577 * really a minor hack, but it wins some CPU back.
578 */
579
580 if (size <= 8 * 1048576) {
581 if ((p = mmap(NULL, (size_t)size, PROT_READ,
582 MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
583 == MAP_FAILED) {
584 goto mmap_failed;
585 }
586 #ifdef MADV_SEQUENTIAL
587 if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
588 && errno != EOPNOTSUPP)
589 warnx("madvise: %s", strerror(errno));
590 #endif
591
592 if (write(to_fd, p, size) != size) {
593 serrno = errno;
594 (void)unlink(to_name);
595 errx(1, "%s: %s",
596 to_name, strerror(serrno));
597 }
598 } else {
599 mmap_failed:
600 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
601 if ((nw = write(to_fd, buf, nr)) != nr) {
602 serrno = errno;
603 (void)unlink(to_name);
604 errx(1, "%s: %s", to_name,
605 strerror(nw > 0 ? EIO : serrno));
606 }
607 }
608 if (nr != 0) {
609 serrno = errno;
610 (void)unlink(to_name);
611 errx(1, "%s: %s", from_name, strerror(serrno));
612 }
613 }
614 }
615 }
616
617 /*
618 * strip --
619 * use strip(1) to strip the target file
620 */
621 void
622 strip(char *to_name)
623 {
624 int serrno, status;
625 char *stripprog;
626
627 switch (vfork()) {
628 case -1:
629 serrno = errno;
630 (void)unlink(to_name);
631 errx(1, "vfork: %s", strerror(serrno));
632 case 0:
633 stripprog = getenv("STRIP");
634 if (stripprog == NULL)
635 stripprog = _PATH_STRIP;
636
637 if (stripArgs) {
638 /*
639 * build up a command line and let /bin/sh
640 * parse the arguments
641 */
642 char* cmd = (char*)malloc(sizeof(char)*
643 (3+strlen(stripprog)+
644 strlen(stripArgs)+
645 strlen(to_name)));
646
647 if (cmd == NULL)
648 errx(1, "%s", strerror(ENOMEM));
649
650 sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
651
652 execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
653 } else
654 execlp(stripprog, "strip", to_name, NULL);
655
656 warn("%s", stripprog);
657 _exit(1);
658 default:
659 if (wait(&status) == -1 || status)
660 (void)unlink(to_name);
661 }
662 }
663
664 /*
665 * backup file "to_name" to to_name.suffix
666 * if suffix contains a "%", it's taken as a printf(3) pattern
667 * used for a numbered backup.
668 */
669 void
670 backup(const char *to_name)
671 {
672 char backup[FILENAME_MAX];
673
674 if (numberedbackup) {
675 /* Do numbered backup */
676 int cnt;
677 char suffix_expanded[FILENAME_MAX];
678
679 cnt=0;
680 do {
681 (void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
682 cnt);
683 (void)snprintf(backup, FILENAME_MAX, "%s%s",
684 to_name, suffix_expanded);
685 cnt++;
686 } while (access(backup, F_OK)==0);
687 } else {
688 /* Do simple backup */
689 (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
690 }
691
692 (void)rename(to_name, backup);
693 }
694
695 /*
696 * install_dir --
697 * build directory hierarchy
698 */
699 void
700 install_dir(char *path, u_int flags)
701 {
702 char *p;
703 struct stat sb;
704 int ch;
705
706 for (p = path;; ++p)
707 if (!*p || (p != path && *p == '/')) {
708 ch = *p;
709 *p = '\0';
710 if (stat(path, &sb)) {
711 if (errno != ENOENT || mkdir(path, 0777) < 0) {
712 err(1, "%s", path);
713 /* NOTREACHED */
714 }
715 }
716 if (!(*p = ch))
717 break;
718 }
719
720 if (!dounpriv && (
721 ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
722 || chmod(path, mode) == -1 )) {
723 warn("%s", path);
724 }
725 metadata_log(path, "dir", NULL, NULL);
726 }
727
728 /*
729 * metadata_log --
730 * if metafp is not NULL, output mtree(8) full path name and settings to
731 * metafp, to allow permissions to be set correctly by other tools.
732 */
733 void
734 metadata_log(const char *path, const char *type, struct timeval *tv,
735 const char *link)
736 {
737 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
738 char *buf;
739
740 if (!metafp)
741 return;
742 buf = (char *)malloc(4 * strlen(path) + 1); /* buf for strsvis(3) */
743 if (buf == NULL) {
744 warnx("%s", strerror(ENOMEM));
745 return;
746 }
747 if (flock(fileno(metafp), LOCK_EX) == -1) { /* lock log file */
748 warn("can't lock %s", metafile);
749 return;
750 }
751
752 strsvis(buf, path, VIS_CSTYLE, extra); /* encode name */
753 fprintf(metafp, ".%s%s type=%s mode=%#o", /* print details */
754 buf[0] == '/' ? "" : "/", buf, type, mode);
755 if (link)
756 fprintf(metafp, " link=%s", link);
757 if (owner)
758 fprintf(metafp, " uname=%s", owner);
759 if (group)
760 fprintf(metafp, " gname=%s", group);
761 if (fflags)
762 fprintf(metafp, " flags=%s", fflags);
763 if (tags)
764 fprintf(metafp, " tags=%s", tags);
765 if (tv != NULL && dopreserve)
766 fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
767 fputc('\n', metafp);
768 fflush(metafp); /* flush output */
769 if (flock(fileno(metafp), LOCK_UN) == -1) { /* unlock log file */
770 warn("can't unlock %s", metafile);
771 }
772 free(buf);
773 }
774
775
776
777 /*
778 * usage --
779 * print a usage message and die
780 */
781 void
782 usage(void)
783 {
784
785 (void)fprintf(stderr, "\
786 usage: install [-Ubcprs] [-M log] [-T tags] [-B suffix] [-f flags] [-m mode]\n\
787 [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
788 install [-Ubcprs] [-M log] [-T tags] [-B suffix] [-f flags] [-m mode]\n\
789 [-o owner] [-g group] [-l linkflags] [-S stripflags]\n\
790 file1 ... fileN directory\n\
791 install [-Up] [-M log] [-T tags] -d [-m mode]\n\
792 [-o owner] [-g group] directory ...\n");
793 exit(1);
794 }
795