sparse.c 22 KB

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