nsinstall.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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"
  26. #ifdef HAVE_GETOPT_H
  27. #include <getopt.h>
  28. #endif
  29. #ifdef SUNOS4
  30. #include "sunos4.h"
  31. #endif
  32. #ifdef NEXTSTEP
  33. #include <bsd/libc.h>
  34. #endif
  35. #ifdef __QNX__
  36. #include <unix.h>
  37. #endif
  38. #ifdef NEED_S_ISLNK
  39. #if !defined(S_ISLNK) && defined(S_IFLNK)
  40. #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
  41. #endif
  42. #endif
  43. #ifndef _DIRECTORY_SEPARATOR
  44. #define _DIRECTORY_SEPARATOR "/"
  45. #endif /* _DIRECTORY_SEPARATOR */
  46. #ifdef NEED_FCHMOD_PROTO
  47. extern int fchmod(int fildes, mode_t mode);
  48. #endif
  49. static void
  50. usage(void)
  51. {
  52. fprintf(stderr,
  53. "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
  54. " %*s [-DdltR] file [file ...] directory\n",
  55. program, (int) strlen(program), "");
  56. exit(2);
  57. }
  58. static int
  59. mkdirs(char *path, mode_t mode)
  60. {
  61. char *cp;
  62. struct stat sb;
  63. int res;
  64. int l;
  65. /* strip trailing "/." */
  66. l = strlen(path);
  67. if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/')
  68. path[l - 2] = 0;
  69. while (*path == '/' && path[1] == '/')
  70. path++;
  71. for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
  72. if (cp && cp != path) {
  73. *cp = '\0';
  74. if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
  75. mkdirs(path, mode) < 0) {
  76. return -1;
  77. }
  78. *cp = '/';
  79. }
  80. res = mkdir(path, mode);
  81. if ((res != 0) && (errno == EEXIST))
  82. return 0;
  83. else
  84. return res;
  85. }
  86. static uid_t
  87. touid(char *owner)
  88. {
  89. struct passwd *pw;
  90. uid_t uid;
  91. char *cp;
  92. pw = getpwnam(owner);
  93. if (pw)
  94. return pw->pw_uid;
  95. uid = strtol(owner, &cp, 0);
  96. if (uid == 0 && cp == owner)
  97. fail("cannot find uid for %s", owner);
  98. return uid;
  99. }
  100. static gid_t
  101. togid(char *group)
  102. {
  103. struct group *gr;
  104. gid_t gid;
  105. char *cp;
  106. gr = getgrnam(group);
  107. if (gr)
  108. return gr->gr_gid;
  109. gid = strtol(group, &cp, 0);
  110. if (gid == 0 && cp == group)
  111. fail("cannot find gid for %s", group);
  112. return gid;
  113. }
  114. static void
  115. copyfile( char *name, char *toname, mode_t mode, char *group, char *owner,
  116. int dotimes, uid_t uid, gid_t gid )
  117. {
  118. int fromfd, tofd = -1, cc, wc, exists;
  119. char buf[BUFSIZ], *bp;
  120. struct stat sb, tosb;
  121. struct utimbuf utb;
  122. exists = (lstat(toname, &tosb) == 0);
  123. fromfd = open(name, O_RDONLY);
  124. if (fromfd < 0 || fstat(fromfd, &sb) < 0)
  125. fail("cannot access %s", name);
  126. if (exists) {
  127. if (S_ISREG(tosb.st_mode)) {
  128. /* See if we can open it. This is more reliable than 'access'. */
  129. tofd = open(toname, O_CREAT | O_WRONLY, 0666);
  130. }
  131. if (tofd < 0) {
  132. (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
  133. }
  134. }
  135. if (tofd < 0) {
  136. tofd = open(toname, O_CREAT | O_WRONLY, 0666);
  137. if (tofd < 0)
  138. fail("cannot create %s", toname);
  139. }
  140. bp = buf;
  141. while ((cc = read(fromfd, bp, sizeof buf)) > 0)
  142. {
  143. while ((wc = write(tofd, bp, (unsigned int)cc)) > 0)
  144. {
  145. if ((cc -= wc) == 0)
  146. break;
  147. bp += wc;
  148. }
  149. if (wc < 0)
  150. fail("cannot write to %s", toname);
  151. }
  152. if (cc < 0)
  153. fail("cannot read from %s", name);
  154. if (ftruncate(tofd, sb.st_size) < 0)
  155. fail("cannot truncate %s", toname);
  156. #if !defined(VMS)
  157. if (dotimes)
  158. {
  159. utb.actime = sb.st_atime;
  160. utb.modtime = sb.st_mtime;
  161. if (utime(toname, &utb) < 0)
  162. fail("cannot set times of %s", toname);
  163. }
  164. #ifdef HAVE_FCHMOD
  165. if (fchmod(tofd, mode) < 0)
  166. #else
  167. if (chmod(toname, mode) < 0)
  168. #endif
  169. fail("cannot change mode of %s", toname);
  170. #endif
  171. if ((owner || group) && fchown(tofd, uid, gid) < 0)
  172. fail("cannot change owner of %s", toname);
  173. /* Must check for delayed (NFS) write errors on close. */
  174. if (close(tofd) < 0)
  175. fail("cannot write to %s", toname);
  176. close(fromfd);
  177. #if defined(VMS)
  178. if (chmod(toname, (mode & (S_IREAD | S_IWRITE))) < 0)
  179. fail("cannot change mode of %s", toname);
  180. if (dotimes)
  181. {
  182. utb.actime = sb.st_atime;
  183. utb.modtime = sb.st_mtime;
  184. if (utime(toname, &utb) < 0)
  185. fail("cannot set times of %s", toname);
  186. }
  187. #endif
  188. }
  189. static void
  190. copydir( char *from, char *to, mode_t mode, char *group, char *owner,
  191. int dotimes, uid_t uid, gid_t gid)
  192. {
  193. DIR *dir;
  194. struct dirent *ep;
  195. struct stat sb;
  196. char *base, *destdir, *direntry, *destentry;
  197. base = xbasename(from);
  198. /* create destination directory */
  199. destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1));
  200. sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR, base);
  201. if (mkdirs(destdir, mode) != 0) {
  202. fail("cannot make directory %s\n", destdir);
  203. free(destdir);
  204. return;
  205. }
  206. if (!(dir = opendir(from))) {
  207. fail("cannot open directory %s\n", from);
  208. free(destdir);
  209. return;
  210. }
  211. direntry = xmalloc((unsigned int)PATH_MAX);
  212. destentry = xmalloc((unsigned int)PATH_MAX);
  213. while ((ep = readdir(dir)))
  214. {
  215. if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
  216. continue;
  217. sprintf(direntry, "%s/%s", from, ep->d_name);
  218. sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR, ep->d_name);
  219. if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode))
  220. copydir( direntry, destdir, mode, group, owner, dotimes, uid, gid );
  221. else
  222. copyfile( direntry, destentry, mode, group, owner, dotimes, uid, gid );
  223. }
  224. free(destdir);
  225. free(direntry);
  226. free(destentry);
  227. closedir(dir);
  228. }
  229. int
  230. main(int argc, char **argv)
  231. {
  232. int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists;
  233. mode_t mode = 0755;
  234. char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, buf[BUFSIZ];
  235. uid_t uid;
  236. gid_t gid;
  237. struct stat sb, tosb, fromsb;
  238. program = argv[0];
  239. cwd = linkname = linkprefix = owner = group = 0;
  240. onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
  241. while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
  242. switch (opt) {
  243. case 'C':
  244. cwd = optarg;
  245. break;
  246. case 'D':
  247. onlydir = 1;
  248. break;
  249. case 'd':
  250. dodir = 1;
  251. break;
  252. case 'L':
  253. linkprefix = optarg;
  254. lplen = strlen(linkprefix);
  255. dolink = 1;
  256. break;
  257. case 'R':
  258. dolink = dorelsymlink = 1;
  259. break;
  260. case 'm':
  261. mode = strtoul(optarg, &cp, 8);
  262. if (mode == 0 && cp == optarg)
  263. usage();
  264. break;
  265. case 'o':
  266. owner = optarg;
  267. break;
  268. case 'g':
  269. group = optarg;
  270. break;
  271. case 't':
  272. dotimes = 1;
  273. break;
  274. default:
  275. usage();
  276. }
  277. }
  278. argc -= optind;
  279. argv += optind;
  280. if (argc < 2 - onlydir)
  281. usage();
  282. todir = argv[argc-1];
  283. if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
  284. mkdirs(todir, 0777) < 0) {
  285. fail("cannot make directory %s", todir);
  286. }
  287. if (onlydir)
  288. return 0;
  289. if (!cwd) {
  290. #ifndef NEEDS_GETCWD
  291. #ifndef GETCWD_CANT_MALLOC
  292. cwd = getcwd(0, PATH_MAX);
  293. #else
  294. cwd = malloc(PATH_MAX + 1);
  295. cwd = getcwd(cwd, PATH_MAX);
  296. #endif
  297. #else
  298. cwd = malloc(PATH_MAX + 1);
  299. cwd = getwd(cwd);
  300. #endif
  301. }
  302. xchdir(todir);
  303. #ifndef NEEDS_GETCWD
  304. #ifndef GETCWD_CANT_MALLOC
  305. todir = getcwd(0, PATH_MAX);
  306. #else
  307. todir = malloc(PATH_MAX + 1);
  308. todir = getcwd(todir, PATH_MAX);
  309. #endif
  310. #else
  311. todir = malloc(PATH_MAX + 1);
  312. todir = getwd(todir);
  313. #endif
  314. tdlen = strlen(todir);
  315. xchdir(cwd);
  316. tdlen = strlen(todir);
  317. uid = owner ? touid(owner) : (uid_t)(-1);
  318. gid = group ? togid(group) : (gid_t)(-1);
  319. while (--argc > 0) {
  320. name = *argv++;
  321. len = strlen(name);
  322. base = xbasename(name);
  323. bnlen = strlen(base);
  324. toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1));
  325. sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR, base);
  326. exists = (lstat(toname, &tosb) == 0);
  327. if (dodir) {
  328. /* -d means create a directory, always */
  329. if (exists && !S_ISDIR(tosb.st_mode)) {
  330. (void) unlink(toname);
  331. exists = 0;
  332. }
  333. if (!exists && mkdir(toname, mode) < 0)
  334. fail("cannot make directory %s", toname);
  335. if ((owner || group) && chown(toname, uid, gid) < 0)
  336. fail("cannot change owner of %s", toname);
  337. } else if (dolink) {
  338. if (access(name, R_OK) != 0) {
  339. fail("cannot access %s", name);
  340. }
  341. if (*name == '/') {
  342. /* source is absolute pathname, link to it directly */
  343. linkname = 0;
  344. } else {
  345. if (linkprefix) {
  346. /* -L prefixes names with a $cwd arg. */
  347. len += lplen + 1;
  348. linkname = xmalloc((unsigned int)(len + 1));
  349. sprintf(linkname, "%s/%s", linkprefix, name);
  350. } else if (dorelsymlink) {
  351. /* Symlink the relative path from todir to source name. */
  352. linkname = xmalloc(PATH_MAX);
  353. if (*todir == '/') {
  354. /* todir is absolute: skip over common prefix. */
  355. lplen = relatepaths(todir, cwd, linkname);
  356. strcpy(linkname + lplen, name);
  357. } else {
  358. /* todir is named by a relative path: reverse it. */
  359. reversepath(todir, name, len, linkname);
  360. xchdir(cwd);
  361. }
  362. len = strlen(linkname);
  363. }
  364. name = linkname;
  365. }
  366. /* Check for a pre-existing symlink with identical content. */
  367. if (exists && (!S_ISLNK(tosb.st_mode) ||
  368. readlink(toname, buf, sizeof buf) != len ||
  369. strncmp(buf, name, (unsigned int)len) != 0 ||
  370. ((stat(name, &fromsb) == 0) &&
  371. (fromsb.st_mtime > tosb.st_mtime)
  372. ))) {
  373. (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
  374. exists = 0;
  375. }
  376. if (!exists && symlink(name, toname) < 0)
  377. fail("cannot make symbolic link %s", toname);
  378. #ifdef HAVE_LCHOWN
  379. if ((owner || group) && lchown(toname, uid, gid) < 0)
  380. fail("cannot change owner of %s", toname);
  381. #endif
  382. if (linkname) {
  383. free(linkname);
  384. linkname = 0;
  385. }
  386. } else {
  387. /* Copy from name to toname, which might be the same file. */
  388. if( stat(name, &sb) == 0 && S_IFDIR & sb.st_mode )
  389. {
  390. /* then is directory: must explicitly create destination dir */
  391. /* and manually copy files over */
  392. copydir( name, todir, mode, group, owner, dotimes, uid, gid );
  393. }
  394. else
  395. {
  396. copyfile(name, toname, mode, group, owner, dotimes, uid, gid);
  397. }
  398. }
  399. free(toname);
  400. }
  401. free(cwd);
  402. free(todir);
  403. return 0;
  404. }