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 | #endif
|
---|
45 |
|
---|
46 | #include <sys/cdefs.h>
|
---|
47 | __FBSDID("$FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.66 2005/01/25 14:34:57 ssouhlal Exp $");
|
---|
48 |
|
---|
49 | #include <sys/param.h>
|
---|
50 | #include <sys/mman.h>
|
---|
51 | #include <sys/mount.h>
|
---|
52 | #include <sys/stat.h>
|
---|
53 | #include <sys/time.h>
|
---|
54 | #include <sys/wait.h>
|
---|
55 |
|
---|
56 | #include <ctype.h>
|
---|
57 | #include <err.h>
|
---|
58 | #include <errno.h>
|
---|
59 | #include <fcntl.h>
|
---|
60 | #include <grp.h>
|
---|
61 | #include <paths.h>
|
---|
62 | #include <pwd.h>
|
---|
63 | #include <stdio.h>
|
---|
64 | #include <stdlib.h>
|
---|
65 | #include <string.h>
|
---|
66 | #include <sysexits.h>
|
---|
67 | #include <unistd.h>
|
---|
68 |
|
---|
69 | /* Bootstrap aid - this doesn't exist in most older releases */
|
---|
70 | #ifndef MAP_FAILED
|
---|
71 | #define MAP_FAILED ((void *)-1) /* from <sys/mman.h> */
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | #define MAX_CMP_SIZE (16 * 1024 * 1024)
|
---|
75 |
|
---|
76 | #define DIRECTORY 0x01 /* Tell install it's a directory. */
|
---|
77 | #define SETFLAGS 0x02 /* Tell install to set flags. */
|
---|
78 | #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
|
---|
79 | #define BACKUP_SUFFIX ".old"
|
---|
80 |
|
---|
81 | #ifndef O_BINARY
|
---|
82 | # define O_BINARY 0
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | static struct passwd *pp;
|
---|
86 | static struct group *gp;
|
---|
87 | static gid_t gid;
|
---|
88 | static uid_t uid;
|
---|
89 | static int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
|
---|
90 | static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
---|
91 | static const char *suffix = BACKUP_SUFFIX;
|
---|
92 |
|
---|
93 | static void copy(int, const char *, int, const char *, off_t);
|
---|
94 | static int compare(int, const char *, size_t, int, const char *, size_t);
|
---|
95 | static int create_newfile(const char *, int, struct stat *);
|
---|
96 | static int create_tempfile(const char *, char *, size_t);
|
---|
97 | static void install(const char *, const char *, u_long, u_int);
|
---|
98 | static void install_dir(char *);
|
---|
99 | static u_long numeric_id(const char *, const char *);
|
---|
100 | static void strip(const char *);
|
---|
101 | static int trymmap(int);
|
---|
102 | static void usage(void);
|
---|
103 |
|
---|
104 | int
|
---|
105 | kmk_builtin_install(int argc, char *argv[])
|
---|
106 | {
|
---|
107 | struct stat from_sb, to_sb;
|
---|
108 | mode_t *set;
|
---|
109 | u_long fset;
|
---|
110 | int ch, no_target;
|
---|
111 | u_int iflags;
|
---|
112 | char *flags;
|
---|
113 | const char *group, *owner, *to_name;
|
---|
114 |
|
---|
115 | iflags = 0;
|
---|
116 | group = owner = NULL;
|
---|
117 | while ((ch = getopt(argc, argv, "B:bCcdf:g:Mm:o:pSsv")) != -1)
|
---|
118 | switch((char)ch) {
|
---|
119 | case 'B':
|
---|
120 | suffix = optarg;
|
---|
121 | /* FALLTHROUGH */
|
---|
122 | case 'b':
|
---|
123 | dobackup = 1;
|
---|
124 | break;
|
---|
125 | case 'C':
|
---|
126 | docompare = 1;
|
---|
127 | break;
|
---|
128 | case 'c':
|
---|
129 | /* For backwards compatibility. */
|
---|
130 | break;
|
---|
131 | case 'd':
|
---|
132 | dodir = 1;
|
---|
133 | break;
|
---|
134 | case 'f':
|
---|
135 | #ifdef UF_IMMUTABLE
|
---|
136 | flags = optarg;
|
---|
137 | if (strtofflags(&flags, &fset, NULL))
|
---|
138 | errx(EX_USAGE, "%s: invalid flag", flags);
|
---|
139 | iflags |= SETFLAGS;
|
---|
140 | #endif
|
---|
141 | break;
|
---|
142 | case 'g':
|
---|
143 | group = optarg;
|
---|
144 | break;
|
---|
145 | case 'M':
|
---|
146 | nommap = 1;
|
---|
147 | break;
|
---|
148 | case 'm':
|
---|
149 | #ifdef __EMX__
|
---|
150 | if (!(set = bsd_setmode(optarg)))
|
---|
151 | #else
|
---|
152 | if (!(set = setmode(optarg)))
|
---|
153 | #endif
|
---|
154 | errx(EX_USAGE, "invalid file mode: %s",
|
---|
155 | optarg);
|
---|
156 | mode = getmode(set, 0);
|
---|
157 | free(set);
|
---|
158 | break;
|
---|
159 | case 'o':
|
---|
160 | owner = optarg;
|
---|
161 | break;
|
---|
162 | case 'p':
|
---|
163 | docompare = dopreserve = 1;
|
---|
164 | break;
|
---|
165 | case 'S':
|
---|
166 | safecopy = 1;
|
---|
167 | break;
|
---|
168 | case 's':
|
---|
169 | dostrip = 1;
|
---|
170 | break;
|
---|
171 | case 'v':
|
---|
172 | verbose = 1;
|
---|
173 | break;
|
---|
174 | case '?':
|
---|
175 | default:
|
---|
176 | usage();
|
---|
177 | }
|
---|
178 | argc -= optind;
|
---|
179 | argv += optind;
|
---|
180 |
|
---|
181 | /* some options make no sense when creating directories */
|
---|
182 | if (dostrip && dodir) {
|
---|
183 | warnx("-d and -s may not be specified together");
|
---|
184 | usage();
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* must have at least two arguments, except when creating directories */
|
---|
188 | if (argc == 0 || (argc == 1 && !dodir))
|
---|
189 | usage();
|
---|
190 |
|
---|
191 | /* need to make a temp copy so we can compare stripped version */
|
---|
192 | if (docompare && dostrip)
|
---|
193 | safecopy = 1;
|
---|
194 |
|
---|
195 | /* get group and owner id's */
|
---|
196 | if (group != NULL) {
|
---|
197 | if ((gp = getgrnam(group)) != NULL)
|
---|
198 | gid = gp->gr_gid;
|
---|
199 | else
|
---|
200 | gid = (gid_t)numeric_id(group, "group");
|
---|
201 | } else
|
---|
202 | gid = (gid_t)-1;
|
---|
203 |
|
---|
204 | if (owner != NULL) {
|
---|
205 | if ((pp = getpwnam(owner)) != NULL)
|
---|
206 | uid = pp->pw_uid;
|
---|
207 | else
|
---|
208 | uid = (uid_t)numeric_id(owner, "user");
|
---|
209 | } else
|
---|
210 | uid = (uid_t)-1;
|
---|
211 |
|
---|
212 | if (dodir) {
|
---|
213 | for (; *argv != NULL; ++argv)
|
---|
214 | install_dir(*argv);
|
---|
215 | exit(EX_OK);
|
---|
216 | /* NOTREACHED */
|
---|
217 | }
|
---|
218 |
|
---|
219 | no_target = stat(to_name = argv[argc - 1], &to_sb);
|
---|
220 | if (!no_target && S_ISDIR(to_sb.st_mode)) {
|
---|
221 | for (; *argv != to_name; ++argv)
|
---|
222 | install(*argv, to_name, fset, iflags | DIRECTORY);
|
---|
223 | exit(EX_OK);
|
---|
224 | /* NOTREACHED */
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* can't do file1 file2 directory/file */
|
---|
228 | if (argc != 2) {
|
---|
229 | warnx("wrong number or types of arguments");
|
---|
230 | usage();
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (!no_target) {
|
---|
234 | if (stat(*argv, &from_sb))
|
---|
235 | err(EX_OSERR, "%s", *argv);
|
---|
236 | if (!S_ISREG(to_sb.st_mode)) {
|
---|
237 | errno = EFTYPE;
|
---|
238 | err(EX_OSERR, "%s", to_name);
|
---|
239 | }
|
---|
240 | if (to_sb.st_dev == from_sb.st_dev &&
|
---|
241 | to_sb.st_ino == from_sb.st_ino)
|
---|
242 | errx(EX_USAGE,
|
---|
243 | "%s and %s are the same file", *argv, to_name);
|
---|
244 | }
|
---|
245 | install(*argv, to_name, fset, iflags);
|
---|
246 | exit(EX_OK);
|
---|
247 | /* NOTREACHED */
|
---|
248 | }
|
---|
249 |
|
---|
250 | u_long
|
---|
251 | numeric_id(const char *name, const char *type)
|
---|
252 | {
|
---|
253 | u_long val;
|
---|
254 | char *ep;
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * XXX
|
---|
258 | * We know that uid_t's and gid_t's are unsigned longs.
|
---|
259 | */
|
---|
260 | errno = 0;
|
---|
261 | val = strtoul(name, &ep, 10);
|
---|
262 | if (errno)
|
---|
263 | err(EX_NOUSER, "%s", name);
|
---|
264 | if (*ep != '\0')
|
---|
265 | errx(EX_NOUSER, "unknown %s %s", type, name);
|
---|
266 | return (val);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * install --
|
---|
271 | * build a path name and install the file
|
---|
272 | */
|
---|
273 | void
|
---|
274 | install(const char *from_name, const char *to_name, u_long fset, u_int flags)
|
---|
275 | {
|
---|
276 | struct stat from_sb, temp_sb, to_sb;
|
---|
277 | struct timeval tvb[2];
|
---|
278 | int devnull, files_match, from_fd, serrno, target;
|
---|
279 | int tempcopy, temp_fd, to_fd;
|
---|
280 | char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
|
---|
281 |
|
---|
282 | files_match = 0;
|
---|
283 | from_fd = -1;
|
---|
284 | to_fd = -1;
|
---|
285 |
|
---|
286 | /* If try to install NULL file to a directory, fails. */
|
---|
287 | if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
|
---|
288 | if (stat(from_name, &from_sb))
|
---|
289 | err(EX_OSERR, "%s", from_name);
|
---|
290 | if (!S_ISREG(from_sb.st_mode)) {
|
---|
291 | errno = EFTYPE;
|
---|
292 | err(EX_OSERR, "%s", from_name);
|
---|
293 | }
|
---|
294 | /* Build the target path. */
|
---|
295 | if (flags & DIRECTORY) {
|
---|
296 | (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
|
---|
297 | to_name,
|
---|
298 | (p = strrchr(from_name, '/')) ? ++p : from_name);
|
---|
299 | to_name = pathbuf;
|
---|
300 | }
|
---|
301 | devnull = 0;
|
---|
302 | } else {
|
---|
303 | devnull = 1;
|
---|
304 | }
|
---|
305 |
|
---|
306 | target = stat(to_name, &to_sb) == 0;
|
---|
307 |
|
---|
308 | /* Only install to regular files. */
|
---|
309 | if (target && !S_ISREG(to_sb.st_mode)) {
|
---|
310 | errno = EFTYPE;
|
---|
311 | warn("%s", to_name);
|
---|
312 | return;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /* Only copy safe if the target exists. */
|
---|
316 | tempcopy = safecopy && target;
|
---|
317 |
|
---|
318 | if (!devnull && (from_fd = open(from_name, O_RDONLY | O_BINARY, 0)) < 0)
|
---|
319 | err(EX_OSERR, "%s", from_name);
|
---|
320 |
|
---|
321 | /* If we don't strip, we can compare first. */
|
---|
322 | if (docompare && !dostrip && target) {
|
---|
323 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
|
---|
324 | err(EX_OSERR, "%s", to_name);
|
---|
325 | if (devnull)
|
---|
326 | files_match = to_sb.st_size == 0;
|
---|
327 | else
|
---|
328 | files_match = !(compare(from_fd, from_name,
|
---|
329 | (size_t)from_sb.st_size, to_fd,
|
---|
330 | to_name, (size_t)to_sb.st_size));
|
---|
331 |
|
---|
332 | /* Close "to" file unless we match. */
|
---|
333 | if (!files_match)
|
---|
334 | (void)close(to_fd);
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (!files_match) {
|
---|
338 | if (tempcopy) {
|
---|
339 | to_fd = create_tempfile(to_name, tempfile,
|
---|
340 | sizeof(tempfile));
|
---|
341 | if (to_fd < 0)
|
---|
342 | err(EX_OSERR, "%s", tempfile);
|
---|
343 | } else {
|
---|
344 | if ((to_fd = create_newfile(to_name, target,
|
---|
345 | &to_sb)) < 0)
|
---|
346 | err(EX_OSERR, "%s", to_name);
|
---|
347 | if (verbose)
|
---|
348 | (void)printf("install: %s -> %s\n",
|
---|
349 | from_name, to_name);
|
---|
350 | }
|
---|
351 | if (!devnull)
|
---|
352 | copy(from_fd, from_name, to_fd,
|
---|
353 | tempcopy ? tempfile : to_name, from_sb.st_size);
|
---|
354 | }
|
---|
355 |
|
---|
356 | if (dostrip) {
|
---|
357 | strip(tempcopy ? tempfile : to_name);
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Re-open our fd on the target, in case we used a strip
|
---|
361 | * that does not work in-place -- like GNU binutils strip.
|
---|
362 | */
|
---|
363 | close(to_fd);
|
---|
364 | to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY | O_BINARY, 0);
|
---|
365 | if (to_fd < 0)
|
---|
366 | err(EX_OSERR, "stripping %s", to_name);
|
---|
367 | }
|
---|
368 |
|
---|
369 | /*
|
---|
370 | * Compare the stripped temp file with the target.
|
---|
371 | */
|
---|
372 | if (docompare && dostrip && target) {
|
---|
373 | temp_fd = to_fd;
|
---|
374 |
|
---|
375 | /* Re-open to_fd using the real target name. */
|
---|
376 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
|
---|
377 | err(EX_OSERR, "%s", to_name);
|
---|
378 |
|
---|
379 | if (fstat(temp_fd, &temp_sb)) {
|
---|
380 | serrno = errno;
|
---|
381 | (void)unlink(tempfile);
|
---|
382 | errno = serrno;
|
---|
383 | err(EX_OSERR, "%s", tempfile);
|
---|
384 | }
|
---|
385 |
|
---|
386 | if (compare(temp_fd, tempfile, (size_t)temp_sb.st_size, to_fd,
|
---|
387 | to_name, (size_t)to_sb.st_size) == 0) {
|
---|
388 | /*
|
---|
389 | * If target has more than one link we need to
|
---|
390 | * replace it in order to snap the extra links.
|
---|
391 | * Need to preserve target file times, though.
|
---|
392 | */
|
---|
393 | if (to_sb.st_nlink != 1) {
|
---|
394 | tvb[0].tv_sec = to_sb.st_atime;
|
---|
395 | tvb[0].tv_usec = 0;
|
---|
396 | tvb[1].tv_sec = to_sb.st_mtime;
|
---|
397 | tvb[1].tv_usec = 0;
|
---|
398 | (void)utimes(tempfile, tvb);
|
---|
399 | } else {
|
---|
400 | files_match = 1;
|
---|
401 | (void)unlink(tempfile);
|
---|
402 | }
|
---|
403 | (void) close(temp_fd);
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | /*
|
---|
408 | * Move the new file into place if doing a safe copy
|
---|
409 | * and the files are different (or just not compared).
|
---|
410 | */
|
---|
411 | if (tempcopy && !files_match) {
|
---|
412 | #ifdef UF_IMMUTABLE
|
---|
413 | /* Try to turn off the immutable bits. */
|
---|
414 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
415 | (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
416 | #endif
|
---|
417 | if (dobackup) {
|
---|
418 | if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
|
---|
419 | suffix) != strlen(to_name) + strlen(suffix)) {
|
---|
420 | unlink(tempfile);
|
---|
421 | errx(EX_OSERR, "%s: backup filename too long",
|
---|
422 | to_name);
|
---|
423 | }
|
---|
424 | if (verbose)
|
---|
425 | (void)printf("install: %s -> %s\n", to_name, backup);
|
---|
426 | if (rename(to_name, backup) < 0) {
|
---|
427 | serrno = errno;
|
---|
428 | unlink(tempfile);
|
---|
429 | errno = serrno;
|
---|
430 | err(EX_OSERR, "rename: %s to %s", to_name,
|
---|
431 | backup);
|
---|
432 | }
|
---|
433 | }
|
---|
434 | if (verbose)
|
---|
435 | (void)printf("install: %s -> %s\n", from_name, to_name);
|
---|
436 | if (rename(tempfile, to_name) < 0) {
|
---|
437 | serrno = errno;
|
---|
438 | unlink(tempfile);
|
---|
439 | errno = serrno;
|
---|
440 | err(EX_OSERR, "rename: %s to %s",
|
---|
441 | tempfile, to_name);
|
---|
442 | }
|
---|
443 |
|
---|
444 | /* Re-open to_fd so we aren't hosed by the rename(2). */
|
---|
445 | (void) close(to_fd);
|
---|
446 | if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
|
---|
447 | err(EX_OSERR, "%s", to_name);
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Preserve the timestamp of the source file if necessary.
|
---|
452 | */
|
---|
453 | if (dopreserve && !files_match && !devnull) {
|
---|
454 | tvb[0].tv_sec = from_sb.st_atime;
|
---|
455 | tvb[0].tv_usec = 0;
|
---|
456 | tvb[1].tv_sec = from_sb.st_mtime;
|
---|
457 | tvb[1].tv_usec = 0;
|
---|
458 | (void)utimes(to_name, tvb);
|
---|
459 | }
|
---|
460 |
|
---|
461 | if (fstat(to_fd, &to_sb) == -1) {
|
---|
462 | serrno = errno;
|
---|
463 | (void)unlink(to_name);
|
---|
464 | errno = serrno;
|
---|
465 | err(EX_OSERR, "%s", to_name);
|
---|
466 | }
|
---|
467 |
|
---|
468 | /*
|
---|
469 | * Set owner, group, mode for target; do the chown first,
|
---|
470 | * chown may lose the setuid bits.
|
---|
471 | */
|
---|
472 | #ifdef UF_IMMUTABLE
|
---|
473 | if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
---|
474 | (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
|
---|
475 | (mode != (to_sb.st_mode & ALLPERMS))) {
|
---|
476 | /* Try to turn off the immutable bits. */
|
---|
477 | if (to_sb.st_flags & NOCHANGEBITS)
|
---|
478 | (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
|
---|
479 | }
|
---|
480 | #endif
|
---|
481 |
|
---|
482 | if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
|
---|
483 | (uid != (uid_t)-1 && uid != to_sb.st_uid))
|
---|
484 | if (fchown(to_fd, uid, gid) == -1) {
|
---|
485 | serrno = errno;
|
---|
486 | (void)unlink(to_name);
|
---|
487 | errno = serrno;
|
---|
488 | err(EX_OSERR,"%s: chown/chgrp", to_name);
|
---|
489 | }
|
---|
490 |
|
---|
491 | if (mode != (to_sb.st_mode & ALLPERMS))
|
---|
492 | if (fchmod(to_fd, mode)) {
|
---|
493 | serrno = errno;
|
---|
494 | (void)unlink(to_name);
|
---|
495 | errno = serrno;
|
---|
496 | err(EX_OSERR, "%s: chmod", to_name);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /*
|
---|
500 | * If provided a set of flags, set them, otherwise, preserve the
|
---|
501 | * flags, except for the dump flag.
|
---|
502 | * NFS does not support flags. Ignore EOPNOTSUPP flags if we're just
|
---|
503 | * trying to turn off UF_NODUMP. If we're trying to set real flags,
|
---|
504 | * then warn if the the fs doesn't support it, otherwise fail.
|
---|
505 | */
|
---|
506 | #ifdef UF_IMMUTABLE
|
---|
507 | if (!devnull && (flags & SETFLAGS ||
|
---|
508 | (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
|
---|
509 | fchflags(to_fd,
|
---|
510 | flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
|
---|
511 | if (flags & SETFLAGS) {
|
---|
512 | if (errno == EOPNOTSUPP)
|
---|
513 | warn("%s: chflags", to_name);
|
---|
514 | else {
|
---|
515 | serrno = errno;
|
---|
516 | (void)unlink(to_name);
|
---|
517 | errno = serrno;
|
---|
518 | err(EX_OSERR, "%s: chflags", to_name);
|
---|
519 | }
|
---|
520 | }
|
---|
521 | }
|
---|
522 | #endif
|
---|
523 |
|
---|
524 | (void)close(to_fd);
|
---|
525 | if (!devnull)
|
---|
526 | (void)close(from_fd);
|
---|
527 | }
|
---|
528 |
|
---|
529 | /*
|
---|
530 | * compare --
|
---|
531 | * compare two files; non-zero means files differ
|
---|
532 | */
|
---|
533 | int
|
---|
534 | compare(int from_fd, const char *from_name __unused, size_t from_len,
|
---|
535 | int to_fd, const char *to_name __unused, size_t to_len)
|
---|
536 | {
|
---|
537 | char *p, *q;
|
---|
538 | int rv;
|
---|
539 | int done_compare;
|
---|
540 |
|
---|
541 | rv = 0;
|
---|
542 | if (from_len != to_len)
|
---|
543 | return 1;
|
---|
544 |
|
---|
545 | if (from_len <= MAX_CMP_SIZE) {
|
---|
546 | #if !defined(__EMX__) && !defined(_MSC_VER)
|
---|
547 | done_compare = 0;
|
---|
548 | if (trymmap(from_fd) && trymmap(to_fd)) {
|
---|
549 | p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
|
---|
550 | if (p == (char *)MAP_FAILED)
|
---|
551 | goto out;
|
---|
552 | q = mmap(NULL, from_len, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
|
---|
553 | if (q == (char *)MAP_FAILED) {
|
---|
554 | munmap(p, from_len);
|
---|
555 | goto out;
|
---|
556 | }
|
---|
557 |
|
---|
558 | rv = memcmp(p, q, from_len);
|
---|
559 | munmap(p, from_len);
|
---|
560 | munmap(q, from_len);
|
---|
561 | done_compare = 1;
|
---|
562 | }
|
---|
563 | #endif
|
---|
564 | out:
|
---|
565 | if (!done_compare) {
|
---|
566 | char buf1[MAXBSIZE];
|
---|
567 | char buf2[MAXBSIZE];
|
---|
568 | int n1, n2;
|
---|
569 |
|
---|
570 | rv = 0;
|
---|
571 | lseek(from_fd, 0, SEEK_SET);
|
---|
572 | lseek(to_fd, 0, SEEK_SET);
|
---|
573 | while (rv == 0) {
|
---|
574 | n1 = read(from_fd, buf1, sizeof(buf1));
|
---|
575 | if (n1 == 0)
|
---|
576 | break; /* EOF */
|
---|
577 | else if (n1 > 0) {
|
---|
578 | n2 = read(to_fd, buf2, n1);
|
---|
579 | if (n2 == n1)
|
---|
580 | rv = memcmp(buf1, buf2, n1);
|
---|
581 | else
|
---|
582 | rv = 1; /* out of sync */
|
---|
583 | } else
|
---|
584 | rv = 1; /* read failure */
|
---|
585 | }
|
---|
586 | lseek(from_fd, 0, SEEK_SET);
|
---|
587 | lseek(to_fd, 0, SEEK_SET);
|
---|
588 | }
|
---|
589 | } else
|
---|
590 | rv = 1; /* don't bother in this case */
|
---|
591 |
|
---|
592 | return rv;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /*
|
---|
596 | * create_tempfile --
|
---|
597 | * create a temporary file based on path and open it
|
---|
598 | */
|
---|
599 | int
|
---|
600 | create_tempfile(const char *path, char *temp, size_t tsize)
|
---|
601 | {
|
---|
602 | char *p;
|
---|
603 |
|
---|
604 | (void)strncpy(temp, path, tsize);
|
---|
605 | temp[tsize - 1] = '\0';
|
---|
606 | if ((p = strrchr(temp, '/')) != NULL)
|
---|
607 | p++;
|
---|
608 | else
|
---|
609 | p = temp;
|
---|
610 | (void)strncpy(p, "INS@XXXX", &temp[tsize - 1] - p);
|
---|
611 | temp[tsize - 1] = '\0';
|
---|
612 | return (mkstemp(temp));
|
---|
613 | }
|
---|
614 |
|
---|
615 | /*
|
---|
616 | * create_newfile --
|
---|
617 | * create a new file, overwriting an existing one if necessary
|
---|
618 | */
|
---|
619 | int
|
---|
620 | create_newfile(const char *path, int target, struct stat *sbp)
|
---|
621 | {
|
---|
622 | char backup[MAXPATHLEN];
|
---|
623 | int saved_errno = 0;
|
---|
624 | int newfd;
|
---|
625 |
|
---|
626 | if (target) {
|
---|
627 | /*
|
---|
628 | * Unlink now... avoid ETXTBSY errors later. Try to turn
|
---|
629 | * off the append/immutable bits -- if we fail, go ahead,
|
---|
630 | * it might work.
|
---|
631 | */
|
---|
632 | #ifdef UF_IMMUTABLE
|
---|
633 | if (sbp->st_flags & NOCHANGEBITS)
|
---|
634 | (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
|
---|
635 | #endif
|
---|
636 |
|
---|
637 | if (dobackup) {
|
---|
638 | if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
|
---|
639 | path, suffix) != strlen(path) + strlen(suffix))
|
---|
640 | errx(EX_OSERR, "%s: backup filename too long",
|
---|
641 | path);
|
---|
642 | (void)snprintf(backup, MAXPATHLEN, "%s%s",
|
---|
643 | path, suffix);
|
---|
644 | if (verbose)
|
---|
645 | (void)printf("install: %s -> %s\n",
|
---|
646 | path, backup);
|
---|
647 | if (rename(path, backup) < 0)
|
---|
648 | err(EX_OSERR, "rename: %s to %s", path, backup);
|
---|
649 | } else
|
---|
650 | if (unlink(path) < 0)
|
---|
651 | saved_errno = errno;
|
---|
652 | }
|
---|
653 |
|
---|
654 | newfd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
|
---|
655 | if (newfd < 0 && saved_errno != 0)
|
---|
656 | errno = saved_errno;
|
---|
657 | return newfd;
|
---|
658 | }
|
---|
659 |
|
---|
660 | /*
|
---|
661 | * copy --
|
---|
662 | * copy from one file to another
|
---|
663 | */
|
---|
664 | void
|
---|
665 | copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
|
---|
666 | off_t size)
|
---|
667 | {
|
---|
668 | int nr, nw;
|
---|
669 | int serrno;
|
---|
670 | char *p, buf[MAXBSIZE];
|
---|
671 | int done_copy;
|
---|
672 |
|
---|
673 | /* Rewind file descriptors. */
|
---|
674 | if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
675 | err(EX_OSERR, "lseek: %s", from_name);
|
---|
676 | if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
|
---|
677 | err(EX_OSERR, "lseek: %s", to_name);
|
---|
678 |
|
---|
679 | /*
|
---|
680 | * Mmap and write if less than 8M (the limit is so we don't totally
|
---|
681 | * trash memory on big files. This is really a minor hack, but it
|
---|
682 | * wins some CPU back.
|
---|
683 | */
|
---|
684 | done_copy = 0;
|
---|
685 | #if !defined(__EMX__) && !defined(_MSC_VER)
|
---|
686 | if (size <= 8 * 1048576 && trymmap(from_fd) &&
|
---|
687 | (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
|
---|
688 | from_fd, (off_t)0)) != (char *)MAP_FAILED) {
|
---|
689 | if ((nw = write(to_fd, p, size)) != size) {
|
---|
690 | serrno = errno;
|
---|
691 | (void)unlink(to_name);
|
---|
692 | errno = nw > 0 ? EIO : serrno;
|
---|
693 | err(EX_OSERR, "%s", to_name);
|
---|
694 | }
|
---|
695 | done_copy = 1;
|
---|
696 | }
|
---|
697 | #endif
|
---|
698 | if (!done_copy) {
|
---|
699 | while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
|
---|
700 | if ((nw = write(to_fd, buf, nr)) != nr) {
|
---|
701 | serrno = errno;
|
---|
702 | (void)unlink(to_name);
|
---|
703 | errno = nw > 0 ? EIO : serrno;
|
---|
704 | err(EX_OSERR, "%s", to_name);
|
---|
705 | }
|
---|
706 | if (nr != 0) {
|
---|
707 | serrno = errno;
|
---|
708 | (void)unlink(to_name);
|
---|
709 | errno = serrno;
|
---|
710 | err(EX_OSERR, "%s", from_name);
|
---|
711 | }
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | /*
|
---|
716 | * strip --
|
---|
717 | * use strip(1) to strip the target file
|
---|
718 | */
|
---|
719 | void
|
---|
720 | strip(const char *to_name)
|
---|
721 | {
|
---|
722 | const char *stripbin;
|
---|
723 | int serrno, status;
|
---|
724 |
|
---|
725 | switch (fork()) {
|
---|
726 | case -1:
|
---|
727 | serrno = errno;
|
---|
728 | (void)unlink(to_name);
|
---|
729 | errno = serrno;
|
---|
730 | err(EX_TEMPFAIL, "fork");
|
---|
731 | case 0:
|
---|
732 | stripbin = getenv("STRIPBIN");
|
---|
733 | if (stripbin == NULL)
|
---|
734 | stripbin = "strip";
|
---|
735 | execlp(stripbin, stripbin, to_name, (char *)NULL);
|
---|
736 | err(EX_OSERR, "exec(%s)", stripbin);
|
---|
737 | default:
|
---|
738 | if (wait(&status) == -1 || status) {
|
---|
739 | serrno = errno;
|
---|
740 | (void)unlink(to_name);
|
---|
741 | errc(EX_SOFTWARE, serrno, "wait");
|
---|
742 | /* NOTREACHED */
|
---|
743 | }
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | /*
|
---|
748 | * install_dir --
|
---|
749 | * build directory heirarchy
|
---|
750 | */
|
---|
751 | void
|
---|
752 | install_dir(char *path)
|
---|
753 | {
|
---|
754 | char *p;
|
---|
755 | struct stat sb;
|
---|
756 | int ch;
|
---|
757 |
|
---|
758 | for (p = path;; ++p)
|
---|
759 | if (!*p || (p != path && *p == '/')) {
|
---|
760 | ch = *p;
|
---|
761 | *p = '\0';
|
---|
762 | if (stat(path, &sb)) {
|
---|
763 | if (errno != ENOENT || mkdir(path, 0755) < 0) {
|
---|
764 | err(EX_OSERR, "mkdir %s", path);
|
---|
765 | /* NOTREACHED */
|
---|
766 | } else if (verbose)
|
---|
767 | (void)printf("install: mkdir %s\n",
|
---|
768 | path);
|
---|
769 | } else if (!S_ISDIR(sb.st_mode))
|
---|
770 | errx(EX_OSERR, "%s exists but is not a directory", path);
|
---|
771 | if (!(*p = ch))
|
---|
772 | break;
|
---|
773 | }
|
---|
774 |
|
---|
775 | if ((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid))
|
---|
776 | warn("chown %u:%u %s", uid, gid, path);
|
---|
777 | if (chmod(path, mode))
|
---|
778 | warn("chmod %o %s", mode, path);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /*
|
---|
782 | * usage --
|
---|
783 | * print a usage message and die
|
---|
784 | */
|
---|
785 | void
|
---|
786 | usage()
|
---|
787 | {
|
---|
788 | (void)fprintf(stderr,
|
---|
789 | "usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
|
---|
790 | " [-o owner] file1 file2\n"
|
---|
791 | " install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]\n"
|
---|
792 | " [-o owner] file1 ... fileN directory\n"
|
---|
793 | " install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
|
---|
794 | exit(EX_USAGE);
|
---|
795 | /* NOTREACHED */
|
---|
796 | }
|
---|
797 |
|
---|
798 | /*
|
---|
799 | * trymmap --
|
---|
800 | * return true (1) if mmap should be tried, false (0) if not.
|
---|
801 | */
|
---|
802 | int
|
---|
803 | trymmap(int fd)
|
---|
804 | {
|
---|
805 | /*
|
---|
806 | * The ifdef is for bootstrapping - f_fstypename doesn't exist in
|
---|
807 | * pre-Lite2-merge systems.
|
---|
808 | */
|
---|
809 | #ifdef MFSNAMELEN
|
---|
810 | struct statfs stfs;
|
---|
811 |
|
---|
812 | if (nommap || fstatfs(fd, &stfs) != 0)
|
---|
813 | return (0);
|
---|
814 | if (strcmp(stfs.f_fstypename, "ufs") == 0 ||
|
---|
815 | strcmp(stfs.f_fstypename, "cd9660") == 0)
|
---|
816 | return (1);
|
---|
817 | #endif
|
---|
818 | return (0);
|
---|
819 | }
|
---|