nsprpub/config/nsinstall.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** Netscape portable install command.
michael@0 8 **
michael@0 9 ** Brendan Eich, 7/20/95
michael@0 10 */
michael@0 11 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
michael@0 12 #include <assert.h>
michael@0 13 #include <fcntl.h>
michael@0 14 #include <grp.h>
michael@0 15 #include <pwd.h>
michael@0 16 #include <stdlib.h>
michael@0 17 #include <string.h>
michael@0 18 #include <unistd.h>
michael@0 19 #include <utime.h>
michael@0 20 #include <sys/types.h>
michael@0 21 #include <sys/stat.h>
michael@0 22 #include <dirent.h>
michael@0 23 #include <errno.h>
michael@0 24 #include <stdarg.h>
michael@0 25 #ifdef USE_REENTRANT_LIBC
michael@0 26 #include "libc_r.h"
michael@0 27 #endif /* USE_REENTRANT_LIBC */
michael@0 28
michael@0 29 #include "pathsub.h"
michael@0 30
michael@0 31 #define HAVE_FCHMOD
michael@0 32
michael@0 33 #if defined(BEOS)
michael@0 34 #undef HAVE_FCHMOD
michael@0 35 #endif
michael@0 36
michael@0 37 /*
michael@0 38 * Does getcwd() take NULL as the first argument and malloc
michael@0 39 * the result buffer?
michael@0 40 */
michael@0 41 #if !defined(DARWIN)
michael@0 42 #define GETCWD_CAN_MALLOC
michael@0 43 #endif
michael@0 44
michael@0 45 #if defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)
michael@0 46 #include <getopt.h>
michael@0 47 #endif
michael@0 48
michael@0 49 #if defined(SCO) || defined(UNIXWARE)
michael@0 50 #if !defined(S_ISLNK) && defined(S_IFLNK)
michael@0 51 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
michael@0 52 #endif
michael@0 53 #endif
michael@0 54
michael@0 55 #ifdef QNX
michael@0 56 #define d_ino d_stat.st_ino
michael@0 57 #endif
michael@0 58
michael@0 59 static void
michael@0 60 usage(void)
michael@0 61 {
michael@0 62 fprintf(stderr,
michael@0 63 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
michael@0 64 " %*s [-DdltR] file [file ...] directory\n",
michael@0 65 program, (int)strlen(program), "");
michael@0 66 exit(2);
michael@0 67 }
michael@0 68
michael@0 69 static int
michael@0 70 mkdirs(char *path, mode_t mode)
michael@0 71 {
michael@0 72 char *cp;
michael@0 73 struct stat sb;
michael@0 74 int res;
michael@0 75
michael@0 76 while (*path == '/' && path[1] == '/')
michael@0 77 path++;
michael@0 78 for (cp = strrchr(path, '/'); cp && cp != path && cp[-1] == '/'; cp--)
michael@0 79 ;
michael@0 80 if (cp && cp != path) {
michael@0 81 *cp = '\0';
michael@0 82 if ((stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
michael@0 83 mkdirs(path, mode) < 0) {
michael@0 84 return -1;
michael@0 85 }
michael@0 86 *cp = '/';
michael@0 87 }
michael@0 88 res = mkdir(path, mode);
michael@0 89 if ((res != 0) && (errno == EEXIST))
michael@0 90 return 0;
michael@0 91 else
michael@0 92 return res;
michael@0 93 }
michael@0 94
michael@0 95 static uid_t
michael@0 96 touid(char *owner)
michael@0 97 {
michael@0 98 struct passwd *pw;
michael@0 99 uid_t uid;
michael@0 100 char *cp;
michael@0 101
michael@0 102 pw = getpwnam(owner);
michael@0 103 if (pw)
michael@0 104 return pw->pw_uid;
michael@0 105 uid = strtol(owner, &cp, 0);
michael@0 106 if (uid == 0 && cp == owner)
michael@0 107 fail("cannot find uid for %s", owner);
michael@0 108 return uid;
michael@0 109 }
michael@0 110
michael@0 111 static gid_t
michael@0 112 togid(char *group)
michael@0 113 {
michael@0 114 struct group *gr;
michael@0 115 gid_t gid;
michael@0 116 char *cp;
michael@0 117
michael@0 118 gr = getgrnam(group);
michael@0 119 if (gr)
michael@0 120 return gr->gr_gid;
michael@0 121 gid = strtol(group, &cp, 0);
michael@0 122 if (gid == 0 && cp == group)
michael@0 123 fail("cannot find gid for %s", group);
michael@0 124 return gid;
michael@0 125 }
michael@0 126
michael@0 127 int
michael@0 128 main(int argc, char **argv)
michael@0 129 {
michael@0 130 int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
michael@0 131 mode_t mode = 0755;
michael@0 132 char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ];
michael@0 133 uid_t uid;
michael@0 134 gid_t gid;
michael@0 135 struct stat sb, tosb;
michael@0 136 struct utimbuf utb;
michael@0 137
michael@0 138 program = argv[0];
michael@0 139 cwd = linkname = linkprefix = owner = group = 0;
michael@0 140 onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
michael@0 141
michael@0 142 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
michael@0 143 switch (opt) {
michael@0 144 case 'C':
michael@0 145 cwd = optarg;
michael@0 146 break;
michael@0 147 case 'D':
michael@0 148 onlydir = 1;
michael@0 149 break;
michael@0 150 case 'd':
michael@0 151 dodir = 1;
michael@0 152 break;
michael@0 153 case 'l':
michael@0 154 dolink = 1;
michael@0 155 break;
michael@0 156 case 'L':
michael@0 157 linkprefix = optarg;
michael@0 158 lplen = strlen(linkprefix);
michael@0 159 dolink = 1;
michael@0 160 break;
michael@0 161 case 'R':
michael@0 162 dolink = dorelsymlink = 1;
michael@0 163 break;
michael@0 164 case 'm':
michael@0 165 mode = strtoul(optarg, &cp, 8);
michael@0 166 if (mode == 0 && cp == optarg)
michael@0 167 usage();
michael@0 168 break;
michael@0 169 case 'o':
michael@0 170 owner = optarg;
michael@0 171 break;
michael@0 172 case 'g':
michael@0 173 group = optarg;
michael@0 174 break;
michael@0 175 case 't':
michael@0 176 dotimes = 1;
michael@0 177 break;
michael@0 178 default:
michael@0 179 usage();
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 argc -= optind;
michael@0 184 argv += optind;
michael@0 185 if (argc < 2 - onlydir)
michael@0 186 usage();
michael@0 187
michael@0 188 todir = argv[argc-1];
michael@0 189 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
michael@0 190 mkdirs(todir, 0777) < 0) {
michael@0 191 fail("cannot make directory %s", todir);
michael@0 192 }
michael@0 193 if (onlydir)
michael@0 194 return 0;
michael@0 195
michael@0 196 if (!cwd) {
michael@0 197 #ifdef GETCWD_CAN_MALLOC
michael@0 198 cwd = getcwd(0, PATH_MAX);
michael@0 199 #else
michael@0 200 cwd = malloc(PATH_MAX + 1);
michael@0 201 cwd = getcwd(cwd, PATH_MAX);
michael@0 202 #endif
michael@0 203 }
michael@0 204 xchdir(todir);
michael@0 205 #ifdef GETCWD_CAN_MALLOC
michael@0 206 todir = getcwd(0, PATH_MAX);
michael@0 207 #else
michael@0 208 todir = malloc(PATH_MAX + 1);
michael@0 209 todir = getcwd(todir, PATH_MAX);
michael@0 210 #endif
michael@0 211 xchdir(cwd);
michael@0 212 tdlen = strlen(todir);
michael@0 213
michael@0 214 uid = owner ? touid(owner) : -1;
michael@0 215 gid = group ? togid(group) : -1;
michael@0 216
michael@0 217 while (--argc > 0) {
michael@0 218 name = *argv++;
michael@0 219 len = strlen(name);
michael@0 220 base = xbasename(name);
michael@0 221 bnlen = strlen(base);
michael@0 222 toname = (char*)xmalloc(tdlen + 1 + bnlen + 1);
michael@0 223 sprintf(toname, "%s/%s", todir, base);
michael@0 224 exists = (lstat(toname, &tosb) == 0);
michael@0 225
michael@0 226 if (dodir) {
michael@0 227 /* -d means create a directory, always */
michael@0 228 if (exists && !S_ISDIR(tosb.st_mode)) {
michael@0 229 (void) unlink(toname);
michael@0 230 exists = 0;
michael@0 231 }
michael@0 232 if (!exists && mkdir(toname, mode) < 0)
michael@0 233 fail("cannot make directory %s", toname);
michael@0 234 if ((owner || group) && chown(toname, uid, gid) < 0)
michael@0 235 fail("cannot change owner of %s", toname);
michael@0 236 } else if (dolink) {
michael@0 237 if (*name == '/') {
michael@0 238 /* source is absolute pathname, link to it directly */
michael@0 239 linkname = 0;
michael@0 240 } else {
michael@0 241 if (linkprefix) {
michael@0 242 /* -L implies -l and prefixes names with a $cwd arg. */
michael@0 243 len += lplen + 1;
michael@0 244 linkname = (char*)xmalloc(len + 1);
michael@0 245 sprintf(linkname, "%s/%s", linkprefix, name);
michael@0 246 } else if (dorelsymlink) {
michael@0 247 /* Symlink the relative path from todir to source name. */
michael@0 248 linkname = (char*)xmalloc(PATH_MAX);
michael@0 249
michael@0 250 if (*todir == '/') {
michael@0 251 /* todir is absolute: skip over common prefix. */
michael@0 252 lplen = relatepaths(todir, cwd, linkname);
michael@0 253 strcpy(linkname + lplen, name);
michael@0 254 } else {
michael@0 255 /* todir is named by a relative path: reverse it. */
michael@0 256 reversepath(todir, name, len, linkname);
michael@0 257 xchdir(cwd);
michael@0 258 }
michael@0 259
michael@0 260 len = strlen(linkname);
michael@0 261 }
michael@0 262 name = linkname;
michael@0 263 }
michael@0 264
michael@0 265 /* Check for a pre-existing symlink with identical content. */
michael@0 266 if (exists &&
michael@0 267 (!S_ISLNK(tosb.st_mode) ||
michael@0 268 readlink(toname, buf, sizeof buf) != len ||
michael@0 269 strncmp(buf, name, len) != 0)) {
michael@0 270 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
michael@0 271 exists = 0;
michael@0 272 }
michael@0 273 if (!exists && symlink(name, toname) < 0)
michael@0 274 fail("cannot make symbolic link %s", toname);
michael@0 275 #ifdef HAVE_LCHOWN
michael@0 276 if ((owner || group) && lchown(toname, uid, gid) < 0)
michael@0 277 fail("cannot change owner of %s", toname);
michael@0 278 #endif
michael@0 279
michael@0 280 if (linkname) {
michael@0 281 free(linkname);
michael@0 282 linkname = 0;
michael@0 283 }
michael@0 284 } else {
michael@0 285 /* Copy from name to toname, which might be the same file. */
michael@0 286 fromfd = open(name, O_RDONLY);
michael@0 287 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
michael@0 288 fail("cannot access %s", name);
michael@0 289 if (exists && (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0))
michael@0 290 (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
michael@0 291 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
michael@0 292 if (tofd < 0)
michael@0 293 fail("cannot create %s", toname);
michael@0 294
michael@0 295 bp = buf;
michael@0 296 while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
michael@0 297 while ((wc = write(tofd, bp, cc)) > 0) {
michael@0 298 if ((cc -= wc) == 0)
michael@0 299 break;
michael@0 300 bp += wc;
michael@0 301 }
michael@0 302 if (wc < 0)
michael@0 303 fail("cannot write to %s", toname);
michael@0 304 }
michael@0 305 if (cc < 0)
michael@0 306 fail("cannot read from %s", name);
michael@0 307
michael@0 308 if (ftruncate(tofd, sb.st_size) < 0)
michael@0 309 fail("cannot truncate %s", toname);
michael@0 310 if (dotimes) {
michael@0 311 utb.actime = sb.st_atime;
michael@0 312 utb.modtime = sb.st_mtime;
michael@0 313 if (utime(toname, &utb) < 0)
michael@0 314 fail("cannot set times of %s", toname);
michael@0 315 }
michael@0 316 #ifdef HAVE_FCHMOD
michael@0 317 if (fchmod(tofd, mode) < 0)
michael@0 318 #else
michael@0 319 if (chmod(toname, mode) < 0)
michael@0 320 #endif
michael@0 321 fail("cannot change mode of %s", toname);
michael@0 322 if ((owner || group) && fchown(tofd, uid, gid) < 0)
michael@0 323 fail("cannot change owner of %s", toname);
michael@0 324
michael@0 325 /* Must check for delayed (NFS) write errors on close. */
michael@0 326 if (close(tofd) < 0)
michael@0 327 fail("cannot write to %s", toname);
michael@0 328 close(fromfd);
michael@0 329 }
michael@0 330
michael@0 331 free(toname);
michael@0 332 }
michael@0 333
michael@0 334 free(cwd);
michael@0 335 free(todir);
michael@0 336 return 0;
michael@0 337 }
michael@0 338
michael@0 339 /*
michael@0 340 ** Pathname subroutines.
michael@0 341 **
michael@0 342 ** Brendan Eich, 8/29/95
michael@0 343 */
michael@0 344
michael@0 345 char *program;
michael@0 346
michael@0 347 void
michael@0 348 fail(char *format, ...)
michael@0 349 {
michael@0 350 int error;
michael@0 351 va_list ap;
michael@0 352
michael@0 353 #ifdef USE_REENTRANT_LIBC
michael@0 354 R_STRERROR_INIT_R();
michael@0 355 #endif
michael@0 356
michael@0 357 error = errno;
michael@0 358 fprintf(stderr, "%s: ", program);
michael@0 359 va_start(ap, format);
michael@0 360 vfprintf(stderr, format, ap);
michael@0 361 va_end(ap);
michael@0 362 if (error)
michael@0 363
michael@0 364 #ifdef USE_REENTRANT_LIBC
michael@0 365 R_STRERROR_R(errno);
michael@0 366 fprintf(stderr, ": %s", r_strerror_r);
michael@0 367 #else
michael@0 368 fprintf(stderr, ": %s", strerror(errno));
michael@0 369 #endif
michael@0 370
michael@0 371 putc('\n', stderr);
michael@0 372 exit(1);
michael@0 373 }
michael@0 374
michael@0 375 char *
michael@0 376 getcomponent(char *path, char *name)
michael@0 377 {
michael@0 378 if (*path == '\0')
michael@0 379 return 0;
michael@0 380 if (*path == '/') {
michael@0 381 *name++ = '/';
michael@0 382 } else {
michael@0 383 do {
michael@0 384 *name++ = *path++;
michael@0 385 } while (*path != '/' && *path != '\0');
michael@0 386 }
michael@0 387 *name = '\0';
michael@0 388 while (*path == '/')
michael@0 389 path++;
michael@0 390 return path;
michael@0 391 }
michael@0 392
michael@0 393 #ifdef UNIXWARE_READDIR_BUFFER_TOO_SMALL
michael@0 394 /* Sigh. The static buffer in Unixware's readdir is too small. */
michael@0 395 struct dirent * readdir(DIR *d)
michael@0 396 {
michael@0 397 static struct dirent *buf = NULL;
michael@0 398 #define MAX_PATH_LEN 1024
michael@0 399
michael@0 400
michael@0 401 if(buf == NULL)
michael@0 402 buf = (struct dirent *) malloc(sizeof(struct dirent) + MAX_PATH_LEN)
michael@0 403 ;
michael@0 404 return(readdir_r(d, buf));
michael@0 405 }
michael@0 406 #endif
michael@0 407
michael@0 408 char *
michael@0 409 ino2name(ino_t ino, char *dir)
michael@0 410 {
michael@0 411 DIR *dp;
michael@0 412 struct dirent *ep;
michael@0 413 char *name;
michael@0 414
michael@0 415 dp = opendir("..");
michael@0 416 if (!dp)
michael@0 417 fail("cannot read parent directory");
michael@0 418 for (;;) {
michael@0 419 if (!(ep = readdir(dp)))
michael@0 420 fail("cannot find current directory");
michael@0 421 if (ep->d_ino == ino)
michael@0 422 break;
michael@0 423 }
michael@0 424 name = xstrdup(ep->d_name);
michael@0 425 closedir(dp);
michael@0 426 return name;
michael@0 427 }
michael@0 428
michael@0 429 void *
michael@0 430 xmalloc(size_t size)
michael@0 431 {
michael@0 432 void *p = malloc(size);
michael@0 433 if (!p)
michael@0 434 fail("cannot allocate %u bytes", size);
michael@0 435 return p;
michael@0 436 }
michael@0 437
michael@0 438 char *
michael@0 439 xstrdup(char *s)
michael@0 440 {
michael@0 441 return strcpy((char*)xmalloc(strlen(s) + 1), s);
michael@0 442 }
michael@0 443
michael@0 444 char *
michael@0 445 xbasename(char *path)
michael@0 446 {
michael@0 447 char *cp;
michael@0 448
michael@0 449 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
michael@0 450 *cp = '\0';
michael@0 451 if (!cp) return path;
michael@0 452 return cp + 1;
michael@0 453 }
michael@0 454
michael@0 455 void
michael@0 456 xchdir(char *dir)
michael@0 457 {
michael@0 458 if (chdir(dir) < 0)
michael@0 459 fail("cannot change directory to %s", dir);
michael@0 460 }
michael@0 461
michael@0 462 int
michael@0 463 relatepaths(char *from, char *to, char *outpath)
michael@0 464 {
michael@0 465 char *cp, *cp2;
michael@0 466 int len;
michael@0 467 char buf[NAME_MAX];
michael@0 468
michael@0 469 assert(*from == '/' && *to == '/');
michael@0 470 for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
michael@0 471 if (*cp == '\0')
michael@0 472 break;
michael@0 473 while (cp[-1] != '/')
michael@0 474 cp--, cp2--;
michael@0 475 if (cp - 1 == to) {
michael@0 476 /* closest common ancestor is /, so use full pathname */
michael@0 477 len = strlen(strcpy(outpath, to));
michael@0 478 if (outpath[len] != '/') {
michael@0 479 outpath[len++] = '/';
michael@0 480 outpath[len] = '\0';
michael@0 481 }
michael@0 482 } else {
michael@0 483 len = 0;
michael@0 484 while ((cp2 = getcomponent(cp2, buf)) != 0) {
michael@0 485 strcpy(outpath + len, "../");
michael@0 486 len += 3;
michael@0 487 }
michael@0 488 while ((cp = getcomponent(cp, buf)) != 0) {
michael@0 489 sprintf(outpath + len, "%s/", buf);
michael@0 490 len += strlen(outpath + len);
michael@0 491 }
michael@0 492 }
michael@0 493 return len;
michael@0 494 }
michael@0 495
michael@0 496 void
michael@0 497 reversepath(char *inpath, char *name, int len, char *outpath)
michael@0 498 {
michael@0 499 char *cp, *cp2;
michael@0 500 char buf[NAME_MAX];
michael@0 501 struct stat sb;
michael@0 502
michael@0 503 cp = strcpy(outpath + PATH_MAX - (len + 1), name);
michael@0 504 cp2 = inpath;
michael@0 505 while ((cp2 = getcomponent(cp2, buf)) != 0) {
michael@0 506 if (strcmp(buf, ".") == 0)
michael@0 507 continue;
michael@0 508 if (strcmp(buf, "..") == 0) {
michael@0 509 if (stat(".", &sb) < 0)
michael@0 510 fail("cannot stat current directory");
michael@0 511 name = ino2name(sb.st_ino, "..");
michael@0 512 len = strlen(name);
michael@0 513 cp -= len + 1;
michael@0 514 strcpy(cp, name);
michael@0 515 cp[len] = '/';
michael@0 516 free(name);
michael@0 517 xchdir("..");
michael@0 518 } else {
michael@0 519 cp -= 3;
michael@0 520 strncpy(cp, "../", 3);
michael@0 521 xchdir(buf);
michael@0 522 }
michael@0 523 }
michael@0 524 strcpy(outpath, cp);
michael@0 525 }

mercurial