sparse.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /* Functions for dealing with sparse files
  2. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "system.h"
  15. #include <quotearg.h>
  16. #include "common.h"
  17. struct tar_sparse_file;
  18. enum sparse_scan_state
  19. {
  20. scan_begin,
  21. scan_block,
  22. scan_end
  23. };
  24. struct tar_sparse_optab
  25. {
  26. bool (*init) (struct tar_sparse_file *);
  27. bool (*done) (struct tar_sparse_file *);
  28. bool (*sparse_member_p) (struct tar_sparse_file *);
  29. bool (*dump_header) (struct tar_sparse_file *);
  30. bool (*fixup_header) (struct tar_sparse_file *);
  31. bool (*decode_header) (struct tar_sparse_file *);
  32. bool (*scan_block) (struct tar_sparse_file *, enum sparse_scan_state,
  33. void *);
  34. bool (*dump_region) (struct tar_sparse_file *, size_t);
  35. bool (*extract_region) (struct tar_sparse_file *, size_t);
  36. };
  37. struct tar_sparse_file
  38. {
  39. int fd; /* File descriptor */
  40. size_t dumped_size; /* Number of bytes actually written
  41. to the archive */
  42. struct tar_stat_info *stat_info; /* Information about the file */
  43. struct tar_sparse_optab *optab;
  44. void *closure; /* Any additional data optab calls might
  45. reqiure */
  46. };
  47. static bool
  48. tar_sparse_member_p (struct tar_sparse_file *file)
  49. {
  50. if (file->optab->sparse_member_p)
  51. return file->optab->sparse_member_p (file);
  52. return false;
  53. }
  54. static bool
  55. tar_sparse_init (struct tar_sparse_file *file)
  56. {
  57. file->dumped_size = 0;
  58. if (file->optab->init)
  59. return file->optab->init (file);
  60. return true;
  61. }
  62. static bool
  63. tar_sparse_done (struct tar_sparse_file *file)
  64. {
  65. if (file->optab->done)
  66. return file->optab->done (file);
  67. return true;
  68. }
  69. static bool
  70. tar_sparse_scan (struct tar_sparse_file *file, enum sparse_scan_state state,
  71. void *block)
  72. {
  73. if (file->optab->scan_block)
  74. return file->optab->scan_block (file, state, block);
  75. return true;
  76. }
  77. static bool
  78. tar_sparse_dump_region (struct tar_sparse_file *file, size_t i)
  79. {
  80. if (file->optab->dump_region)
  81. return file->optab->dump_region (file, i);
  82. return false;
  83. }
  84. static bool
  85. tar_sparse_extract_region (struct tar_sparse_file *file, size_t i)
  86. {
  87. if (file->optab->extract_region)
  88. return file->optab->extract_region (file, i);
  89. return false;
  90. }
  91. static bool
  92. tar_sparse_dump_header (struct tar_sparse_file *file)
  93. {
  94. if (file->optab->dump_header)
  95. return file->optab->dump_header (file);
  96. return false;
  97. }
  98. static bool
  99. tar_sparse_decode_header (struct tar_sparse_file *file)
  100. {
  101. if (file->optab->decode_header)
  102. return file->optab->decode_header (file);
  103. return true;
  104. }
  105. static bool
  106. tar_sparse_fixup_header (struct tar_sparse_file *file)
  107. {
  108. if (file->optab->fixup_header)
  109. return file->optab->fixup_header (file);
  110. return true;
  111. }
  112. static bool
  113. lseek_or_error (struct tar_sparse_file *file, off_t offset, int whence)
  114. {
  115. if (lseek (file->fd, offset, whence) < 0)
  116. {
  117. seek_diag_details (file->stat_info->orig_file_name, offset);
  118. return false;
  119. }
  120. return true;
  121. }
  122. /* Takes a blockful of data and basically cruises through it to see if
  123. it's made *entirely* of zeros, returning a 0 the instant it finds
  124. something that is a nonzero, i.e., useful data. */
  125. static bool
  126. zero_block_p (char *buffer, size_t size)
  127. {
  128. while (size--)
  129. if (*buffer++)
  130. return false;
  131. return true;
  132. }
  133. #define clear_block(p) memset (p, 0, BLOCKSIZE);
  134. #define SPARSES_INIT_COUNT SPARSES_IN_SPARSE_HEADER
  135. static void
  136. sparse_add_map (struct tar_sparse_file *file, struct sp_array *sp)
  137. {
  138. if (file->stat_info->sparse_map == NULL)
  139. {
  140. file->stat_info->sparse_map =
  141. xmalloc (SPARSES_INIT_COUNT * sizeof file->stat_info->sparse_map[0]);
  142. file->stat_info->sparse_map_size = SPARSES_INIT_COUNT;
  143. }
  144. else if (file->stat_info->sparse_map_avail == file->stat_info->sparse_map_size)
  145. {
  146. file->stat_info->sparse_map_size *= 2;
  147. file->stat_info->sparse_map =
  148. xrealloc (file->stat_info->sparse_map,
  149. file->stat_info->sparse_map_size
  150. * sizeof file->stat_info->sparse_map[0]);
  151. }
  152. file->stat_info->sparse_map[file->stat_info->sparse_map_avail++] = *sp;
  153. }
  154. /* Scan the sparse file and create its map */
  155. static bool
  156. sparse_scan_file (struct tar_sparse_file *file)
  157. {
  158. static char buffer[BLOCKSIZE];
  159. size_t count;
  160. size_t offset = 0;
  161. struct sp_array sp = {0, 0};
  162. if (!lseek_or_error (file, 0, SEEK_SET))
  163. return false;
  164. clear_block (buffer);
  165. file->stat_info->sparse_map_avail = 0;
  166. file->stat_info->archive_file_size = 0;
  167. if (!tar_sparse_scan (file, scan_begin, NULL))
  168. return false;
  169. while ((count = safe_read (file->fd, buffer, sizeof buffer)) != 0
  170. && count != SAFE_READ_ERROR)
  171. {
  172. /* Analize the block */
  173. if (zero_block_p (buffer, count))
  174. {
  175. if (sp.numbytes)
  176. {
  177. sparse_add_map (file, &sp);
  178. sp.numbytes = 0;
  179. if (!tar_sparse_scan (file, scan_block, NULL))
  180. return false;
  181. }
  182. }
  183. else
  184. {
  185. if (sp.numbytes == 0)
  186. sp.offset = offset;
  187. sp.numbytes += count;
  188. file->stat_info->archive_file_size += count;
  189. if (!tar_sparse_scan (file, scan_block, buffer))
  190. return false;
  191. }
  192. offset += count;
  193. clear_block (buffer);
  194. }
  195. if (sp.numbytes == 0)
  196. sp.offset = offset;
  197. sparse_add_map (file, &sp);
  198. file->stat_info->archive_file_size += count;
  199. return tar_sparse_scan (file, scan_end, NULL);
  200. }
  201. static struct tar_sparse_optab oldgnu_optab;
  202. static struct tar_sparse_optab star_optab;
  203. static struct tar_sparse_optab pax_optab;
  204. static bool
  205. sparse_select_optab (struct tar_sparse_file *file)
  206. {
  207. switch (current_format == DEFAULT_FORMAT ? archive_format : current_format)
  208. {
  209. case V7_FORMAT:
  210. case USTAR_FORMAT:
  211. return false;
  212. case OLDGNU_FORMAT:
  213. case GNU_FORMAT: /*FIXME: This one should disappear? */
  214. file->optab = &oldgnu_optab;
  215. break;
  216. case POSIX_FORMAT:
  217. file->optab = &pax_optab;
  218. break;
  219. case STAR_FORMAT:
  220. file->optab = &star_optab;
  221. break;
  222. default:
  223. return false;
  224. }
  225. return true;
  226. }
  227. static bool
  228. sparse_dump_region (struct tar_sparse_file *file, size_t i)
  229. {
  230. union block *blk;
  231. off_t bytes_left = file->stat_info->sparse_map[i].numbytes;
  232. if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset,
  233. SEEK_SET))
  234. return false;
  235. while (bytes_left > 0)
  236. {
  237. size_t bufsize = (bytes_left > BLOCKSIZE) ? BLOCKSIZE : bytes_left;
  238. size_t bytes_read;
  239. blk = find_next_block ();
  240. memset (blk->buffer, 0, BLOCKSIZE);
  241. bytes_read = safe_read (file->fd, blk->buffer, bufsize);
  242. if (bytes_read == SAFE_READ_ERROR)
  243. {
  244. read_diag_details (file->stat_info->orig_file_name,
  245. file->stat_info->sparse_map[i].offset
  246. + file->stat_info->sparse_map[i].numbytes
  247. - bytes_left,
  248. bufsize);
  249. return false;
  250. }
  251. bytes_left -= bytes_read;
  252. file->dumped_size += bytes_read;
  253. set_next_block_after (blk);
  254. }
  255. return true;
  256. }
  257. static bool
  258. sparse_extract_region (struct tar_sparse_file *file, size_t i)
  259. {
  260. size_t write_size;
  261. if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset,
  262. SEEK_SET))
  263. return false;
  264. write_size = file->stat_info->sparse_map[i].numbytes;
  265. if (write_size == 0)
  266. {
  267. /* Last block of the file is a hole */
  268. if (sys_truncate (file->fd))
  269. truncate_warn (file->stat_info->orig_file_name);
  270. }
  271. else while (write_size > 0)
  272. {
  273. size_t count;
  274. size_t wrbytes = (write_size > BLOCKSIZE) ? BLOCKSIZE : write_size;
  275. union block *blk = find_next_block ();
  276. if (!blk)
  277. {
  278. ERROR ((0, 0, _("Unexpected EOF in archive")));
  279. return false;
  280. }
  281. set_next_block_after (blk);
  282. count = full_write (file->fd, blk->buffer, wrbytes);
  283. write_size -= count;
  284. file->dumped_size += count;
  285. if (count != wrbytes)
  286. {
  287. write_error_details (file->stat_info->orig_file_name,
  288. count, wrbytes);
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. /* Interface functions */
  295. enum dump_status
  296. sparse_dump_file (int fd, struct tar_stat_info *st)
  297. {
  298. bool rc;
  299. struct tar_sparse_file file;
  300. file.stat_info = st;
  301. file.fd = fd;
  302. if (!sparse_select_optab (&file)
  303. || !tar_sparse_init (&file))
  304. return dump_status_not_implemented;
  305. rc = sparse_scan_file (&file);
  306. if (rc && file.optab->dump_region)
  307. {
  308. tar_sparse_dump_header (&file);
  309. if (fd >= 0)
  310. {
  311. size_t i;
  312. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  313. rc = tar_sparse_dump_region (&file, i);
  314. }
  315. }
  316. pad_archive(file.stat_info->archive_file_size - file.dumped_size);
  317. return (tar_sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
  318. }
  319. /* Returns true if the file represented by stat is a sparse one */
  320. bool
  321. sparse_file_p (struct tar_stat_info *st)
  322. {
  323. return (ST_NBLOCKS (st->stat)
  324. < (st->stat.st_size / ST_NBLOCKSIZE
  325. + (st->stat.st_size % ST_NBLOCKSIZE != 0)));
  326. }
  327. bool
  328. sparse_member_p (struct tar_stat_info *st)
  329. {
  330. struct tar_sparse_file file;
  331. if (!sparse_select_optab (&file))
  332. return false;
  333. file.stat_info = st;
  334. return tar_sparse_member_p (&file);
  335. }
  336. bool
  337. sparse_fixup_header (struct tar_stat_info *st)
  338. {
  339. struct tar_sparse_file file;
  340. if (!sparse_select_optab (&file))
  341. return false;
  342. file.stat_info = st;
  343. return tar_sparse_fixup_header (&file);
  344. }
  345. enum dump_status
  346. sparse_extract_file (int fd, struct tar_stat_info *st, off_t *size)
  347. {
  348. bool rc = true;
  349. struct tar_sparse_file file;
  350. size_t i;
  351. file.stat_info = st;
  352. file.fd = fd;
  353. if (!sparse_select_optab (&file)
  354. || !tar_sparse_init (&file))
  355. return dump_status_not_implemented;
  356. rc = tar_sparse_decode_header (&file);
  357. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  358. rc = tar_sparse_extract_region (&file, i);
  359. *size = file.stat_info->archive_file_size - file.dumped_size;
  360. return (tar_sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
  361. }
  362. enum dump_status
  363. sparse_skip_file (struct tar_stat_info *st)
  364. {
  365. bool rc = true;
  366. struct tar_sparse_file file;
  367. file.stat_info = st;
  368. file.fd = -1;
  369. if (!sparse_select_optab (&file)
  370. || !tar_sparse_init (&file))
  371. return dump_status_not_implemented;
  372. rc = tar_sparse_decode_header (&file);
  373. skip_file (file.stat_info->archive_file_size);
  374. return (tar_sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
  375. }
  376. static char diff_buffer[BLOCKSIZE];
  377. static bool
  378. check_sparse_region (struct tar_sparse_file *file, off_t beg, off_t end)
  379. {
  380. if (!lseek_or_error (file, beg, SEEK_SET))
  381. return false;
  382. while (beg < end)
  383. {
  384. size_t bytes_read;
  385. size_t rdsize = end - beg;
  386. if (rdsize > BLOCKSIZE)
  387. rdsize = BLOCKSIZE;
  388. clear_block (diff_buffer);
  389. bytes_read = safe_read (file->fd, diff_buffer, rdsize);
  390. if (bytes_read == SAFE_READ_ERROR)
  391. {
  392. read_diag_details (file->stat_info->orig_file_name,
  393. beg,
  394. rdsize);
  395. return false;
  396. }
  397. if (!zero_block_p (diff_buffer, bytes_read))
  398. {
  399. report_difference (file->stat_info,
  400. _("File fragment at %lu is not a hole"), beg);
  401. return false;
  402. }
  403. beg += bytes_read;
  404. }
  405. return true;
  406. }
  407. static bool
  408. check_data_region (struct tar_sparse_file *file, size_t i)
  409. {
  410. size_t size_left;
  411. if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset,
  412. SEEK_SET))
  413. return false;
  414. size_left = file->stat_info->sparse_map[i].numbytes;
  415. while (size_left > 0)
  416. {
  417. size_t bytes_read;
  418. size_t rdsize = (size_left > BLOCKSIZE) ? BLOCKSIZE : size_left;
  419. union block *blk = find_next_block ();
  420. if (!blk)
  421. {
  422. ERROR ((0, 0, _("Unexpected EOF in archive")));
  423. return false;
  424. }
  425. set_next_block_after (blk);
  426. bytes_read = safe_read (file->fd, diff_buffer, rdsize);
  427. if (bytes_read == SAFE_READ_ERROR)
  428. {
  429. read_diag_details (file->stat_info->orig_file_name,
  430. file->stat_info->sparse_map[i].offset
  431. + file->stat_info->sparse_map[i].numbytes
  432. - size_left,
  433. rdsize);
  434. return false;
  435. }
  436. file->dumped_size += bytes_read;
  437. size_left -= bytes_read;
  438. if (memcmp (blk->buffer, diff_buffer, rdsize))
  439. {
  440. report_difference (file->stat_info, _("Contents differ"));
  441. return false;
  442. }
  443. }
  444. return true;
  445. }
  446. bool
  447. sparse_diff_file (int fd, struct tar_stat_info *st)
  448. {
  449. bool rc = true;
  450. struct tar_sparse_file file;
  451. size_t i;
  452. off_t offset = 0;
  453. file.stat_info = st;
  454. file.fd = fd;
  455. if (!sparse_select_optab (&file)
  456. || !tar_sparse_init (&file))
  457. return dump_status_not_implemented;
  458. rc = tar_sparse_decode_header (&file);
  459. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  460. {
  461. rc = check_sparse_region (&file,
  462. offset, file.stat_info->sparse_map[i].offset)
  463. && check_data_region (&file, i);
  464. offset = file.stat_info->sparse_map[i].offset
  465. + file.stat_info->sparse_map[i].numbytes;
  466. }
  467. if (!rc)
  468. skip_file (file.stat_info->archive_file_size - file.dumped_size);
  469. tar_sparse_done (&file);
  470. return rc;
  471. }
  472. /* Old GNU Format. The sparse file information is stored in the
  473. oldgnu_header in the following manner:
  474. The header is marked with type 'S'. Its `size' field contains
  475. the cumulative size of all non-empty blocks of the file. The
  476. actual file size is stored in `realsize' member of oldgnu_header.
  477. The map of the file is stored in a list of `struct sparse'.
  478. Each struct contains offset to the block of data and its
  479. size (both as octal numbers). The first file header contains
  480. at most 4 such structs (SPARSES_IN_OLDGNU_HEADER). If the map
  481. contains more structs, then the field `isextended' of the main
  482. header is set to 1 (binary) and the `struct sparse_header'
  483. header follows, containing at most 21 following structs
  484. (SPARSES_IN_SPARSE_HEADER). If more structs follow, `isextended'
  485. field of the extended header is set and next next extension header
  486. follows, etc... */
  487. enum oldgnu_add_status
  488. {
  489. add_ok,
  490. add_finish,
  491. add_fail
  492. };
  493. static bool
  494. oldgnu_sparse_member_p (struct tar_sparse_file *file __attribute__ ((unused)))
  495. {
  496. return current_header->header.typeflag == GNUTYPE_SPARSE;
  497. }
  498. /* Add a sparse item to the sparse file and its obstack */
  499. static enum oldgnu_add_status
  500. oldgnu_add_sparse (struct tar_sparse_file *file, struct sparse *s)
  501. {
  502. struct sp_array sp;
  503. if (s->numbytes[0] == '\0')
  504. return add_finish;
  505. sp.offset = OFF_FROM_HEADER (s->offset);
  506. sp.numbytes = SIZE_FROM_HEADER (s->numbytes);
  507. if (sp.offset < 0
  508. || file->stat_info->stat.st_size < sp.offset + sp.numbytes
  509. || file->stat_info->archive_file_size < 0)
  510. return add_fail;
  511. sparse_add_map (file, &sp);
  512. return add_ok;
  513. }
  514. static bool
  515. oldgnu_fixup_header (struct tar_sparse_file *file)
  516. {
  517. /* NOTE! st_size was initialized from the header
  518. which actually contains archived size. The following fixes it */
  519. file->stat_info->archive_file_size = file->stat_info->stat.st_size;
  520. file->stat_info->stat.st_size =
  521. OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
  522. return true;
  523. }
  524. /* Convert old GNU format sparse data to internal representation */
  525. static bool
  526. oldgnu_get_sparse_info (struct tar_sparse_file *file)
  527. {
  528. size_t i;
  529. union block *h = current_header;
  530. int ext_p;
  531. static enum oldgnu_add_status rc;
  532. file->stat_info->sparse_map_avail = 0;
  533. for (i = 0; i < SPARSES_IN_OLDGNU_HEADER; i++)
  534. {
  535. rc = oldgnu_add_sparse (file, &h->oldgnu_header.sp[i]);
  536. if (rc != add_ok)
  537. break;
  538. }
  539. for (ext_p = h->oldgnu_header.isextended;
  540. rc == add_ok && ext_p; ext_p = h->sparse_header.isextended)
  541. {
  542. h = find_next_block ();
  543. if (!h)
  544. {
  545. ERROR ((0, 0, _("Unexpected EOF in archive")));
  546. return false;
  547. }
  548. set_next_block_after (h);
  549. for (i = 0; i < SPARSES_IN_SPARSE_HEADER && rc == add_ok; i++)
  550. rc = oldgnu_add_sparse (file, &h->sparse_header.sp[i]);
  551. }
  552. if (rc == add_fail)
  553. {
  554. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  555. file->stat_info->orig_file_name));
  556. return false;
  557. }
  558. return true;
  559. }
  560. static void
  561. oldgnu_store_sparse_info (struct tar_sparse_file *file, size_t *pindex,
  562. struct sparse *sp, size_t sparse_size)
  563. {
  564. for (; *pindex < file->stat_info->sparse_map_avail
  565. && sparse_size > 0; sparse_size--, sp++, ++*pindex)
  566. {
  567. OFF_TO_CHARS (file->stat_info->sparse_map[*pindex].offset,
  568. sp->offset);
  569. SIZE_TO_CHARS (file->stat_info->sparse_map[*pindex].numbytes,
  570. sp->numbytes);
  571. }
  572. }
  573. static bool
  574. oldgnu_dump_header (struct tar_sparse_file *file)
  575. {
  576. off_t block_ordinal = current_block_ordinal ();
  577. union block *blk;
  578. size_t i;
  579. blk = start_header (file->stat_info);
  580. blk->header.typeflag = GNUTYPE_SPARSE;
  581. if (file->stat_info->sparse_map_avail > SPARSES_IN_OLDGNU_HEADER)
  582. blk->oldgnu_header.isextended = 1;
  583. /* Store the real file size */
  584. OFF_TO_CHARS (file->stat_info->stat.st_size, blk->oldgnu_header.realsize);
  585. /* Store the effective (shrunken) file size */
  586. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  587. i = 0;
  588. oldgnu_store_sparse_info (file, &i,
  589. blk->oldgnu_header.sp,
  590. SPARSES_IN_OLDGNU_HEADER);
  591. blk->oldgnu_header.isextended = i < file->stat_info->sparse_map_avail;
  592. finish_header (file->stat_info, blk, block_ordinal);
  593. while (i < file->stat_info->sparse_map_avail)
  594. {
  595. blk = find_next_block ();
  596. memset (blk->buffer, 0, BLOCKSIZE);
  597. oldgnu_store_sparse_info (file, &i,
  598. blk->sparse_header.sp,
  599. SPARSES_IN_SPARSE_HEADER);
  600. set_next_block_after (blk);
  601. if (i < file->stat_info->sparse_map_avail)
  602. blk->sparse_header.isextended = 1;
  603. else
  604. break;
  605. }
  606. return true;
  607. }
  608. static struct tar_sparse_optab oldgnu_optab = {
  609. NULL, /* No init function */
  610. NULL, /* No done function */
  611. oldgnu_sparse_member_p,
  612. oldgnu_dump_header,
  613. oldgnu_fixup_header,
  614. oldgnu_get_sparse_info,
  615. NULL, /* No scan_block function */
  616. sparse_dump_region,
  617. sparse_extract_region,
  618. };
  619. /* Star */
  620. static bool
  621. star_sparse_member_p (struct tar_sparse_file *file __attribute__ ((unused)))
  622. {
  623. return current_header->header.typeflag == GNUTYPE_SPARSE;
  624. }
  625. static bool
  626. star_fixup_header (struct tar_sparse_file *file)
  627. {
  628. /* NOTE! st_size was initialized from the header
  629. which actually contains archived size. The following fixes it */
  630. file->stat_info->archive_file_size = file->stat_info->stat.st_size;
  631. file->stat_info->stat.st_size =
  632. OFF_FROM_HEADER (current_header->star_in_header.realsize);
  633. return true;
  634. }
  635. /* Convert STAR format sparse data to internal representation */
  636. static bool
  637. star_get_sparse_info (struct tar_sparse_file *file)
  638. {
  639. size_t i;
  640. union block *h = current_header;
  641. int ext_p;
  642. static enum oldgnu_add_status rc;
  643. file->stat_info->sparse_map_avail = 0;
  644. if (h->star_in_header.prefix[0] == '\0'
  645. && h->star_in_header.sp[0].offset[10] != '\0')
  646. {
  647. /* Old star format */
  648. for (i = 0; i < SPARSES_IN_STAR_HEADER; i++)
  649. {
  650. rc = oldgnu_add_sparse (file, &h->star_in_header.sp[i]);
  651. if (rc != add_ok)
  652. break;
  653. }
  654. ext_p = h->star_in_header.isextended;
  655. }
  656. else
  657. ext_p = 1;
  658. for (; rc == add_ok && ext_p; ext_p = h->star_ext_header.isextended)
  659. {
  660. h = find_next_block ();
  661. if (!h)
  662. {
  663. ERROR ((0, 0, _("Unexpected EOF in archive")));
  664. return false;
  665. }
  666. set_next_block_after (h);
  667. for (i = 0; i < SPARSES_IN_STAR_EXT_HEADER && rc == add_ok; i++)
  668. rc = oldgnu_add_sparse (file, &h->star_ext_header.sp[i]);
  669. }
  670. if (rc == add_fail)
  671. {
  672. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  673. file->stat_info->orig_file_name));
  674. return false;
  675. }
  676. return true;
  677. }
  678. static struct tar_sparse_optab star_optab = {
  679. NULL, /* No init function */
  680. NULL, /* No done function */
  681. star_sparse_member_p,
  682. NULL,
  683. star_fixup_header,
  684. star_get_sparse_info,
  685. NULL, /* No scan_block function */
  686. NULL, /* No dump region function */
  687. sparse_extract_region,
  688. };
  689. /* GNU PAX sparse file format. The sparse file map is stored in
  690. x header:
  691. GNU.sparse.size Real size of the stored file
  692. GNU.sparse.numblocks Number of blocks in the sparse map
  693. repeat numblocks time
  694. GNU.sparse.offset Offset of the next data block
  695. GNU.sparse.numbytes Size of the next data block
  696. end repeat
  697. */
  698. static bool
  699. pax_sparse_member_p (struct tar_sparse_file *file)
  700. {
  701. return file->stat_info->archive_file_size != file->stat_info->stat.st_size;
  702. }
  703. static bool
  704. pax_dump_header (struct tar_sparse_file *file)
  705. {
  706. off_t block_ordinal = current_block_ordinal ();
  707. union block *blk;
  708. size_t i;
  709. /* Store the real file size */
  710. xheader_store ("GNU.sparse.size", file->stat_info, NULL);
  711. xheader_store ("GNU.sparse.numblocks", file->stat_info, NULL);
  712. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  713. {
  714. xheader_store ("GNU.sparse.offset", file->stat_info, &i);
  715. xheader_store ("GNU.sparse.numbytes", file->stat_info, &i);
  716. }
  717. blk = start_header (file->stat_info);
  718. /* Store the effective (shrunken) file size */
  719. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  720. finish_header (file->stat_info, blk, block_ordinal);
  721. return true;
  722. }
  723. static struct tar_sparse_optab pax_optab = {
  724. NULL, /* No init function */
  725. NULL, /* No done function */
  726. pax_sparse_member_p,
  727. pax_dump_header,
  728. NULL, /* No decode_header function */
  729. NULL, /* No fixup_header function */
  730. NULL, /* No scan_block function */
  731. sparse_dump_region,
  732. sparse_extract_region,
  733. };