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