xinstall.c revision 1.90 1 1.90 lukem /* $NetBSD: xinstall.c,v 1.90 2005/05/10 01:24:42 lukem Exp $ */
2 1.5 jtc
3 1.1 cgd /*
4 1.5 jtc * Copyright (c) 1987, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.79 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.80 lukem #if HAVE_NBTOOL_CONFIG_H
33 1.80 lukem #include "nbtool_config.h"
34 1.65 tv #else
35 1.65 tv #define HAVE_FUTIMES 1
36 1.65 tv #define HAVE_STRUCT_STAT_ST_FLAGS 1
37 1.65 tv #endif
38 1.65 tv
39 1.20 mrg #include <sys/cdefs.h>
40 1.67 tv #if defined(__COPYRIGHT) && !defined(lint)
41 1.20 mrg __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
42 1.20 mrg The Regents of the University of California. All rights reserved.\n");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.67 tv #if defined(__RCSID) && !defined(lint)
46 1.5 jtc #if 0
47 1.5 jtc static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
48 1.20 mrg #else
49 1.90 lukem __RCSID("$NetBSD: xinstall.c,v 1.90 2005/05/10 01:24:42 lukem Exp $");
50 1.5 jtc #endif
51 1.1 cgd #endif /* not lint */
52 1.1 cgd
53 1.1 cgd #include <sys/param.h>
54 1.5 jtc #include <sys/mman.h>
55 1.1 cgd #include <sys/stat.h>
56 1.61 simonb #include <sys/wait.h>
57 1.5 jtc
58 1.5 jtc #include <ctype.h>
59 1.67 tv #include <err.h>
60 1.5 jtc #include <errno.h>
61 1.5 jtc #include <fcntl.h>
62 1.1 cgd #include <grp.h>
63 1.67 tv #include <libgen.h>
64 1.5 jtc #include <paths.h>
65 1.1 cgd #include <pwd.h>
66 1.1 cgd #include <stdio.h>
67 1.5 jtc #include <stdlib.h>
68 1.5 jtc #include <string.h>
69 1.5 jtc #include <unistd.h>
70 1.50 lukem #include <vis.h>
71 1.5 jtc
72 1.84 lukem #include <md5.h>
73 1.84 lukem #include <rmd160.h>
74 1.84 lukem #include <sha1.h>
75 1.84 lukem
76 1.1 cgd #include "pathnames.h"
77 1.29 mrg #include "stat_flags.h"
78 1.74 lukem #include "mtree.h"
79 1.1 cgd
80 1.28 wsanchez #define STRIP_ARGS_MAX 32
81 1.31 hubertf #define BACKUP_SUFFIX ".old"
82 1.28 wsanchez
83 1.83 simonb int dobackup, dodir, dostrip, dolink, dopreserve, dorename, dounpriv;
84 1.50 lukem int numberedbackup;
85 1.50 lukem int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
86 1.50 lukem char pathbuf[MAXPATHLEN];
87 1.82 heas id_t uid = -1, gid = -1;
88 1.54 lukem char *group, *owner, *fflags, *tags;
89 1.50 lukem FILE *metafp;
90 1.50 lukem char *metafile;
91 1.50 lukem u_long fileflags;
92 1.50 lukem char *stripArgs;
93 1.69 lukem char *afterinstallcmd;
94 1.50 lukem char *suffix = BACKUP_SUFFIX;
95 1.75 lukem char *destdir;
96 1.5 jtc
97 1.84 lukem enum {
98 1.84 lukem DIGEST_NONE = 0,
99 1.84 lukem DIGEST_MD5,
100 1.84 lukem DIGEST_RMD160,
101 1.84 lukem DIGEST_SHA1,
102 1.84 lukem } digesttype = DIGEST_NONE;
103 1.84 lukem char *digest;
104 1.84 lukem
105 1.16 christos #define LN_ABSOLUTE 0x01
106 1.16 christos #define LN_RELATIVE 0x02
107 1.16 christos #define LN_HARD 0x04
108 1.16 christos #define LN_SYMBOLIC 0x08
109 1.16 christos #define LN_MIXED 0x10
110 1.16 christos
111 1.5 jtc #define DIRECTORY 0x01 /* Tell install it's a directory. */
112 1.5 jtc #define SETFLAGS 0x02 /* Tell install to set flags. */
113 1.50 lukem #define HASUID 0x04 /* Tell install the uid was given */
114 1.50 lukem #define HASGID 0x08 /* Tell install the gid was given */
115 1.5 jtc
116 1.69 lukem void afterinstall(const char *, const char *, int);
117 1.50 lukem void backup(const char *);
118 1.84 lukem char *copy(int, char *, int, char *, off_t);
119 1.61 simonb int do_link(char *, char *);
120 1.61 simonb void do_symlink(char *, char *);
121 1.50 lukem void install(char *, char *, u_int);
122 1.50 lukem void install_dir(char *, u_int);
123 1.50 lukem int main(int, char *[]);
124 1.48 simonb void makelink(char *, char *);
125 1.84 lukem void metadata_log(const char *, const char *, struct timeval *,
126 1.84 lukem const char *, const char *);
127 1.50 lukem int parseid(char *, id_t *);
128 1.48 simonb void strip(char *);
129 1.48 simonb void usage(void);
130 1.61 simonb char *xbasename(char *);
131 1.61 simonb char *xdirname(char *);
132 1.1 cgd
133 1.5 jtc int
134 1.48 simonb main(int argc, char *argv[])
135 1.1 cgd {
136 1.53 lukem struct stat from_sb, to_sb;
137 1.53 lukem void *set;
138 1.53 lukem u_int iflags;
139 1.53 lukem int ch, no_target;
140 1.53 lukem char *p, *to_name;
141 1.43 cgd
142 1.43 cgd setprogname(argv[0]);
143 1.1 cgd
144 1.5 jtc iflags = 0;
145 1.84 lukem while ((ch = getopt(argc, argv, "a:cbB:dD:f:g:h:l:m:M:N:o:prsS:T:U"))
146 1.75 lukem != -1)
147 1.1 cgd switch((char)ch) {
148 1.69 lukem case 'a':
149 1.69 lukem afterinstallcmd = strdup(optarg);
150 1.69 lukem if (afterinstallcmd == NULL)
151 1.69 lukem errx(1, "%s", strerror(ENOMEM));
152 1.69 lukem break;
153 1.31 hubertf case 'B':
154 1.31 hubertf suffix = optarg;
155 1.35 hubertf numberedbackup = 0;
156 1.35 hubertf {
157 1.35 hubertf /* Check if given suffix really generates
158 1.35 hubertf different suffixes - catch e.g. ".%" */
159 1.35 hubertf char suffix_expanded0[FILENAME_MAX],
160 1.35 hubertf suffix_expanded1[FILENAME_MAX];
161 1.35 hubertf (void)snprintf(suffix_expanded0, FILENAME_MAX,
162 1.35 hubertf suffix, 0);
163 1.35 hubertf (void)snprintf(suffix_expanded1, FILENAME_MAX,
164 1.35 hubertf suffix, 1);
165 1.35 hubertf if (strcmp(suffix_expanded0, suffix_expanded1)
166 1.35 hubertf != 0)
167 1.35 hubertf numberedbackup = 1;
168 1.35 hubertf }
169 1.31 hubertf /* fall through; -B implies -b */
170 1.61 simonb /*FALLTHROUGH*/
171 1.31 hubertf case 'b':
172 1.31 hubertf dobackup = 1;
173 1.31 hubertf break;
174 1.1 cgd case 'c':
175 1.83 simonb /* ignored; was "docopy" which is now the default. */
176 1.1 cgd break;
177 1.26 christos case 'd':
178 1.26 christos dodir = 1;
179 1.26 christos break;
180 1.75 lukem case 'D':
181 1.75 lukem destdir = optarg;
182 1.75 lukem break;
183 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
184 1.5 jtc case 'f':
185 1.52 tv fflags = optarg;
186 1.5 jtc break;
187 1.65 tv #endif
188 1.1 cgd case 'g':
189 1.1 cgd group = optarg;
190 1.1 cgd break;
191 1.84 lukem case 'h':
192 1.84 lukem digest = optarg;
193 1.84 lukem break;
194 1.16 christos case 'l':
195 1.16 christos for (p = optarg; *p; p++)
196 1.16 christos switch (*p) {
197 1.16 christos case 's':
198 1.16 christos dolink &= ~(LN_HARD|LN_MIXED);
199 1.16 christos dolink |= LN_SYMBOLIC;
200 1.16 christos break;
201 1.16 christos case 'h':
202 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_MIXED);
203 1.16 christos dolink |= LN_HARD;
204 1.16 christos break;
205 1.16 christos case 'm':
206 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_HARD);
207 1.16 christos dolink |= LN_MIXED;
208 1.16 christos break;
209 1.16 christos case 'a':
210 1.16 christos dolink &= ~LN_RELATIVE;
211 1.16 christos dolink |= LN_ABSOLUTE;
212 1.16 christos break;
213 1.16 christos case 'r':
214 1.16 christos dolink &= ~LN_ABSOLUTE;
215 1.16 christos dolink |= LN_RELATIVE;
216 1.16 christos break;
217 1.16 christos default:
218 1.16 christos errx(1, "%c: invalid link type", *p);
219 1.61 simonb /* NOTREACHED */
220 1.16 christos }
221 1.16 christos break;
222 1.26 christos case 'm':
223 1.26 christos if (!(set = setmode(optarg)))
224 1.26 christos errx(1, "%s: invalid file mode", optarg);
225 1.26 christos mode = getmode(set, 0);
226 1.42 enami free(set);
227 1.26 christos break;
228 1.50 lukem case 'M':
229 1.50 lukem metafile = optarg;
230 1.50 lukem break;
231 1.74 lukem case 'N':
232 1.74 lukem if (! setup_getid(optarg))
233 1.74 lukem errx(1,
234 1.74 lukem "Unable to use user and group databases in `%s'",
235 1.74 lukem optarg);
236 1.74 lukem break;
237 1.26 christos case 'o':
238 1.26 christos owner = optarg;
239 1.26 christos break;
240 1.26 christos case 'p':
241 1.26 christos dopreserve = 1;
242 1.26 christos break;
243 1.33 christos case 'r':
244 1.33 christos dorename = 1;
245 1.33 christos break;
246 1.28 wsanchez case 'S':
247 1.49 simonb stripArgs = strdup(optarg);
248 1.49 simonb if (stripArgs == NULL)
249 1.49 simonb errx(1, "%s", strerror(ENOMEM));
250 1.28 wsanchez /* fall through; -S implies -s */
251 1.61 simonb /*FALLTHROUGH*/
252 1.26 christos case 's':
253 1.26 christos dostrip = 1;
254 1.26 christos break;
255 1.54 lukem case 'T':
256 1.54 lukem tags = optarg;
257 1.54 lukem break;
258 1.38 sommerfe case 'U':
259 1.50 lukem dounpriv = 1;
260 1.38 sommerfe break;
261 1.1 cgd case '?':
262 1.1 cgd default:
263 1.1 cgd usage();
264 1.1 cgd }
265 1.1 cgd argc -= optind;
266 1.1 cgd argv += optind;
267 1.2 jtc
268 1.23 tv /* strip and link options make no sense when creating directories */
269 1.23 tv if ((dostrip || dolink) && dodir)
270 1.16 christos usage();
271 1.16 christos
272 1.16 christos /* strip and flags make no sense with links */
273 1.52 tv if ((dostrip || fflags) && dolink)
274 1.2 jtc usage();
275 1.2 jtc
276 1.2 jtc /* must have at least two arguments, except when creating directories */
277 1.2 jtc if (argc < 2 && !dodir)
278 1.1 cgd usage();
279 1.1 cgd
280 1.84 lukem if (digest) {
281 1.84 lukem if (0) {
282 1.84 lukem } else if (strcmp(digest, "none") == 0) {
283 1.84 lukem digesttype = DIGEST_NONE;
284 1.84 lukem } else if (strcmp(digest, "md5") == 0) {
285 1.84 lukem digesttype = DIGEST_MD5;
286 1.84 lukem } else if (strcmp(digest, "rmd160") == 0) {
287 1.84 lukem digesttype = DIGEST_RMD160;
288 1.84 lukem } else if (strcmp(digest, "sha1") == 0) {
289 1.84 lukem digesttype = DIGEST_SHA1;
290 1.84 lukem } else {
291 1.84 lukem warnx("unknown digest `%s'", digest);
292 1.84 lukem usage();
293 1.84 lukem }
294 1.84 lukem }
295 1.84 lukem
296 1.1 cgd /* get group and owner id's */
297 1.52 tv if (group && !dounpriv) {
298 1.74 lukem if (gid_from_group(group, &gid) == -1 && ! parseid(group, &gid))
299 1.41 cgd errx(1, "unknown group %s", group);
300 1.50 lukem iflags |= HASGID;
301 1.50 lukem }
302 1.52 tv if (owner && !dounpriv) {
303 1.74 lukem if (uid_from_user(owner, &uid) == -1 && ! parseid(owner, &uid))
304 1.41 cgd errx(1, "unknown user %s", owner);
305 1.50 lukem iflags |= HASUID;
306 1.50 lukem }
307 1.50 lukem
308 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
309 1.52 tv if (fflags && !dounpriv) {
310 1.52 tv if (string_to_flags(&fflags, &fileflags, NULL))
311 1.52 tv errx(1, "%s: invalid flag", fflags);
312 1.52 tv iflags |= SETFLAGS;
313 1.52 tv }
314 1.65 tv #endif
315 1.52 tv
316 1.50 lukem if (metafile) {
317 1.50 lukem if ((metafp = fopen(metafile, "a")) == NULL)
318 1.50 lukem warn("open %s", metafile);
319 1.84 lukem } else
320 1.84 lukem digesttype = DIGEST_NONE;
321 1.1 cgd
322 1.2 jtc if (dodir) {
323 1.2 jtc for (; *argv != NULL; ++argv)
324 1.50 lukem install_dir(*argv, iflags);
325 1.2 jtc exit (0);
326 1.5 jtc }
327 1.2 jtc
328 1.1 cgd no_target = stat(to_name = argv[argc - 1], &to_sb);
329 1.2 jtc if (!no_target && S_ISDIR(to_sb.st_mode)) {
330 1.1 cgd for (; *argv != to_name; ++argv)
331 1.50 lukem install(*argv, to_name, iflags | DIRECTORY);
332 1.1 cgd exit(0);
333 1.1 cgd }
334 1.1 cgd
335 1.1 cgd /* can't do file1 file2 directory/file */
336 1.1 cgd if (argc != 2)
337 1.1 cgd usage();
338 1.1 cgd
339 1.1 cgd if (!no_target) {
340 1.61 simonb /* makelink() handles checks for links */
341 1.61 simonb if (!dolink) {
342 1.61 simonb if (stat(*argv, &from_sb))
343 1.66 tv err(1, "%s: stat", *argv);
344 1.61 simonb if (!S_ISREG(to_sb.st_mode))
345 1.61 simonb errx(1, "%s: not a regular file", to_name);
346 1.61 simonb if (to_sb.st_dev == from_sb.st_dev &&
347 1.61 simonb to_sb.st_ino == from_sb.st_ino)
348 1.61 simonb errx(1, "%s and %s are the same file", *argv,
349 1.61 simonb to_name);
350 1.61 simonb }
351 1.5 jtc /*
352 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
353 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
354 1.5 jtc * it might work.
355 1.5 jtc */
356 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
357 1.5 jtc #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
358 1.5 jtc if (to_sb.st_flags & NOCHANGEBITS)
359 1.5 jtc (void)chflags(to_name,
360 1.5 jtc to_sb.st_flags & ~(NOCHANGEBITS));
361 1.65 tv #endif
362 1.32 hubertf if (dobackup)
363 1.32 hubertf backup(to_name);
364 1.34 christos else if (!dorename)
365 1.31 hubertf (void)unlink(to_name);
366 1.1 cgd }
367 1.50 lukem install(*argv, to_name, iflags);
368 1.1 cgd exit(0);
369 1.1 cgd }
370 1.1 cgd
371 1.1 cgd /*
372 1.50 lukem * parseid --
373 1.50 lukem * parse uid or gid from arg into id, returning non-zero if successful
374 1.50 lukem */
375 1.50 lukem int
376 1.50 lukem parseid(char *name, id_t *id)
377 1.50 lukem {
378 1.50 lukem char *ep;
379 1.50 lukem
380 1.50 lukem errno = 0;
381 1.50 lukem *id = (id_t)strtoul(name, &ep, 10);
382 1.50 lukem if (errno || *ep != '\0')
383 1.50 lukem return (0);
384 1.50 lukem return (1);
385 1.50 lukem }
386 1.50 lukem
387 1.50 lukem /*
388 1.61 simonb * do_link --
389 1.61 simonb * make a hard link, obeying dorename if set
390 1.61 simonb * return -1 on failure
391 1.61 simonb */
392 1.61 simonb int
393 1.61 simonb do_link(char *from_name, char *to_name)
394 1.61 simonb {
395 1.61 simonb char tmpl[MAXPATHLEN];
396 1.61 simonb int ret;
397 1.61 simonb
398 1.61 simonb if (dorename) {
399 1.61 simonb (void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
400 1.61 simonb xdirname(to_name));
401 1.62 perry /* This usage is safe. The linker will bitch anyway. */
402 1.61 simonb if (mktemp(tmpl) == NULL)
403 1.66 tv err(1, "%s: mktemp", tmpl);
404 1.61 simonb ret = link(from_name, tmpl);
405 1.61 simonb if (ret == 0) {
406 1.61 simonb ret = rename(tmpl, to_name);
407 1.61 simonb if (ret < 0)
408 1.61 simonb /* remove temporary link before exiting */
409 1.61 simonb (void)unlink(tmpl);
410 1.61 simonb }
411 1.61 simonb return (ret);
412 1.61 simonb } else
413 1.61 simonb return (link(from_name, to_name));
414 1.61 simonb }
415 1.61 simonb
416 1.61 simonb /*
417 1.61 simonb * do_symlink --
418 1.61 simonb * make a symbolic link, obeying dorename if set
419 1.61 simonb * exit on failure
420 1.61 simonb */
421 1.61 simonb void
422 1.61 simonb do_symlink(char *from_name, char *to_name)
423 1.61 simonb {
424 1.61 simonb char tmpl[MAXPATHLEN];
425 1.61 simonb
426 1.61 simonb if (dorename) {
427 1.61 simonb (void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
428 1.61 simonb xdirname(to_name));
429 1.62 perry /* This usage is safe. The linker will bitch anyway. */
430 1.61 simonb if (mktemp(tmpl) == NULL)
431 1.66 tv err(1, "%s: mktemp", tmpl);
432 1.61 simonb
433 1.61 simonb if (symlink(from_name, tmpl) == -1)
434 1.61 simonb err(1, "symlink %s -> %s", from_name, tmpl);
435 1.61 simonb if (rename(tmpl, to_name) == -1) {
436 1.61 simonb /* remove temporary link before exiting */
437 1.61 simonb (void)unlink(tmpl);
438 1.61 simonb err(1, "%s: rename", to_name);
439 1.61 simonb }
440 1.61 simonb } else {
441 1.61 simonb if (symlink(from_name, to_name) == -1)
442 1.61 simonb err(1, "symlink %s -> %s", from_name, to_name);
443 1.61 simonb }
444 1.61 simonb }
445 1.61 simonb
446 1.61 simonb /*
447 1.16 christos * makelink --
448 1.16 christos * make a link from source to destination
449 1.16 christos */
450 1.16 christos void
451 1.48 simonb makelink(char *from_name, char *to_name)
452 1.16 christos {
453 1.53 lukem char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
454 1.68 lukem struct stat to_sb;
455 1.16 christos
456 1.16 christos /* Try hard links first */
457 1.16 christos if (dolink & (LN_HARD|LN_MIXED)) {
458 1.61 simonb if (do_link(from_name, to_name) == -1) {
459 1.16 christos if ((dolink & LN_HARD) || errno != EXDEV)
460 1.16 christos err(1, "link %s -> %s", from_name, to_name);
461 1.68 lukem } else {
462 1.68 lukem if (stat(to_name, &to_sb))
463 1.68 lukem err(1, "%s: stat", to_name);
464 1.69 lukem if (S_ISREG(to_sb.st_mode)) {
465 1.69 lukem /* XXX: only metalog hardlinked files */
466 1.69 lukem int omode;
467 1.69 lukem char *oowner, *ogroup, *offlags;
468 1.85 lukem char *dres;
469 1.69 lukem
470 1.69 lukem /* XXX: use underlying perms */
471 1.69 lukem omode = mode;
472 1.69 lukem mode = (to_sb.st_mode & 0777);
473 1.69 lukem oowner = owner;
474 1.69 lukem owner = NULL;
475 1.69 lukem ogroup = group;
476 1.69 lukem group = NULL;
477 1.69 lukem offlags = fflags;
478 1.69 lukem fflags = NULL;
479 1.85 lukem switch (digesttype) {
480 1.85 lukem case DIGEST_MD5:
481 1.85 lukem dres = MD5File(from_name, NULL);
482 1.85 lukem break;
483 1.85 lukem case DIGEST_RMD160:
484 1.85 lukem dres = RMD160File(from_name, NULL);
485 1.85 lukem break;
486 1.85 lukem case DIGEST_SHA1:
487 1.85 lukem dres = SHA1File(from_name, NULL);
488 1.85 lukem break;
489 1.85 lukem default:
490 1.85 lukem dres = NULL;
491 1.85 lukem }
492 1.85 lukem metadata_log(to_name, "file", NULL, NULL, dres);
493 1.85 lukem free(dres);
494 1.69 lukem mode = omode;
495 1.69 lukem owner = oowner;
496 1.69 lukem group = ogroup;
497 1.69 lukem fflags = offlags;
498 1.69 lukem }
499 1.16 christos return;
500 1.57 lukem }
501 1.16 christos }
502 1.16 christos
503 1.16 christos /* Symbolic links */
504 1.16 christos if (dolink & LN_ABSOLUTE) {
505 1.16 christos /* Convert source path to absolute */
506 1.16 christos if (realpath(from_name, src) == NULL)
507 1.66 tv err(1, "%s: realpath", from_name);
508 1.61 simonb do_symlink(src, to_name);
509 1.90 lukem /* XXX: src may point outside of destdir */
510 1.84 lukem metadata_log(to_name, "link", NULL, src, NULL);
511 1.16 christos return;
512 1.16 christos }
513 1.16 christos
514 1.16 christos if (dolink & LN_RELATIVE) {
515 1.61 simonb char *cp, *d, *s;
516 1.16 christos
517 1.16 christos /* Resolve pathnames */
518 1.16 christos if (realpath(from_name, src) == NULL)
519 1.66 tv err(1, "%s: realpath", from_name);
520 1.61 simonb
521 1.61 simonb /*
522 1.61 simonb * The last component of to_name may be a symlink,
523 1.61 simonb * so use realpath to resolve only the directory.
524 1.61 simonb */
525 1.63 perry cp = xdirname(to_name);
526 1.61 simonb if (realpath(cp, dst) == NULL)
527 1.66 tv err(1, "%s: realpath", cp);
528 1.61 simonb /* .. and add the last component */
529 1.61 simonb if (strcmp(dst, "/") != 0) {
530 1.61 simonb if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
531 1.61 simonb errx(1, "resolved pathname too long");
532 1.61 simonb }
533 1.61 simonb cp = xbasename(to_name);
534 1.61 simonb if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
535 1.61 simonb errx(1, "resolved pathname too long");
536 1.16 christos
537 1.16 christos /* trim common path components */
538 1.16 christos for (s = src, d = dst; *s == *d; s++, d++)
539 1.16 christos continue;
540 1.16 christos while (*s != '/')
541 1.16 christos s--, d--;
542 1.16 christos
543 1.16 christos /* count the number of directories we need to backtrack */
544 1.16 christos for (++d, lnk[0] = '\0'; *d; d++)
545 1.16 christos if (*d == '/')
546 1.78 itojun (void)strlcat(lnk, "../", sizeof(lnk));
547 1.16 christos
548 1.78 itojun (void)strlcat(lnk, ++s, sizeof(lnk));
549 1.16 christos
550 1.90 lukem do_symlink(lnk, to_name);
551 1.90 lukem /* XXX: lnk may point outside of destdir */
552 1.90 lukem metadata_log(to_name, "link", NULL, lnk, NULL);
553 1.16 christos return;
554 1.16 christos }
555 1.16 christos
556 1.16 christos /*
557 1.16 christos * If absolute or relative was not specified,
558 1.16 christos * try the names the user provided
559 1.16 christos */
560 1.61 simonb do_symlink(from_name, to_name);
561 1.90 lukem /* XXX: from_name may point outside of destdir */
562 1.84 lukem metadata_log(to_name, "link", NULL, from_name, NULL);
563 1.16 christos }
564 1.16 christos
565 1.16 christos /*
566 1.1 cgd * install --
567 1.1 cgd * build a path name and install the file
568 1.1 cgd */
569 1.5 jtc void
570 1.50 lukem install(char *from_name, char *to_name, u_int flags)
571 1.1 cgd {
572 1.65 tv struct stat from_sb;
573 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
574 1.65 tv struct stat to_sb;
575 1.65 tv #endif
576 1.53 lukem struct timeval tv[2];
577 1.60 dillo int devnull, from_fd, to_fd, serrno, tmpmode;
578 1.84 lukem char *p, tmpl[MAXPATHLEN], *oto_name, *digestresult;
579 1.5 jtc
580 1.71 lukem if (!dolink) {
581 1.71 lukem /* ensure that from_sb & tv are sane if !dolink */
582 1.71 lukem if (stat(from_name, &from_sb))
583 1.71 lukem err(1, "%s: stat", from_name);
584 1.87 jmc #if BSD4_4 && !HAVE_NBTOOL_CONFIG_H
585 1.71 lukem TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
586 1.71 lukem TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
587 1.70 lukem #else
588 1.71 lukem tv[0].tv_sec = from_sb.st_atime;
589 1.71 lukem tv[0].tv_usec = 0;
590 1.71 lukem tv[1].tv_sec = from_sb.st_mtime;
591 1.71 lukem tv[1].tv_usec = 0;
592 1.70 lukem #endif
593 1.71 lukem }
594 1.70 lukem
595 1.5 jtc if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
596 1.56 perry if (!dolink) {
597 1.56 perry if (!S_ISREG(from_sb.st_mode))
598 1.56 perry errx(1, "%s: not a regular file", from_name);
599 1.56 perry }
600 1.5 jtc /* Build the target path. */
601 1.5 jtc if (flags & DIRECTORY) {
602 1.5 jtc (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
603 1.5 jtc to_name,
604 1.21 lukem (p = strrchr(from_name, '/')) ? ++p : from_name);
605 1.1 cgd to_name = pathbuf;
606 1.1 cgd }
607 1.1 cgd devnull = 0;
608 1.5 jtc } else {
609 1.65 tv #if HAVE_STRUCT_STAT_ST_FLAGS
610 1.5 jtc from_sb.st_flags = 0; /* XXX */
611 1.65 tv #endif
612 1.1 cgd devnull = 1;
613 1.5 jtc }
614 1.1 cgd
615 1.5 jtc /*
616 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
617 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
618 1.5 jtc * it might work.
619 1.5 jtc */
620 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
621 1.5 jtc if (stat(to_name, &to_sb) == 0 &&
622 1.5 jtc to_sb.st_flags & (NOCHANGEBITS))
623 1.5 jtc (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
624 1.65 tv #endif
625 1.33 christos if (dorename) {
626 1.64 msaitoh (void)snprintf(tmpl, sizeof(tmpl), "%s/inst.XXXXXX",
627 1.61 simonb xdirname(to_name));
628 1.33 christos oto_name = to_name;
629 1.33 christos to_name = tmpl;
630 1.33 christos } else {
631 1.37 christos oto_name = NULL; /* pacify gcc */
632 1.33 christos if (dobackup)
633 1.33 christos backup(to_name);
634 1.33 christos else
635 1.33 christos (void)unlink(to_name);
636 1.33 christos }
637 1.16 christos
638 1.16 christos if (dolink) {
639 1.61 simonb makelink(from_name, dorename ? oto_name : to_name);
640 1.16 christos return;
641 1.16 christos }
642 1.1 cgd
643 1.5 jtc /* Create target. */
644 1.33 christos if (dorename) {
645 1.33 christos if ((to_fd = mkstemp(to_name)) == -1)
646 1.66 tv err(1, "%s: mkstemp", to_name);
647 1.33 christos } else {
648 1.33 christos if ((to_fd = open(to_name,
649 1.33 christos O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
650 1.66 tv err(1, "%s: open", to_name);
651 1.33 christos }
652 1.86 lukem digestresult = NULL;
653 1.1 cgd if (!devnull) {
654 1.1 cgd if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
655 1.1 cgd (void)unlink(to_name);
656 1.66 tv err(1, "%s: open", from_name);
657 1.1 cgd }
658 1.84 lukem digestresult =
659 1.84 lukem copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
660 1.1 cgd (void)close(from_fd);
661 1.1 cgd }
662 1.9 jonathan
663 1.9 jonathan if (dostrip) {
664 1.1 cgd strip(to_name);
665 1.9 jonathan
666 1.9 jonathan /*
667 1.9 jonathan * Re-open our fd on the target, in case we used a strip
668 1.9 jonathan * that does not work in-place -- like gnu binutils strip.
669 1.9 jonathan */
670 1.9 jonathan close(to_fd);
671 1.9 jonathan if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
672 1.69 lukem err(1, "stripping %s", to_name);
673 1.69 lukem }
674 1.69 lukem
675 1.69 lukem if (afterinstallcmd != NULL) {
676 1.69 lukem afterinstall(afterinstallcmd, to_name, 1);
677 1.69 lukem
678 1.69 lukem /*
679 1.69 lukem * Re-open our fd on the target, in case we used an
680 1.69 lukem * after-install command that does not work in-place
681 1.69 lukem */
682 1.69 lukem close(to_fd);
683 1.69 lukem if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
684 1.69 lukem err(1, "running after install command on %s", to_name);
685 1.9 jonathan }
686 1.9 jonathan
687 1.41 cgd /*
688 1.41 cgd * Set owner, group, mode for target; do the chown first,
689 1.41 cgd * chown may lose the setuid bits.
690 1.41 cgd */
691 1.50 lukem if (!dounpriv &&
692 1.50 lukem (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
693 1.41 cgd serrno = errno;
694 1.41 cgd (void)unlink(to_name);
695 1.41 cgd errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
696 1.1 cgd }
697 1.60 dillo tmpmode = mode;
698 1.58 tv if (dounpriv)
699 1.60 dillo tmpmode &= S_IRWXU|S_IRWXG|S_IRWXO;
700 1.60 dillo if (fchmod(to_fd, tmpmode) == -1) {
701 1.5 jtc serrno = errno;
702 1.5 jtc (void)unlink(to_name);
703 1.5 jtc errx(1, "%s: chmod: %s", to_name, strerror(serrno));
704 1.5 jtc }
705 1.5 jtc
706 1.5 jtc /*
707 1.26 christos * Preserve the date of the source file.
708 1.26 christos */
709 1.26 christos if (dopreserve) {
710 1.65 tv #if HAVE_FUTIMES
711 1.65 tv if (futimes(to_fd, tv) == -1)
712 1.65 tv warn("%s: futimes", to_name);
713 1.65 tv #else
714 1.65 tv if (utimes(to_name, tv) == -1)
715 1.66 tv warn("%s: utimes", to_name);
716 1.65 tv #endif
717 1.5 jtc }
718 1.5 jtc
719 1.1 cgd (void)close(to_fd);
720 1.33 christos
721 1.50 lukem if (dorename) {
722 1.33 christos if (rename(to_name, oto_name) == -1)
723 1.33 christos err(1, "%s: rename", to_name);
724 1.50 lukem to_name = oto_name;
725 1.50 lukem }
726 1.33 christos
727 1.50 lukem /*
728 1.50 lukem * If provided a set of flags, set them, otherwise, preserve the
729 1.50 lukem * flags, except for the dump flag.
730 1.50 lukem */
731 1.80 lukem #if ! HAVE_NBTOOL_CONFIG_H
732 1.50 lukem if (!dounpriv && chflags(to_name,
733 1.51 lukem flags & SETFLAGS ? fileflags : from_sb.st_flags & ~UF_NODUMP) == -1)
734 1.51 lukem {
735 1.50 lukem if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
736 1.50 lukem warn("%s: chflags", to_name);
737 1.50 lukem }
738 1.65 tv #endif
739 1.50 lukem
740 1.84 lukem metadata_log(to_name, "file", tv, NULL, digestresult);
741 1.84 lukem free(digestresult);
742 1.1 cgd }
743 1.1 cgd
744 1.1 cgd /*
745 1.1 cgd * copy --
746 1.1 cgd * copy from one file to another
747 1.1 cgd */
748 1.84 lukem char *
749 1.48 simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
750 1.1 cgd {
751 1.53 lukem ssize_t nr, nw;
752 1.53 lukem int serrno;
753 1.53 lukem char *p;
754 1.53 lukem char buf[MAXBSIZE];
755 1.84 lukem MD5_CTX ctxMD5;
756 1.84 lukem RMD160_CTX ctxRMD160;
757 1.84 lukem SHA1_CTX ctxSHA1;
758 1.84 lukem
759 1.84 lukem switch (digesttype) {
760 1.84 lukem case DIGEST_MD5:
761 1.84 lukem MD5Init(&ctxMD5);
762 1.84 lukem break;
763 1.84 lukem case DIGEST_RMD160:
764 1.84 lukem RMD160Init(&ctxRMD160);
765 1.84 lukem break;
766 1.84 lukem case DIGEST_SHA1:
767 1.84 lukem SHA1Init(&ctxSHA1);
768 1.84 lukem break;
769 1.84 lukem case DIGEST_NONE:
770 1.84 lukem default:
771 1.84 lukem break;
772 1.84 lukem }
773 1.5 jtc /*
774 1.28 wsanchez * There's no reason to do anything other than close the file
775 1.28 wsanchez * now if it's empty, so let's not bother.
776 1.5 jtc */
777 1.28 wsanchez if (size > 0) {
778 1.45 chs
779 1.28 wsanchez /*
780 1.45 chs * Mmap and write if less than 8M (the limit is so we
781 1.45 chs * don't totally trash memory on big files). This is
782 1.45 chs * really a minor hack, but it wins some CPU back.
783 1.28 wsanchez */
784 1.45 chs
785 1.28 wsanchez if (size <= 8 * 1048576) {
786 1.28 wsanchez if ((p = mmap(NULL, (size_t)size, PROT_READ,
787 1.45 chs MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
788 1.45 chs == MAP_FAILED) {
789 1.45 chs goto mmap_failed;
790 1.36 thorpej }
791 1.77 thorpej #if defined(MADV_SEQUENTIAL) && !defined(__APPLE__)
792 1.39 christos if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
793 1.39 christos && errno != EOPNOTSUPP)
794 1.38 sommerfe warnx("madvise: %s", strerror(errno));
795 1.44 cgd #endif
796 1.38 sommerfe
797 1.36 thorpej if (write(to_fd, p, size) != size) {
798 1.36 thorpej serrno = errno;
799 1.36 thorpej (void)unlink(to_name);
800 1.66 tv errx(1, "%s: write: %s",
801 1.36 thorpej to_name, strerror(serrno));
802 1.36 thorpej }
803 1.84 lukem switch (digesttype) {
804 1.84 lukem case DIGEST_MD5:
805 1.84 lukem MD5Update(&ctxMD5, p, size);
806 1.84 lukem break;
807 1.84 lukem case DIGEST_RMD160:
808 1.84 lukem RMD160Update(&ctxRMD160, p, size);
809 1.84 lukem break;
810 1.84 lukem case DIGEST_SHA1:
811 1.84 lukem SHA1Update(&ctxSHA1, p, size);
812 1.84 lukem break;
813 1.84 lukem default:
814 1.84 lukem break;
815 1.84 lukem }
816 1.81 christos (void)munmap(p, size);
817 1.28 wsanchez } else {
818 1.84 lukem mmap_failed:
819 1.36 thorpej while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
820 1.28 wsanchez if ((nw = write(to_fd, buf, nr)) != nr) {
821 1.28 wsanchez serrno = errno;
822 1.28 wsanchez (void)unlink(to_name);
823 1.66 tv errx(1, "%s: write: %s", to_name,
824 1.45 chs strerror(nw > 0 ? EIO : serrno));
825 1.28 wsanchez }
826 1.84 lukem switch (digesttype) {
827 1.84 lukem case DIGEST_MD5:
828 1.84 lukem MD5Update(&ctxMD5, buf, nr);
829 1.84 lukem break;
830 1.84 lukem case DIGEST_RMD160:
831 1.84 lukem RMD160Update(&ctxRMD160, buf, nr);
832 1.84 lukem break;
833 1.84 lukem case DIGEST_SHA1:
834 1.84 lukem SHA1Update(&ctxSHA1, buf, nr);
835 1.84 lukem break;
836 1.84 lukem default:
837 1.84 lukem break;
838 1.84 lukem }
839 1.36 thorpej }
840 1.28 wsanchez if (nr != 0) {
841 1.5 jtc serrno = errno;
842 1.5 jtc (void)unlink(to_name);
843 1.66 tv errx(1, "%s: read: %s", from_name, strerror(serrno));
844 1.5 jtc }
845 1.1 cgd }
846 1.1 cgd }
847 1.84 lukem switch (digesttype) {
848 1.84 lukem case DIGEST_MD5:
849 1.84 lukem return MD5End(&ctxMD5, NULL);
850 1.84 lukem case DIGEST_RMD160:
851 1.84 lukem return RMD160End(&ctxRMD160, NULL);
852 1.84 lukem case DIGEST_SHA1:
853 1.84 lukem return SHA1End(&ctxSHA1, NULL);
854 1.84 lukem default:
855 1.84 lukem return NULL;
856 1.84 lukem }
857 1.1 cgd }
858 1.1 cgd
859 1.1 cgd /*
860 1.1 cgd * strip --
861 1.1 cgd * use strip(1) to strip the target file
862 1.1 cgd */
863 1.5 jtc void
864 1.48 simonb strip(char *to_name)
865 1.1 cgd {
866 1.53 lukem int serrno, status;
867 1.53 lukem char *stripprog;
868 1.1 cgd
869 1.1 cgd switch (vfork()) {
870 1.1 cgd case -1:
871 1.5 jtc serrno = errno;
872 1.5 jtc (void)unlink(to_name);
873 1.22 thorpej errx(1, "vfork: %s", strerror(serrno));
874 1.61 simonb /*NOTREACHED*/
875 1.1 cgd case 0:
876 1.18 thorpej stripprog = getenv("STRIP");
877 1.18 thorpej if (stripprog == NULL)
878 1.18 thorpej stripprog = _PATH_STRIP;
879 1.28 wsanchez
880 1.28 wsanchez if (stripArgs) {
881 1.50 lukem /*
882 1.50 lukem * build up a command line and let /bin/sh
883 1.50 lukem * parse the arguments
884 1.50 lukem */
885 1.28 wsanchez char* cmd = (char*)malloc(sizeof(char)*
886 1.28 wsanchez (3+strlen(stripprog)+
887 1.28 wsanchez strlen(stripArgs)+
888 1.28 wsanchez strlen(to_name)));
889 1.49 simonb
890 1.49 simonb if (cmd == NULL)
891 1.49 simonb errx(1, "%s", strerror(ENOMEM));
892 1.28 wsanchez
893 1.28 wsanchez sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
894 1.28 wsanchez
895 1.28 wsanchez execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
896 1.28 wsanchez } else
897 1.40 cgd execlp(stripprog, "strip", to_name, NULL);
898 1.28 wsanchez
899 1.66 tv warn("%s: exec of strip", stripprog);
900 1.22 thorpej _exit(1);
901 1.61 simonb /*NOTREACHED*/
902 1.1 cgd default:
903 1.1 cgd if (wait(&status) == -1 || status)
904 1.5 jtc (void)unlink(to_name);
905 1.1 cgd }
906 1.32 hubertf }
907 1.32 hubertf
908 1.32 hubertf /*
909 1.69 lukem * afterinstall --
910 1.69 lukem * run provided command on the target file or directory after it's been
911 1.69 lukem * installed and stripped, but before permissions are set or it's renamed
912 1.69 lukem */
913 1.69 lukem void
914 1.69 lukem afterinstall(const char *command, const char *to_name, int errunlink)
915 1.69 lukem {
916 1.69 lukem int serrno, status;
917 1.69 lukem char *cmd;
918 1.69 lukem
919 1.69 lukem switch (vfork()) {
920 1.69 lukem case -1:
921 1.69 lukem serrno = errno;
922 1.69 lukem if (errunlink)
923 1.69 lukem (void)unlink(to_name);
924 1.69 lukem errx(1, "vfork: %s", strerror(serrno));
925 1.69 lukem /*NOTREACHED*/
926 1.69 lukem case 0:
927 1.69 lukem /*
928 1.69 lukem * build up a command line and let /bin/sh
929 1.69 lukem * parse the arguments
930 1.69 lukem */
931 1.69 lukem cmd = (char*)malloc(sizeof(char)*
932 1.69 lukem (2+strlen(command)+
933 1.69 lukem strlen(to_name)));
934 1.69 lukem
935 1.69 lukem if (cmd == NULL)
936 1.69 lukem errx(1, "%s", strerror(ENOMEM));
937 1.69 lukem
938 1.69 lukem sprintf(cmd, "%s %s", command, to_name);
939 1.69 lukem
940 1.69 lukem execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
941 1.69 lukem
942 1.69 lukem warn("%s: exec of after install command", command);
943 1.69 lukem _exit(1);
944 1.69 lukem /*NOTREACHED*/
945 1.69 lukem default:
946 1.69 lukem if ((wait(&status) == -1 || status) && errunlink)
947 1.69 lukem (void)unlink(to_name);
948 1.69 lukem }
949 1.69 lukem }
950 1.69 lukem
951 1.69 lukem /*
952 1.61 simonb * backup --
953 1.61 simonb * backup file "to_name" to to_name.suffix
954 1.61 simonb * if suffix contains a "%", it's taken as a printf(3) pattern
955 1.61 simonb * used for a numbered backup.
956 1.32 hubertf */
957 1.32 hubertf void
958 1.48 simonb backup(const char *to_name)
959 1.32 hubertf {
960 1.61 simonb char bname[FILENAME_MAX];
961 1.32 hubertf
962 1.35 hubertf if (numberedbackup) {
963 1.32 hubertf /* Do numbered backup */
964 1.32 hubertf int cnt;
965 1.32 hubertf char suffix_expanded[FILENAME_MAX];
966 1.32 hubertf
967 1.32 hubertf cnt=0;
968 1.32 hubertf do {
969 1.50 lukem (void)snprintf(suffix_expanded, FILENAME_MAX, suffix,
970 1.50 lukem cnt);
971 1.61 simonb (void)snprintf(bname, FILENAME_MAX, "%s%s", to_name,
972 1.61 simonb suffix_expanded);
973 1.32 hubertf cnt++;
974 1.61 simonb } while (access(bname, F_OK) == 0);
975 1.32 hubertf } else {
976 1.32 hubertf /* Do simple backup */
977 1.61 simonb (void)snprintf(bname, FILENAME_MAX, "%s%s", to_name, suffix);
978 1.32 hubertf }
979 1.32 hubertf
980 1.61 simonb (void)rename(to_name, bname);
981 1.1 cgd }
982 1.1 cgd
983 1.1 cgd /*
984 1.5 jtc * install_dir --
985 1.47 wiz * build directory hierarchy
986 1.2 jtc */
987 1.7 jtc void
988 1.50 lukem install_dir(char *path, u_int flags)
989 1.2 jtc {
990 1.53 lukem char *p;
991 1.53 lukem struct stat sb;
992 1.53 lukem int ch;
993 1.2 jtc
994 1.4 cgd for (p = path;; ++p)
995 1.4 cgd if (!*p || (p != path && *p == '/')) {
996 1.2 jtc ch = *p;
997 1.2 jtc *p = '\0';
998 1.2 jtc if (stat(path, &sb)) {
999 1.2 jtc if (errno != ENOENT || mkdir(path, 0777) < 0) {
1000 1.66 tv err(1, "%s: mkdir", path);
1001 1.2 jtc }
1002 1.2 jtc }
1003 1.2 jtc if (!(*p = ch))
1004 1.4 cgd break;
1005 1.2 jtc }
1006 1.2 jtc
1007 1.69 lukem if (afterinstallcmd != NULL)
1008 1.69 lukem afterinstall(afterinstallcmd, path, 0);
1009 1.69 lukem
1010 1.50 lukem if (!dounpriv && (
1011 1.50 lukem ((flags & (HASUID | HASGID)) && chown(path, uid, gid) == -1)
1012 1.50 lukem || chmod(path, mode) == -1 )) {
1013 1.66 tv warn("%s: chown/chmod", path);
1014 1.41 cgd }
1015 1.84 lukem metadata_log(path, "dir", NULL, NULL, NULL);
1016 1.2 jtc }
1017 1.2 jtc
1018 1.2 jtc /*
1019 1.50 lukem * metadata_log --
1020 1.50 lukem * if metafp is not NULL, output mtree(8) full path name and settings to
1021 1.50 lukem * metafp, to allow permissions to be set correctly by other tools.
1022 1.50 lukem */
1023 1.50 lukem void
1024 1.59 perry metadata_log(const char *path, const char *type, struct timeval *tv,
1025 1.84 lukem const char *link, const char *digestresult)
1026 1.50 lukem {
1027 1.72 yamt static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
1028 1.88 lukem const char *p;
1029 1.88 lukem char *buf;
1030 1.75 lukem size_t destlen;
1031 1.73 lukem struct flock metalog_lock;
1032 1.50 lukem
1033 1.50 lukem if (!metafp)
1034 1.50 lukem return;
1035 1.50 lukem buf = (char *)malloc(4 * strlen(path) + 1); /* buf for strsvis(3) */
1036 1.50 lukem if (buf == NULL) {
1037 1.50 lukem warnx("%s", strerror(ENOMEM));
1038 1.50 lukem return;
1039 1.50 lukem }
1040 1.73 lukem /* lock log file */
1041 1.73 lukem metalog_lock.l_start = 0;
1042 1.73 lukem metalog_lock.l_len = 0;
1043 1.73 lukem metalog_lock.l_whence = SEEK_SET;
1044 1.73 lukem metalog_lock.l_type = F_WRLCK;
1045 1.73 lukem if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1046 1.50 lukem warn("can't lock %s", metafile);
1047 1.50 lukem return;
1048 1.50 lukem }
1049 1.50 lukem
1050 1.88 lukem p = path; /* remove destdir */
1051 1.75 lukem if (destdir) {
1052 1.75 lukem destlen = strlen(destdir);
1053 1.76 lukem if (strncmp(p, destdir, destlen) == 0 &&
1054 1.76 lukem (p[destlen] == '/' || p[destlen] == '\0'))
1055 1.75 lukem p += destlen;
1056 1.75 lukem }
1057 1.75 lukem while (*p && *p == '/') /* remove leading /s */
1058 1.75 lukem p++;
1059 1.88 lukem strsvis(buf, p, VIS_CSTYLE, extra); /* encode name */
1060 1.88 lukem p = buf;
1061 1.75 lukem /* print details */
1062 1.76 lukem fprintf(metafp, ".%s%s type=%s mode=%#o", *p ? "/" : "", p, type, mode);
1063 1.89 lukem if (link) {
1064 1.89 lukem strsvis(buf, link, VIS_CSTYLE, extra); /* encode link */
1065 1.89 lukem fprintf(metafp, " link=%s", buf);
1066 1.89 lukem }
1067 1.52 tv if (owner)
1068 1.52 tv fprintf(metafp, " uname=%s", owner);
1069 1.52 tv if (group)
1070 1.52 tv fprintf(metafp, " gname=%s", group);
1071 1.52 tv if (fflags)
1072 1.52 tv fprintf(metafp, " flags=%s", fflags);
1073 1.54 lukem if (tags)
1074 1.54 lukem fprintf(metafp, " tags=%s", tags);
1075 1.57 lukem if (tv != NULL && dopreserve)
1076 1.50 lukem fprintf(metafp, " time=%ld.%ld", tv[1].tv_sec, tv[1].tv_usec);
1077 1.84 lukem if (digestresult && digest)
1078 1.84 lukem fprintf(metafp, " %s=%s", digest, digestresult);
1079 1.50 lukem fputc('\n', metafp);
1080 1.50 lukem fflush(metafp); /* flush output */
1081 1.73 lukem /* unlock log file */
1082 1.73 lukem metalog_lock.l_type = F_UNLCK;
1083 1.73 lukem if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
1084 1.50 lukem warn("can't unlock %s", metafile);
1085 1.50 lukem }
1086 1.50 lukem free(buf);
1087 1.50 lukem }
1088 1.50 lukem
1089 1.61 simonb /*
1090 1.61 simonb * xbasename --
1091 1.61 simonb * libc basename(3) that returns a pointer to a static buffer
1092 1.61 simonb * instead of overwriting that passed-in string.
1093 1.61 simonb */
1094 1.61 simonb char *
1095 1.61 simonb xbasename(char *path)
1096 1.61 simonb {
1097 1.61 simonb static char tmp[MAXPATHLEN];
1098 1.61 simonb
1099 1.61 simonb (void)strlcpy(tmp, path, sizeof(tmp));
1100 1.61 simonb return (basename(tmp));
1101 1.61 simonb }
1102 1.61 simonb
1103 1.61 simonb /*
1104 1.61 simonb * xdirname --
1105 1.61 simonb * libc dirname(3) that returns a pointer to a static buffer
1106 1.61 simonb * instead of overwriting that passed-in string.
1107 1.61 simonb */
1108 1.61 simonb char *
1109 1.61 simonb xdirname(char *path)
1110 1.61 simonb {
1111 1.61 simonb static char tmp[MAXPATHLEN];
1112 1.50 lukem
1113 1.61 simonb (void)strlcpy(tmp, path, sizeof(tmp));
1114 1.61 simonb return (dirname(tmp));
1115 1.61 simonb }
1116 1.50 lukem
1117 1.50 lukem /*
1118 1.1 cgd * usage --
1119 1.1 cgd * print a usage message and die
1120 1.1 cgd */
1121 1.5 jtc void
1122 1.48 simonb usage(void)
1123 1.1 cgd {
1124 1.69 lukem const char *prog;
1125 1.69 lukem
1126 1.69 lukem prog = getprogname();
1127 1.53 lukem
1128 1.69 lukem (void)fprintf(stderr,
1129 1.75 lukem "usage: %s [-Ubcprs] [-M log] [-D dest] [-T tags] [-B suffix]\n"
1130 1.75 lukem " [-a aftercmd] [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group] \n"
1131 1.84 lukem " [-l linkflags] [-h hash] [-S stripflags] file1 file2\n"
1132 1.75 lukem " %s [-Ubcprs] [-M log] [-D dest] [-T tags] [-B suffix]\n"
1133 1.75 lukem " [-a aftercmd] [-f flags] [-m mode] [-N dbdir] [-o owner] [-g group]\n"
1134 1.84 lukem " [-l linkflags] [-h hash] [-S stripflags] file1 ... fileN directory\n"
1135 1.75 lukem " %s -d [-Up] [-M log] [-D dest] [-T tags] [-a aftercmd] [-m mode]\n"
1136 1.74 lukem " [-N dbdir] [-o owner] [-g group] directory ...\n",
1137 1.69 lukem prog, prog, prog);
1138 1.6 jtc exit(1);
1139 1.1 cgd }
1140