extract.c 9.8 KB

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