extract.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* Extract files from a tar archive.
  2. Copyright (C) 1988 Free Software Foundation
  3. This file is part of GNU Tar.
  4. GNU Tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Tar; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. * Extract files from a tar archive.
  17. *
  18. * Written 19 Nov 1985 by John Gilmore, ihnp4!hoptoad!gnu.
  19. */
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #ifndef STDC_HEADERS
  23. extern int errno;
  24. #endif
  25. #include <sys/types.h>
  26. #include <time.h>
  27. time_t time();
  28. #ifdef BSD42
  29. #include <sys/file.h>
  30. #else
  31. #ifndef V7
  32. #include <fcntl.h>
  33. #endif
  34. #endif
  35. #if defined(POSIX)
  36. #include <utime.h>
  37. #endif
  38. #ifdef NO_OPEN3
  39. /* We need the #define's even though we don't use them. */
  40. #include "open3.h"
  41. #endif
  42. #ifdef EMUL_OPEN3
  43. /* Simulated 3-argument open for systems that don't have it */
  44. #include "open3.h"
  45. #endif
  46. #include "tar.h"
  47. #include "port.h"
  48. extern FILE *msg_file;
  49. extern union record *head; /* Points to current tape header */
  50. extern struct stat hstat; /* Stat struct corresponding */
  51. extern int head_standard; /* Tape header is in ANSI format */
  52. extern char *save_name;
  53. extern long save_totsize;
  54. extern long save_sizeleft;
  55. int confirm();
  56. void decode_header();
  57. void extract_mangle();
  58. void extract_sparse_file();
  59. long from_oct();
  60. void gnu_restore();
  61. extern void print_header();
  62. extern void skip_file();
  63. extern void skip_extended_headers();
  64. extern void pr_mkdir();
  65. void saverec();
  66. int make_dirs(); /* Makes required directories */
  67. static time_t now = 0; /* Current time */
  68. static we_are_root = 0; /* True if our effective uid == 0 */
  69. static int notumask = ~0; /* Masks out bits user doesn't want */
  70. /*
  71. * "Scratch" space to store the information about a sparse file before
  72. * writing the info into the header or extended header
  73. */
  74. /*struct sp_array *sparsearray;*/
  75. /* number of elts storable in the sparsearray */
  76. /*int sp_array_size = 10;*/
  77. /*
  78. * Set up to extract files.
  79. */
  80. void
  81. extr_init()
  82. {
  83. int ourmask;
  84. now = time((time_t *)0);
  85. if (geteuid() == 0)
  86. we_are_root = 1;
  87. /*
  88. * We need to know our umask. But if f_use_protection is set,
  89. * leave our kernel umask at 0, and our "notumask" at ~0.
  90. */
  91. ourmask = umask(0); /* Read it */
  92. if (!f_use_protection) {
  93. (void) umask (ourmask); /* Set it back how it was */
  94. notumask = ~ourmask; /* Make umask override permissions */
  95. }
  96. }
  97. /*
  98. * Extract a file from the archive.
  99. */
  100. void
  101. extract_archive()
  102. {
  103. register char *data;
  104. int fd, check, namelen, written, openflag;
  105. long size;
  106. time_t acc_upd_times[2];
  107. register int skipcrud;
  108. register int i;
  109. /* int sparse_ind = 0;*/
  110. union record *exhdr;
  111. /* int end_nulls; */
  112. saverec(&head); /* Make sure it sticks around */
  113. userec(head); /* And go past it in the archive */
  114. decode_header(head, &hstat, &head_standard, 1); /* Snarf fields */
  115. if(f_confirm && !confirm("extract",head->header.name)) {
  116. if (head->header.isextended)
  117. skip_extended_headers();
  118. skip_file((long)hstat.st_size);
  119. saverec((union record **)0);
  120. return;
  121. }
  122. /* Print the record from 'head' and 'hstat' */
  123. if (f_verbose)
  124. print_header();
  125. /*
  126. * Check for fully specified pathnames and other atrocities.
  127. *
  128. * Note, we can't just make a pointer to the new file name,
  129. * since saverec() might move the header and adjust "head".
  130. * We have to start from "head" every time we want to touch
  131. * the header record.
  132. */
  133. skipcrud = 0;
  134. while (!f_absolute_paths && '/' == head->header.name[skipcrud]) {
  135. static int warned_once = 0;
  136. skipcrud++; /* Force relative path */
  137. if (!warned_once++) {
  138. msg("Removing leading / from absolute path names in the archive.");
  139. }
  140. }
  141. switch (head->header.linkflag) {
  142. default:
  143. msg("Unknown file type '%c' for %s, extracted as normal file",
  144. head->header.linkflag, skipcrud+head->header.name);
  145. /* FALL THRU */
  146. /*
  147. * JK - What we want to do if the file is sparse is loop through
  148. * the array of sparse structures in the header and read in
  149. * and translate the character strings representing 1) the offset
  150. * at which to write and 2) how many bytes to write into numbers,
  151. * which we store into the scratch array, "sparsearray". This
  152. * array makes our life easier the same way it did in creating
  153. * the tar file that had to deal with a sparse file.
  154. *
  155. * After we read in the first five (at most) sparse structures,
  156. * we check to see if the file has an extended header, i.e.,
  157. * if more sparse structures are needed to describe the contents
  158. * of the new file. If so, we read in the extended headers
  159. * and continue to store their contents into the sparsearray.
  160. */
  161. case LF_SPARSE:
  162. sp_array_size = 10;
  163. sparsearray = (struct sp_array *) malloc(sp_array_size * sizeof(struct sp_array));
  164. for (i = 0; i < SPARSE_IN_HDR; i++) {
  165. sparsearray[i].offset =
  166. from_oct(1+12, head->header.sp[i].offset);
  167. sparsearray[i].numbytes =
  168. from_oct(1+12, head->header.sp[i].numbytes);
  169. if (!sparsearray[i].numbytes)
  170. break;
  171. }
  172. /* end_nulls = from_oct(1+12, head->header.ending_blanks);*/
  173. if (head->header.isextended) {
  174. /* read in the list of extended headers
  175. and translate them into the sparsearray
  176. as before */
  177. /* static */ int ind = SPARSE_IN_HDR;
  178. for (;;) {
  179. exhdr = findrec();
  180. for (i = 0; i < SPARSE_EXT_HDR; i++) {
  181. if (i+ind > sp_array_size-1) {
  182. /*
  183. * realloc the scratch area
  184. * since we've run out of room --
  185. */
  186. sparsearray = (struct sp_array *)
  187. realloc(sparsearray,
  188. 2 * sp_array_size * (sizeof(struct sp_array)));
  189. sp_array_size *= 2;
  190. }
  191. if (!exhdr->ext_hdr.sp[i].numbytes)
  192. break;
  193. sparsearray[i+ind].offset =
  194. from_oct(1+12, exhdr->ext_hdr.sp[i].offset);
  195. sparsearray[i+ind].numbytes =
  196. from_oct(1+12, exhdr->ext_hdr.sp[i].numbytes);
  197. }
  198. if (!exhdr->ext_hdr.isextended)
  199. break;
  200. else {
  201. ind += SPARSE_EXT_HDR;
  202. userec(exhdr);
  203. }
  204. }
  205. userec(exhdr);
  206. }
  207. /* FALL THRU */
  208. case LF_OLDNORMAL:
  209. case LF_NORMAL:
  210. case LF_CONTIG:
  211. /*
  212. * Appears to be a file.
  213. * See if it's really a directory.
  214. */
  215. namelen = strlen(skipcrud+head->header.name)-1;
  216. if (head->header.name[skipcrud+namelen] == '/')
  217. goto really_dir;
  218. /* FIXME, deal with protection issues */
  219. again_file:
  220. openflag = (f_keep?
  221. O_BINARY|O_NDELAY|O_WRONLY|O_CREAT|O_EXCL:
  222. O_BINARY|O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC)
  223. | ((head->header.linkflag == LF_SPARSE) ? 0 : O_APPEND);
  224. /*
  225. * JK - The last | is a kludge to solve the problem
  226. * the O_APPEND flag causes with files we are
  227. * trying to make sparse: when a file is opened
  228. * with O_APPEND, it writes to the last place
  229. * that something was written, thereby ignoring
  230. * any lseeks that we have done. We add this
  231. * extra condition to make it able to lseek when
  232. * a file is sparse, i.e., we don't open the new
  233. * file with this flag. (Grump -- this bug caused
  234. * me to waste a good deal of time, I might add)
  235. */
  236. if(f_exstdout) {
  237. fd = 1;
  238. goto extract_file;
  239. }
  240. #ifdef O_CTG
  241. /*
  242. * Contiguous files (on the Masscomp) have to specify
  243. * the size in the open call that creates them.
  244. */
  245. if (head->header.linkflag == LF_CONTIG)
  246. fd = open(skipcrud+head->header.name, openflag | O_CTG,
  247. hstat.st_mode, hstat.st_size);
  248. else
  249. #endif
  250. {
  251. #ifdef NO_OPEN3
  252. /*
  253. * On raw V7 we won't let them specify -k (f_keep), but
  254. * we just bull ahead and create the files.
  255. */
  256. fd = creat(skipcrud+head->header.name,
  257. hstat.st_mode);
  258. #else
  259. /*
  260. * With 3-arg open(), we can do this up right.
  261. */
  262. fd = open(skipcrud+head->header.name, openflag,
  263. hstat.st_mode);
  264. #endif
  265. }
  266. if (fd < 0) {
  267. if (make_dirs(skipcrud+head->header.name))
  268. goto again_file;
  269. msg_perror("Could not create file %s",skipcrud+head->header.name);
  270. if (head->header.isextended)
  271. skip_extended_headers();
  272. skip_file((long)hstat.st_size);
  273. goto quit;
  274. }
  275. extract_file:
  276. if (head->header.linkflag == LF_SPARSE) {
  277. char *name;
  278. int namelen;
  279. /*
  280. * Kludge alert. NAME is assigned to header.name
  281. * because during the extraction, the space that
  282. * contains the header will get scribbled on, and
  283. * the name will get munged, so any error messages
  284. * that happen to contain the filename will look
  285. * REAL interesting unless we do this.
  286. */
  287. namelen = strlen(skipcrud+head->header.name);
  288. name = (char *) malloc((sizeof(char)) * namelen);
  289. bcopy(skipcrud+head->header.name, name, namelen);
  290. size = hstat.st_size;
  291. extract_sparse_file(fd, &size, hstat.st_size,
  292. name);
  293. }
  294. else
  295. for (size = hstat.st_size;
  296. size > 0;
  297. size -= written) {
  298. /* long offset,
  299. numbytes;*/
  300. if(f_multivol) {
  301. save_name=head->header.name;
  302. save_totsize=hstat.st_size;
  303. save_sizeleft=size;
  304. }
  305. /*
  306. * Locate data, determine max length
  307. * writeable, write it, record that
  308. * we have used the data, then check
  309. * if the write worked.
  310. */
  311. data = findrec()->charptr;
  312. if (data == NULL) { /* Check it... */
  313. msg("Unexpected EOF on archive file");
  314. break;
  315. }
  316. /*
  317. * JK - If the file is sparse, use the sparsearray
  318. * that we created before to lseek into the new
  319. * file the proper amount, and to see how many
  320. * bytes we want to write at that position.
  321. */
  322. /* if (head->header.linkflag == LF_SPARSE) {
  323. off_t pos;
  324. pos = lseek(fd, (off_t) sparsearray[sparse_ind].offset, 0);
  325. printf("%d at %d\n", (int) pos, sparse_ind);
  326. written = sparsearray[sparse_ind++].numbytes;
  327. } else*/
  328. written = endofrecs()->charptr - data;
  329. if (written > size)
  330. written = size;
  331. errno = 0;
  332. check = write(fd, data, written);
  333. /*
  334. * The following is in violation of strict
  335. * typing, since the arg to userec
  336. * should be a struct rec *. FIXME.
  337. */
  338. userec((union record *)(data + written - 1));
  339. if (check == written) continue;
  340. /*
  341. * Error in writing to file.
  342. * Print it, skip to next file in archive.
  343. */
  344. if(check<0)
  345. msg_perror("couldn't write to file %s",skipcrud+head->header.name);
  346. else
  347. msg("could only write %d of %d bytes to file %s",written,check,skipcrud+head->header.name);
  348. skip_file((long)(size - written));
  349. break; /* Still do the close, mod time, chmod, etc */
  350. }
  351. if(f_multivol)
  352. save_name = 0;
  353. /* If writing to stdout, don't try to do anything
  354. to the filename; it doesn't exist, or we don't
  355. want to touch it anyway */
  356. if(f_exstdout)
  357. break;
  358. /* if (head->header.isextended) {
  359. register union record *exhdr;
  360. register int i;
  361. for (i = 0; i < 21; i++) {
  362. long offset;
  363. if (!exhdr->ext_hdr.sp[i].numbytes)
  364. break;
  365. offset = from_oct(1+12,
  366. exhdr->ext_hdr.sp[i].offset);
  367. written = from_oct(1+12,
  368. exhdr->ext_hdr.sp[i].numbytes);
  369. lseek(fd, offset, 0);
  370. check = write(fd, data, written);
  371. if (check == written) continue;
  372. }
  373. }*/
  374. check = close(fd);
  375. if (check < 0) {
  376. msg_perror("Error while closing %s",skipcrud+head->header.name);
  377. }
  378. set_filestat:
  379. /*
  380. * If we are root, set the owner and group of the extracted
  381. * file. This does what is wanted both on real Unix and on
  382. * System V. If we are running as a user, we extract as that
  383. * user; if running as root, we extract as the original owner.
  384. */
  385. if (we_are_root || f_do_chown) {
  386. if (chown(skipcrud+head->header.name, hstat.st_uid,
  387. hstat.st_gid) < 0) {
  388. msg_perror("cannot chown file %s to uid %d gid %d",skipcrud+head->header.name,hstat.st_uid,hstat.st_gid);
  389. }
  390. }
  391. /*
  392. * Set the modified time of the file.
  393. *
  394. * Note that we set the accessed time to "now", which
  395. * is really "the time we started extracting files".
  396. * unless f_gnudump is used, in which case .st_atime is used
  397. */
  398. if (!f_modified) {
  399. /* fixme if f_gnudump should set ctime too, but how? */
  400. if(f_gnudump)
  401. acc_upd_times[0]=hstat.st_atime;
  402. else acc_upd_times[0] = now; /* Accessed now */
  403. acc_upd_times[1] = hstat.st_mtime; /* Mod'd */
  404. if (utime(skipcrud+head->header.name,
  405. acc_upd_times) < 0) {
  406. msg_perror("couldn't change access and modification times of %s",skipcrud+head->header.name);
  407. }
  408. }
  409. /* We do the utime before the chmod because some versions of
  410. utime are broken and trash the modes of the file. Since
  411. we then change the mode anyway, we don't care. . . */
  412. /*
  413. * If '-k' is not set, open() or creat() could have saved
  414. * the permission bits from a previously created file,
  415. * ignoring the ones we specified.
  416. * Even if -k is set, if the file has abnormal
  417. * mode bits, we must chmod since writing or chown() has
  418. * probably reset them.
  419. *
  420. * If -k is set, we know *we* created this file, so the mode
  421. * bits were set by our open(). If the file is "normal", we
  422. * skip the chmod. This works because we did umask(0) if -p
  423. * is set, so umask will have left the specified mode alone.
  424. */
  425. if ((!f_keep)
  426. || (hstat.st_mode & (S_ISUID|S_ISGID|S_ISVTX))) {
  427. if (chmod(skipcrud+head->header.name,
  428. notumask & (int)hstat.st_mode) < 0) {
  429. msg_perror("cannot change mode of file %s to %ld",skipcrud+head->header.name,notumask & (int)hstat.st_mode);
  430. }
  431. }
  432. quit:
  433. break;
  434. case LF_LINK:
  435. again_link:
  436. {
  437. struct stat st1,st2;
  438. check = link (head->header.linkname,
  439. skipcrud+head->header.name);
  440. if (check == 0)
  441. break;
  442. if (make_dirs(skipcrud+head->header.name))
  443. goto again_link;
  444. if(f_gnudump && errno==EEXIST)
  445. break;
  446. if( stat(head->header.linkname, &st1) == 0
  447. && stat(skipcrud+head->header.name, &st2)==0
  448. && st1.st_dev==st2.st_dev
  449. && st1.st_ino==st2.st_ino)
  450. break;
  451. msg_perror("Could not link %s to %s",
  452. skipcrud+head->header.name,head->header.linkname);
  453. }
  454. break;
  455. #ifdef S_ISLNK
  456. case LF_SYMLINK:
  457. again_symlink:
  458. check = symlink(head->header.linkname,
  459. skipcrud+head->header.name);
  460. /* FIXME, don't worry uid, gid, etc... */
  461. if (check == 0)
  462. break;
  463. if (make_dirs(skipcrud+head->header.name))
  464. goto again_symlink;
  465. msg_perror("Could not create symlink to %s",head->header.linkname);
  466. break;
  467. #endif
  468. #ifdef S_IFCHR
  469. case LF_CHR:
  470. hstat.st_mode |= S_IFCHR;
  471. goto make_node;
  472. #endif
  473. #ifdef S_IFBLK
  474. case LF_BLK:
  475. hstat.st_mode |= S_IFBLK;
  476. #endif
  477. #if defined(S_IFCHR) || defined(S_IFBLK)
  478. make_node:
  479. check = mknod(skipcrud+head->header.name,
  480. (int) hstat.st_mode, (int) hstat.st_rdev);
  481. if (check != 0) {
  482. if (make_dirs(skipcrud+head->header.name))
  483. goto make_node;
  484. msg_perror("Could not make %s",skipcrud+head->header.name);
  485. break;
  486. };
  487. goto set_filestat;
  488. #endif
  489. #ifdef S_ISFIFO
  490. /* If local system doesn't support FIFOs, use default case */
  491. case LF_FIFO:
  492. make_fifo:
  493. check = mkfifo(skipcrud+head->header.name,
  494. (int) hstat.st_mode);
  495. if (check != 0) {
  496. if (make_dirs(skipcrud+head->header.name))
  497. goto make_fifo;
  498. msg_perror("Could not make %s",skipcrud+head->header.name);
  499. break;
  500. };
  501. goto set_filestat;
  502. #endif
  503. case LF_DIR:
  504. case LF_DUMPDIR:
  505. namelen = strlen(skipcrud+head->header.name)-1;
  506. really_dir:
  507. /* Check for trailing /, and zap as many as we find. */
  508. while (namelen && head->header.name[skipcrud+namelen] == '/')
  509. head->header.name[skipcrud+namelen--] = '\0';
  510. if(f_gnudump) { /* Read the entry and delete files
  511. that aren't listed in the archive */
  512. gnu_restore(skipcrud);
  513. } else if(head->header.linkflag==LF_DUMPDIR)
  514. skip_file((long)(hstat.st_size));
  515. again_dir:
  516. check = mkdir(skipcrud+head->header.name,
  517. (we_are_root ? 0 : 0300) | (int)hstat.st_mode);
  518. if (check != 0) {
  519. struct stat st1;
  520. if (make_dirs(skipcrud+head->header.name))
  521. goto again_dir;
  522. /* If we're trying to create '.', let it be. */
  523. if (head->header.name[skipcrud+namelen] == '.' &&
  524. (namelen==0 ||
  525. head->header.name[skipcrud+namelen-1]=='/'))
  526. goto check_perms;
  527. if( errno==EEXIST
  528. && stat(skipcrud+head->header.name,&st1)==0
  529. && (S_ISDIR(st1.st_mode)))
  530. break;
  531. msg_perror("Could not create directory %s",skipcrud+head->header.name);
  532. break;
  533. }
  534. check_perms:
  535. if (!we_are_root && 0300 != (0300 & (int) hstat.st_mode)) {
  536. hstat.st_mode |= 0300;
  537. msg("Added write and execute permission to directory %s",
  538. skipcrud+head->header.name);
  539. }
  540. goto set_filestat;
  541. /* FIXME, Remember timestamps for after files created? */
  542. /* FIXME, change mode after files created (if was R/O dir) */
  543. case LF_VOLHDR:
  544. if(f_verbose) {
  545. printf("Reading %s\n",head->header.name);
  546. }
  547. break;
  548. case LF_NAMES:
  549. extract_mangle(head);
  550. break;
  551. case LF_MULTIVOL:
  552. msg("Can't extract '%s'--file is continued from another volume\n",head->header.name);
  553. skip_file((long)hstat.st_size);
  554. break;
  555. }
  556. /* We don't need to save it any longer. */
  557. saverec((union record **) 0); /* Unsave it */
  558. }
  559. /*
  560. * After a file/link/symlink/dir creation has failed, see if
  561. * it's because some required directory was not present, and if
  562. * so, create all required dirs.
  563. */
  564. int
  565. make_dirs(pathname)
  566. char *pathname;
  567. {
  568. char *p; /* Points into path */
  569. int madeone = 0; /* Did we do anything yet? */
  570. int save_errno = errno; /* Remember caller's errno */
  571. int check;
  572. if (errno != ENOENT)
  573. return 0; /* Not our problem */
  574. for (p = index(pathname, '/'); p != NULL; p = index(p+1, '/')) {
  575. /* Avoid mkdir of empty string, if leading or double '/' */
  576. if (p == pathname || p[-1] == '/')
  577. continue;
  578. /* Avoid mkdir where last part of path is '.' */
  579. if (p[-1] == '.' && (p == pathname+1 || p[-2] == '/'))
  580. continue;
  581. *p = 0; /* Truncate the path there */
  582. check = mkdir (pathname, 0777); /* Try to create it as a dir */
  583. if (check == 0) {
  584. /* Fix ownership */
  585. if (we_are_root) {
  586. if (chown(pathname, hstat.st_uid,
  587. hstat.st_gid) < 0) {
  588. msg_perror("cannot change owner of %s to uid %d gid %d",pathname,hstat.st_uid,hstat.st_gid);
  589. }
  590. }
  591. pr_mkdir(pathname, p-pathname, notumask&0777);
  592. madeone++; /* Remember if we made one */
  593. *p = '/';
  594. continue;
  595. }
  596. *p = '/';
  597. if (errno == EEXIST) /* Directory already exists */
  598. continue;
  599. /*
  600. * Some other error in the mkdir. We return to the caller.
  601. */
  602. break;
  603. }
  604. errno = save_errno; /* Restore caller's errno */
  605. return madeone; /* Tell them to retry if we made one */
  606. }
  607. void
  608. extract_sparse_file(fd, sizeleft, totalsize, name)
  609. int fd;
  610. long *sizeleft,
  611. totalsize;
  612. char *name;
  613. {
  614. /* register char *data;*/
  615. union record *datarec;
  616. int sparse_ind = 0;
  617. int written,
  618. count;
  619. /* assuming sizeleft is initially totalsize */
  620. while (*sizeleft > 0) {
  621. datarec = findrec();
  622. if (datarec == NULL) {
  623. msg("Unexpected EOF on archive file");
  624. return;
  625. }
  626. lseek(fd, sparsearray[sparse_ind].offset, 0);
  627. written = sparsearray[sparse_ind++].numbytes;
  628. while (written > RECORDSIZE) {
  629. count = write(fd, datarec->charptr, RECORDSIZE);
  630. if (count < 0)
  631. msg_perror("couldn't write to file %s", name);
  632. written -= count;
  633. *sizeleft -= count;
  634. userec(datarec);
  635. datarec = findrec();
  636. }
  637. count = write(fd, datarec->charptr, written);
  638. if (count < 0) {
  639. msg_perror("couldn't write to file %s", name);
  640. } else if (count != written) {
  641. msg("could only write %d of %d bytes to file %s", totalsize - *sizeleft, totalsize, name);
  642. skip_file((long) (*sizeleft));
  643. }
  644. written -= count;
  645. *sizeleft -= count;
  646. userec(datarec);
  647. }
  648. free(sparsearray);
  649. /* if (end_nulls) {
  650. register int i;
  651. printf("%d\n", (int) end_nulls);
  652. for (i = 0; i < end_nulls; i++)
  653. write(fd, "\000", 1);
  654. }*/
  655. userec(datarec);
  656. }