sparse.c 20 KB

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