Bug 459878, fix up errors for 3.0.5 -> 3.1b2 MU test
[mozilla-1.9.git] / config / nsinstall.c
blobb46aeaa1a0499ce4e717b0a4da7adfb1c43604cf
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 ** Netscape portable install command.
40 ** Brendan Eich, 7/20/95
42 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
43 #include <assert.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <dirent.h>
47 #include <limits.h>
48 #include <grp.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <utime.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include "pathsub.h"
59 #ifdef HAVE_GETOPT_H
60 #include <getopt.h>
61 #endif
63 #ifdef SUNOS4
64 #include "sunos4.h"
65 #endif
67 #ifdef NEXTSTEP
68 #include <bsd/libc.h>
69 #endif
71 #ifdef __QNX__
72 #include <unix.h>
73 #endif
75 #ifdef NEED_S_ISLNK
76 #if !defined(S_ISLNK) && defined(S_IFLNK)
77 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
78 #endif
79 #endif
81 #ifndef _DIRECTORY_SEPARATOR
82 #define _DIRECTORY_SEPARATOR "/"
83 #endif /* _DIRECTORY_SEPARATOR */
85 #ifdef NEED_FCHMOD_PROTO
86 extern int fchmod(int fildes, mode_t mode);
87 #endif
89 static void
90 usage(void)
92 fprintf(stderr,
93 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
94 " %*s [-DdltR] file [file ...] directory\n",
95 program, (int) strlen(program), "");
96 exit(2);
99 static int
100 mkdirs(char *path, mode_t mode)
102 char *cp;
103 struct stat sb;
104 int res;
105 int l;
107 /* strip trailing "/." */
108 l = strlen(path);
109 if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/')
110 path[l - 2] = 0;
112 while (*path == '/' && path[1] == '/')
113 path++;
114 for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
115 if (cp && cp != path) {
116 *cp = '\0';
117 if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
118 mkdirs(path, mode) < 0) {
119 return -1;
121 *cp = '/';
124 res = mkdir(path, mode);
125 if ((res != 0) && (errno == EEXIST))
126 return 0;
127 else
128 return res;
131 static uid_t
132 touid(char *owner)
134 struct passwd *pw;
135 uid_t uid;
136 char *cp;
138 pw = getpwnam(owner);
139 if (pw)
140 return pw->pw_uid;
141 uid = strtol(owner, &cp, 0);
142 if (uid == 0 && cp == owner)
143 fail("cannot find uid for %s", owner);
144 return uid;
147 static gid_t
148 togid(char *group)
150 struct group *gr;
151 gid_t gid;
152 char *cp;
154 gr = getgrnam(group);
155 if (gr)
156 return gr->gr_gid;
157 gid = strtol(group, &cp, 0);
158 if (gid == 0 && cp == group)
159 fail("cannot find gid for %s", group);
160 return gid;
163 static void
164 copyfile( char *name, char *toname, mode_t mode, char *group, char *owner,
165 int dotimes, uid_t uid, gid_t gid )
167 int fromfd, tofd, cc, wc, exists;
168 char buf[BUFSIZ], *bp;
169 struct stat sb, tosb;
170 struct utimbuf utb;
172 exists = (lstat(toname, &tosb) == 0);
174 fromfd = open(name, O_RDONLY);
175 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
176 fail("cannot access %s", name);
177 if (exists && (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0))
178 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
179 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
180 if (tofd < 0)
181 fail("cannot create %s", toname);
183 bp = buf;
184 while ((cc = read(fromfd, bp, sizeof buf)) > 0)
186 while ((wc = write(tofd, bp, (unsigned int)cc)) > 0)
188 if ((cc -= wc) == 0)
189 break;
190 bp += wc;
192 if (wc < 0)
193 fail("cannot write to %s", toname);
195 if (cc < 0)
196 fail("cannot read from %s", name);
198 if (ftruncate(tofd, sb.st_size) < 0)
199 fail("cannot truncate %s", toname);
200 #if !defined(VMS)
201 if (dotimes)
203 utb.actime = sb.st_atime;
204 utb.modtime = sb.st_mtime;
205 if (utime(toname, &utb) < 0)
206 fail("cannot set times of %s", toname);
208 #ifdef HAVE_FCHMOD
209 if (fchmod(tofd, mode) < 0)
210 #else
211 if (chmod(toname, mode) < 0)
212 #endif
213 fail("cannot change mode of %s", toname);
214 #endif
215 if ((owner || group) && fchown(tofd, uid, gid) < 0)
216 fail("cannot change owner of %s", toname);
218 /* Must check for delayed (NFS) write errors on close. */
219 if (close(tofd) < 0)
220 fail("cannot write to %s", toname);
221 close(fromfd);
222 #if defined(VMS)
223 if (chmod(toname, (mode & (S_IREAD | S_IWRITE))) < 0)
224 fail("cannot change mode of %s", toname);
225 if (dotimes)
227 utb.actime = sb.st_atime;
228 utb.modtime = sb.st_mtime;
229 if (utime(toname, &utb) < 0)
230 fail("cannot set times of %s", toname);
232 #endif
235 static void
236 copydir( char *from, char *to, mode_t mode, char *group, char *owner,
237 int dotimes, uid_t uid, gid_t gid)
239 int i;
240 DIR *dir;
241 struct dirent *ep;
242 struct stat sb;
243 char *base, *destdir, *direntry, *destentry;
245 base = xbasename(from);
247 /* create destination directory */
248 destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1));
249 sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR, base);
250 if (mkdirs(destdir, mode) != 0) {
251 fail("cannot make directory %s\n", destdir);
252 return;
255 dir = opendir(from);
257 direntry = xmalloc((unsigned int)PATH_MAX);
258 destentry = xmalloc((unsigned int)PATH_MAX);
260 while ((ep = readdir(dir)))
262 if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
263 continue;
265 sprintf(direntry, "%s/%s", from, ep->d_name);
266 sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR, ep->d_name);
268 if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode))
269 copydir( direntry, destdir, mode, group, owner, dotimes, uid, gid );
270 else
271 copyfile( direntry, destentry, mode, group, owner, dotimes, uid, gid );
274 free(direntry);
275 free(destentry);
276 closedir(dir);
280 main(int argc, char **argv)
282 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
283 mode_t mode = 0755;
284 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ];
285 uid_t uid;
286 gid_t gid;
287 struct stat sb, tosb, fromsb;
288 struct utimbuf utb;
290 program = argv[0];
291 cwd = linkname = linkprefix = owner = group = 0;
292 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
294 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
295 switch (opt) {
296 case 'C':
297 cwd = optarg;
298 break;
299 case 'D':
300 onlydir = 1;
301 break;
302 case 'd':
303 dodir = 1;
304 break;
305 case 'l':
306 dolink = 1;
307 break;
308 case 'L':
309 linkprefix = optarg;
310 lplen = strlen(linkprefix);
311 dolink = 1;
312 break;
313 case 'R':
314 dolink = dorelsymlink = 1;
315 break;
316 case 'm':
317 mode = strtoul(optarg, &cp, 8);
318 if (mode == 0 && cp == optarg)
319 usage();
320 break;
321 case 'o':
322 owner = optarg;
323 break;
324 case 'g':
325 group = optarg;
326 break;
327 case 't':
328 dotimes = 1;
329 break;
330 default:
331 usage();
335 argc -= optind;
336 argv += optind;
337 if (argc < 2 - onlydir)
338 usage();
340 todir = argv[argc-1];
341 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
342 mkdirs(todir, 0777) < 0) {
343 fail("cannot make directory %s", todir);
345 if (onlydir)
346 return 0;
348 if (!cwd) {
349 #ifndef NEEDS_GETCWD
350 #ifndef GETCWD_CANT_MALLOC
351 cwd = getcwd(0, PATH_MAX);
352 #else
353 cwd = malloc(PATH_MAX + 1);
354 cwd = getcwd(cwd, PATH_MAX);
355 #endif
356 #else
357 cwd = malloc(PATH_MAX + 1);
358 cwd = getwd(cwd);
359 #endif
362 xchdir(todir);
363 #ifndef NEEDS_GETCWD
364 #ifndef GETCWD_CANT_MALLOC
365 todir = getcwd(0, PATH_MAX);
366 #else
367 todir = malloc(PATH_MAX + 1);
368 todir = getcwd(todir, PATH_MAX);
369 #endif
370 #else
371 todir = malloc(PATH_MAX + 1);
372 todir = getwd(todir);
373 #endif
374 tdlen = strlen(todir);
375 xchdir(cwd);
376 tdlen = strlen(todir);
378 uid = owner ? touid(owner) : (uid_t)(-1);
379 gid = group ? togid(group) : (gid_t)(-1);
381 while (--argc > 0) {
382 name = *argv++;
383 len = strlen(name);
384 base = xbasename(name);
385 bnlen = strlen(base);
386 toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1));
387 sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR, base);
388 exists = (lstat(toname, &tosb) == 0);
390 if (dodir) {
391 /* -d means create a directory, always */
392 if (exists && !S_ISDIR(tosb.st_mode)) {
393 (void) unlink(toname);
394 exists = 0;
396 if (!exists && mkdir(toname, mode) < 0)
397 fail("cannot make directory %s", toname);
398 if ((owner || group) && chown(toname, uid, gid) < 0)
399 fail("cannot change owner of %s", toname);
400 } else if (dolink) {
401 if (access(name, R_OK) != 0) {
402 fail("cannot access %s", name);
404 if (*name == '/') {
405 /* source is absolute pathname, link to it directly */
406 linkname = 0;
407 } else {
408 if (linkprefix) {
409 /* -L implies -l and prefixes names with a $cwd arg. */
410 len += lplen + 1;
411 linkname = xmalloc((unsigned int)(len + 1));
412 sprintf(linkname, "%s/%s", linkprefix, name);
413 } else if (dorelsymlink) {
414 /* Symlink the relative path from todir to source name. */
415 linkname = xmalloc(PATH_MAX);
417 if (*todir == '/') {
418 /* todir is absolute: skip over common prefix. */
419 lplen = relatepaths(todir, cwd, linkname);
420 strcpy(linkname + lplen, name);
421 } else {
422 /* todir is named by a relative path: reverse it. */
423 reversepath(todir, name, len, linkname);
424 xchdir(cwd);
427 len = strlen(linkname);
429 name = linkname;
432 /* Check for a pre-existing symlink with identical content. */
433 if ((exists && (!S_ISLNK(tosb.st_mode) ||
434 readlink(toname, buf, sizeof buf) != len ||
435 strncmp(buf, name, (unsigned int)len) != 0)) ||
436 ((stat(name, &fromsb) == 0) &&
437 (fromsb.st_mtime > tosb.st_mtime))) {
438 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
439 exists = 0;
441 if (!exists && symlink(name, toname) < 0)
442 fail("cannot make symbolic link %s", toname);
443 #ifdef HAVE_LCHOWN
444 if ((owner || group) && lchown(toname, uid, gid) < 0)
445 fail("cannot change owner of %s", toname);
446 #endif
448 if (linkname) {
449 free(linkname);
450 linkname = 0;
452 } else {
453 /* Copy from name to toname, which might be the same file. */
454 if( stat(name, &sb) == 0 && S_IFDIR & sb.st_mode )
456 /* then is directory: must explicitly create destination dir */
457 /* and manually copy files over */
458 copydir( name, todir, mode, group, owner, dotimes, uid, gid );
460 else
462 copyfile(name, toname, mode, group, owner, dotimes, uid, gid);
466 free(toname);
469 free(cwd);
470 free(todir);
471 return 0;