extract.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** extract.c - libtar code to extract a file from a tar archive
  7. **
  8. ** Mark D. Roth <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #include <libtarint/internal.h>
  13. #include <stdio.h>
  14. #include <libtar/compat.h>
  15. #include <sys/types.h>
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #ifdef _MSC_VER
  19. #include <sys/utime.h>
  20. #include <io.h>
  21. #include <direct.h>
  22. #else
  23. #include <utime.h>
  24. #include <sys/param.h>
  25. #endif
  26. #ifdef STDC_HEADERS
  27. # include <stdlib.h>
  28. # include <string.h>
  29. #endif
  30. #ifdef HAVE_UNISTD_H
  31. # include <unistd.h>
  32. #endif
  33. struct linkname
  34. {
  35. char ln_save[MAXPATHLEN];
  36. char ln_real[MAXPATHLEN];
  37. };
  38. typedef struct linkname linkname_t;
  39. static int
  40. tar_set_file_perms(TAR *t, char *realname)
  41. {
  42. mode_t mode;
  43. uid_t uid;
  44. gid_t gid;
  45. struct utimbuf ut;
  46. char *filename;
  47. filename = (realname ? realname : th_get_pathname(t));
  48. mode = th_get_mode(t);
  49. uid = th_get_uid(t);
  50. gid = th_get_gid(t);
  51. ut.modtime = ut.actime = th_get_mtime(t);
  52. /* change owner/group */
  53. #ifndef WIN32
  54. if (geteuid() == 0)
  55. #ifdef HAVE_LCHOWN
  56. if (lchown(filename, uid, gid) == -1)
  57. {
  58. # ifdef DEBUG
  59. fprintf(stderr, "lchown(\"%s\", %d, %d): %s\n",
  60. filename, uid, gid, strerror(errno));
  61. # endif
  62. #else /* ! HAVE_LCHOWN */
  63. if (!TH_ISSYM(t) && chown(filename, uid, gid) == -1)
  64. {
  65. # ifdef DEBUG
  66. fprintf(stderr, "chown(\"%s\", %d, %d): %s\n",
  67. filename, uid, gid, strerror(errno));
  68. # endif
  69. #endif /* HAVE_LCHOWN */
  70. return -1;
  71. }
  72. /* change access/modification time */
  73. if (!TH_ISSYM(t) && utime(filename, &ut) == -1)
  74. {
  75. #ifdef DEBUG
  76. perror("utime()");
  77. #endif
  78. return -1;
  79. }
  80. /* change permissions */
  81. if (!TH_ISSYM(t) && chmod(filename, mode) == -1)
  82. {
  83. #ifdef DEBUG
  84. perror("chmod()");
  85. #endif
  86. return -1;
  87. }
  88. #endif /* WIN32 */
  89. return 0;
  90. }
  91. /* switchboard */
  92. int
  93. tar_extract_file(TAR *t, char *realname)
  94. {
  95. int i;
  96. linkname_t *lnp;
  97. if (t->options & TAR_NOOVERWRITE)
  98. {
  99. struct stat s;
  100. #ifdef WIN32
  101. if (stat(realname, &s) == 0 || errno != ENOENT)
  102. #else
  103. if (lstat(realname, &s) == 0 || errno != ENOENT)
  104. #endif
  105. {
  106. errno = EEXIST;
  107. return -1;
  108. }
  109. }
  110. if (TH_ISDIR(t))
  111. {
  112. i = tar_extract_dir(t, realname);
  113. if (i == 1)
  114. i = 0;
  115. }
  116. #ifndef _WIN32
  117. else if (TH_ISLNK(t))
  118. i = tar_extract_hardlink(t, realname);
  119. else if (TH_ISSYM(t))
  120. i = tar_extract_symlink(t, realname);
  121. else if (TH_ISCHR(t))
  122. i = tar_extract_chardev(t, realname);
  123. else if (TH_ISBLK(t))
  124. i = tar_extract_blockdev(t, realname);
  125. else if (TH_ISFIFO(t))
  126. i = tar_extract_fifo(t, realname);
  127. #endif
  128. else /* if (TH_ISREG(t)) */
  129. i = tar_extract_regfile(t, realname);
  130. if (i != 0)
  131. return i;
  132. i = tar_set_file_perms(t, realname);
  133. if (i != 0)
  134. return i;
  135. lnp = (linkname_t *)calloc(1, sizeof(linkname_t));
  136. if (lnp == NULL)
  137. return -1;
  138. strlcpy(lnp->ln_save, th_get_pathname(t), sizeof(lnp->ln_save));
  139. strlcpy(lnp->ln_real, realname, sizeof(lnp->ln_real));
  140. #ifdef DEBUG
  141. printf("tar_extract_file(): calling libtar_hash_add(): key=\"%s\", "
  142. "value=\"%s\"\n", th_get_pathname(t), realname);
  143. #endif
  144. if (libtar_hash_add(t->h, lnp) != 0)
  145. return -1;
  146. return 0;
  147. }
  148. /* extract regular file */
  149. int
  150. tar_extract_regfile(TAR *t, char *realname)
  151. {
  152. mode_t mode;
  153. size_t size;
  154. uid_t uid;
  155. gid_t gid;
  156. int fdout;
  157. int i, k;
  158. char buf[T_BLOCKSIZE];
  159. char *filename;
  160. #ifdef DEBUG
  161. printf("==> tar_extract_regfile(t=0x%lx, realname=\"%s\")\n", t,
  162. realname);
  163. #endif
  164. if (!TH_ISREG(t))
  165. {
  166. errno = EINVAL;
  167. return -1;
  168. }
  169. filename = (realname ? realname : th_get_pathname(t));
  170. mode = th_get_mode(t);
  171. size = th_get_size(t);
  172. uid = th_get_uid(t);
  173. gid = th_get_gid(t);
  174. if (mkdirhier(dirname(filename)) == -1)
  175. return -1;
  176. #ifdef DEBUG
  177. printf(" ==> extracting: %s (mode %04o, uid %d, gid %d, %d bytes)\n",
  178. filename, mode, uid, gid, size);
  179. #endif
  180. fdout = open(filename, O_WRONLY | O_CREAT | O_TRUNC
  181. #ifdef O_BINARY
  182. | O_BINARY
  183. #endif
  184. , 0666);
  185. if (fdout == -1)
  186. {
  187. #ifdef DEBUG
  188. perror("open()");
  189. #endif
  190. return -1;
  191. }
  192. #if 0
  193. /* change the owner. (will only work if run as root) */
  194. if (fchown(fdout, uid, gid) == -1 && errno != EPERM)
  195. {
  196. #ifdef DEBUG
  197. perror("fchown()");
  198. #endif
  199. return -1;
  200. }
  201. /* make sure the mode isn't inheritted from a file we're overwriting */
  202. if (fchmod(fdout, mode & 07777) == -1)
  203. {
  204. #ifdef DEBUG
  205. perror("fchmod()");
  206. #endif
  207. return -1;
  208. }
  209. #endif
  210. /* extract the file */
  211. for (i = size; i > 0; i -= T_BLOCKSIZE)
  212. {
  213. k = tar_block_read(t, buf);
  214. if (k != T_BLOCKSIZE)
  215. {
  216. if (k != -1)
  217. errno = EINVAL;
  218. return -1;
  219. }
  220. /* write block to output file */
  221. if (write(fdout, buf,
  222. ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : i)) == -1)
  223. return -1;
  224. }
  225. /* close output file */
  226. if (close(fdout) == -1)
  227. return -1;
  228. #ifdef DEBUG
  229. printf("### done extracting %s\n", filename);
  230. #endif
  231. return 0;
  232. }
  233. /* skip regfile */
  234. int
  235. tar_skip_regfile(TAR *t)
  236. {
  237. int i, k;
  238. size_t size;
  239. char buf[T_BLOCKSIZE];
  240. if (!TH_ISREG(t))
  241. {
  242. errno = EINVAL;
  243. return -1;
  244. }
  245. size = th_get_size(t);
  246. for (i = size; i > 0; i -= T_BLOCKSIZE)
  247. {
  248. k = tar_block_read(t, buf);
  249. if (k != T_BLOCKSIZE)
  250. {
  251. if (k != -1)
  252. errno = EINVAL;
  253. return -1;
  254. }
  255. }
  256. return 0;
  257. }
  258. /* hardlink */
  259. int
  260. tar_extract_hardlink(TAR * t, char *realname)
  261. {
  262. char *filename;
  263. char *linktgt = NULL;
  264. linkname_t *lnp;
  265. libtar_hashptr_t hp;
  266. if (!TH_ISLNK(t))
  267. {
  268. errno = EINVAL;
  269. return -1;
  270. }
  271. filename = (realname ? realname : th_get_pathname(t));
  272. if (mkdirhier(dirname(filename)) == -1)
  273. return -1;
  274. libtar_hashptr_reset(&hp);
  275. if (libtar_hash_getkey(t->h, &hp, th_get_linkname(t),
  276. (libtar_matchfunc_t)libtar_str_match) != 0)
  277. {
  278. lnp = (linkname_t *)libtar_hashptr_data(&hp);
  279. linktgt = lnp->ln_real;
  280. }
  281. else
  282. linktgt = th_get_linkname(t);
  283. #ifdef DEBUG
  284. printf(" ==> extracting: %s (link to %s)\n", filename, linktgt);
  285. #endif
  286. #ifndef WIN32
  287. if (link(linktgt, filename) == -1)
  288. #endif
  289. {
  290. #ifdef DEBUG
  291. perror("link()");
  292. #endif
  293. return -1;
  294. }
  295. return 0;
  296. }
  297. /* symlink */
  298. int
  299. tar_extract_symlink(TAR *t, char *realname)
  300. {
  301. char *filename;
  302. #ifndef _WIN32
  303. if (!TH_ISSYM(t))
  304. {
  305. errno = EINVAL;
  306. return -1;
  307. }
  308. #endif
  309. filename = (realname ? realname : th_get_pathname(t));
  310. if (mkdirhier(dirname(filename)) == -1)
  311. return -1;
  312. if (unlink(filename) == -1 && errno != ENOENT)
  313. return -1;
  314. #ifdef DEBUG
  315. printf(" ==> extracting: %s (symlink to %s)\n",
  316. filename, th_get_linkname(t));
  317. #endif
  318. #ifndef WIN32
  319. if (symlink(th_get_linkname(t), filename) == -1)
  320. #endif
  321. {
  322. #ifdef DEBUG
  323. perror("symlink()");
  324. #endif
  325. return -1;
  326. }
  327. return 0;
  328. }
  329. /* character device */
  330. int
  331. tar_extract_chardev(TAR *t, char *realname)
  332. {
  333. mode_t mode;
  334. unsigned long devmaj, devmin;
  335. char *filename;
  336. #ifndef _WIN32
  337. if (!TH_ISCHR(t))
  338. {
  339. errno = EINVAL;
  340. return -1;
  341. }
  342. #endif
  343. filename = (realname ? realname : th_get_pathname(t));
  344. mode = th_get_mode(t);
  345. devmaj = th_get_devmajor(t);
  346. devmin = th_get_devminor(t);
  347. if (mkdirhier(dirname(filename)) == -1)
  348. return -1;
  349. #ifdef DEBUG
  350. printf(" ==> extracting: %s (character device %ld,%ld)\n",
  351. filename, devmaj, devmin);
  352. #endif
  353. #ifndef WIN32
  354. if (mknod(filename, mode | S_IFCHR,
  355. compat_makedev(devmaj, devmin)) == -1)
  356. #endif
  357. {
  358. #ifdef DEBUG
  359. perror("mknod()");
  360. #endif
  361. return -1;
  362. }
  363. return 0;
  364. }
  365. /* block device */
  366. int
  367. tar_extract_blockdev(TAR *t, char *realname)
  368. {
  369. mode_t mode;
  370. unsigned long devmaj, devmin;
  371. char *filename;
  372. if (!TH_ISBLK(t))
  373. {
  374. errno = EINVAL;
  375. return -1;
  376. }
  377. filename = (realname ? realname : th_get_pathname(t));
  378. mode = th_get_mode(t);
  379. devmaj = th_get_devmajor(t);
  380. devmin = th_get_devminor(t);
  381. if (mkdirhier(dirname(filename)) == -1)
  382. return -1;
  383. #ifdef DEBUG
  384. printf(" ==> extracting: %s (block device %ld,%ld)\n",
  385. filename, devmaj, devmin);
  386. #endif
  387. #ifndef WIN32
  388. if (mknod(filename, mode | S_IFBLK,
  389. compat_makedev(devmaj, devmin)) == -1)
  390. #endif
  391. {
  392. #ifdef DEBUG
  393. perror("mknod()");
  394. #endif
  395. return -1;
  396. }
  397. return 0;
  398. }
  399. /* directory */
  400. int
  401. tar_extract_dir(TAR *t, char *realname)
  402. {
  403. mode_t mode;
  404. char *filename;
  405. if (!TH_ISDIR(t))
  406. {
  407. errno = EINVAL;
  408. return -1;
  409. }
  410. filename = (realname ? realname : th_get_pathname(t));
  411. mode = th_get_mode(t);
  412. if (mkdirhier(dirname(filename)) == -1)
  413. return -1;
  414. #ifdef DEBUG
  415. printf(" ==> extracting: %s (mode %04o, directory)\n", filename,
  416. mode);
  417. #endif
  418. #ifdef WIN32
  419. if (mkdir(filename) == -1)
  420. #else
  421. if (mkdir(filename, mode) == -1)
  422. #endif
  423. {
  424. if (errno == EEXIST)
  425. {
  426. if (chmod(filename, mode) == -1)
  427. {
  428. #ifdef DEBUG
  429. perror("chmod()");
  430. #endif
  431. return -1;
  432. }
  433. else
  434. {
  435. #ifdef DEBUG
  436. puts(" *** using existing directory");
  437. #endif
  438. return 1;
  439. }
  440. }
  441. else
  442. {
  443. #ifdef DEBUG
  444. perror("mkdir()");
  445. #endif
  446. return -1;
  447. }
  448. }
  449. return 0;
  450. }
  451. /* FIFO */
  452. int
  453. tar_extract_fifo(TAR *t, char *realname)
  454. {
  455. mode_t mode;
  456. char *filename;
  457. if (!TH_ISFIFO(t))
  458. {
  459. errno = EINVAL;
  460. return -1;
  461. }
  462. filename = (realname ? realname : th_get_pathname(t));
  463. mode = th_get_mode(t);
  464. if (mkdirhier(dirname(filename)) == -1)
  465. return -1;
  466. #ifdef DEBUG
  467. printf(" ==> extracting: %s (fifo)\n", filename);
  468. #endif
  469. #ifndef WIN32
  470. if (mkfifo(filename, mode) == -1)
  471. #endif
  472. {
  473. #ifdef DEBUG
  474. perror("mkfifo()");
  475. #endif
  476. return -1;
  477. }
  478. return 0;
  479. }