xinstall.c revision 1.59 1 /* $NetBSD: xinstall.c,v 1.59 2001/11/19 02:51:00 perry 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.59 2001/11/19 02:51:00 perry 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;
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 if (dounpriv)
506 mode &= S_IRWXU|S_IRWXG|S_IRWXO;
507 if (fchmod(to_fd, mode) == -1) {
508 serrno = errno;
509 (void)unlink(to_name);
510 errx(1, "%s: chmod: %s", to_name, strerror(serrno));
511 }
512
513 /*
514 * Preserve the date of the source file.
515 */
516 if (dopreserve) {
517 #ifdef BSD4_4
518 TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
519 TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
520 #else
521 tv[0].tv_sec = from_sb.st_atime;
522 tv[0].tv_usec = 0;
523 tv[1].tv_sec = from_sb.st_mtime;
524 tv[1].tv_usec = 0;
525 #endif
526 if (!dounpriv && futimes(to_fd, tv) == -1)
527 warn("%s: futimes", to_name);
528 }
529
530 (void)close(to_fd);
531
532 if (dorename) {
533 if (rename(to_name, oto_name) == -1)
534 err(1, "%s: rename", to_name);
535 to_name = oto_name;
536 }
537
538 if (!docopy && !devnull && unlink(from_name))
539 err(1, "%s", from_name);
540
541 /*
542 * If provided a set of flags, set them, otherwise, preserve the
543 * flags, except for the dump flag.
544 */
545 if (!dounpriv && chflags(to_name,
546 flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
547 {
548 if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
549 warn("%s: chflags", to_name);
550 }
551
552 metadata_log(to_name, "file", tv, NULL);
553 }
554
555 /*
556 * copy --
557 * copy from one file to another
558 */
559 void
560 copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
561 {
562 ssize_t nr, nw;
563 int serrno;
564 char *p;
565 char buf[MAXBSIZE];
566
567 /*
568 * There's no reason to do anything other than close the file
569 * now if it's empty, so let's not bother.
570 */
571 if (size > 0) {
572
573 /*
574 * Mmap and write if less than 8M (the limit is so we
575 * don't totally trash memory on big files). This is
576 * really a minor hack, but it wins some CPU back.
577 */
578
579 if (size <= 8 * 1048576) {
580 if ((p = mmap(NULL, (size_t)size, PROT_READ,
581 MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
582 == MAP_FAILED) {
583 goto mmap_failed;
584 }
585 #ifdef MADV_SEQUENTIAL
586 if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
587 && errno != EOPNOTSUPP)
588 warnx("madvise: %s", strerror(errno));
589 #endif
590
591 if (write(to_fd, p, size) != size) {
592 serrno = errno;
593 (void)unlink(to_name);
594 errx(1, "%s: %s",
595 to_name, strerror(serrno));
596 }
597 } else {
598 mmap_failed:
599 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
600 if ((nw = write(to_fd, buf, nr)) != nr) {
601 serrno = errno;
602 (void)unlink(to_name);
603 errx(1, "%s: %s", to_name,
604 strerror(nw > 0 ? EIO : serrno));
605 }
606 }
607 if (nr != 0) {
608 serrno = errno;
609 (void)unlink(to_name);
610 errx(1, "%s: %s", from_name, strerror(serrno));
611 }
612 }
613 }
614 }
615
616 /*
617 * strip --
618 * use strip(1) to strip the target file
619 */
620 void
621 strip(char *to_name)
622 {
623 int serrno, status;
624 char *stripprog;
625
626 switch (vfork()) {
627 case -1:
628 serrno = errno;
629 (void)unlink(to_name);
630 errx(1, "vfork: %s", strerror(serrno));
631 case 0:
632 stripprog = getenv("STRIP");
633 if (stripprog == NULL)
634 stripprog = _PATH_STRIP;
635
636 if (stripArgs) {
637 /*
638 * build up a command line and let /bin/sh
639 * parse the arguments
640 */
641 char* cmd = (char*)malloc(sizeof(char)*
642 (3+strlen(stripprog)+
643 strlen(stripArgs)+
644 strlen(to_name)));
645
646 if (cmd == NULL)
647 errx(1, "%s", strerror(ENOMEM));
648
649 sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
650
651 execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
652 } else
653 execlp(stripprog, "strip", to_name, NULL);
654
655 warn("%s", stripprog);
656 _exit(1);
657 default:
658 if (wait(&status) == -1 || status)
659 (void)unlink(to_name);
660 }
661 }
662
663 /*
664 * backup file "to_name" to to_name.suffix
665 * if suffix contains a "%", it's taken as a printf(3) pattern
666 * used for a numbered backup.
667 */
668 void
669 backup(const char *to_name)
670 {
671 char backup[FILENAME_MAX];
672
673 if (numberedbackup) {
674 /* Do numbered backup */
675 int cnt;
676 char suffix_expanded[FILENAME_MAX];
677
678 cnt=0;
679 do {
680 (void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
681 cnt);
682 (void)snprintf(backup, FILENAME_MAX, "%s%s",
683 to_name, suffix_expanded);
684 cnt++;
685 } while (access(backup, F_OK)==0);
686 } else {
687 /* Do simple backup */
688 (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
689 }
690
691 (void)rename(to_name, backup);
692 }
693
694 /*
695 * install_dir --
696 * build directory hierarchy
697 */
698 void
699 install_dir(char *path, u_int flags)
700 {
701 char *p;
702 struct stat sb;
703 int ch;
704
705 for (p = path;; ++p)
706 if (!*p || (p != path && *p == '/')) {
707 ch = *p;
708 *p = '\0';
709 if (stat(path, &sb)) {
710 if (errno != ENOENT || mkdir(path, 0777) < 0) {
711 err(1, "%s", path);
712 /* NOTREACHED */
713 }
714 }
715 if (!(*p = ch))
716 break;
717 }
718
719 if (!dounpriv && (
720 ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
721 || chmod(path, mode) == -1 )) {
722 warn("%s", path);
723 }
724 metadata_log(path, "dir", NULL, NULL);
725 }
726
727 /*
728 * metadata_log --
729 * if metafp is not NULL, output mtree(8) full path name and settings to
730 * metafp, to allow permissions to be set correctly by other tools.
731 */
732 void
733 metadata_log(const char *path, const char *type, struct timeval *tv,
734 const char *link)
735 {
736 const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
737 char *buf;
738
739 if (!metafp)
740 return;
741 buf = (char *)malloc(4 * strlen(path) + 1); /* buf for strsvis(3) */
742 if (buf == NULL) {
743 warnx("%s", strerror(ENOMEM));
744 return;
745 }
746 if (flock(fileno(metafp), LOCK_EX) == -1) { /* lock log file */
747 warn("can't lock %s", metafile);
748 return;
749 }
750
751 strsvis(buf, path, VIS_CSTYLE, extra); /* encode name */
752 fprintf(metafp, ".%s%s type=%s mode=%#o", /* print details */
753 buf[0] == '/' ? "" : "/", buf, type, mode);
754 if (link)
755 fprintf(metafp, " link=%s", link);
756 if (owner)
757 fprintf(metafp, " uname=%s", owner);
758 if (group)
759 fprintf(metafp, " gname=%s", group);
760 if (fflags)
761 fprintf(metafp, " flags=%s", fflags);
762 if (tags)
763 fprintf(metafp, " tags=%s", tags);
764 if (tv != NULL && dopreserve)
765 fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
766 fputc('\n', metafp);
767 fflush(metafp); /* flush output */
768 if (flock(fileno(metafp), LOCK_UN) == -1) { /* unlock log file */
769 warn("can't unlock %s", metafile);
770 }
771 free(buf);
772 }
773
774
775
776 /*
777 * usage --
778 * print a usage message and die
779 */
780 void
781 usage(void)
782 {
783
784 (void)fprintf(stderr, "\
785 usage: install [-Ubcprs] [-M log] [-T tags] [-B suffix] [-f flags] [-m mode]\n\
786 [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
787 install [-Ubcprs] [-M log] [-T tags] [-B suffix] [-f flags] [-m mode]\n\
788 [-o owner] [-g group] [-l linkflags] [-S stripflags]\n\
789 file1 ... fileN directory\n\
790 install [-Up] [-M log] [-T tags] -d [-m mode]\n\
791 [-o owner] [-g group] directory ...\n");
792 exit(1);
793 }
794