michael@0: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: /* michael@0: ** Netscape portable install command. michael@0: ** michael@0: ** Brendan Eich, 7/20/95 michael@0: */ michael@0: #include /* OSF/1 requires this before grp.h, so put it first */ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "pathsub.h" michael@0: michael@0: #ifdef HAVE_GETOPT_H michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef SUNOS4 michael@0: #include "sunos4.h" michael@0: #endif michael@0: michael@0: #ifdef NEXTSTEP michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef __QNX__ michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef NEED_S_ISLNK michael@0: #if !defined(S_ISLNK) && defined(S_IFLNK) michael@0: #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK) michael@0: #endif michael@0: #endif michael@0: michael@0: #ifndef _DIRECTORY_SEPARATOR michael@0: #define _DIRECTORY_SEPARATOR "/" michael@0: #endif /* _DIRECTORY_SEPARATOR */ michael@0: michael@0: #ifdef NEED_FCHMOD_PROTO michael@0: extern int fchmod(int fildes, mode_t mode); michael@0: #endif michael@0: michael@0: static void michael@0: usage(void) michael@0: { michael@0: fprintf(stderr, michael@0: "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n" michael@0: " %*s [-DdltR] file [file ...] directory\n", michael@0: program, (int) strlen(program), ""); michael@0: exit(2); michael@0: } michael@0: michael@0: static int michael@0: mkdirs(char *path, mode_t mode) michael@0: { michael@0: char *cp; michael@0: struct stat sb; michael@0: int res; michael@0: int l; michael@0: michael@0: /* strip trailing "/." */ michael@0: l = strlen(path); michael@0: if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/') michael@0: path[l - 2] = 0; michael@0: michael@0: while (*path == '/' && path[1] == '/') michael@0: path++; michael@0: for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--); michael@0: if (cp && cp != path) { michael@0: *cp = '\0'; michael@0: if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) && michael@0: mkdirs(path, mode) < 0) { michael@0: return -1; michael@0: } michael@0: *cp = '/'; michael@0: } michael@0: michael@0: res = mkdir(path, mode); michael@0: if ((res != 0) && (errno == EEXIST)) michael@0: return 0; michael@0: else michael@0: return res; michael@0: } michael@0: michael@0: static uid_t michael@0: touid(char *owner) michael@0: { michael@0: struct passwd *pw; michael@0: uid_t uid; michael@0: char *cp; michael@0: michael@0: pw = getpwnam(owner); michael@0: if (pw) michael@0: return pw->pw_uid; michael@0: uid = strtol(owner, &cp, 0); michael@0: if (uid == 0 && cp == owner) michael@0: fail("cannot find uid for %s", owner); michael@0: return uid; michael@0: } michael@0: michael@0: static gid_t michael@0: togid(char *group) michael@0: { michael@0: struct group *gr; michael@0: gid_t gid; michael@0: char *cp; michael@0: michael@0: gr = getgrnam(group); michael@0: if (gr) michael@0: return gr->gr_gid; michael@0: gid = strtol(group, &cp, 0); michael@0: if (gid == 0 && cp == group) michael@0: fail("cannot find gid for %s", group); michael@0: return gid; michael@0: } michael@0: michael@0: static void michael@0: copyfile( char *name, char *toname, mode_t mode, char *group, char *owner, michael@0: int dotimes, uid_t uid, gid_t gid ) michael@0: { michael@0: int fromfd, tofd = -1, cc, wc, exists; michael@0: char buf[BUFSIZ], *bp; michael@0: struct stat sb, tosb; michael@0: struct utimbuf utb; michael@0: michael@0: exists = (lstat(toname, &tosb) == 0); michael@0: michael@0: fromfd = open(name, O_RDONLY); michael@0: if (fromfd < 0 || fstat(fromfd, &sb) < 0) michael@0: fail("cannot access %s", name); michael@0: if (exists) { michael@0: if (S_ISREG(tosb.st_mode)) { michael@0: /* See if we can open it. This is more reliable than 'access'. */ michael@0: tofd = open(toname, O_CREAT | O_WRONLY, 0666); michael@0: } michael@0: if (tofd < 0) { michael@0: (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname); michael@0: } michael@0: } michael@0: if (tofd < 0) { michael@0: tofd = open(toname, O_CREAT | O_WRONLY, 0666); michael@0: if (tofd < 0) michael@0: fail("cannot create %s", toname); michael@0: } michael@0: michael@0: bp = buf; michael@0: while ((cc = read(fromfd, bp, sizeof buf)) > 0) michael@0: { michael@0: while ((wc = write(tofd, bp, (unsigned int)cc)) > 0) michael@0: { michael@0: if ((cc -= wc) == 0) michael@0: break; michael@0: bp += wc; michael@0: } michael@0: if (wc < 0) michael@0: fail("cannot write to %s", toname); michael@0: } michael@0: if (cc < 0) michael@0: fail("cannot read from %s", name); michael@0: michael@0: if (ftruncate(tofd, sb.st_size) < 0) michael@0: fail("cannot truncate %s", toname); michael@0: #if !defined(VMS) michael@0: if (dotimes) michael@0: { michael@0: utb.actime = sb.st_atime; michael@0: utb.modtime = sb.st_mtime; michael@0: if (utime(toname, &utb) < 0) michael@0: fail("cannot set times of %s", toname); michael@0: } michael@0: #ifdef HAVE_FCHMOD michael@0: if (fchmod(tofd, mode) < 0) michael@0: #else michael@0: if (chmod(toname, mode) < 0) michael@0: #endif michael@0: fail("cannot change mode of %s", toname); michael@0: #endif michael@0: if ((owner || group) && fchown(tofd, uid, gid) < 0) michael@0: fail("cannot change owner of %s", toname); michael@0: michael@0: /* Must check for delayed (NFS) write errors on close. */ michael@0: if (close(tofd) < 0) michael@0: fail("cannot write to %s", toname); michael@0: close(fromfd); michael@0: #if defined(VMS) michael@0: if (chmod(toname, (mode & (S_IREAD | S_IWRITE))) < 0) michael@0: fail("cannot change mode of %s", toname); michael@0: if (dotimes) michael@0: { michael@0: utb.actime = sb.st_atime; michael@0: utb.modtime = sb.st_mtime; michael@0: if (utime(toname, &utb) < 0) michael@0: fail("cannot set times of %s", toname); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: static void michael@0: copydir( char *from, char *to, mode_t mode, char *group, char *owner, michael@0: int dotimes, uid_t uid, gid_t gid) michael@0: { michael@0: int i; michael@0: DIR *dir; michael@0: struct dirent *ep; michael@0: struct stat sb; michael@0: char *base, *destdir, *direntry, *destentry; michael@0: michael@0: base = xbasename(from); michael@0: michael@0: /* create destination directory */ michael@0: destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1)); michael@0: sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR, base); michael@0: if (mkdirs(destdir, mode) != 0) { michael@0: fail("cannot make directory %s\n", destdir); michael@0: free(destdir); michael@0: return; michael@0: } michael@0: michael@0: if (!(dir = opendir(from))) { michael@0: fail("cannot open directory %s\n", from); michael@0: free(destdir); michael@0: return; michael@0: } michael@0: michael@0: direntry = xmalloc((unsigned int)PATH_MAX); michael@0: destentry = xmalloc((unsigned int)PATH_MAX); michael@0: michael@0: while ((ep = readdir(dir))) michael@0: { michael@0: if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) michael@0: continue; michael@0: michael@0: sprintf(direntry, "%s/%s", from, ep->d_name); michael@0: sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR, ep->d_name); michael@0: michael@0: if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode)) michael@0: copydir( direntry, destdir, mode, group, owner, dotimes, uid, gid ); michael@0: else michael@0: copyfile( direntry, destentry, mode, group, owner, dotimes, uid, gid ); michael@0: } michael@0: michael@0: free(destdir); michael@0: free(direntry); michael@0: free(destentry); michael@0: closedir(dir); michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc; michael@0: mode_t mode = 0755; michael@0: char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ]; michael@0: uid_t uid; michael@0: gid_t gid; michael@0: struct stat sb, tosb, fromsb; michael@0: struct utimbuf utb; michael@0: michael@0: program = argv[0]; michael@0: cwd = linkname = linkprefix = owner = group = 0; michael@0: onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0; michael@0: michael@0: while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) { michael@0: switch (opt) { michael@0: case 'C': michael@0: cwd = optarg; michael@0: break; michael@0: case 'D': michael@0: onlydir = 1; michael@0: break; michael@0: case 'd': michael@0: dodir = 1; michael@0: break; michael@0: case 'L': michael@0: linkprefix = optarg; michael@0: lplen = strlen(linkprefix); michael@0: dolink = 1; michael@0: break; michael@0: case 'R': michael@0: dolink = dorelsymlink = 1; michael@0: break; michael@0: case 'm': michael@0: mode = strtoul(optarg, &cp, 8); michael@0: if (mode == 0 && cp == optarg) michael@0: usage(); michael@0: break; michael@0: case 'o': michael@0: owner = optarg; michael@0: break; michael@0: case 'g': michael@0: group = optarg; michael@0: break; michael@0: case 't': michael@0: dotimes = 1; michael@0: break; michael@0: default: michael@0: usage(); michael@0: } michael@0: } michael@0: michael@0: argc -= optind; michael@0: argv += optind; michael@0: if (argc < 2 - onlydir) michael@0: usage(); michael@0: michael@0: todir = argv[argc-1]; michael@0: if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) && michael@0: mkdirs(todir, 0777) < 0) { michael@0: fail("cannot make directory %s", todir); michael@0: } michael@0: if (onlydir) michael@0: return 0; michael@0: michael@0: if (!cwd) { michael@0: #ifndef NEEDS_GETCWD michael@0: #ifndef GETCWD_CANT_MALLOC michael@0: cwd = getcwd(0, PATH_MAX); michael@0: #else michael@0: cwd = malloc(PATH_MAX + 1); michael@0: cwd = getcwd(cwd, PATH_MAX); michael@0: #endif michael@0: #else michael@0: cwd = malloc(PATH_MAX + 1); michael@0: cwd = getwd(cwd); michael@0: #endif michael@0: } michael@0: michael@0: xchdir(todir); michael@0: #ifndef NEEDS_GETCWD michael@0: #ifndef GETCWD_CANT_MALLOC michael@0: todir = getcwd(0, PATH_MAX); michael@0: #else michael@0: todir = malloc(PATH_MAX + 1); michael@0: todir = getcwd(todir, PATH_MAX); michael@0: #endif michael@0: #else michael@0: todir = malloc(PATH_MAX + 1); michael@0: todir = getwd(todir); michael@0: #endif michael@0: tdlen = strlen(todir); michael@0: xchdir(cwd); michael@0: tdlen = strlen(todir); michael@0: michael@0: uid = owner ? touid(owner) : (uid_t)(-1); michael@0: gid = group ? togid(group) : (gid_t)(-1); michael@0: michael@0: while (--argc > 0) { michael@0: name = *argv++; michael@0: len = strlen(name); michael@0: base = xbasename(name); michael@0: bnlen = strlen(base); michael@0: toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1)); michael@0: sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR, base); michael@0: exists = (lstat(toname, &tosb) == 0); michael@0: michael@0: if (dodir) { michael@0: /* -d means create a directory, always */ michael@0: if (exists && !S_ISDIR(tosb.st_mode)) { michael@0: (void) unlink(toname); michael@0: exists = 0; michael@0: } michael@0: if (!exists && mkdir(toname, mode) < 0) michael@0: fail("cannot make directory %s", toname); michael@0: if ((owner || group) && chown(toname, uid, gid) < 0) michael@0: fail("cannot change owner of %s", toname); michael@0: } else if (dolink) { michael@0: if (access(name, R_OK) != 0) { michael@0: fail("cannot access %s", name); michael@0: } michael@0: if (*name == '/') { michael@0: /* source is absolute pathname, link to it directly */ michael@0: linkname = 0; michael@0: } else { michael@0: if (linkprefix) { michael@0: /* -L prefixes names with a $cwd arg. */ michael@0: len += lplen + 1; michael@0: linkname = xmalloc((unsigned int)(len + 1)); michael@0: sprintf(linkname, "%s/%s", linkprefix, name); michael@0: } else if (dorelsymlink) { michael@0: /* Symlink the relative path from todir to source name. */ michael@0: linkname = xmalloc(PATH_MAX); michael@0: michael@0: if (*todir == '/') { michael@0: /* todir is absolute: skip over common prefix. */ michael@0: lplen = relatepaths(todir, cwd, linkname); michael@0: strcpy(linkname + lplen, name); michael@0: } else { michael@0: /* todir is named by a relative path: reverse it. */ michael@0: reversepath(todir, name, len, linkname); michael@0: xchdir(cwd); michael@0: } michael@0: michael@0: len = strlen(linkname); michael@0: } michael@0: name = linkname; michael@0: } michael@0: michael@0: /* Check for a pre-existing symlink with identical content. */ michael@0: if ((exists && (!S_ISLNK(tosb.st_mode) || michael@0: readlink(toname, buf, sizeof buf) != len || michael@0: strncmp(buf, name, (unsigned int)len) != 0)) || michael@0: ((stat(name, &fromsb) == 0) && michael@0: (fromsb.st_mtime > tosb.st_mtime))) { michael@0: (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname); michael@0: exists = 0; michael@0: } michael@0: if (!exists && symlink(name, toname) < 0) michael@0: fail("cannot make symbolic link %s", toname); michael@0: #ifdef HAVE_LCHOWN michael@0: if ((owner || group) && lchown(toname, uid, gid) < 0) michael@0: fail("cannot change owner of %s", toname); michael@0: #endif michael@0: michael@0: if (linkname) { michael@0: free(linkname); michael@0: linkname = 0; michael@0: } michael@0: } else { michael@0: /* Copy from name to toname, which might be the same file. */ michael@0: if( stat(name, &sb) == 0 && S_IFDIR & sb.st_mode ) michael@0: { michael@0: /* then is directory: must explicitly create destination dir */ michael@0: /* and manually copy files over */ michael@0: copydir( name, todir, mode, group, owner, dotimes, uid, gid ); michael@0: } michael@0: else michael@0: { michael@0: copyfile(name, toname, mode, group, owner, dotimes, uid, gid); michael@0: } michael@0: } michael@0: michael@0: free(toname); michael@0: } michael@0: michael@0: free(cwd); michael@0: free(todir); michael@0: return 0; michael@0: }