sparse.c 24 KB

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