1 | /*
|
---|
2 | * Copyright (c) 1987, 1993
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 3. All advertising materials mentioning features or use of this software
|
---|
14 | * must display the following acknowledgement:
|
---|
15 | * This product includes software developed by the University of
|
---|
16 | * California, Berkeley and its contributors.
|
---|
17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #ifndef lint
|
---|
35 | static const char copyright[] =
|
---|
36 | "@(#) Copyright (c) 1987, 1993\n\
|
---|
37 | The Regents of the University of California. All rights reserved.\n";
|
---|
38 | #endif /* not lint */
|
---|
39 |
|
---|
40 | #if 0
|
---|
41 | #ifndef lint
|
---|
42 | static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
|
---|
43 | #endif /* not lint */
|
---|
44 |
|
---|
45 | #include <sys/cdefs.h>
|
---|
46 | __FBSDID("$FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.66 2005/01/25 14:34:57 ssouhlal Exp $");
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #include "config.h"
|
---|
50 | #ifndef _MSC_VER
|
---|
51 | # include <sys/param.h>
|
---|
52 | # ifdef USE_MMAP
|
---|
53 | # include <sys/mman.h>
|
---|
54 | # endif
|
---|
55 | # include <sys/mount.h>
|
---|
56 | # include <sys/wait.h>
|
---|
57 | # include <sys/time.h>
|
---|
58 | #endif /* !_MSC_VER */
|
---|
59 | #include <sys/stat.h>
|
---|
60 |
|
---|
61 | #include <ctype.h>
|
---|
62 | #include "err.h"
|
---|
63 | #include <errno.h>
|
---|
64 | #include <fcntl.h>
|
---|
65 | #include <grp.h>
|
---|
66 | #include <paths.h>
|
---|
67 | #include <pwd.h>
|
---|
68 | #include <stdio.h>
|
---|
69 | #include <stdlib.h>
|
---|
70 | #include <string.h>
|
---|
71 | #include <sysexits.h>
|
---|
72 | #include <unistd.h>
|
---|
73 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
74 | # include <process.h>
|
---|
75 | #endif
|
---|
76 | #include "getopt.h"
|
---|
77 | #ifdef __sun__
|
---|
78 | # include "solfakes.h"
|
---|
79 | #endif
|
---|
80 | #ifdef _MSC_VER
|
---|
81 | # include "mscfakes.h"
|
---|
82 | #endif
|
---|
83 | #include "kmkbuiltin.h"
|
---|
84 |
|
---|
85 |
|
---|
86 | extern void * bsd_setmode(const char *p);
|
---|
87 | extern mode_t bsd_getmode(const void *bbox, mode_t omode);
|
---|
88 |
|
---|
89 | #ifndef __unused
|
---|
90 | # define __unused
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #ifndef MAXBSIZE
|
---|
94 | # define MAXBSIZE 0x20000
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | /* Bootstrap aid - this doesn't exist in most older releases */
|
---|
98 | #ifndef MAP_FAILED
|
---|
99 | #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | #define MAX_CMP_SIZE (16 * 1024 * 1024)
|
---|
103 |
|
---|
104 | #define DIRECTORY 0x01 /* Tell install it's a directory. */
|
---|
105 | #define SETFLAGS 0x02 /* Tell install to set flags. */
|
---|
106 | #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
|
---|
107 | #define BACKUP_SUFFIX ".old"
|
---|
108 |
|
---|
109 | #ifndef O_BINARY
|
---|
110 | # define O_BINARY 0
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | #ifndef EFTYPE
|
---|
114 | # define EFTYPE EINVAL
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | #if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
|
---|
118 | # define IS_SLASH(ch) ((ch) == '/' || (ch) == '\\')
|
---|
119 | #else
|
---|
120 | # define IS_SLASH(ch) ((ch) == '/')
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | static gid_t gid;
|
---|
124 | static uid_t uid;
|
---|
125 | static int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose, mode_given;
|
---|
126 | static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
---|
127 | static const char *suffix = BACKUP_SUFFIX;
|
---|
128 | static int ignore_perm_errors;
|
---|
129 | static int hard_link_files_when_possible;
|
---|
130 |
|
---|
131 | static struct option long_options[] =
|
---|
132 | {
|
---|
133 | { "help", no_argument, 0, 261 },
|
---|
134 | { "version", no_argument, 0, 262 },
|
---|
135 | { "ignore-perm-errors", no_argument, 0, 263 },
|
---|
136 | { "no-ignore-perm-errors", no_argument, 0, 264 },
|
---|
137 | { "hard-link-files-when-possible", no_argument, 0, 265 },
|
---|
138 | { "no-hard-link-files-when-possible", no_argument, 0, 266 },
|
---|
139 | { 0, 0, 0, 0 },
|
---|
140 | };
|
---|
141 |
|
---|
142 |
|
---|
143 | static int copy(int, const char *, int, const char *, off_t);
|
---|
144 | static int compare(int, const char *, size_t, int, const char *, size_t);
|
---|
145 | static int create_newfile(const char *, int, struct stat *);
|
---|
146 | static int create_tempfile(const char *, char *, size_t);
|
---|
147 | static int install(const char *, const char *, u_long, u_int);
|
---|
148 | static int install_dir(char *);
|
---|
149 | static u_long numeric_id(const char *, const char *);
|
---|
150 | static int strip(const char *);
|
---|
151 | #ifdef USE_MMAP
|
---|
152 | static int trymmap(int);
|
---|
153 | #endif
|
---|
154 | static int usage(FILE *);
|
---|
155 | static char *last_slash(const char *);
|
---|
156 |
|
---|
157 | int
|
---|
158 | kmk_builtin_install(int argc, char *argv[], char ** envp)
|
---|
159 | {
|
---|
160 | struct stat from_sb, to_sb;
|
---|
161 | mode_t *set;
|
---|
162 | u_long fset = 0;
|
---|
163 | int ch, no_target;
|
---|
164 | u_int iflags;
|
---|
165 | char *flags;
|
---|
166 | const char *group, *owner, *to_name;
|
---|
167 | (void)envp;
|
---|
168 |
|
---|
169 | /* reinitialize globals */
|
---|
170 | mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
---|
171 | suffix = BACKUP_SUFFIX;
|
---|
172 | gid = 0;
|
---|
173 | uid = 0;
|
---|
174 | dobackup = docompare = dodir = dopreserve = dostrip = nommap = safecopy = verbose = mode_given = 0;
|
---|
175 | ignore_perm_errors = geteuid() != 0;
|
---|
176 | hard_link_files_when_possible = 0;
|
---|
177 |
|
---|
178 | /* reset getopt and set progname. */
|
---|
179 | g_progname = argv[0];
|
---|
180 | opterr = 1;
|
---|
181 | optarg = NULL;
|
---|
182 | optopt = 0;
|
---|
183 | optind = 0; /* init */
|
---|
184 |
|
---|
185 | iflags = 0;
|
---|
186 | group = owner = NULL;
|
---|
187 | while ((ch = getopt_long(argc, argv, "B:bCcdf:g:Mm:o:pSsv", long_options, NULL)) != -1)
|
---|
188 | switch(ch) {
|
---|
189 | case 'B':
|
---|
190 | suffix = optarg;
|
---|
191 | /* FALLTHROUGH */
|
---|
192 | case 'b':
|
---|
193 | dobackup = 1;
|
---|
194 | break;
|
---|
195 | case 'C':
|
---|
196 | docompare = 1;
|
---|
197 | break;
|
---|
198 | case 'c':
|
---|
199 | /* For backwards compatibility. */
|
---|
200 | break;
|
---|
201 | case 'd':
|
---|
202 | dodir = 1;
|
---|
203 | break;
|
---|
204 | case 'f':
|
---|
205 | #ifdef UF_IMMUTABLE
|
---|
206 | flags = optarg;
|
---|
207 | if (strtofflags(&flags, &fset, NULL))
|
---|
208 | return errx(EX_USAGE, "%s: invalid flag", flags);
|
---|
209 | iflags |= SETFLAGS;
|
---|
210 | #else
|
---|
211 | (void)flags;
|
---|
212 | #endif
|
---|
213 | break;
|
---|
214 | case 'g':
|
---|
215 | group = optarg;
|
---|
216 | break;
|
---|
217 | case 'M':
|
---|
218 | nommap = 1;
|
---|
219 | break;
|
---|
220 | case 'm':
|
---|
221 | if (!(set = bsd_setmode(optarg)))
|
---|
222 | return errx(EX_USAGE, "invalid file mode: %s",
|
---|
223 | optarg);
|
---|
224 | mode = bsd_getmode(set, 0);
|
---|
225 | free(set);
|
---|
226 | mode_given = 1;
|
---|
227 | break;
|
---|
228 | case 'o':
|
---|
229 | owner = optarg;
|
---|
230 | break;
|
---|
231 | case 'p':
|
---|
232 | docompare = dopreserve = 1;
|
---|
233 | break;
|
---|
234 | case 'S':
|
---|
235 | safecopy = 1;
|
---|
236 | break;
|
---|
237 | case 's':
|
---|
238 | dostrip = 1;
|
---|
239 | break;
|
---|
240 | case 'v':
|
---|
241 | verbose = 1;
|
---|
242 | break;
|
---|
243 | case 261:
|
---|
244 | usage(stdout);
|
---|
245 | return 0;
|
---|
246 | case 262:
|
---|
247 | return kbuild_version(argv[0]);
|
---|
248 | case 263:
|
---|
249 | ignore_perm_errors = 1;
|
---|
250 | break;
|
---|
251 | case 264:
|
---|
252 | ignore_perm_errors = 0;
|
---|
253 | break;
|
---|
254 | case 265:
|
---|
255 | hard_link_files_when_possible = 1;
|
---|
256 | break;
|
---|
257 | case 266:
|
---|
258 | hard_link_files_when_possible = 0;
|
---|
259 | break;
|
---|
260 | case '?':
|
---|
261 | default:
|
---|
262 | return usage(stderr);
|
---|
263 | }
|
---|
264 | argc -= optind;
|
---|
265 | argv += optind;
|
---|
266 |
|
---|
267 | /* some options make no sense when creating directories */
|
---|
268 | if (dostrip && dodir) {
|
---|
269 | warnx("-d and -s may not be specified together");
|
---|
270 | return usage(stderr);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* must have at least two arguments, except when creating directories */
|
---|
274 | if (argc == 0 || (argc == 1 && !dodir))
|
---|
275 | return usage(stderr);
|
---|
276 |
|
---|
277 | /* need to make a temp copy so we can compare stripped version */
|
---|
278 | if (docompare && dostrip)
|
---|
279 | safecopy = 1;
|
---|
280 |
|
---|
281 | /* get group and owner id's */
|
---|
282 | if (group != NULL) {
|
---|
283 | #ifndef _MSC_VER
|
---|
284 | struct group *gp;
|
---|
285 | if ((gp = getgrnam(group)) != NULL)
|
---|
286 | gid = gp->gr_gid;
|
---|
287 | else
|
---|
288 | #endif
|
---|
289 | {
|
---|
290 | gid = (gid_t)numeric_id(group, "group");
|
---|
291 | if (gid == (gid_t)-1)
|
---|
292 | return 1;
|
---|
293 | }
|
---|
294 | } else
|
---|
295 | gid = (gid_t)-1;
|
---|
296 |
|
---|
297 | if (owner != NULL) {
|
---|
298 | #ifndef _MSC_VER
|
---|
299 | struct passwd *pp;
|
---|
300 | if ((pp = getpwnam(owner)) != NULL)
|
---|
301 | uid = pp->pw_uid;
|
---|
302 | else
|
---|
303 | #endif
|
---|
304 | {
|
---|
305 | uid = (uid_t)numeric_id(owner, "user");
|
---|
306 | if (uid == (uid_t)-1)
|
---|
307 | return 1;
|
---|
308 | }
|
---|
309 | } else
|
---|
310 | uid = (uid_t)-1;
|
---|
311 |
|
---|
312 | if (dodir) {
|
---|
313 | for (; *argv != NULL; ++argv) {
|
---|
314 | int rc = install_dir(*argv);
|
---|
315 | if (rc)
|
---|
316 | return rc;
|
---|
317 | }
|
---|
318 | return EX_OK;
|
---|
319 | /* NOTREACHED */
|
---|
320 | }
|
---|
321 |
|
---|
322 | no_target = stat(to_name = argv[argc - 1], &to_sb);
|
---|
323 | if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
---|
324 | for (; *argv != to_name; ++argv) {
|
---|
325 | int rc = install(*argv, to_name, fset, iflags | DIRECTORY);
|
---|
326 | if (rc)
|
---|
327 | return rc;
|
---|
328 | }
|
---|
329 | return EX_OK;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* can't do file1 file2 directory/file */
|
---|
333 | if (argc != 2) {
|
---|
334 | warnx("wrong number or types of arguments");
|
---|
335 | return usage(stderr);
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (!no_target) {
|
---|
339 | if (stat(*argv, &from_sb))
|
---|
340 | return err(EX_OSERR, "%s", *argv);
|
---|
341 | if (!S_ISREG(to_sb.st_mode)) {
|
---|
342 | errno = EFTYPE;
|
---|
343 | return err(EX_OSERR, "%s", to_name);
|
---|
344 | }
|
---|
345 | if (to_sb.st_dev == from_sb.st_dev &&
|
---|
346 | to_sb.st_dev != 0 &&
|
---|
347 | to_sb.st_ino == from_sb.st_ino &&
|
---|
348 | to_sb.st_ino != 0 &&
|
---|
349 | !hard_link_files_when_possible)
|
---|
350 | return errx(EX_USAGE,
|
---|
351 | "%s and %s are the same file", *argv, to_name);
|
---|
352 | }
|
---|
353 | return install(*argv, to_name, fset, iflags);
|
---|
354 | }
|
---|
355 |
|
---|
356 | static u_long
|
---|
357 | numeric_id(const char *name, const char *type)
|
---|
358 | {
|
---|
359 | u_long val;
|
---|
360 | char *ep;
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * XXX
|
---|
364 | * We know that uid_t's and gid_t's are unsigned longs.
|
---|
365 | */
|
---|
366 | errno = 0;
|
---|
367 | val = strtoul(name, &ep, 10);
|
---|
368 | if (errno)
|
---|
369 | return err(-1, "%s", name);
|
---|
370 | if (*ep != '\0')
|
---|
371 | return errx(-1, "unknown %s %s", type, name);
|
---|
372 | return (val);
|
---|
373 | }
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * install --
|
---|
377 | * build a path name and install the file
|
---|
378 | */
|
---|
379 | static int
|
---|
380 | install(const char *from_name, const char *to_name, u_long fset, u_int flags)
|
---|
381 | {
|
---|
382 | struct stat from_sb, temp_sb, to_sb;
|
---|
383 | struct timeval tvb[2];
|
---|
384 | int devnull, files_match, from_fd, serrno, target;
|
---|
385 | int tempcopy, temp_fd, to_fd;
|
---|
386 | char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
|
---|
387 | int rc = EX_OK;
|
---|
388 |
|
---|
389 | files_match = 0;
|
---|
390 | from_fd = -1;
|
---|
391 | to_fd = -1;
|
---|
392 | temp_fd = -1;
|
---|
393 |
|
---|
394 | /* If try to install NULL file to a directory, fails. */
|
---|
395 | if (flags & DIRECTORY
|
---|
396 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
397 | || ( stricmp(from_name, _PATH_DEVNULL)
|
---|
398 | && stricmp(from_name, "nul")
|
---|
399 | # ifdef __EMX__
|
---|
400 | && stricmp(from_name, "/dev/nul")
|
---|
401 | # endif
|
---|
402 | )
|
---|
403 | #else
|
---|
404 | || strcmp(from_name, _PATH_DEVNULL)
|
---|
405 | #endif
|
---|
406 | ) {
|
---|
407 | if (stat(from_name, &from_sb))
|
---|
408 | return err(EX_OSERR, "%s", from_name);
|
---|
409 | if (!S_ISREG(from_sb.st_mode)) {
|
---|
410 | errno = EFTYPE;
|
---|
411 | return err(EX_OSERR, "%s", 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 = last_slash(from_name)) ? ++p : from_name);
|
---|
418 | to_name = pathbuf;
|
---|
419 | }
|
---|
420 | devnull = 0;
|
---|
421 | } else {
|
---|
422 | devnull = 1;
|
---|
423 | }
|
---|
424 |
|
---|
425 | target = stat(to_name, &to_sb) == 0;
|
---|
426 |
|
---|
427 | /* Only install to regular files. */
|
---|
428 | if (target && !S_ISREG(to_sb.st_mode)) {
|
---|
429 | errno = EFTYPE;
|
---|
430 | warn("%s", to_name);
|
---|
431 | return EX_OK;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /* Only copy safe if the target exists. */
|
---|
435 | tempcopy = safecopy && target;
|
---|
436 |
|
---|
437 | /* Try hard linking if wanted and possible. */
|
---|
438 | if (hard_link_files_when_possible)
|
---|
439 | {
|
---|
440 | #ifdef KBUILD_OS_OS2
|
---|
441 | const char *why_not = "not supported on OS/2";
|
---|
442 | #else
|
---|
443 | const char *why_not = NULL;
|
---|
444 | if (devnull) {
|
---|
445 | why_not = "/dev/null";
|
---|
446 | } else if (dostrip) {
|
---|
447 | why_not = "strip (-s)";
|
---|
448 | } else if (docompare) {
|
---|
449 | why_not = "compare (-C)";
|
---|
450 | } else if (dobackup) {
|
---|
451 | why_not = "backup (-b/-B)";
|
---|
452 | } else if (safecopy) {
|
---|
453 | why_not = "safe copy (-S)";
|
---|
454 | # if defined(KBUILD_OS_WINDOWS) || defined(KBUILD_OS_OS2)
|
---|
455 | } else if ((mode & S_IWUSR) != (from_sb.st_mode & S_IWUSR)) {
|
---|
456 | # else
|
---|
457 | } else if (mode != (from_sb.st_mode & ALLPERMS)) {
|
---|
458 | # endif
|
---|
459 | printf("install: warning: Not hard linking, mode differs: 0%03o, desires 0%03o\n"
|
---|
460 | "install: src path '%s'\n"
|
---|
461 | "install: dst path '%s'\n",
|
---|
462 | (from_sb.st_mode & ALLPERMS), mode, from_name, to_name);
|
---|
463 | why_not = NULL;
|
---|
464 | } else if (uid != (uid_t)-1 && gid != from_sb.st_uid) {
|
---|
465 | why_not = "uid mismatch";
|
---|
466 | } else if (gid != (gid_t)-1 && gid != from_sb.st_gid) {
|
---|
467 | why_not = "gid mismatch";
|
---|
468 | } else {
|
---|
469 | int rcLink = link(from_name, to_name);
|
---|
470 | if (rcLink != 0 && errno == EEXIST) {
|
---|
471 | unlink(to_name);
|
---|
472 | rcLink = link(from_name, to_name);
|
---|
473 | }
|
---|
474 | if (rcLink == 0) {
|
---|
475 | if (verbose)
|
---|
476 | printf("install: %s -> %s (hardlinked)\n", from_name, to_name);
|
---|
477 | goto l_done;
|
---|
478 | }
|
---|
479 | if (verbose)
|
---|
480 | printf("install: hard linking '%s' to '%s' failed: %s\n",
|
---|
481 | to_name, from_name, strerror(errno));
|
---|
482 | why_not = NULL;
|
---|
483 | }
|
---|
484 | #endif
|
---|
485 | if (verbose && why_not)
|
---|
486 | printf("install: not hard linking '%s' to '%s' because: %s\n",
|
---|
487 | to_name, from_name, why_not);
|
---|
488 |
|
---|
489 | /* Can't hard link or we failed, continue as nothing happend. */
|
---|
490 | }
|
---|
491 |
|
---|
492 | if (!devnull && (from_fd = open(from_name, O_RDONLY | O_BINARY, 0)) < 0)
|
---|
493 | return err(EX_OSERR, "%s", from_name);
|
---|
494 |
|
---|
495 | /* If we don't strip, we can compare first. */
|
---|
496 | if (docompare && !dostrip && target) {
|
---|
497 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
498 | rc = err(EX_OSERR, "%s", to_name);
|
---|
499 | goto l_done;
|
---|
500 | }
|
---|
501 | if (devnull)
|
---|
502 | files_match = to_sb.st_size == 0;
|
---|
503 | else
|
---|
504 | files_match = !(compare(from_fd, from_name,
|
---|
505 | (size_t)from_sb.st_size, to_fd,
|
---|
506 | to_name, (size_t)to_sb.st_size));
|
---|
507 |
|
---|
508 | /* Close "to" file unless we match. */
|
---|
509 | if (!files_match) {
|
---|
510 | (void)close(to_fd);
|
---|
511 | to_fd = -1;
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | if (!files_match) {
|
---|
516 | if (tempcopy) {
|
---|
517 | to_fd = create_tempfile(to_name, tempfile,
|
---|
518 | sizeof(tempfile));
|
---|
519 | if (to_fd < 0) {
|
---|
520 | rc = err(EX_OSERR, "%s", tempfile);
|
---|
521 | goto l_done;
|
---|
522 | }
|
---|
523 | } else {
|
---|
524 | if ((to_fd = create_newfile(to_name, target,
|
---|
525 | &to_sb)) < 0) {
|
---|
526 | rc = err(EX_OSERR, "%s", to_name);
|
---|
527 | goto l_done;
|
---|
528 | }
|
---|
529 | if (verbose)
|
---|
530 | (void)printf("install: %s -> %s\n",
|
---|
531 | from_name, to_name);
|
---|
532 | }
|
---|
533 | if (!devnull) {
|
---|
534 | rc = copy(from_fd, from_name, to_fd,
|
---|
535 | tempcopy ? tempfile : to_name, from_sb.st_size);
|
---|
536 | if (rc)
|
---|
537 | goto l_done;
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | if (dostrip) {
|
---|
542 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
543 | /* close before we strip. */
|
---|
544 | close(to_fd);
|
---|
545 | to_fd = -1;
|
---|
546 | #endif
|
---|
547 | rc = strip(tempcopy ? tempfile : to_name);
|
---|
548 | if (rc)
|
---|
549 | goto l_done;
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * Re-open our fd on the target, in case we used a strip
|
---|
553 | * that does not work in-place -- like GNU binutils strip.
|
---|
554 | */
|
---|
555 | #if !defined(__EMX__) && !defined(_MSC_VER)
|
---|
556 | close(to_fd);
|
---|
557 | #endif
|
---|
558 | to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY | O_BINARY, 0);
|
---|
559 | if (to_fd < 0) {
|
---|
560 | rc = err(EX_OSERR, "stripping %s", to_name);
|
---|
561 | goto l_done;
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 | /*
|
---|
566 | * Compare the stripped temp file with the target.
|
---|
567 | */
|
---|
568 | if (docompare && dostrip && target) {
|
---|
569 | temp_fd = to_fd;
|
---|
570 |
|
---|
571 | /* Re-open to_fd using the real target name. */
|
---|
572 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
573 | rc = err(EX_OSERR, "%s", to_name);
|
---|
574 | goto l_done;
|
---|
575 | }
|
---|
576 |
|
---|
577 | if (fstat(temp_fd, &temp_sb)) {
|
---|
578 | serrno = errno;
|
---|
579 | (void)unlink(tempfile);
|
---|
580 | errno = serrno;
|
---|
581 | rc = err(EX_OSERR, "%s", tempfile);
|
---|
582 | goto l_done;
|
---|
583 | }
|
---|
584 |
|
---|
585 | if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
|
---|
586 | to_name, (size_t)to_sb.st_size) == 0) {
|
---|
587 | /*
|
---|
588 | * If target has more than one link we need to
|
---|
589 | * replace it in order to snap the extra links.
|
---|
590 | * Need to preserve target file times, though.
|
---|
591 | */
|
---|
592 | #if !defined(_MSC_VER) && !defined(__EMX__)
|
---|
593 | if (to_sb.st_nlink != 1) {
|
---|
594 | tvb[0].tv_sec = to_sb.st_atime;
|
---|
595 | tvb[0].tv_usec = 0;
|
---|
596 | tvb[1].tv_sec = to_sb.st_mtime;
|
---|
597 | tvb[1].tv_usec = 0;
|
---|
598 | (void)utimes(tempfile, tvb);
|
---|
599 | } else
|
---|
600 | #endif
|
---|
601 | {
|
---|
602 |
|
---|
603 | files_match = 1;
|
---|
604 | (void)unlink(tempfile);
|
---|
605 | }
|
---|
606 | (void) close(temp_fd);
|
---|
607 | temp_fd = -1;
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | /*
|
---|
612 | * Move the new file into place if doing a safe copy
|
---|
613 | * and the files are different (or just not compared).
|
---|
614 | */
|
---|
615 | if (tempcopy && !files_match) {
|
---|
616 | #ifdef UF_IMMUTABLE
|
---|
617 | /* Try to turn off the immutable bits. */
|
---|
618 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
619 | (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
620 | #endif
|
---|
621 | if (dobackup) {
|
---|
622 | if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
|
---|
623 | suffix) != strlen(to_name) + strlen(suffix)) {
|
---|
624 | unlink(tempfile);
|
---|
625 | rc = errx(EX_OSERR, "%s: backup filename too long",
|
---|
626 | to_name);
|
---|
627 | goto l_done;
|
---|
628 | }
|
---|
629 | if (verbose)
|
---|
630 | (void)printf("install: %s -> %s\n", to_name, backup);
|
---|
631 | if (rename(to_name, backup) < 0) {
|
---|
632 | serrno = errno;
|
---|
633 | unlink(tempfile);
|
---|
634 | errno = serrno;
|
---|
635 | rc = err(EX_OSERR, "rename: %s to %s", to_name,
|
---|
636 | backup);
|
---|
637 | goto l_done;
|
---|
638 | }
|
---|
639 | }
|
---|
640 | if (verbose)
|
---|
641 | (void)printf("install: %s -> %s\n", from_name, to_name);
|
---|
642 | if (rename(tempfile, to_name) < 0) {
|
---|
643 | serrno = errno;
|
---|
644 | unlink(tempfile);
|
---|
645 | errno = serrno;
|
---|
646 | rc = err(EX_OSERR, "rename: %s to %s",
|
---|
647 | tempfile, to_name);
|
---|
648 | goto l_done;
|
---|
649 | }
|
---|
650 |
|
---|
651 | /* Re-open to_fd so we aren't hosed by the rename(2). */
|
---|
652 | (void) close(to_fd);
|
---|
653 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
654 | rc = err(EX_OSERR, "%s", to_name);
|
---|
655 | goto l_done;
|
---|
656 | }
|
---|
657 | }
|
---|
658 |
|
---|
659 | /*
|
---|
660 | * Preserve the timestamp of the source file if necessary.
|
---|
661 | */
|
---|
662 | if (dopreserve && !files_match && !devnull) {
|
---|
663 | tvb[0].tv_sec = from_sb.st_atime;
|
---|
664 | tvb[0].tv_usec = 0;
|
---|
665 | tvb[1].tv_sec = from_sb.st_mtime;
|
---|
666 | tvb[1].tv_usec = 0;
|
---|
667 | (void)utimes(to_name, tvb);
|
---|
668 | }
|
---|
669 |
|
---|
670 | if (fstat(to_fd, &to_sb) == -1) {
|
---|
671 | serrno = errno;
|
---|
672 | (void)unlink(to_name);
|
---|
673 | errno = serrno;
|
---|
674 | rc = err(EX_OSERR, "%s", to_name);
|
---|
675 | goto l_done;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /*
|
---|
679 | * Set owner, group, mode for target; do the chown first,
|
---|
680 | * chown may lose the setuid bits.
|
---|
681 | */
|
---|
682 | #ifdef UF_IMMUTABLE
|
---|
683 | if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
---|
684 | (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
|
---|
685 | (mode != (to_sb.st_mode & ALLPERMS))) {
|
---|
686 | /* Try to turn off the immutable bits. */
|
---|
687 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
688 | (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
689 | }
|
---|
690 | #endif
|
---|
691 |
|
---|
692 | if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
---|
693 | (uid != (uid_t)-1 && uid != to_sb.st_uid))
|
---|
694 | if (fchown(to_fd, uid, gid) == -1) {
|
---|
695 | if (errno == EPERM && ignore_perm_errors) {
|
---|
696 | warn("%s: ignoring chown uid=%d gid=%d failure", to_name, (int)uid, (int)gid);
|
---|
697 | } else {
|
---|
698 | serrno = errno;
|
---|
699 | (void)unlink(to_name);
|
---|
700 | errno = serrno;
|
---|
701 | rc = err(EX_OSERR,"%s: chown/chgrp", to_name);
|
---|
702 | goto l_done;
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 | if (mode != (to_sb.st_mode & ALLPERMS))
|
---|
707 | if (fchmod(to_fd, mode)) {
|
---|
708 | serrno = errno;
|
---|
709 | if (serrno == EPERM && ignore_perm_errors) {
|
---|
710 | fchmod(to_fd, mode & (ALLPERMS & ~0007000));
|
---|
711 | errno = errno;
|
---|
712 | warn("%s: ignoring chmod 0%o failure", to_name, (int)(mode & ALLPERMS));
|
---|
713 | } else {
|
---|
714 | serrno = errno;
|
---|
715 | (void)unlink(to_name);
|
---|
716 | errno = serrno;
|
---|
717 | rc = err(EX_OSERR, "%s: chmod", to_name);
|
---|
718 | goto l_done;
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | /*
|
---|
723 | * If provided a set of flags, set them, otherwise, preserve the
|
---|
724 | * flags, except for the dump flag.
|
---|
725 | * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
|
---|
726 | * trying to turn off UF_NODUMP. If we're trying to set real flags,
|
---|
727 | * then warn if the the fs doesn't support it, otherwise fail.
|
---|
728 | */
|
---|
729 | #ifdef UF_IMMUTABLE
|
---|
730 | if (!devnull && (flags & SETFLAGS ||
|
---|
731 | (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
|
---|
732 | fchflags(to_fd,
|
---|
733 | flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
|
---|
734 | if (flags & SETFLAGS) {
|
---|
735 | if (errno == EOPNOTSUPP)
|
---|
736 | warn("%s: chflags", to_name);
|
---|
737 | else {
|
---|
738 | serrno = errno;
|
---|
739 | (void)unlink(to_name);
|
---|
740 | errno = serrno;
|
---|
741 | rc = err(EX_OSERR, "%s: chflags", to_name);
|
---|
742 | goto l_done;
|
---|
743 | }
|
---|
744 | }
|
---|
745 | }
|
---|
746 | #endif
|
---|
747 |
|
---|
748 | l_done:
|
---|
749 | if (to_fd >= 0)
|
---|
750 | (void)close(to_fd);
|
---|
751 | if (temp_fd >= 0)
|
---|
752 | (void)close(temp_fd);
|
---|
753 | if (from_fd >= 0 && !devnull)
|
---|
754 | (void)close(from_fd);
|
---|
755 | return rc;
|
---|
756 | }
|
---|
757 |
|
---|
758 | /*
|
---|
759 | * compare --
|
---|
760 | * compare two files; non-zero means files differ
|
---|
761 | */
|
---|
762 | static int
|
---|
763 | compare(int from_fd, const char *from_name __unused, size_t from_len,
|
---|
764 | int to_fd, const char *to_name __unused, size_t to_len)
|
---|
765 | {
|
---|
766 | char *p, *q;
|
---|
767 | int rv;
|
---|
768 | int done_compare;
|
---|
769 |
|
---|
770 | rv = 0;
|
---|
771 | if (from_len != to_len)
|
---|
772 | return 1;
|
---|
773 |
|
---|
774 | if (from_len <= MAX_CMP_SIZE) {
|
---|
775 | #ifdef USE_MMAP
|
---|
776 | done_compare = 0;
|
---|
777 | if (trymmap(from_fd) && trymmap(to_fd)) {
|
---|
778 | p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
|
---|
779 | if (p == (char *)MAP_FAILED)
|
---|
780 | goto out;
|
---|
781 | q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
|
---|
782 | if (q == (char *)MAP_FAILED) {
|
---|
783 | munmap(p, from_len);
|
---|
784 | goto out;
|
---|
785 | }
|
---|
786 |
|
---|
787 | rv = memcmp(p, q, from_len);
|
---|
788 | munmap(p, from_len);
|
---|
789 | munmap(q, from_len);
|
---|
790 | done_compare = 1;
|
---|
791 | }
|
---|
792 | out:
|
---|
793 | #else
|
---|
794 | (void)p; (void)q;
|
---|
795 | done_compare = 0;
|
---|
796 | #endif
|
---|
797 | if (!done_compare) {
|
---|
798 | char buf1[MAXBSIZE];
|
---|
799 | char buf2[MAXBSIZE];
|
---|
800 | int n1, n2;
|
---|
801 |
|
---|
802 | rv = 0;
|
---|
803 | lseek(from_fd, 0, SEEK_SET);
|
---|
804 | lseek(to_fd, 0, SEEK_SET);
|
---|
805 | while (rv == 0) {
|
---|
806 | n1 = read(from_fd, buf1, sizeof(buf1));
|
---|
807 | if (n1 == 0)
|
---|
808 | break; /* EOF */
|
---|
809 | else if (n1 > 0) {
|
---|
810 | n2 = read(to_fd, buf2, n1);
|
---|
811 | if (n2 == n1)
|
---|
812 | rv = memcmp(buf1, buf2, n1);
|
---|
813 | else
|
---|
814 | rv = 1; /* out of sync */
|
---|
815 | } else
|
---|
816 | rv = 1; /* read failure */
|
---|
817 | }
|
---|
818 | lseek(from_fd, 0, SEEK_SET);
|
---|
819 | lseek(to_fd, 0, SEEK_SET);
|
---|
820 | }
|
---|
821 | } else
|
---|
822 | rv = 1; /* don't bother in this case */
|
---|
823 |
|
---|
824 | return rv;
|
---|
825 | }
|
---|
826 |
|
---|
827 | /*
|
---|
828 | * create_tempfile --
|
---|
829 | * create a temporary file based on path and open it
|
---|
830 | */
|
---|
831 | int
|
---|
832 | create_tempfile(const char *path, char *temp, size_t tsize)
|
---|
833 | {
|
---|
834 | char *p;
|
---|
835 |
|
---|
836 | (void)strncpy(temp, path, tsize);
|
---|
837 | temp[tsize - 1] = '\0';
|
---|
838 | if ((p = last_slash(temp)) != NULL)
|
---|
839 | p++;
|
---|
840 | else
|
---|
841 | p = temp;
|
---|
842 | (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
|
---|
843 | temp[tsize - 1] = '\0';
|
---|
844 | return (mkstemp(temp));
|
---|
845 | }
|
---|
846 |
|
---|
847 | /*
|
---|
848 | * create_newfile --
|
---|
849 | * create a new file, overwriting an existing one if necessary
|
---|
850 | */
|
---|
851 | int
|
---|
852 | create_newfile(const char *path, int target, struct stat *sbp)
|
---|
853 | {
|
---|
854 | char backup[MAXPATHLEN];
|
---|
855 | int saved_errno = 0;
|
---|
856 | int newfd;
|
---|
857 |
|
---|
858 | if (target) {
|
---|
859 | /*
|
---|
860 | * Unlink now... avoid ETXTBSY errors later. Try to turn
|
---|
861 | * off the append/immutable bits -- if we fail, go ahead,
|
---|
862 | * it might work.
|
---|
863 | */
|
---|
864 | #ifdef UF_IMMUTABLE
|
---|
865 | if (sbp->st_flags & NOCHANGEBITS)
|
---|
866 | (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
|
---|
867 | #endif
|
---|
868 |
|
---|
869 | if (dobackup) {
|
---|
870 | if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
|
---|
871 | path, suffix) != strlen(path) + strlen(suffix)) {
|
---|
872 | errx(EX_OSERR, "%s: backup filename too long",
|
---|
873 | path);
|
---|
874 | errno = ENAMETOOLONG;
|
---|
875 | return -1;
|
---|
876 | }
|
---|
877 | (void)snprintf(backup, MAXPATHLEN, "%s%s",
|
---|
878 | path, suffix);
|
---|
879 | if (verbose)
|
---|
880 | (void)printf("install: %s -> %s\n",
|
---|
881 | path, backup);
|
---|
882 | if (rename(path, backup) < 0) {
|
---|
883 | err(EX_OSERR, "rename: %s to %s", path, backup);
|
---|
884 | return -1;
|
---|
885 | }
|
---|
886 | } else
|
---|
887 | if (unlink(path) < 0)
|
---|
888 | saved_errno = errno;
|
---|
889 | }
|
---|
890 |
|
---|
891 | newfd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
|
---|
892 | if (newfd < 0 && saved_errno != 0)
|
---|
893 | errno = saved_errno;
|
---|
894 | return newfd;
|
---|
895 | }
|
---|
896 |
|
---|
897 | /*
|
---|
898 | * copy --
|
---|
899 | * copy from one file to another
|
---|
900 | */
|
---|
901 | static int
|
---|
902 | copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
|
---|
903 | off_t size)
|
---|
904 | {
|
---|
905 | int nr, nw;
|
---|
906 | int serrno;
|
---|
907 | char *p, buf[MAXBSIZE];
|
---|
908 | int done_copy;
|
---|
909 |
|
---|
910 | /* Rewind file descriptors. */
|
---|
911 | if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
912 | return err(EX_OSERR, "lseek: %s", from_name);
|
---|
913 | if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
914 | return err(EX_OSERR, "lseek: %s", to_name);
|
---|
915 |
|
---|
916 | /*
|
---|
917 | * Mmap and write if less than 8M (the limit is so we don't totally
|
---|
918 | * trash memory on big files. This is really a minor hack, but it
|
---|
919 | * wins some CPU back.
|
---|
920 | */
|
---|
921 | done_copy = 0;
|
---|
922 | #ifdef USE_MMAP
|
---|
923 | if (size <= 8 * 1048576 && trymmap(from_fd) &&
|
---|
924 | (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
|
---|
925 | from_fd, (off_t)0)) != (char *)MAP_FAILED) {
|
---|
926 | if ((nw = write(to_fd, p, size)) != size) {
|
---|
927 | serrno = errno;
|
---|
928 | (void)unlink(to_name);
|
---|
929 | errno = nw > 0 ? EIO : serrno;
|
---|
930 | err(EX_OSERR, "%s", to_name);
|
---|
931 | }
|
---|
932 | done_copy = 1;
|
---|
933 | }
|
---|
934 | #else
|
---|
935 | (void)p; (void)size;
|
---|
936 | #endif
|
---|
937 | if (!done_copy) {
|
---|
938 | while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
|
---|
939 | if ((nw = write(to_fd, buf, nr)) != nr) {
|
---|
940 | serrno = errno;
|
---|
941 | (void)unlink(to_name);
|
---|
942 | errno = nw > 0 ? EIO : serrno;
|
---|
943 | return err(EX_OSERR, "%s", to_name);
|
---|
944 | }
|
---|
945 | if (nr != 0) {
|
---|
946 | serrno = errno;
|
---|
947 | (void)unlink(to_name);
|
---|
948 | errno = serrno;
|
---|
949 | return err(EX_OSERR, "%s", from_name);
|
---|
950 | }
|
---|
951 | }
|
---|
952 | return EX_OK;
|
---|
953 | }
|
---|
954 |
|
---|
955 | /*
|
---|
956 | * strip --
|
---|
957 | * use strip(1) to strip the target file
|
---|
958 | */
|
---|
959 | static int
|
---|
960 | strip(const char *to_name)
|
---|
961 | {
|
---|
962 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
963 | const char *stripbin = getenv("STRIPBIN");
|
---|
964 | if (stripbin == NULL)
|
---|
965 | stripbin = "strip";
|
---|
966 | return spawnlp(P_WAIT, stripbin, stripbin, to_name, NULL);
|
---|
967 | #else
|
---|
968 | const char *stripbin;
|
---|
969 | int serrno, status;
|
---|
970 | pid_t pid;
|
---|
971 |
|
---|
972 | pid = fork();
|
---|
973 | switch (pid) {
|
---|
974 | case -1:
|
---|
975 | serrno = errno;
|
---|
976 | (void)unlink(to_name);
|
---|
977 | errno = serrno;
|
---|
978 | return err(EX_TEMPFAIL, "fork");
|
---|
979 | case 0:
|
---|
980 | stripbin = getenv("STRIPBIN");
|
---|
981 | if (stripbin == NULL)
|
---|
982 | stripbin = "strip";
|
---|
983 | execlp(stripbin, stripbin, to_name, (char *)NULL);
|
---|
984 | err(EX_OSERR, "exec(%s)", stripbin);
|
---|
985 | exit(EX_OSERR);
|
---|
986 | default:
|
---|
987 | if (waitpid(pid, &status, 0) == -1 || status) {
|
---|
988 | serrno = errno;
|
---|
989 | (void)unlink(to_name);
|
---|
990 | errno = serrno;
|
---|
991 | return err(EX_SOFTWARE, "waitpid");
|
---|
992 | /* NOTREACHED */
|
---|
993 | }
|
---|
994 | }
|
---|
995 | return 0;
|
---|
996 | #endif
|
---|
997 | }
|
---|
998 |
|
---|
999 | /*
|
---|
1000 | * install_dir --
|
---|
1001 | * build directory heirarchy
|
---|
1002 | */
|
---|
1003 | static int
|
---|
1004 | install_dir(char *path)
|
---|
1005 | {
|
---|
1006 | char *p;
|
---|
1007 | struct stat sb;
|
---|
1008 | int ch;
|
---|
1009 |
|
---|
1010 | for (p = path;; ++p)
|
---|
1011 | if (!*p || (p != path && IS_SLASH(*p))) {
|
---|
1012 | ch = *p;
|
---|
1013 | *p = '\0';
|
---|
1014 | if (stat(path, &sb)) {
|
---|
1015 | if (errno != ENOENT || mkdir(path, 0755) < 0) {
|
---|
1016 | return err(EX_OSERR, "mkdir %s", path);
|
---|
1017 | /* NOTREACHED */
|
---|
1018 | } else if (verbose)
|
---|
1019 | (void)printf("install: mkdir %s\n",
|
---|
1020 | path);
|
---|
1021 | } else if (!S_ISDIR(sb.st_mode))
|
---|
1022 | return errx(EX_OSERR, "%s exists but is not a directory", path);
|
---|
1023 | if (!(*p = ch))
|
---|
1024 | break;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
|
---|
1028 | warn("chown %u:%u %s", uid, gid, path);
|
---|
1029 | if (chmod(path, mode))
|
---|
1030 | warn("chmod %o %s", mode, path);
|
---|
1031 | return EX_OK;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | /*
|
---|
1035 | * usage --
|
---|
1036 | * print a usage message and die
|
---|
1037 | */
|
---|
1038 | static int
|
---|
1039 | usage(FILE *pf)
|
---|
1040 | {
|
---|
1041 | fprintf(pf,
|
---|
1042 | "usage: %s [-bCcpSsv] [--[no-]hard-link-files-when-possible]\n"
|
---|
1043 | " [--[no-]ignore-perm-errors] [-B suffix] [-f flags]\n"
|
---|
1044 | " [-g group] [-m mode] [-o owner] file1 file2\n"
|
---|
1045 | " or: %s [-bCcpSsv] [--[no-]ignore-perm-errors] [-B suffix] [-f flags]\n"
|
---|
1046 | " [-g group] [-m mode] [-o owner] file1 ... fileN directory\n"
|
---|
1047 | " or: %s -d [-v] [-g group] [-m mode] [-o owner] directory ...\n"
|
---|
1048 | " or: %s --help\n"
|
---|
1049 | " or: %s --version\n",
|
---|
1050 | g_progname, g_progname, g_progname, g_progname, g_progname);
|
---|
1051 | return EX_USAGE;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | #ifdef USE_MMAP
|
---|
1055 | /*
|
---|
1056 | * trymmap --
|
---|
1057 | * return true (1) if mmap should be tried, false (0) if not.
|
---|
1058 | */
|
---|
1059 | static int
|
---|
1060 | trymmap(int fd)
|
---|
1061 | {
|
---|
1062 | /*
|
---|
1063 | * The ifdef is for bootstrapping - f_fstypename doesn't exist in
|
---|
1064 | * pre-Lite2-merge systems.
|
---|
1065 | */
|
---|
1066 | #ifdef MFSNAMELEN
|
---|
1067 | struct statfs stfs;
|
---|
1068 |
|
---|
1069 | if (nommap || fstatfs(fd, &stfs) != 0)
|
---|
1070 | return (0);
|
---|
1071 | if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
|
---|
1072 | strcmp(stfs.f_fstypename, "cd9660") == 0)
|
---|
1073 | return (1);
|
---|
1074 | #endif
|
---|
1075 | return (0);
|
---|
1076 | }
|
---|
1077 | #endif
|
---|
1078 |
|
---|
1079 | /* figures out where the last slash or colon is. */
|
---|
1080 | static char *
|
---|
1081 | last_slash(const char *path)
|
---|
1082 | {
|
---|
1083 | #if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
|
---|
1084 | char *p = (char *)strrchr(path, '/');
|
---|
1085 | if (p)
|
---|
1086 | {
|
---|
1087 | char *p2 = strrchr(p, '\\');
|
---|
1088 | if (p2)
|
---|
1089 | p = p2;
|
---|
1090 | }
|
---|
1091 | else
|
---|
1092 | {
|
---|
1093 | p = (char *)strrchr(path, '\\');
|
---|
1094 | if (!p && isalpha(path[0]) && path[1] == ':')
|
---|
1095 | p = (char *)&path[1];
|
---|
1096 | }
|
---|
1097 | return p;
|
---|
1098 | #else
|
---|
1099 | return strrchr(path, '/');
|
---|
1100 | #endif
|
---|
1101 | }
|
---|
1102 |
|
---|