sparse.c 23 KB

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