Bug 1467936 [wpt PR 11436] - Split up editing/run/* with `variant`, a=testonly
[gecko.git] / config / nsinstall.c
blobd36176c0129a0ce731334e0852ae28041868cc77
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /*
6 ** Netscape portable install command.
7 **
8 ** Brendan Eich, 7/20/95
9 */
10 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
11 #include <assert.h>
12 #include <fcntl.h>
13 #include <errno.h>
14 #include <dirent.h>
15 #include <limits.h>
16 #include <grp.h>
17 #include <pwd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <utime.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include "pathsub.h"
27 #ifdef HAVE_GETOPT_H
28 #include <getopt.h>
29 #endif
31 #ifdef SUNOS4
32 #include "sunos4.h"
33 #endif
35 #ifdef NEXTSTEP
36 #include <bsd/libc.h>
37 #endif
39 #ifdef __QNX__
40 #include <unix.h>
41 #endif
43 #ifdef NEED_S_ISLNK
44 #if !defined(S_ISLNK) && defined(S_IFLNK)
45 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
46 #endif
47 #endif
49 #ifndef _DIRECTORY_SEPARATOR
50 #define _DIRECTORY_SEPARATOR "/"
51 #endif /* _DIRECTORY_SEPARATOR */
53 #ifdef NEED_FCHMOD_PROTO
54 extern int fchmod(int fildes, mode_t mode);
55 #endif
57 static void
58 usage(void)
60 fprintf(stderr,
61 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
62 " %*s [-DdltR] file [file ...] directory\n",
63 program, (int) strlen(program), "");
64 exit(2);
67 static int
68 mkdirs(char *path, mode_t mode)
70 char *cp;
71 struct stat sb;
72 int res;
73 int l;
75 /* strip trailing "/." */
76 l = strlen(path);
77 if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/')
78 path[l - 2] = 0;
80 while (*path == '/' && path[1] == '/')
81 path++;
82 for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
83 if (cp && cp != path) {
84 *cp = '\0';
85 if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
86 mkdirs(path, mode) < 0) {
87 return -1;
89 *cp = '/';
92 res = mkdir(path, mode);
93 if ((res != 0) && (errno == EEXIST))
94 return 0;
95 else
96 return res;
99 static uid_t
100 touid(char *owner)
102 struct passwd *pw;
103 uid_t uid;
104 char *cp;
106 pw = getpwnam(owner);
107 if (pw)
108 return pw->pw_uid;
109 uid = strtol(owner, &cp, 0);
110 if (uid == 0 && cp == owner)
111 fail("cannot find uid for %s", owner);
112 return uid;
115 static gid_t
116 togid(char *group)
118 struct group *gr;
119 gid_t gid;
120 char *cp;
122 gr = getgrnam(group);
123 if (gr)
124 return gr->gr_gid;
125 gid = strtol(group, &cp, 0);
126 if (gid == 0 && cp == group)
127 fail("cannot find gid for %s", group);
128 return gid;
131 static void
132 copyfile( char *name, char *toname, mode_t mode, char *group, char *owner,
133 int dotimes, uid_t uid, gid_t gid )
135 int fromfd, tofd = -1, cc, wc, exists;
136 char buf[BUFSIZ], *bp;
137 struct stat sb, tosb;
138 struct utimbuf utb;
140 exists = (lstat(toname, &tosb) == 0);
142 fromfd = open(name, O_RDONLY);
143 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
144 fail("cannot access %s", name);
145 if (exists) {
146 if (S_ISREG(tosb.st_mode)) {
147 /* See if we can open it. This is more reliable than 'access'. */
148 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
150 if (tofd < 0) {
151 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
154 if (tofd < 0) {
155 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
156 if (tofd < 0)
157 fail("cannot create %s", toname);
160 bp = buf;
161 while ((cc = read(fromfd, bp, sizeof buf)) > 0)
163 while ((wc = write(tofd, bp, (unsigned int)cc)) > 0)
165 if ((cc -= wc) == 0)
166 break;
167 bp += wc;
169 if (wc < 0)
170 fail("cannot write to %s", toname);
172 if (cc < 0)
173 fail("cannot read from %s", name);
175 if (ftruncate(tofd, sb.st_size) < 0)
176 fail("cannot truncate %s", toname);
177 #if !defined(VMS)
178 if (dotimes)
180 utb.actime = sb.st_atime;
181 utb.modtime = sb.st_mtime;
182 if (utime(toname, &utb) < 0)
183 fail("cannot set times of %s", toname);
185 #ifdef HAVE_FCHMOD
186 if (fchmod(tofd, mode) < 0)
187 #else
188 if (chmod(toname, mode) < 0)
189 #endif
190 fail("cannot change mode of %s", toname);
191 #endif
192 if ((owner || group) && fchown(tofd, uid, gid) < 0)
193 fail("cannot change owner of %s", toname);
195 /* Must check for delayed (NFS) write errors on close. */
196 if (close(tofd) < 0)
197 fail("cannot write to %s", toname);
198 close(fromfd);
199 #if defined(VMS)
200 if (chmod(toname, (mode & (S_IREAD | S_IWRITE))) < 0)
201 fail("cannot change mode of %s", toname);
202 if (dotimes)
204 utb.actime = sb.st_atime;
205 utb.modtime = sb.st_mtime;
206 if (utime(toname, &utb) < 0)
207 fail("cannot set times of %s", toname);
209 #endif
212 static void
213 copydir( char *from, char *to, mode_t mode, char *group, char *owner,
214 int dotimes, uid_t uid, gid_t gid)
216 DIR *dir;
217 struct dirent *ep;
218 struct stat sb;
219 char *base, *destdir, *direntry, *destentry;
221 base = xbasename(from);
223 /* create destination directory */
224 destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1));
225 sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR, base);
226 if (mkdirs(destdir, mode) != 0) {
227 fail("cannot make directory %s\n", destdir);
228 free(destdir);
229 return;
232 if (!(dir = opendir(from))) {
233 fail("cannot open directory %s\n", from);
234 free(destdir);
235 return;
238 direntry = xmalloc((unsigned int)PATH_MAX);
239 destentry = xmalloc((unsigned int)PATH_MAX);
241 while ((ep = readdir(dir)))
243 if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
244 continue;
246 sprintf(direntry, "%s/%s", from, ep->d_name);
247 sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR, ep->d_name);
249 if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode))
250 copydir( direntry, destdir, mode, group, owner, dotimes, uid, gid );
251 else
252 copyfile( direntry, destentry, mode, group, owner, dotimes, uid, gid );
255 free(destdir);
256 free(direntry);
257 free(destentry);
258 closedir(dir);
262 main(int argc, char **argv)
264 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists;
265 mode_t mode = 0755;
266 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, buf[BUFSIZ];
267 uid_t uid;
268 gid_t gid;
269 struct stat sb, tosb, fromsb;
271 program = argv[0];
272 cwd = linkname = linkprefix = owner = group = 0;
273 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
275 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
276 switch (opt) {
277 case 'C':
278 cwd = optarg;
279 break;
280 case 'D':
281 onlydir = 1;
282 break;
283 case 'd':
284 dodir = 1;
285 break;
286 case 'L':
287 linkprefix = optarg;
288 lplen = strlen(linkprefix);
289 dolink = 1;
290 break;
291 case 'R':
292 dolink = dorelsymlink = 1;
293 break;
294 case 'm':
295 mode = strtoul(optarg, &cp, 8);
296 if (mode == 0 && cp == optarg)
297 usage();
298 break;
299 case 'o':
300 owner = optarg;
301 break;
302 case 'g':
303 group = optarg;
304 break;
305 case 't':
306 dotimes = 1;
307 break;
308 default:
309 usage();
313 argc -= optind;
314 argv += optind;
315 if (argc < 2 - onlydir)
316 usage();
318 todir = argv[argc-1];
319 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
320 mkdirs(todir, 0777) < 0) {
321 fail("cannot make directory %s", todir);
323 if (onlydir)
324 return 0;
326 if (!cwd) {
327 #ifndef NEEDS_GETCWD
328 #ifndef GETCWD_CANT_MALLOC
329 cwd = getcwd(0, PATH_MAX);
330 #else
331 cwd = malloc(PATH_MAX + 1);
332 cwd = getcwd(cwd, PATH_MAX);
333 #endif
334 #else
335 cwd = malloc(PATH_MAX + 1);
336 cwd = getwd(cwd);
337 #endif
340 xchdir(todir);
341 #ifndef NEEDS_GETCWD
342 #ifndef GETCWD_CANT_MALLOC
343 todir = getcwd(0, PATH_MAX);
344 #else
345 todir = malloc(PATH_MAX + 1);
346 todir = getcwd(todir, PATH_MAX);
347 #endif
348 #else
349 todir = malloc(PATH_MAX + 1);
350 todir = getwd(todir);
351 #endif
352 tdlen = strlen(todir);
353 xchdir(cwd);
354 tdlen = strlen(todir);
356 uid = owner ? touid(owner) : (uid_t)(-1);
357 gid = group ? togid(group) : (gid_t)(-1);
359 while (--argc > 0) {
360 name = *argv++;
361 len = strlen(name);
362 base = xbasename(name);
363 bnlen = strlen(base);
364 toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1));
365 sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR, base);
366 exists = (lstat(toname, &tosb) == 0);
368 if (dodir) {
369 /* -d means create a directory, always */
370 if (exists && !S_ISDIR(tosb.st_mode)) {
371 (void) unlink(toname);
372 exists = 0;
374 if (!exists && mkdir(toname, mode) < 0)
375 fail("cannot make directory %s", toname);
376 if ((owner || group) && chown(toname, uid, gid) < 0)
377 fail("cannot change owner of %s", toname);
378 } else if (dolink) {
379 if (access(name, R_OK) != 0) {
380 fail("cannot access %s", name);
382 if (*name == '/') {
383 /* source is absolute pathname, link to it directly */
384 linkname = 0;
385 } else {
386 if (linkprefix) {
387 /* -L prefixes names with a $cwd arg. */
388 len += lplen + 1;
389 linkname = xmalloc((unsigned int)(len + 1));
390 sprintf(linkname, "%s/%s", linkprefix, name);
391 } else if (dorelsymlink) {
392 /* Symlink the relative path from todir to source name. */
393 linkname = xmalloc(PATH_MAX);
395 if (*todir == '/') {
396 /* todir is absolute: skip over common prefix. */
397 lplen = relatepaths(todir, cwd, linkname);
398 strcpy(linkname + lplen, name);
399 } else {
400 /* todir is named by a relative path: reverse it. */
401 reversepath(todir, name, len, linkname);
402 xchdir(cwd);
405 len = strlen(linkname);
407 name = linkname;
410 /* Check for a pre-existing symlink with identical content. */
411 if (exists && (!S_ISLNK(tosb.st_mode) ||
412 readlink(toname, buf, sizeof buf) != len ||
413 strncmp(buf, name, (unsigned int)len) != 0 ||
414 ((stat(name, &fromsb) == 0) &&
415 (fromsb.st_mtime > tosb.st_mtime)
416 ))) {
417 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
418 exists = 0;
420 if (!exists && symlink(name, toname) < 0)
421 fail("cannot make symbolic link %s", toname);
422 #ifdef HAVE_LCHOWN
423 if ((owner || group) && lchown(toname, uid, gid) < 0)
424 fail("cannot change owner of %s", toname);
425 #endif
427 if (linkname) {
428 free(linkname);
429 linkname = 0;
431 } else {
432 /* Copy from name to toname, which might be the same file. */
433 if( stat(name, &sb) == 0 && S_IFDIR & sb.st_mode )
435 /* then is directory: must explicitly create destination dir */
436 /* and manually copy files over */
437 copydir( name, todir, mode, group, owner, dotimes, uid, gid );
439 else
441 copyfile(name, toname, mode, group, owner, dotimes, uid, gid);
445 free(toname);
448 free(cwd);
449 free(todir);
450 return 0;