xinstall.c revision 1.48 1 1.48 simonb /* $NetBSD: xinstall.c,v 1.48 2001/09/15 14:55:38 simonb Exp $ */
2 1.5 jtc
3 1.1 cgd /*
4 1.5 jtc * Copyright (c) 1987, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.20 mrg #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.20 mrg __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
39 1.20 mrg The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.5 jtc #if 0
44 1.5 jtc static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
45 1.20 mrg #else
46 1.48 simonb __RCSID("$NetBSD: xinstall.c,v 1.48 2001/09/15 14:55:38 simonb Exp $");
47 1.5 jtc #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.5 jtc #include <sys/wait.h>
52 1.5 jtc #include <sys/mman.h>
53 1.1 cgd #include <sys/stat.h>
54 1.5 jtc
55 1.5 jtc #include <ctype.h>
56 1.5 jtc #include <errno.h>
57 1.5 jtc #include <fcntl.h>
58 1.1 cgd #include <grp.h>
59 1.5 jtc #include <paths.h>
60 1.1 cgd #include <pwd.h>
61 1.1 cgd #include <stdio.h>
62 1.5 jtc #include <stdlib.h>
63 1.5 jtc #include <string.h>
64 1.5 jtc #include <unistd.h>
65 1.5 jtc #include <err.h>
66 1.5 jtc
67 1.1 cgd #include "pathnames.h"
68 1.29 mrg #include "stat_flags.h"
69 1.1 cgd
70 1.28 wsanchez #define STRIP_ARGS_MAX 32
71 1.31 hubertf #define BACKUP_SUFFIX ".old"
72 1.28 wsanchez
73 1.5 jtc struct passwd *pp;
74 1.5 jtc struct group *gp;
75 1.33 christos int dobackup=0, docopy=0, dodir=0, dostrip=0, dolink=0, dopreserve=0,
76 1.38 sommerfe dorename = 0, unpriv = 0;
77 1.35 hubertf static int numberedbackup = 0;
78 1.5 jtc int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
79 1.8 ghudson char pathbuf[MAXPATHLEN];
80 1.8 ghudson uid_t uid;
81 1.8 ghudson gid_t gid;
82 1.28 wsanchez char *stripArgs=NULL;
83 1.31 hubertf char *suffix=BACKUP_SUFFIX;
84 1.5 jtc
85 1.16 christos #define LN_ABSOLUTE 0x01
86 1.16 christos #define LN_RELATIVE 0x02
87 1.16 christos #define LN_HARD 0x04
88 1.16 christos #define LN_SYMBOLIC 0x08
89 1.16 christos #define LN_MIXED 0x10
90 1.16 christos
91 1.5 jtc #define DIRECTORY 0x01 /* Tell install it's a directory. */
92 1.5 jtc #define SETFLAGS 0x02 /* Tell install to set flags. */
93 1.5 jtc
94 1.48 simonb void copy(int, char *, int, char *, off_t);
95 1.48 simonb void makelink(char *, char *);
96 1.48 simonb void install(char *, char *, u_long, u_int);
97 1.48 simonb void install_dir(char *);
98 1.48 simonb void strip(char *);
99 1.48 simonb void backup(const char *);
100 1.48 simonb void usage(void);
101 1.48 simonb int main(int, char *[]);
102 1.1 cgd
103 1.5 jtc int
104 1.48 simonb main(int argc, char *argv[])
105 1.1 cgd {
106 1.1 cgd struct stat from_sb, to_sb;
107 1.42 enami void *set;
108 1.5 jtc u_long fset;
109 1.5 jtc u_int iflags;
110 1.1 cgd int ch, no_target;
111 1.16 christos char *p;
112 1.16 christos char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
113 1.43 cgd
114 1.43 cgd setprogname(argv[0]);
115 1.1 cgd
116 1.5 jtc iflags = 0;
117 1.38 sommerfe while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:prsS:U")) != -1)
118 1.1 cgd switch((char)ch) {
119 1.31 hubertf case 'B':
120 1.31 hubertf suffix = optarg;
121 1.35 hubertf numberedbackup = 0;
122 1.35 hubertf {
123 1.35 hubertf /* Check if given suffix really generates
124 1.35 hubertf different suffixes - catch e.g. ".%" */
125 1.35 hubertf char suffix_expanded0[FILENAME_MAX],
126 1.35 hubertf suffix_expanded1[FILENAME_MAX];
127 1.35 hubertf (void)snprintf(suffix_expanded0, FILENAME_MAX,
128 1.35 hubertf suffix, 0);
129 1.35 hubertf (void)snprintf(suffix_expanded1, FILENAME_MAX,
130 1.35 hubertf suffix, 1);
131 1.35 hubertf if (strcmp(suffix_expanded0, suffix_expanded1)
132 1.35 hubertf != 0)
133 1.35 hubertf numberedbackup = 1;
134 1.35 hubertf }
135 1.31 hubertf /* fall through; -B implies -b */
136 1.31 hubertf case 'b':
137 1.31 hubertf dobackup = 1;
138 1.31 hubertf break;
139 1.1 cgd case 'c':
140 1.1 cgd docopy = 1;
141 1.1 cgd break;
142 1.26 christos case 'd':
143 1.26 christos dodir = 1;
144 1.26 christos break;
145 1.5 jtc case 'f':
146 1.5 jtc flags = optarg;
147 1.5 jtc if (string_to_flags(&flags, &fset, NULL))
148 1.5 jtc errx(1, "%s: invalid flag", flags);
149 1.5 jtc iflags |= SETFLAGS;
150 1.5 jtc break;
151 1.1 cgd case 'g':
152 1.1 cgd group = optarg;
153 1.1 cgd break;
154 1.16 christos case 'l':
155 1.16 christos for (p = optarg; *p; p++)
156 1.16 christos switch (*p) {
157 1.16 christos case 's':
158 1.16 christos dolink &= ~(LN_HARD|LN_MIXED);
159 1.16 christos dolink |= LN_SYMBOLIC;
160 1.16 christos break;
161 1.16 christos case 'h':
162 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_MIXED);
163 1.16 christos dolink |= LN_HARD;
164 1.16 christos break;
165 1.16 christos case 'm':
166 1.16 christos dolink &= ~(LN_SYMBOLIC|LN_HARD);
167 1.16 christos dolink |= LN_MIXED;
168 1.16 christos break;
169 1.16 christos case 'a':
170 1.16 christos dolink &= ~LN_RELATIVE;
171 1.16 christos dolink |= LN_ABSOLUTE;
172 1.16 christos break;
173 1.16 christos case 'r':
174 1.16 christos dolink &= ~LN_ABSOLUTE;
175 1.16 christos dolink |= LN_RELATIVE;
176 1.16 christos break;
177 1.16 christos default:
178 1.16 christos errx(1, "%c: invalid link type", *p);
179 1.16 christos break;
180 1.16 christos }
181 1.16 christos break;
182 1.26 christos case 'm':
183 1.26 christos if (!(set = setmode(optarg)))
184 1.26 christos errx(1, "%s: invalid file mode", optarg);
185 1.26 christos mode = getmode(set, 0);
186 1.42 enami free(set);
187 1.26 christos break;
188 1.26 christos case 'o':
189 1.26 christos owner = optarg;
190 1.26 christos break;
191 1.26 christos case 'p':
192 1.26 christos dopreserve = 1;
193 1.26 christos break;
194 1.33 christos case 'r':
195 1.33 christos dorename = 1;
196 1.33 christos break;
197 1.28 wsanchez case 'S':
198 1.28 wsanchez stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
199 1.28 wsanchez strcpy(stripArgs,optarg);
200 1.28 wsanchez /* fall through; -S implies -s */
201 1.26 christos case 's':
202 1.26 christos dostrip = 1;
203 1.26 christos break;
204 1.38 sommerfe case 'U':
205 1.38 sommerfe unpriv = 1;
206 1.38 sommerfe break;
207 1.1 cgd case '?':
208 1.1 cgd default:
209 1.1 cgd usage();
210 1.1 cgd }
211 1.1 cgd argc -= optind;
212 1.1 cgd argv += optind;
213 1.2 jtc
214 1.23 tv /* strip and link options make no sense when creating directories */
215 1.23 tv if ((dostrip || dolink) && dodir)
216 1.16 christos usage();
217 1.16 christos
218 1.16 christos /* strip and flags make no sense with links */
219 1.16 christos if ((dostrip || flags) && dolink)
220 1.2 jtc usage();
221 1.2 jtc
222 1.2 jtc /* must have at least two arguments, except when creating directories */
223 1.2 jtc if (argc < 2 && !dodir)
224 1.1 cgd usage();
225 1.1 cgd
226 1.1 cgd /* get group and owner id's */
227 1.41 cgd if (unpriv)
228 1.41 cgd uid = gid = -1;
229 1.41 cgd else {
230 1.41 cgd if (group && !(gp = getgrnam(group)) &&
231 1.41 cgd !isdigit((unsigned char)*group))
232 1.41 cgd errx(1, "unknown group %s", group);
233 1.41 cgd gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
234 1.41 cgd if (owner && !(pp = getpwnam(owner)) &&
235 1.41 cgd !isdigit((unsigned char)*owner))
236 1.41 cgd errx(1, "unknown user %s", owner);
237 1.41 cgd uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
238 1.41 cgd }
239 1.1 cgd
240 1.2 jtc if (dodir) {
241 1.2 jtc for (; *argv != NULL; ++argv)
242 1.5 jtc install_dir(*argv);
243 1.2 jtc exit (0);
244 1.5 jtc }
245 1.2 jtc
246 1.1 cgd no_target = stat(to_name = argv[argc - 1], &to_sb);
247 1.2 jtc if (!no_target && S_ISDIR(to_sb.st_mode)) {
248 1.1 cgd for (; *argv != to_name; ++argv)
249 1.5 jtc install(*argv, to_name, fset, iflags | DIRECTORY);
250 1.1 cgd exit(0);
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd /* can't do file1 file2 directory/file */
254 1.1 cgd if (argc != 2)
255 1.1 cgd usage();
256 1.1 cgd
257 1.1 cgd if (!no_target) {
258 1.5 jtc if (stat(*argv, &from_sb))
259 1.5 jtc err(1, "%s", *argv);
260 1.5 jtc if (!S_ISREG(to_sb.st_mode))
261 1.41 cgd errx(1, "%s: not a regular file", to_name);
262 1.16 christos if (!dolink && to_sb.st_dev == from_sb.st_dev &&
263 1.5 jtc to_sb.st_ino == from_sb.st_ino)
264 1.5 jtc errx(1, "%s and %s are the same file", *argv, to_name);
265 1.5 jtc /*
266 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
267 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
268 1.5 jtc * it might work.
269 1.5 jtc */
270 1.5 jtc #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
271 1.5 jtc if (to_sb.st_flags & NOCHANGEBITS)
272 1.5 jtc (void)chflags(to_name,
273 1.5 jtc to_sb.st_flags & ~(NOCHANGEBITS));
274 1.32 hubertf if (dobackup)
275 1.32 hubertf backup(to_name);
276 1.34 christos else if (!dorename)
277 1.31 hubertf (void)unlink(to_name);
278 1.1 cgd }
279 1.5 jtc install(*argv, to_name, fset, iflags);
280 1.1 cgd exit(0);
281 1.1 cgd }
282 1.1 cgd
283 1.1 cgd /*
284 1.16 christos * makelink --
285 1.16 christos * make a link from source to destination
286 1.16 christos */
287 1.16 christos void
288 1.48 simonb makelink(char *from_name, char *to_name)
289 1.16 christos {
290 1.16 christos char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
291 1.16 christos
292 1.16 christos /* Try hard links first */
293 1.16 christos if (dolink & (LN_HARD|LN_MIXED)) {
294 1.16 christos if (link(from_name, to_name) == -1) {
295 1.16 christos if ((dolink & LN_HARD) || errno != EXDEV)
296 1.16 christos err(1, "link %s -> %s", from_name, to_name);
297 1.16 christos }
298 1.16 christos else
299 1.16 christos return;
300 1.16 christos }
301 1.16 christos
302 1.16 christos /* Symbolic links */
303 1.16 christos if (dolink & LN_ABSOLUTE) {
304 1.16 christos /* Convert source path to absolute */
305 1.16 christos if (realpath(from_name, src) == NULL)
306 1.16 christos err(1, "%s", src);
307 1.16 christos if (symlink(src, to_name) == -1)
308 1.16 christos err(1, "symlink %s -> %s", src, to_name);
309 1.16 christos return;
310 1.16 christos }
311 1.16 christos
312 1.16 christos if (dolink & LN_RELATIVE) {
313 1.16 christos char *s, *d;
314 1.16 christos
315 1.16 christos /* Resolve pathnames */
316 1.16 christos if (realpath(from_name, src) == NULL)
317 1.16 christos err(1, "%s", src);
318 1.16 christos if (realpath(to_name, dst) == NULL)
319 1.16 christos err(1, "%s", dst);
320 1.16 christos
321 1.16 christos /* trim common path components */
322 1.16 christos for (s = src, d = dst; *s == *d; s++, d++)
323 1.16 christos continue;
324 1.16 christos while (*s != '/')
325 1.16 christos s--, d--;
326 1.16 christos
327 1.16 christos /* count the number of directories we need to backtrack */
328 1.16 christos for (++d, lnk[0] = '\0'; *d; d++)
329 1.16 christos if (*d == '/')
330 1.16 christos (void) strcat(lnk, "../");
331 1.16 christos
332 1.16 christos (void) strcat(lnk, ++s);
333 1.16 christos
334 1.16 christos if (symlink(lnk, dst) == -1)
335 1.16 christos err(1, "symlink %s -> %s", lnk, dst);
336 1.16 christos return;
337 1.16 christos }
338 1.16 christos
339 1.16 christos /*
340 1.16 christos * If absolute or relative was not specified,
341 1.16 christos * try the names the user provided
342 1.16 christos */
343 1.16 christos if (symlink(from_name, to_name) == -1)
344 1.17 christos err(1, "symlink %s -> %s", from_name, to_name);
345 1.16 christos
346 1.16 christos }
347 1.16 christos
348 1.16 christos /*
349 1.1 cgd * install --
350 1.1 cgd * build a path name and install the file
351 1.1 cgd */
352 1.5 jtc void
353 1.48 simonb install(char *from_name, char *to_name, u_long fset, u_int flags)
354 1.1 cgd {
355 1.5 jtc struct stat from_sb, to_sb;
356 1.5 jtc int devnull, from_fd, to_fd, serrno;
357 1.33 christos char *p, tmpl[MAXPATHLEN], *oto_name;
358 1.5 jtc
359 1.5 jtc /* If try to install NULL file to a directory, fails. */
360 1.5 jtc if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
361 1.5 jtc if (stat(from_name, &from_sb))
362 1.5 jtc err(1, "%s", from_name);
363 1.5 jtc if (!S_ISREG(from_sb.st_mode))
364 1.41 cgd errx(1, "%s: not a regular file", to_name);
365 1.5 jtc /* Build the target path. */
366 1.5 jtc if (flags & DIRECTORY) {
367 1.5 jtc (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
368 1.5 jtc to_name,
369 1.21 lukem (p = strrchr(from_name, '/')) ? ++p : from_name);
370 1.1 cgd to_name = pathbuf;
371 1.1 cgd }
372 1.1 cgd devnull = 0;
373 1.5 jtc } else {
374 1.5 jtc from_sb.st_flags = 0; /* XXX */
375 1.1 cgd devnull = 1;
376 1.5 jtc }
377 1.1 cgd
378 1.5 jtc /*
379 1.5 jtc * Unlink now... avoid ETXTBSY errors later. Try and turn
380 1.5 jtc * off the append/immutable bits -- if we fail, go ahead,
381 1.5 jtc * it might work.
382 1.5 jtc */
383 1.5 jtc if (stat(to_name, &to_sb) == 0 &&
384 1.5 jtc to_sb.st_flags & (NOCHANGEBITS))
385 1.5 jtc (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
386 1.33 christos if (dorename) {
387 1.33 christos char *ptr, c, *dir;
388 1.33 christos
389 1.33 christos if ((ptr = strrchr(to_name, '/')) != NULL) {
390 1.33 christos c = *++ptr;
391 1.33 christos *ptr = '\0';
392 1.33 christos dir = to_name;
393 1.33 christos } else {
394 1.37 christos c = '\0'; /* pacify gcc */
395 1.33 christos dir = tmpl;
396 1.33 christos *dir = '\0';
397 1.33 christos }
398 1.33 christos (void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
399 1.33 christos if (ptr)
400 1.33 christos *ptr = c;
401 1.33 christos oto_name = to_name;
402 1.33 christos to_name = tmpl;
403 1.33 christos
404 1.33 christos } else {
405 1.37 christos oto_name = NULL; /* pacify gcc */
406 1.33 christos if (dobackup)
407 1.33 christos backup(to_name);
408 1.33 christos else
409 1.33 christos (void)unlink(to_name);
410 1.33 christos }
411 1.16 christos
412 1.16 christos if (dolink) {
413 1.16 christos makelink(from_name, to_name);
414 1.16 christos return;
415 1.16 christos }
416 1.1 cgd
417 1.5 jtc /* Create target. */
418 1.33 christos if (dorename) {
419 1.33 christos if ((to_fd = mkstemp(to_name)) == -1)
420 1.33 christos err(1, "%s", to_name);
421 1.33 christos } else {
422 1.33 christos if ((to_fd = open(to_name,
423 1.33 christos O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
424 1.33 christos err(1, "%s", to_name);
425 1.33 christos }
426 1.1 cgd if (!devnull) {
427 1.1 cgd if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
428 1.1 cgd (void)unlink(to_name);
429 1.5 jtc err(1, "%s", from_name);
430 1.1 cgd }
431 1.5 jtc copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
432 1.1 cgd (void)close(from_fd);
433 1.1 cgd }
434 1.9 jonathan
435 1.9 jonathan if (dostrip) {
436 1.1 cgd strip(to_name);
437 1.9 jonathan
438 1.9 jonathan /*
439 1.9 jonathan * Re-open our fd on the target, in case we used a strip
440 1.9 jonathan * that does not work in-place -- like gnu binutils strip.
441 1.9 jonathan */
442 1.9 jonathan close(to_fd);
443 1.9 jonathan if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
444 1.9 jonathan err(1, "stripping %s", to_name);
445 1.9 jonathan }
446 1.9 jonathan
447 1.41 cgd /*
448 1.41 cgd * Set owner, group, mode for target; do the chown first,
449 1.41 cgd * chown may lose the setuid bits.
450 1.41 cgd */
451 1.41 cgd if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
452 1.41 cgd serrno = errno;
453 1.41 cgd (void)unlink(to_name);
454 1.41 cgd errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
455 1.1 cgd }
456 1.5 jtc if (fchmod(to_fd, mode)) {
457 1.5 jtc serrno = errno;
458 1.5 jtc (void)unlink(to_name);
459 1.5 jtc errx(1, "%s: chmod: %s", to_name, strerror(serrno));
460 1.5 jtc }
461 1.5 jtc
462 1.5 jtc /*
463 1.5 jtc * If provided a set of flags, set them, otherwise, preserve the
464 1.5 jtc * flags, except for the dump flag.
465 1.5 jtc */
466 1.5 jtc if (fchflags(to_fd,
467 1.5 jtc flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
468 1.11 fvdl if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
469 1.13 fvdl warn("%s: chflags", to_name);
470 1.26 christos }
471 1.26 christos
472 1.26 christos /*
473 1.26 christos * Preserve the date of the source file.
474 1.26 christos */
475 1.26 christos if (dopreserve) {
476 1.26 christos struct timeval tv[2];
477 1.26 christos
478 1.37 christos #ifdef BSD4_4
479 1.26 christos TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
480 1.26 christos TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
481 1.37 christos #else
482 1.37 christos tv[0].tv_sec = from_sb.st_atime;
483 1.37 christos tv[0].tv_usec = 0;
484 1.37 christos tv[1].tv_sec = from_sb.st_mtime;
485 1.37 christos tv[1].tv_usec = 0;
486 1.37 christos #endif
487 1.26 christos if (futimes(to_fd, tv) == -1)
488 1.26 christos warn("%s: futimes", to_name);
489 1.5 jtc }
490 1.5 jtc
491 1.1 cgd (void)close(to_fd);
492 1.33 christos
493 1.33 christos if (dorename)
494 1.33 christos if (rename(to_name, oto_name) == -1)
495 1.33 christos err(1, "%s: rename", to_name);
496 1.33 christos
497 1.5 jtc if (!docopy && !devnull && unlink(from_name))
498 1.5 jtc err(1, "%s", from_name);
499 1.1 cgd }
500 1.1 cgd
501 1.1 cgd /*
502 1.1 cgd * copy --
503 1.1 cgd * copy from one file to another
504 1.1 cgd */
505 1.5 jtc void
506 1.48 simonb copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size)
507 1.1 cgd {
508 1.36 thorpej ssize_t nr, nw;
509 1.5 jtc int serrno;
510 1.14 thorpej char *p;
511 1.14 thorpej char buf[MAXBSIZE];
512 1.1 cgd
513 1.5 jtc /*
514 1.28 wsanchez * There's no reason to do anything other than close the file
515 1.28 wsanchez * now if it's empty, so let's not bother.
516 1.5 jtc */
517 1.28 wsanchez if (size > 0) {
518 1.45 chs
519 1.28 wsanchez /*
520 1.45 chs * Mmap and write if less than 8M (the limit is so we
521 1.45 chs * don't totally trash memory on big files). This is
522 1.45 chs * really a minor hack, but it wins some CPU back.
523 1.28 wsanchez */
524 1.45 chs
525 1.28 wsanchez if (size <= 8 * 1048576) {
526 1.28 wsanchez if ((p = mmap(NULL, (size_t)size, PROT_READ,
527 1.45 chs MAP_FILE|MAP_SHARED, from_fd, (off_t)0))
528 1.45 chs == MAP_FAILED) {
529 1.45 chs goto mmap_failed;
530 1.36 thorpej }
531 1.44 cgd #ifdef MADV_SEQUENTIAL
532 1.39 christos if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1
533 1.39 christos && errno != EOPNOTSUPP)
534 1.38 sommerfe warnx("madvise: %s", strerror(errno));
535 1.44 cgd #endif
536 1.38 sommerfe
537 1.36 thorpej if (write(to_fd, p, size) != size) {
538 1.36 thorpej serrno = errno;
539 1.36 thorpej (void)unlink(to_name);
540 1.36 thorpej errx(1, "%s: %s",
541 1.36 thorpej to_name, strerror(serrno));
542 1.36 thorpej }
543 1.28 wsanchez } else {
544 1.45 chs mmap_failed:
545 1.36 thorpej while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
546 1.28 wsanchez if ((nw = write(to_fd, buf, nr)) != nr) {
547 1.28 wsanchez serrno = errno;
548 1.28 wsanchez (void)unlink(to_name);
549 1.45 chs errx(1, "%s: %s", to_name,
550 1.45 chs strerror(nw > 0 ? EIO : serrno));
551 1.28 wsanchez }
552 1.36 thorpej }
553 1.28 wsanchez if (nr != 0) {
554 1.5 jtc serrno = errno;
555 1.5 jtc (void)unlink(to_name);
556 1.28 wsanchez errx(1, "%s: %s", from_name, strerror(serrno));
557 1.5 jtc }
558 1.1 cgd }
559 1.1 cgd }
560 1.1 cgd }
561 1.1 cgd
562 1.1 cgd /*
563 1.1 cgd * strip --
564 1.1 cgd * use strip(1) to strip the target file
565 1.1 cgd */
566 1.5 jtc void
567 1.48 simonb strip(char *to_name)
568 1.1 cgd {
569 1.5 jtc int serrno, status;
570 1.18 thorpej char *stripprog;
571 1.1 cgd
572 1.1 cgd switch (vfork()) {
573 1.1 cgd case -1:
574 1.5 jtc serrno = errno;
575 1.5 jtc (void)unlink(to_name);
576 1.22 thorpej errx(1, "vfork: %s", strerror(serrno));
577 1.1 cgd case 0:
578 1.18 thorpej stripprog = getenv("STRIP");
579 1.18 thorpej if (stripprog == NULL)
580 1.18 thorpej stripprog = _PATH_STRIP;
581 1.28 wsanchez
582 1.28 wsanchez if (stripArgs) {
583 1.28 wsanchez /* build up a command line and let /bin/sh parse the arguments */
584 1.28 wsanchez char* cmd = (char*)malloc(sizeof(char)*
585 1.28 wsanchez (3+strlen(stripprog)+
586 1.28 wsanchez strlen(stripArgs)+
587 1.28 wsanchez strlen(to_name)));
588 1.28 wsanchez
589 1.28 wsanchez sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
590 1.28 wsanchez
591 1.28 wsanchez execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
592 1.28 wsanchez } else
593 1.40 cgd execlp(stripprog, "strip", to_name, NULL);
594 1.28 wsanchez
595 1.22 thorpej warn("%s", stripprog);
596 1.22 thorpej _exit(1);
597 1.1 cgd default:
598 1.1 cgd if (wait(&status) == -1 || status)
599 1.5 jtc (void)unlink(to_name);
600 1.1 cgd }
601 1.32 hubertf }
602 1.32 hubertf
603 1.32 hubertf /*
604 1.32 hubertf * backup file "to_name" to to_name.suffix
605 1.32 hubertf * if suffix contains a "%", it's taken as a printf(3) pattern
606 1.32 hubertf * used for a numbered backup.
607 1.32 hubertf */
608 1.32 hubertf void
609 1.48 simonb backup(const char *to_name)
610 1.32 hubertf {
611 1.32 hubertf char backup[FILENAME_MAX];
612 1.32 hubertf
613 1.35 hubertf if (numberedbackup) {
614 1.32 hubertf /* Do numbered backup */
615 1.32 hubertf int cnt;
616 1.32 hubertf char suffix_expanded[FILENAME_MAX];
617 1.32 hubertf
618 1.32 hubertf cnt=0;
619 1.32 hubertf do {
620 1.32 hubertf (void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
621 1.32 hubertf (void)snprintf(backup, FILENAME_MAX, "%s%s",
622 1.32 hubertf to_name, suffix_expanded);
623 1.32 hubertf cnt++;
624 1.32 hubertf } while (access(backup, F_OK)==0);
625 1.32 hubertf } else {
626 1.32 hubertf /* Do simple backup */
627 1.32 hubertf (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
628 1.32 hubertf }
629 1.32 hubertf
630 1.32 hubertf (void)rename(to_name, backup);
631 1.1 cgd }
632 1.1 cgd
633 1.1 cgd /*
634 1.5 jtc * install_dir --
635 1.47 wiz * build directory hierarchy
636 1.2 jtc */
637 1.7 jtc void
638 1.48 simonb install_dir(char *path)
639 1.2 jtc {
640 1.20 mrg char *p;
641 1.2 jtc struct stat sb;
642 1.4 cgd int ch;
643 1.2 jtc
644 1.4 cgd for (p = path;; ++p)
645 1.4 cgd if (!*p || (p != path && *p == '/')) {
646 1.2 jtc ch = *p;
647 1.2 jtc *p = '\0';
648 1.2 jtc if (stat(path, &sb)) {
649 1.2 jtc if (errno != ENOENT || mkdir(path, 0777) < 0) {
650 1.7 jtc err(1, "%s", path);
651 1.7 jtc /* NOTREACHED */
652 1.2 jtc }
653 1.2 jtc }
654 1.2 jtc if (!(*p = ch))
655 1.4 cgd break;
656 1.2 jtc }
657 1.2 jtc
658 1.8 ghudson if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
659 1.2 jtc chmod(path, mode)) {
660 1.5 jtc warn("%s", path);
661 1.41 cgd }
662 1.2 jtc }
663 1.2 jtc
664 1.2 jtc /*
665 1.1 cgd * usage --
666 1.1 cgd * print a usage message and die
667 1.1 cgd */
668 1.5 jtc void
669 1.48 simonb usage(void)
670 1.1 cgd {
671 1.2 jtc (void)fprintf(stderr, "\
672 1.46 kleink usage: install [-Ubcprs] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
673 1.46 kleink install [-Ubcprs] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
674 1.38 sommerfe install [-Up] -d [-m mode] [-o owner] [-g group] directory ...\n");
675 1.6 jtc exit(1);
676 1.1 cgd }
677