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 | #ifndef _MSC_VER
|
---|
50 | # include <sys/param.h>
|
---|
51 | # ifdef USE_MMAP
|
---|
52 | # include <sys/mman.h>
|
---|
53 | # endif
|
---|
54 | # include <sys/mount.h>
|
---|
55 | # include <sys/wait.h>
|
---|
56 | # include <sys/time.h>
|
---|
57 | #endif /* !_MSC_VER */
|
---|
58 | #include <sys/stat.h>
|
---|
59 |
|
---|
60 | #include <ctype.h>
|
---|
61 | #include "err.h"
|
---|
62 | #include <errno.h>
|
---|
63 | #include <fcntl.h>
|
---|
64 | #include <grp.h>
|
---|
65 | #include <paths.h>
|
---|
66 | #include <pwd.h>
|
---|
67 | #include <stdio.h>
|
---|
68 | #include <stdlib.h>
|
---|
69 | #include <string.h>
|
---|
70 | #include <sysexits.h>
|
---|
71 | #include <unistd.h>
|
---|
72 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
73 | # include <process.h>
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | #ifdef __sun__
|
---|
77 | # include "solfakes.h"
|
---|
78 | #endif
|
---|
79 | #ifdef _MSC_VER
|
---|
80 | # include "mscfakes.h"
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | #include "kmkbuiltin.h"
|
---|
84 |
|
---|
85 |
|
---|
86 | extern void * setmode(const char *p);
|
---|
87 | extern mode_t 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;
|
---|
126 | static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
---|
127 | static const char *suffix = BACKUP_SUFFIX;
|
---|
128 |
|
---|
129 | static struct option long_options[] =
|
---|
130 | {
|
---|
131 | { "help", no_argument, 0, 261 },
|
---|
132 | { "version", no_argument, 0, 262 },
|
---|
133 | { 0, 0, 0, 0 },
|
---|
134 | };
|
---|
135 |
|
---|
136 |
|
---|
137 | static int copy(int, const char *, int, const char *, off_t);
|
---|
138 | static int compare(int, const char *, size_t, int, const char *, size_t);
|
---|
139 | static int create_newfile(const char *, int, struct stat *);
|
---|
140 | static int create_tempfile(const char *, char *, size_t);
|
---|
141 | static int install(const char *, const char *, u_long, u_int);
|
---|
142 | static int install_dir(char *);
|
---|
143 | static u_long numeric_id(const char *, const char *);
|
---|
144 | static int strip(const char *);
|
---|
145 | #ifdef USE_MMAP
|
---|
146 | static int trymmap(int);
|
---|
147 | #endif
|
---|
148 | static int usage(FILE *);
|
---|
149 | static char *last_slash(const char *);
|
---|
150 |
|
---|
151 | int
|
---|
152 | kmk_builtin_install(int argc, char *argv[], char **envp)
|
---|
153 | {
|
---|
154 | struct stat from_sb, to_sb;
|
---|
155 | mode_t *set;
|
---|
156 | u_long fset = 0;
|
---|
157 | int ch, no_target;
|
---|
158 | u_int iflags;
|
---|
159 | char *flags;
|
---|
160 | const char *group, *owner, *to_name;
|
---|
161 |
|
---|
162 | /* reinitialize globals */
|
---|
163 | mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
---|
164 | suffix = BACKUP_SUFFIX;
|
---|
165 | gid = 0;
|
---|
166 | uid = 0;
|
---|
167 | dobackup = docompare = dodir = dopreserve = dostrip = nommap = safecopy = verbose = 0;
|
---|
168 |
|
---|
169 | /* reset getopt and set progname. */
|
---|
170 | g_progname = argv[0];
|
---|
171 | opterr = 1;
|
---|
172 | optarg = NULL;
|
---|
173 | optopt = 0;
|
---|
174 | optind = 0; /* init */
|
---|
175 |
|
---|
176 | iflags = 0;
|
---|
177 | group = owner = NULL;
|
---|
178 | while ((ch = getopt_long(argc, argv, "B:bCcdf:g:Mm:o:pSsv", long_options, NULL)) != -1)
|
---|
179 | switch((char)ch) {
|
---|
180 | case 'B':
|
---|
181 | suffix = optarg;
|
---|
182 | /* FALLTHROUGH */
|
---|
183 | case 'b':
|
---|
184 | dobackup = 1;
|
---|
185 | break;
|
---|
186 | case 'C':
|
---|
187 | docompare = 1;
|
---|
188 | break;
|
---|
189 | case 'c':
|
---|
190 | /* For backwards compatibility. */
|
---|
191 | break;
|
---|
192 | case 'd':
|
---|
193 | dodir = 1;
|
---|
194 | break;
|
---|
195 | case 'f':
|
---|
196 | #ifdef UF_IMMUTABLE
|
---|
197 | flags = optarg;
|
---|
198 | if (strtofflags(&flags, &fset, NULL))
|
---|
199 | return errx(EX_USAGE, "%s: invalid flag", flags);
|
---|
200 | iflags |= SETFLAGS;
|
---|
201 | #else
|
---|
202 | (void)flags;
|
---|
203 | #endif
|
---|
204 | break;
|
---|
205 | case 'g':
|
---|
206 | group = optarg;
|
---|
207 | break;
|
---|
208 | case 'M':
|
---|
209 | nommap = 1;
|
---|
210 | break;
|
---|
211 | case 'm':
|
---|
212 | #ifdef __EMX__
|
---|
213 | if (!(set = bsd_setmode(optarg)))
|
---|
214 | #else
|
---|
215 | if (!(set = setmode(optarg)))
|
---|
216 | #endif
|
---|
217 | return errx(EX_USAGE, "invalid file mode: %s",
|
---|
218 | optarg);
|
---|
219 | mode = getmode(set, 0);
|
---|
220 | free(set);
|
---|
221 | break;
|
---|
222 | case 'o':
|
---|
223 | owner = optarg;
|
---|
224 | break;
|
---|
225 | case 'p':
|
---|
226 | docompare = dopreserve = 1;
|
---|
227 | break;
|
---|
228 | case 'S':
|
---|
229 | safecopy = 1;
|
---|
230 | break;
|
---|
231 | case 's':
|
---|
232 | dostrip = 1;
|
---|
233 | break;
|
---|
234 | case 'v':
|
---|
235 | verbose = 1;
|
---|
236 | break;
|
---|
237 | case 261:
|
---|
238 | usage(stdout);
|
---|
239 | return 0;
|
---|
240 | case 262:
|
---|
241 | return kbuild_version(argv[0]);
|
---|
242 | case '?':
|
---|
243 | default:
|
---|
244 | return usage(stderr);
|
---|
245 | }
|
---|
246 | argc -= optind;
|
---|
247 | argv += optind;
|
---|
248 |
|
---|
249 | /* some options make no sense when creating directories */
|
---|
250 | if (dostrip && dodir) {
|
---|
251 | warnx("-d and -s may not be specified together");
|
---|
252 | return usage(stderr);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /* must have at least two arguments, except when creating directories */
|
---|
256 | if (argc == 0 || (argc == 1 && !dodir))
|
---|
257 | return usage(stderr);
|
---|
258 |
|
---|
259 | /* need to make a temp copy so we can compare stripped version */
|
---|
260 | if (docompare && dostrip)
|
---|
261 | safecopy = 1;
|
---|
262 |
|
---|
263 | /* get group and owner id's */
|
---|
264 | if (group != NULL) {
|
---|
265 | #ifndef _MSC_VER
|
---|
266 | struct group *gp;
|
---|
267 | if ((gp = getgrnam(group)) != NULL)
|
---|
268 | gid = gp->gr_gid;
|
---|
269 | else
|
---|
270 | #endif
|
---|
271 | {
|
---|
272 | gid = (gid_t)numeric_id(group, "group");
|
---|
273 | if (gid == (gid_t)-1)
|
---|
274 | return 1;
|
---|
275 | }
|
---|
276 | } else
|
---|
277 | gid = (gid_t)-1;
|
---|
278 |
|
---|
279 | if (owner != NULL) {
|
---|
280 | #ifndef _MSC_VER
|
---|
281 | struct passwd *pp;
|
---|
282 | if ((pp = getpwnam(owner)) != NULL)
|
---|
283 | uid = pp->pw_uid;
|
---|
284 | else
|
---|
285 | #endif
|
---|
286 | {
|
---|
287 | uid = (uid_t)numeric_id(owner, "user");
|
---|
288 | if (uid == (uid_t)-1)
|
---|
289 | return 1;
|
---|
290 | }
|
---|
291 | } else
|
---|
292 | uid = (uid_t)-1;
|
---|
293 |
|
---|
294 | if (dodir) {
|
---|
295 | for (; *argv != NULL; ++argv) {
|
---|
296 | int rc = install_dir(*argv);
|
---|
297 | if (rc)
|
---|
298 | return rc;
|
---|
299 | }
|
---|
300 | return EX_OK;
|
---|
301 | /* NOTREACHED */
|
---|
302 | }
|
---|
303 |
|
---|
304 | no_target = stat(to_name = argv[argc - 1], &to_sb);
|
---|
305 | if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
---|
306 | for (; *argv != to_name; ++argv) {
|
---|
307 | int rc = install(*argv, to_name, fset, iflags | DIRECTORY);
|
---|
308 | if (rc)
|
---|
309 | return rc;
|
---|
310 | }
|
---|
311 | return EX_OK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /* can't do file1 file2 directory/file */
|
---|
315 | if (argc != 2) {
|
---|
316 | warnx("wrong number or types of arguments");
|
---|
317 | return usage(stderr);
|
---|
318 | }
|
---|
319 |
|
---|
320 | if (!no_target) {
|
---|
321 | if (stat(*argv, &from_sb))
|
---|
322 | return err(EX_OSERR, "%s", *argv);
|
---|
323 | if (!S_ISREG(to_sb.st_mode)) {
|
---|
324 | errno = EFTYPE;
|
---|
325 | return err(EX_OSERR, "%s", to_name);
|
---|
326 | }
|
---|
327 | if (to_sb.st_dev == from_sb.st_dev &&
|
---|
328 | to_sb.st_dev != 0 &&
|
---|
329 | to_sb.st_ino == from_sb.st_ino &&
|
---|
330 | to_sb.st_ino != 0)
|
---|
331 | return errx(EX_USAGE,
|
---|
332 | "%s and %s are the same file", *argv, to_name);
|
---|
333 | }
|
---|
334 | return install(*argv, to_name, fset, iflags);
|
---|
335 | }
|
---|
336 |
|
---|
337 | static u_long
|
---|
338 | numeric_id(const char *name, const char *type)
|
---|
339 | {
|
---|
340 | u_long val;
|
---|
341 | char *ep;
|
---|
342 |
|
---|
343 | /*
|
---|
344 | * XXX
|
---|
345 | * We know that uid_t's and gid_t's are unsigned longs.
|
---|
346 | */
|
---|
347 | errno = 0;
|
---|
348 | val = strtoul(name, &ep, 10);
|
---|
349 | if (errno)
|
---|
350 | return err(-1, "%s", name);
|
---|
351 | if (*ep != '\0')
|
---|
352 | return errx(-1, "unknown %s %s", type, name);
|
---|
353 | return (val);
|
---|
354 | }
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * install --
|
---|
358 | * build a path name and install the file
|
---|
359 | */
|
---|
360 | static int
|
---|
361 | install(const char *from_name, const char *to_name, u_long fset, u_int flags)
|
---|
362 | {
|
---|
363 | struct stat from_sb, temp_sb, to_sb;
|
---|
364 | struct timeval tvb[2];
|
---|
365 | int devnull, files_match, from_fd, serrno, target;
|
---|
366 | int tempcopy, temp_fd, to_fd;
|
---|
367 | char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
|
---|
368 | int rc = EX_OK;
|
---|
369 |
|
---|
370 | files_match = 0;
|
---|
371 | from_fd = -1;
|
---|
372 | to_fd = -1;
|
---|
373 | temp_fd = -1;
|
---|
374 |
|
---|
375 | /* If try to install NULL file to a directory, fails. */
|
---|
376 | if (flags & DIRECTORY
|
---|
377 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
378 | || ( stricmp(from_name, _PATH_DEVNULL)
|
---|
379 | && stricmp(from_name, "nul")
|
---|
380 | #ifdef __EMX__
|
---|
381 | && stricmp(from_name, "/dev/nul")
|
---|
382 | #endif
|
---|
383 | )
|
---|
384 | #else
|
---|
385 | || strcmp(from_name, _PATH_DEVNULL)
|
---|
386 | #endif
|
---|
387 | ) {
|
---|
388 | if (stat(from_name, &from_sb))
|
---|
389 | return err(EX_OSERR, "%s", from_name);
|
---|
390 | if (!S_ISREG(from_sb.st_mode)) {
|
---|
391 | errno = EFTYPE;
|
---|
392 | return err(EX_OSERR, "%s", from_name);
|
---|
393 | }
|
---|
394 | /* Build the target path. */
|
---|
395 | if (flags & DIRECTORY) {
|
---|
396 | (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
|
---|
397 | to_name,
|
---|
398 | (p = last_slash(from_name)) ? ++p : from_name);
|
---|
399 | to_name = pathbuf;
|
---|
400 | }
|
---|
401 | devnull = 0;
|
---|
402 | } else {
|
---|
403 | devnull = 1;
|
---|
404 | }
|
---|
405 |
|
---|
406 | target = stat(to_name, &to_sb) == 0;
|
---|
407 |
|
---|
408 | /* Only install to regular files. */
|
---|
409 | if (target && !S_ISREG(to_sb.st_mode)) {
|
---|
410 | errno = EFTYPE;
|
---|
411 | warn("%s", to_name);
|
---|
412 | return EX_OK;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /* Only copy safe if the target exists. */
|
---|
416 | tempcopy = safecopy && target;
|
---|
417 |
|
---|
418 | if (!devnull && (from_fd = open(from_name, O_RDONLY | O_BINARY, 0)) < 0)
|
---|
419 | return err(EX_OSERR, "%s", from_name);
|
---|
420 |
|
---|
421 | /* If we don't strip, we can compare first. */
|
---|
422 | if (docompare && !dostrip && target) {
|
---|
423 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
424 | rc = err(EX_OSERR, "%s", to_name);
|
---|
425 | goto l_done;
|
---|
426 | }
|
---|
427 | if (devnull)
|
---|
428 | files_match = to_sb.st_size == 0;
|
---|
429 | else
|
---|
430 | files_match = !(compare(from_fd, from_name,
|
---|
431 | (size_t)from_sb.st_size, to_fd,
|
---|
432 | to_name, (size_t)to_sb.st_size));
|
---|
433 |
|
---|
434 | /* Close "to" file unless we match. */
|
---|
435 | if (!files_match) {
|
---|
436 | (void)close(to_fd);
|
---|
437 | to_fd = -1;
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | if (!files_match) {
|
---|
442 | if (tempcopy) {
|
---|
443 | to_fd = create_tempfile(to_name, tempfile,
|
---|
444 | sizeof(tempfile));
|
---|
445 | if (to_fd < 0) {
|
---|
446 | rc = err(EX_OSERR, "%s", tempfile);
|
---|
447 | goto l_done;
|
---|
448 | }
|
---|
449 | } else {
|
---|
450 | if ((to_fd = create_newfile(to_name, target,
|
---|
451 | &to_sb)) < 0) {
|
---|
452 | rc = err(EX_OSERR, "%s", to_name);
|
---|
453 | goto l_done;
|
---|
454 | }
|
---|
455 | if (verbose)
|
---|
456 | (void)printf("install: %s -> %s\n",
|
---|
457 | from_name, to_name);
|
---|
458 | }
|
---|
459 | if (!devnull) {
|
---|
460 | rc = copy(from_fd, from_name, to_fd,
|
---|
461 | tempcopy ? tempfile : to_name, from_sb.st_size);
|
---|
462 | if (rc)
|
---|
463 | goto l_done;
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | if (dostrip) {
|
---|
468 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
469 | /* close before we strip. */
|
---|
470 | close(to_fd);
|
---|
471 | to_fd = -1;
|
---|
472 | #endif
|
---|
473 | rc = strip(tempcopy ? tempfile : to_name);
|
---|
474 | if (rc)
|
---|
475 | goto l_done;
|
---|
476 |
|
---|
477 | /*
|
---|
478 | * Re-open our fd on the target, in case we used a strip
|
---|
479 | * that does not work in-place -- like GNU binutils strip.
|
---|
480 | */
|
---|
481 | #if !defined(__EMX__) && !defined(_MSC_VER)
|
---|
482 | close(to_fd);
|
---|
483 | #endif
|
---|
484 | to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY | O_BINARY, 0);
|
---|
485 | if (to_fd < 0) {
|
---|
486 | rc = err(EX_OSERR, "stripping %s", to_name);
|
---|
487 | goto l_done;
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * Compare the stripped temp file with the target.
|
---|
493 | */
|
---|
494 | if (docompare && dostrip && target) {
|
---|
495 | temp_fd = to_fd;
|
---|
496 |
|
---|
497 | /* Re-open to_fd using the real target name. */
|
---|
498 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
499 | rc = err(EX_OSERR, "%s", to_name);
|
---|
500 | goto l_done;
|
---|
501 | }
|
---|
502 |
|
---|
503 | if (fstat(temp_fd, &temp_sb)) {
|
---|
504 | serrno = errno;
|
---|
505 | (void)unlink(tempfile);
|
---|
506 | errno = serrno;
|
---|
507 | rc = err(EX_OSERR, "%s", tempfile);
|
---|
508 | goto l_done;
|
---|
509 | }
|
---|
510 |
|
---|
511 | if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
|
---|
512 | to_name, (size_t)to_sb.st_size) == 0) {
|
---|
513 | /*
|
---|
514 | * If target has more than one link we need to
|
---|
515 | * replace it in order to snap the extra links.
|
---|
516 | * Need to preserve target file times, though.
|
---|
517 | */
|
---|
518 | #if !defined(_MSC_VER) && !defined(__EMX__)
|
---|
519 | if (to_sb.st_nlink != 1) {
|
---|
520 | tvb[0].tv_sec = to_sb.st_atime;
|
---|
521 | tvb[0].tv_usec = 0;
|
---|
522 | tvb[1].tv_sec = to_sb.st_mtime;
|
---|
523 | tvb[1].tv_usec = 0;
|
---|
524 | (void)utimes(tempfile, tvb);
|
---|
525 | } else
|
---|
526 | #endif
|
---|
527 | {
|
---|
528 |
|
---|
529 | files_match = 1;
|
---|
530 | (void)unlink(tempfile);
|
---|
531 | }
|
---|
532 | (void) close(temp_fd);
|
---|
533 | temp_fd = -1;
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | /*
|
---|
538 | * Move the new file into place if doing a safe copy
|
---|
539 | * and the files are different (or just not compared).
|
---|
540 | */
|
---|
541 | if (tempcopy && !files_match) {
|
---|
542 | #ifdef UF_IMMUTABLE
|
---|
543 | /* Try to turn off the immutable bits. */
|
---|
544 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
545 | (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
546 | #endif
|
---|
547 | if (dobackup) {
|
---|
548 | if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
|
---|
549 | suffix) != strlen(to_name) + strlen(suffix)) {
|
---|
550 | unlink(tempfile);
|
---|
551 | rc = errx(EX_OSERR, "%s: backup filename too long",
|
---|
552 | to_name);
|
---|
553 | goto l_done;
|
---|
554 | }
|
---|
555 | if (verbose)
|
---|
556 | (void)printf("install: %s -> %s\n", to_name, backup);
|
---|
557 | if (rename(to_name, backup) < 0) {
|
---|
558 | serrno = errno;
|
---|
559 | unlink(tempfile);
|
---|
560 | errno = serrno;
|
---|
561 | rc = err(EX_OSERR, "rename: %s to %s", to_name,
|
---|
562 | backup);
|
---|
563 | goto l_done;
|
---|
564 | }
|
---|
565 | }
|
---|
566 | if (verbose)
|
---|
567 | (void)printf("install: %s -> %s\n", from_name, to_name);
|
---|
568 | if (rename(tempfile, to_name) < 0) {
|
---|
569 | serrno = errno;
|
---|
570 | unlink(tempfile);
|
---|
571 | errno = serrno;
|
---|
572 | rc = err(EX_OSERR, "rename: %s to %s",
|
---|
573 | tempfile, to_name);
|
---|
574 | goto l_done;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* Re-open to_fd so we aren't hosed by the rename(2). */
|
---|
578 | (void) close(to_fd);
|
---|
579 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
|
---|
580 | rc = err(EX_OSERR, "%s", to_name);
|
---|
581 | goto l_done;
|
---|
582 | }
|
---|
583 | }
|
---|
584 |
|
---|
585 | /*
|
---|
586 | * Preserve the timestamp of the source file if necessary.
|
---|
587 | */
|
---|
588 | if (dopreserve && !files_match && !devnull) {
|
---|
589 | tvb[0].tv_sec = from_sb.st_atime;
|
---|
590 | tvb[0].tv_usec = 0;
|
---|
591 | tvb[1].tv_sec = from_sb.st_mtime;
|
---|
592 | tvb[1].tv_usec = 0;
|
---|
593 | (void)utimes(to_name, tvb);
|
---|
594 | }
|
---|
595 |
|
---|
596 | if (fstat(to_fd, &to_sb) == -1) {
|
---|
597 | serrno = errno;
|
---|
598 | (void)unlink(to_name);
|
---|
599 | errno = serrno;
|
---|
600 | rc = err(EX_OSERR, "%s", to_name);
|
---|
601 | goto l_done;
|
---|
602 | }
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * Set owner, group, mode for target; do the chown first,
|
---|
606 | * chown may lose the setuid bits.
|
---|
607 | */
|
---|
608 | #ifdef UF_IMMUTABLE
|
---|
609 | if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
---|
610 | (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
|
---|
611 | (mode != (to_sb.st_mode & ALLPERMS))) {
|
---|
612 | /* Try to turn off the immutable bits. */
|
---|
613 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
614 | (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
615 | }
|
---|
616 | #endif
|
---|
617 |
|
---|
618 | if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
---|
619 | (uid != (uid_t)-1 && uid != to_sb.st_uid))
|
---|
620 | if (fchown(to_fd, uid, gid) == -1) {
|
---|
621 | serrno = errno;
|
---|
622 | (void)unlink(to_name);
|
---|
623 | errno = serrno;
|
---|
624 | rc = err(EX_OSERR,"%s: chown/chgrp", to_name);
|
---|
625 | goto l_done;
|
---|
626 | }
|
---|
627 |
|
---|
628 | if (mode != (to_sb.st_mode & ALLPERMS))
|
---|
629 | if (fchmod(to_fd, mode)) {
|
---|
630 | serrno = errno;
|
---|
631 | (void)unlink(to_name);
|
---|
632 | errno = serrno;
|
---|
633 | rc = err(EX_OSERR, "%s: chmod", to_name);
|
---|
634 | goto l_done;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /*
|
---|
638 | * If provided a set of flags, set them, otherwise, preserve the
|
---|
639 | * flags, except for the dump flag.
|
---|
640 | * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
|
---|
641 | * trying to turn off UF_NODUMP. If we're trying to set real flags,
|
---|
642 | * then warn if the the fs doesn't support it, otherwise fail.
|
---|
643 | */
|
---|
644 | #ifdef UF_IMMUTABLE
|
---|
645 | if (!devnull && (flags & SETFLAGS ||
|
---|
646 | (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
|
---|
647 | fchflags(to_fd,
|
---|
648 | flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
|
---|
649 | if (flags & SETFLAGS) {
|
---|
650 | if (errno == EOPNOTSUPP)
|
---|
651 | warn("%s: chflags", to_name);
|
---|
652 | else {
|
---|
653 | serrno = errno;
|
---|
654 | (void)unlink(to_name);
|
---|
655 | errno = serrno;
|
---|
656 | rc = err(EX_OSERR, "%s: chflags", to_name);
|
---|
657 | goto l_done;
|
---|
658 | }
|
---|
659 | }
|
---|
660 | }
|
---|
661 | #endif
|
---|
662 |
|
---|
663 | l_done:
|
---|
664 | if (to_fd >= 0)
|
---|
665 | (void)close(to_fd);
|
---|
666 | if (temp_fd >= 0)
|
---|
667 | (void)close(temp_fd);
|
---|
668 | if (!devnull)
|
---|
669 | (void)close(from_fd);
|
---|
670 | return rc;
|
---|
671 | }
|
---|
672 |
|
---|
673 | /*
|
---|
674 | * compare --
|
---|
675 | * compare two files; non-zero means files differ
|
---|
676 | */
|
---|
677 | static int
|
---|
678 | compare(int from_fd, const char *from_name __unused, size_t from_len,
|
---|
679 | int to_fd, const char *to_name __unused, size_t to_len)
|
---|
680 | {
|
---|
681 | char *p, *q;
|
---|
682 | int rv;
|
---|
683 | int done_compare;
|
---|
684 |
|
---|
685 | rv = 0;
|
---|
686 | if (from_len != to_len)
|
---|
687 | return 1;
|
---|
688 |
|
---|
689 | if (from_len <= MAX_CMP_SIZE) {
|
---|
690 | #ifdef USE_MMAP
|
---|
691 | done_compare = 0;
|
---|
692 | if (trymmap(from_fd) && trymmap(to_fd)) {
|
---|
693 | p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
|
---|
694 | if (p == (char *)MAP_FAILED)
|
---|
695 | goto out;
|
---|
696 | q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
|
---|
697 | if (q == (char *)MAP_FAILED) {
|
---|
698 | munmap(p, from_len);
|
---|
699 | goto out;
|
---|
700 | }
|
---|
701 |
|
---|
702 | rv = memcmp(p, q, from_len);
|
---|
703 | munmap(p, from_len);
|
---|
704 | munmap(q, from_len);
|
---|
705 | done_compare = 1;
|
---|
706 | }
|
---|
707 | out:
|
---|
708 | #else
|
---|
709 | (void)p; (void)q;
|
---|
710 | done_compare = 0;
|
---|
711 | #endif
|
---|
712 | if (!done_compare) {
|
---|
713 | char buf1[MAXBSIZE];
|
---|
714 | char buf2[MAXBSIZE];
|
---|
715 | int n1, n2;
|
---|
716 |
|
---|
717 | rv = 0;
|
---|
718 | lseek(from_fd, 0, SEEK_SET);
|
---|
719 | lseek(to_fd, 0, SEEK_SET);
|
---|
720 | while (rv == 0) {
|
---|
721 | n1 = read(from_fd, buf1, sizeof(buf1));
|
---|
722 | if (n1 == 0)
|
---|
723 | break; /* EOF */
|
---|
724 | else if (n1 > 0) {
|
---|
725 | n2 = read(to_fd, buf2, n1);
|
---|
726 | if (n2 == n1)
|
---|
727 | rv = memcmp(buf1, buf2, n1);
|
---|
728 | else
|
---|
729 | rv = 1; /* out of sync */
|
---|
730 | } else
|
---|
731 | rv = 1; /* read failure */
|
---|
732 | }
|
---|
733 | lseek(from_fd, 0, SEEK_SET);
|
---|
734 | lseek(to_fd, 0, SEEK_SET);
|
---|
735 | }
|
---|
736 | } else
|
---|
737 | rv = 1; /* don't bother in this case */
|
---|
738 |
|
---|
739 | return rv;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /*
|
---|
743 | * create_tempfile --
|
---|
744 | * create a temporary file based on path and open it
|
---|
745 | */
|
---|
746 | int
|
---|
747 | create_tempfile(const char *path, char *temp, size_t tsize)
|
---|
748 | {
|
---|
749 | char *p;
|
---|
750 |
|
---|
751 | (void)strncpy(temp, path, tsize);
|
---|
752 | temp[tsize - 1] = '\0';
|
---|
753 | if ((p = last_slash(temp)) != NULL)
|
---|
754 | p++;
|
---|
755 | else
|
---|
756 | p = temp;
|
---|
757 | (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
|
---|
758 | temp[tsize - 1] = '\0';
|
---|
759 | return (mkstemp(temp));
|
---|
760 | }
|
---|
761 |
|
---|
762 | /*
|
---|
763 | * create_newfile --
|
---|
764 | * create a new file, overwriting an existing one if necessary
|
---|
765 | */
|
---|
766 | int
|
---|
767 | create_newfile(const char *path, int target, struct stat *sbp)
|
---|
768 | {
|
---|
769 | char backup[MAXPATHLEN];
|
---|
770 | int saved_errno = 0;
|
---|
771 | int newfd;
|
---|
772 |
|
---|
773 | if (target) {
|
---|
774 | /*
|
---|
775 | * Unlink now... avoid ETXTBSY errors later. Try to turn
|
---|
776 | * off the append/immutable bits -- if we fail, go ahead,
|
---|
777 | * it might work.
|
---|
778 | */
|
---|
779 | #ifdef UF_IMMUTABLE
|
---|
780 | if (sbp->st_flags & NOCHANGEBITS)
|
---|
781 | (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
|
---|
782 | #endif
|
---|
783 |
|
---|
784 | if (dobackup) {
|
---|
785 | if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
|
---|
786 | path, suffix) != strlen(path) + strlen(suffix)) {
|
---|
787 | errx(EX_OSERR, "%s: backup filename too long",
|
---|
788 | path);
|
---|
789 | errno = ENAMETOOLONG;
|
---|
790 | return -1;
|
---|
791 | }
|
---|
792 | (void)snprintf(backup, MAXPATHLEN, "%s%s",
|
---|
793 | path, suffix);
|
---|
794 | if (verbose)
|
---|
795 | (void)printf("install: %s -> %s\n",
|
---|
796 | path, backup);
|
---|
797 | if (rename(path, backup) < 0) {
|
---|
798 | err(EX_OSERR, "rename: %s to %s", path, backup);
|
---|
799 | return -1;
|
---|
800 | }
|
---|
801 | } else
|
---|
802 | if (unlink(path) < 0)
|
---|
803 | saved_errno = errno;
|
---|
804 | }
|
---|
805 |
|
---|
806 | newfd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
|
---|
807 | if (newfd < 0 && saved_errno != 0)
|
---|
808 | errno = saved_errno;
|
---|
809 | return newfd;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /*
|
---|
813 | * copy --
|
---|
814 | * copy from one file to another
|
---|
815 | */
|
---|
816 | static int
|
---|
817 | copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
|
---|
818 | off_t size)
|
---|
819 | {
|
---|
820 | int nr, nw;
|
---|
821 | int serrno;
|
---|
822 | char *p, buf[MAXBSIZE];
|
---|
823 | int done_copy;
|
---|
824 |
|
---|
825 | /* Rewind file descriptors. */
|
---|
826 | if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
827 | return err(EX_OSERR, "lseek: %s", from_name);
|
---|
828 | if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
829 | return err(EX_OSERR, "lseek: %s", to_name);
|
---|
830 |
|
---|
831 | /*
|
---|
832 | * Mmap and write if less than 8M (the limit is so we don't totally
|
---|
833 | * trash memory on big files. This is really a minor hack, but it
|
---|
834 | * wins some CPU back.
|
---|
835 | */
|
---|
836 | done_copy = 0;
|
---|
837 | #ifdef USE_MMAP
|
---|
838 | if (size <= 8 * 1048576 && trymmap(from_fd) &&
|
---|
839 | (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
|
---|
840 | from_fd, (off_t)0)) != (char *)MAP_FAILED) {
|
---|
841 | if ((nw = write(to_fd, p, size)) != size) {
|
---|
842 | serrno = errno;
|
---|
843 | (void)unlink(to_name);
|
---|
844 | errno = nw > 0 ? EIO : serrno;
|
---|
845 | err(EX_OSERR, "%s", to_name);
|
---|
846 | }
|
---|
847 | done_copy = 1;
|
---|
848 | }
|
---|
849 | #else
|
---|
850 | (void)p;
|
---|
851 | #endif
|
---|
852 | if (!done_copy) {
|
---|
853 | while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
|
---|
854 | if ((nw = write(to_fd, buf, nr)) != nr) {
|
---|
855 | serrno = errno;
|
---|
856 | (void)unlink(to_name);
|
---|
857 | errno = nw > 0 ? EIO : serrno;
|
---|
858 | return err(EX_OSERR, "%s", to_name);
|
---|
859 | }
|
---|
860 | if (nr != 0) {
|
---|
861 | serrno = errno;
|
---|
862 | (void)unlink(to_name);
|
---|
863 | errno = serrno;
|
---|
864 | return err(EX_OSERR, "%s", from_name);
|
---|
865 | }
|
---|
866 | }
|
---|
867 | return EX_OK;
|
---|
868 | }
|
---|
869 |
|
---|
870 | /*
|
---|
871 | * strip --
|
---|
872 | * use strip(1) to strip the target file
|
---|
873 | */
|
---|
874 | static int
|
---|
875 | strip(const char *to_name)
|
---|
876 | {
|
---|
877 | #if defined(__EMX__) || defined(_MSC_VER)
|
---|
878 | const char *stripbin = getenv("STRIPBIN");
|
---|
879 | if (stripbin == NULL)
|
---|
880 | stripbin = "strip";
|
---|
881 | return spawnlp(P_WAIT, stripbin, stripbin, to_name, NULL);
|
---|
882 | #else
|
---|
883 | const char *stripbin;
|
---|
884 | int serrno, status;
|
---|
885 | pid_t pid;
|
---|
886 |
|
---|
887 | pid = fork();
|
---|
888 | switch (pid) {
|
---|
889 | case -1:
|
---|
890 | serrno = errno;
|
---|
891 | (void)unlink(to_name);
|
---|
892 | errno = serrno;
|
---|
893 | err(EX_TEMPFAIL, "fork");
|
---|
894 | case 0:
|
---|
895 | stripbin = getenv("STRIPBIN");
|
---|
896 | if (stripbin == NULL)
|
---|
897 | stripbin = "strip";
|
---|
898 | execlp(stripbin, stripbin, to_name, (char *)NULL);
|
---|
899 | err(EX_OSERR, "exec(%s)", stripbin);
|
---|
900 | default:
|
---|
901 | if (waitpid(pid, &status, 0) == -1 || status) {
|
---|
902 | serrno = errno;
|
---|
903 | (void)unlink(to_name);
|
---|
904 | errno = serrno;
|
---|
905 | err(EX_SOFTWARE, "waitpid");
|
---|
906 | /* NOTREACHED */
|
---|
907 | }
|
---|
908 | }
|
---|
909 | #endif
|
---|
910 | }
|
---|
911 |
|
---|
912 | /*
|
---|
913 | * install_dir --
|
---|
914 | * build directory heirarchy
|
---|
915 | */
|
---|
916 | static int
|
---|
917 | install_dir(char *path)
|
---|
918 | {
|
---|
919 | char *p;
|
---|
920 | struct stat sb;
|
---|
921 | int ch;
|
---|
922 |
|
---|
923 | for (p = path;; ++p)
|
---|
924 | if (!*p || (p != path && IS_SLASH(*p))) {
|
---|
925 | ch = *p;
|
---|
926 | *p = '\0';
|
---|
927 | if (stat(path, &sb)) {
|
---|
928 | if (errno != ENOENT || mkdir(path, 0755) < 0) {
|
---|
929 | return err(EX_OSERR, "mkdir %s", path);
|
---|
930 | /* NOTREACHED */
|
---|
931 | } else if (verbose)
|
---|
932 | (void)printf("install: mkdir %s\n",
|
---|
933 | path);
|
---|
934 | } else if (!S_ISDIR(sb.st_mode))
|
---|
935 | return errx(EX_OSERR, "%s exists but is not a directory", path);
|
---|
936 | if (!(*p = ch))
|
---|
937 | break;
|
---|
938 | }
|
---|
939 |
|
---|
940 | if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
|
---|
941 | warn("chown %u:%u %s", uid, gid, path);
|
---|
942 | if (chmod(path, mode))
|
---|
943 | warn("chmod %o %s", mode, path);
|
---|
944 | return EX_OK;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /*
|
---|
948 | * usage --
|
---|
949 | * print a usage message and die
|
---|
950 | */
|
---|
951 | static int
|
---|
952 | usage(FILE *pf)
|
---|
953 | {
|
---|
954 | fprintf(stderr,
|
---|
955 | "usage: %s [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
|
---|
956 | " [-o owner] file1 file2\n"
|
---|
957 | " or: %s [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
|
---|
958 | " [-o owner] file1 ... fileN directory\n"
|
---|
959 | " or: %s -d [-v] [-g group] [-m mode] [-o owner] directory ...\n"
|
---|
960 | " or: %s --help\n"
|
---|
961 | " or: %s --version\n",
|
---|
962 | g_progname, g_progname, g_progname, g_progname, g_progname);
|
---|
963 | return EX_USAGE;
|
---|
964 | }
|
---|
965 |
|
---|
966 | #ifdef USE_MMAP
|
---|
967 | /*
|
---|
968 | * trymmap --
|
---|
969 | * return true (1) if mmap should be tried, false (0) if not.
|
---|
970 | */
|
---|
971 | static int
|
---|
972 | trymmap(int fd)
|
---|
973 | {
|
---|
974 | /*
|
---|
975 | * The ifdef is for bootstrapping - f_fstypename doesn't exist in
|
---|
976 | * pre-Lite2-merge systems.
|
---|
977 | */
|
---|
978 | #ifdef MFSNAMELEN
|
---|
979 | struct statfs stfs;
|
---|
980 |
|
---|
981 | if (nommap || fstatfs(fd, &stfs) != 0)
|
---|
982 | return (0);
|
---|
983 | if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
|
---|
984 | strcmp(stfs.f_fstypename, "cd9660") == 0)
|
---|
985 | return (1);
|
---|
986 | #endif
|
---|
987 | return (0);
|
---|
988 | }
|
---|
989 | #endif
|
---|
990 |
|
---|
991 | /* figures out where the last slash or colon is. */
|
---|
992 | static char *
|
---|
993 | last_slash(const char *path)
|
---|
994 | {
|
---|
995 | #if defined(__WIN32__) || defined(__WIN64__) || defined(__OS2__)
|
---|
996 | char *p = (char *)strrchr(path, '/');
|
---|
997 | if (p)
|
---|
998 | {
|
---|
999 | char *p2 = strrchr(p, '\\');
|
---|
1000 | if (p2)
|
---|
1001 | p = p2;
|
---|
1002 | }
|
---|
1003 | else
|
---|
1004 | {
|
---|
1005 | p = (char *)strrchr(path, '\\');
|
---|
1006 | if (!p && isalpha(path[0]) && path[1] == ':')
|
---|
1007 | p = (char *)&path[1];
|
---|
1008 | }
|
---|
1009 | return p;
|
---|
1010 | #else
|
---|
1011 | return strrchr(path, '/');
|
---|
1012 | #endif
|
---|
1013 | }
|
---|
1014 |
|
---|