sparse.c 22 KB

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