sparse.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /* Functions for dealing with sparse files
  2. Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010 Free Software
  3. Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <system.h>
  16. #include <inttostr.h>
  17. #include <quotearg.h>
  18. #include "common.h"
  19. struct tar_sparse_file;
  20. static bool sparse_select_optab (struct tar_sparse_file *file);
  21. enum sparse_scan_state
  22. {
  23. scan_begin,
  24. scan_block,
  25. scan_end
  26. };
  27. struct tar_sparse_optab
  28. {
  29. bool (*init) (struct tar_sparse_file *);
  30. bool (*done) (struct tar_sparse_file *);
  31. bool (*sparse_member_p) (struct tar_sparse_file *);
  32. bool (*dump_header) (struct tar_sparse_file *);
  33. bool (*fixup_header) (struct tar_sparse_file *);
  34. bool (*decode_header) (struct tar_sparse_file *);
  35. bool (*scan_block) (struct tar_sparse_file *, enum sparse_scan_state,
  36. void *);
  37. bool (*dump_region) (struct tar_sparse_file *, size_t);
  38. bool (*extract_region) (struct tar_sparse_file *, size_t);
  39. };
  40. struct tar_sparse_file
  41. {
  42. int fd; /* File descriptor */
  43. bool seekable; /* Is fd seekable? */
  44. off_t offset; /* Current offset in fd if seekable==false.
  45. Otherwise unused */
  46. off_t dumped_size; /* Number of bytes actually written
  47. to the archive */
  48. struct tar_stat_info *stat_info; /* Information about the file */
  49. struct tar_sparse_optab const *optab; /* Operation table */
  50. void *closure; /* Any additional data optab calls might
  51. require */
  52. };
  53. /* Dump zeros to file->fd until offset is reached. It is used instead of
  54. lseek if the output file is not seekable */
  55. static bool
  56. dump_zeros (struct tar_sparse_file *file, off_t offset)
  57. {
  58. static char const zero_buf[BLOCKSIZE];
  59. if (offset < file->offset)
  60. {
  61. errno = EINVAL;
  62. return false;
  63. }
  64. while (file->offset < offset)
  65. {
  66. size_t size = (BLOCKSIZE < offset - file->offset
  67. ? BLOCKSIZE
  68. : offset - file->offset);
  69. ssize_t wrbytes;
  70. wrbytes = write (file->fd, zero_buf, size);
  71. if (wrbytes <= 0)
  72. {
  73. if (wrbytes == 0)
  74. errno = EINVAL;
  75. return false;
  76. }
  77. file->offset += wrbytes;
  78. }
  79. return true;
  80. }
  81. static bool
  82. tar_sparse_member_p (struct tar_sparse_file *file)
  83. {
  84. if (file->optab->sparse_member_p)
  85. return file->optab->sparse_member_p (file);
  86. return false;
  87. }
  88. static bool
  89. tar_sparse_init (struct tar_sparse_file *file)
  90. {
  91. memset (file, 0, sizeof *file);
  92. if (!sparse_select_optab (file))
  93. return false;
  94. if (file->optab->init)
  95. return file->optab->init (file);
  96. return true;
  97. }
  98. static bool
  99. tar_sparse_done (struct tar_sparse_file *file)
  100. {
  101. if (file->optab->done)
  102. return file->optab->done (file);
  103. return true;
  104. }
  105. static bool
  106. tar_sparse_scan (struct tar_sparse_file *file, enum sparse_scan_state state,
  107. void *block)
  108. {
  109. if (file->optab->scan_block)
  110. return file->optab->scan_block (file, state, block);
  111. return true;
  112. }
  113. static bool
  114. tar_sparse_dump_region (struct tar_sparse_file *file, size_t i)
  115. {
  116. if (file->optab->dump_region)
  117. return file->optab->dump_region (file, i);
  118. return false;
  119. }
  120. static bool
  121. tar_sparse_extract_region (struct tar_sparse_file *file, size_t i)
  122. {
  123. if (file->optab->extract_region)
  124. return file->optab->extract_region (file, i);
  125. return false;
  126. }
  127. static bool
  128. tar_sparse_dump_header (struct tar_sparse_file *file)
  129. {
  130. if (file->optab->dump_header)
  131. return file->optab->dump_header (file);
  132. return false;
  133. }
  134. static bool
  135. tar_sparse_decode_header (struct tar_sparse_file *file)
  136. {
  137. if (file->optab->decode_header)
  138. return file->optab->decode_header (file);
  139. return true;
  140. }
  141. static bool
  142. tar_sparse_fixup_header (struct tar_sparse_file *file)
  143. {
  144. if (file->optab->fixup_header)
  145. return file->optab->fixup_header (file);
  146. return true;
  147. }
  148. static bool
  149. lseek_or_error (struct tar_sparse_file *file, off_t offset)
  150. {
  151. if (file->seekable
  152. ? lseek (file->fd, offset, SEEK_SET) < 0
  153. : ! dump_zeros (file, offset))
  154. {
  155. seek_diag_details (file->stat_info->orig_file_name, offset);
  156. return false;
  157. }
  158. return true;
  159. }
  160. /* Takes a blockful of data and basically cruises through it to see if
  161. it's made *entirely* of zeros, returning a 0 the instant it finds
  162. something that is a nonzero, i.e., useful data. */
  163. static bool
  164. zero_block_p (char const *buffer, size_t size)
  165. {
  166. while (size--)
  167. if (*buffer++)
  168. return false;
  169. return true;
  170. }
  171. static void
  172. sparse_add_map (struct tar_stat_info *st, struct sp_array const *sp)
  173. {
  174. struct sp_array *sparse_map = st->sparse_map;
  175. size_t avail = st->sparse_map_avail;
  176. if (avail == st->sparse_map_size)
  177. st->sparse_map = sparse_map =
  178. x2nrealloc (sparse_map, &st->sparse_map_size, sizeof *sparse_map);
  179. sparse_map[avail] = *sp;
  180. st->sparse_map_avail = avail + 1;
  181. }
  182. /* Scan the sparse file and create its map */
  183. static bool
  184. sparse_scan_file (struct tar_sparse_file *file)
  185. {
  186. struct tar_stat_info *st = file->stat_info;
  187. int fd = file->fd;
  188. char buffer[BLOCKSIZE];
  189. size_t count = 0;
  190. off_t offset = 0;
  191. struct sp_array sp = {0, 0};
  192. st->archive_file_size = 0;
  193. if (ST_NBLOCKS (st->stat) == 0)
  194. offset = st->stat.st_size;
  195. else
  196. {
  197. if (!tar_sparse_scan (file, scan_begin, NULL))
  198. return false;
  199. while ((count = blocking_read (fd, buffer, sizeof buffer)) != 0
  200. && count != SAFE_READ_ERROR)
  201. {
  202. /* Analyze the block. */
  203. if (zero_block_p (buffer, count))
  204. {
  205. if (sp.numbytes)
  206. {
  207. sparse_add_map (st, &sp);
  208. sp.numbytes = 0;
  209. if (!tar_sparse_scan (file, scan_block, NULL))
  210. return false;
  211. }
  212. }
  213. else
  214. {
  215. if (sp.numbytes == 0)
  216. sp.offset = offset;
  217. sp.numbytes += count;
  218. st->archive_file_size += count;
  219. if (!tar_sparse_scan (file, scan_block, buffer))
  220. return false;
  221. }
  222. offset += count;
  223. }
  224. }
  225. if (sp.numbytes == 0)
  226. sp.offset = offset;
  227. sparse_add_map (st, &sp);
  228. st->archive_file_size += count;
  229. return tar_sparse_scan (file, scan_end, NULL);
  230. }
  231. static struct tar_sparse_optab const oldgnu_optab;
  232. static struct tar_sparse_optab const star_optab;
  233. static struct tar_sparse_optab const pax_optab;
  234. static bool
  235. sparse_select_optab (struct tar_sparse_file *file)
  236. {
  237. switch (current_format == DEFAULT_FORMAT ? archive_format : current_format)
  238. {
  239. case V7_FORMAT:
  240. case USTAR_FORMAT:
  241. return false;
  242. case OLDGNU_FORMAT:
  243. case GNU_FORMAT: /*FIXME: This one should disappear? */
  244. file->optab = &oldgnu_optab;
  245. break;
  246. case POSIX_FORMAT:
  247. file->optab = &pax_optab;
  248. break;
  249. case STAR_FORMAT:
  250. file->optab = &star_optab;
  251. break;
  252. default:
  253. return false;
  254. }
  255. return true;
  256. }
  257. static bool
  258. sparse_dump_region (struct tar_sparse_file *file, size_t i)
  259. {
  260. union block *blk;
  261. off_t bytes_left = file->stat_info->sparse_map[i].numbytes;
  262. if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset))
  263. return false;
  264. while (bytes_left > 0)
  265. {
  266. size_t bufsize = (bytes_left > BLOCKSIZE) ? BLOCKSIZE : bytes_left;
  267. size_t bytes_read;
  268. blk = find_next_block ();
  269. bytes_read = safe_read (file->fd, blk->buffer, bufsize);
  270. if (bytes_read == SAFE_READ_ERROR)
  271. {
  272. read_diag_details (file->stat_info->orig_file_name,
  273. (file->stat_info->sparse_map[i].offset
  274. + file->stat_info->sparse_map[i].numbytes
  275. - bytes_left),
  276. bufsize);
  277. return false;
  278. }
  279. memset (blk->buffer + bytes_read, 0, BLOCKSIZE - bytes_read);
  280. bytes_left -= bytes_read;
  281. file->dumped_size += bytes_read;
  282. set_next_block_after (blk);
  283. }
  284. return true;
  285. }
  286. static bool
  287. sparse_extract_region (struct tar_sparse_file *file, size_t i)
  288. {
  289. off_t write_size;
  290. if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset))
  291. return false;
  292. write_size = file->stat_info->sparse_map[i].numbytes;
  293. if (write_size == 0)
  294. {
  295. /* Last block of the file is a hole */
  296. if (file->seekable && sys_truncate (file->fd))
  297. truncate_warn (file->stat_info->orig_file_name);
  298. }
  299. else while (write_size > 0)
  300. {
  301. size_t count;
  302. size_t wrbytes = (write_size > BLOCKSIZE) ? BLOCKSIZE : write_size;
  303. union block *blk = find_next_block ();
  304. if (!blk)
  305. {
  306. ERROR ((0, 0, _("Unexpected EOF in archive")));
  307. return false;
  308. }
  309. set_next_block_after (blk);
  310. count = blocking_write (file->fd, blk->buffer, wrbytes);
  311. write_size -= count;
  312. file->dumped_size += count;
  313. mv_size_left (file->stat_info->archive_file_size - file->dumped_size);
  314. file->offset += count;
  315. if (count != wrbytes)
  316. {
  317. write_error_details (file->stat_info->orig_file_name,
  318. count, wrbytes);
  319. return false;
  320. }
  321. }
  322. return true;
  323. }
  324. /* Interface functions */
  325. enum dump_status
  326. sparse_dump_file (int fd, struct tar_stat_info *st)
  327. {
  328. bool rc;
  329. struct tar_sparse_file file;
  330. if (!tar_sparse_init (&file))
  331. return dump_status_not_implemented;
  332. file.stat_info = st;
  333. file.fd = fd;
  334. file.seekable = true; /* File *must* be seekable for dump to work */
  335. rc = sparse_scan_file (&file);
  336. if (rc && file.optab->dump_region)
  337. {
  338. tar_sparse_dump_header (&file);
  339. if (fd >= 0)
  340. {
  341. size_t i;
  342. mv_begin_write (file.stat_info->file_name,
  343. file.stat_info->stat.st_size,
  344. file.stat_info->archive_file_size - file.dumped_size);
  345. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  346. rc = tar_sparse_dump_region (&file, i);
  347. }
  348. }
  349. pad_archive (file.stat_info->archive_file_size - file.dumped_size);
  350. return (tar_sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
  351. }
  352. bool
  353. sparse_member_p (struct tar_stat_info *st)
  354. {
  355. struct tar_sparse_file file;
  356. if (!tar_sparse_init (&file))
  357. return false;
  358. file.stat_info = st;
  359. return tar_sparse_member_p (&file);
  360. }
  361. bool
  362. sparse_fixup_header (struct tar_stat_info *st)
  363. {
  364. struct tar_sparse_file file;
  365. if (!tar_sparse_init (&file))
  366. return false;
  367. file.stat_info = st;
  368. return tar_sparse_fixup_header (&file);
  369. }
  370. enum dump_status
  371. sparse_extract_file (int fd, struct tar_stat_info *st, off_t *size)
  372. {
  373. bool rc = true;
  374. struct tar_sparse_file file;
  375. size_t i;
  376. if (!tar_sparse_init (&file))
  377. return dump_status_not_implemented;
  378. file.stat_info = st;
  379. file.fd = fd;
  380. file.seekable = lseek (fd, 0, SEEK_SET) == 0;
  381. file.offset = 0;
  382. rc = tar_sparse_decode_header (&file);
  383. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  384. rc = tar_sparse_extract_region (&file, i);
  385. *size = file.stat_info->archive_file_size - file.dumped_size;
  386. return (tar_sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
  387. }
  388. enum dump_status
  389. sparse_skip_file (struct tar_stat_info *st)
  390. {
  391. bool rc = true;
  392. struct tar_sparse_file file;
  393. if (!tar_sparse_init (&file))
  394. return dump_status_not_implemented;
  395. file.stat_info = st;
  396. file.fd = -1;
  397. rc = tar_sparse_decode_header (&file);
  398. skip_file (file.stat_info->archive_file_size - file.dumped_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. off_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. mv_size_left (file->stat_info->archive_file_size - file->dumped_size);
  439. while (size_left > 0)
  440. {
  441. size_t bytes_read;
  442. size_t rdsize = (size_left > BLOCKSIZE) ? BLOCKSIZE : size_left;
  443. char diff_buffer[BLOCKSIZE];
  444. union block *blk = find_next_block ();
  445. if (!blk)
  446. {
  447. ERROR ((0, 0, _("Unexpected EOF in archive")));
  448. return false;
  449. }
  450. set_next_block_after (blk);
  451. bytes_read = safe_read (file->fd, diff_buffer, rdsize);
  452. if (bytes_read == SAFE_READ_ERROR)
  453. {
  454. read_diag_details (file->stat_info->orig_file_name,
  455. (file->stat_info->sparse_map[i].offset
  456. + file->stat_info->sparse_map[i].numbytes
  457. - size_left),
  458. rdsize);
  459. return false;
  460. }
  461. file->dumped_size += bytes_read;
  462. size_left -= bytes_read;
  463. mv_size_left (file->stat_info->archive_file_size - file->dumped_size);
  464. if (memcmp (blk->buffer, diff_buffer, rdsize))
  465. {
  466. report_difference (file->stat_info, _("Contents differ"));
  467. return false;
  468. }
  469. }
  470. return true;
  471. }
  472. bool
  473. sparse_diff_file (int fd, struct tar_stat_info *st)
  474. {
  475. bool rc = true;
  476. struct tar_sparse_file file;
  477. size_t i;
  478. off_t offset = 0;
  479. if (!tar_sparse_init (&file))
  480. return dump_status_not_implemented;
  481. file.stat_info = st;
  482. file.fd = fd;
  483. file.seekable = true; /* File *must* be seekable for compare to work */
  484. rc = tar_sparse_decode_header (&file);
  485. mv_begin_read (st);
  486. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  487. {
  488. rc = check_sparse_region (&file,
  489. offset, file.stat_info->sparse_map[i].offset)
  490. && check_data_region (&file, i);
  491. offset = file.stat_info->sparse_map[i].offset
  492. + file.stat_info->sparse_map[i].numbytes;
  493. }
  494. if (!rc)
  495. skip_file (file.stat_info->archive_file_size - file.dumped_size);
  496. mv_end ();
  497. tar_sparse_done (&file);
  498. return rc;
  499. }
  500. /* Old GNU Format. The sparse file information is stored in the
  501. oldgnu_header in the following manner:
  502. The header is marked with type 'S'. Its 'size' field contains
  503. the cumulative size of all non-empty blocks of the file. The
  504. actual file size is stored in 'realsize' member of oldgnu_header.
  505. The map of the file is stored in a list of 'struct sparse'.
  506. Each struct contains offset to the block of data and its
  507. size (both as octal numbers). The first file header contains
  508. at most 4 such structs (SPARSES_IN_OLDGNU_HEADER). If the map
  509. contains more structs, then the field 'isextended' of the main
  510. header is set to 1 (binary) and the 'struct sparse_header'
  511. header follows, containing at most 21 following structs
  512. (SPARSES_IN_SPARSE_HEADER). If more structs follow, 'isextended'
  513. field of the extended header is set and next next extension header
  514. follows, etc... */
  515. enum oldgnu_add_status
  516. {
  517. add_ok,
  518. add_finish,
  519. add_fail
  520. };
  521. static bool
  522. oldgnu_sparse_member_p (struct tar_sparse_file *file __attribute__ ((unused)))
  523. {
  524. return current_header->header.typeflag == GNUTYPE_SPARSE;
  525. }
  526. /* Add a sparse item to the sparse file and its obstack */
  527. static enum oldgnu_add_status
  528. oldgnu_add_sparse (struct tar_sparse_file *file, struct sparse *s)
  529. {
  530. struct sp_array sp;
  531. if (s->numbytes[0] == '\0')
  532. return add_finish;
  533. sp.offset = OFF_FROM_HEADER (s->offset);
  534. sp.numbytes = OFF_FROM_HEADER (s->numbytes);
  535. if (sp.offset < 0
  536. || sp.offset + sp.numbytes < 0
  537. || file->stat_info->stat.st_size < sp.offset + sp.numbytes
  538. || file->stat_info->archive_file_size < 0)
  539. return add_fail;
  540. sparse_add_map (file->stat_info, &sp);
  541. return add_ok;
  542. }
  543. static bool
  544. oldgnu_fixup_header (struct tar_sparse_file *file)
  545. {
  546. /* NOTE! st_size was initialized from the header
  547. which actually contains archived size. The following fixes it */
  548. file->stat_info->archive_file_size = file->stat_info->stat.st_size;
  549. file->stat_info->stat.st_size =
  550. OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
  551. return true;
  552. }
  553. /* Convert old GNU format sparse data to internal representation */
  554. static bool
  555. oldgnu_get_sparse_info (struct tar_sparse_file *file)
  556. {
  557. size_t i;
  558. union block *h = current_header;
  559. int ext_p;
  560. enum oldgnu_add_status rc;
  561. file->stat_info->sparse_map_avail = 0;
  562. for (i = 0; i < SPARSES_IN_OLDGNU_HEADER; i++)
  563. {
  564. rc = oldgnu_add_sparse (file, &h->oldgnu_header.sp[i]);
  565. if (rc != add_ok)
  566. break;
  567. }
  568. for (ext_p = h->oldgnu_header.isextended;
  569. rc == add_ok && ext_p; ext_p = h->sparse_header.isextended)
  570. {
  571. h = find_next_block ();
  572. if (!h)
  573. {
  574. ERROR ((0, 0, _("Unexpected EOF in archive")));
  575. return false;
  576. }
  577. set_next_block_after (h);
  578. for (i = 0; i < SPARSES_IN_SPARSE_HEADER && rc == add_ok; i++)
  579. rc = oldgnu_add_sparse (file, &h->sparse_header.sp[i]);
  580. }
  581. if (rc == add_fail)
  582. {
  583. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  584. file->stat_info->orig_file_name));
  585. return false;
  586. }
  587. return true;
  588. }
  589. static void
  590. oldgnu_store_sparse_info (struct tar_sparse_file *file, size_t *pindex,
  591. struct sparse *sp, size_t sparse_size)
  592. {
  593. for (; *pindex < file->stat_info->sparse_map_avail
  594. && sparse_size > 0; sparse_size--, sp++, ++*pindex)
  595. {
  596. OFF_TO_CHARS (file->stat_info->sparse_map[*pindex].offset,
  597. sp->offset);
  598. OFF_TO_CHARS (file->stat_info->sparse_map[*pindex].numbytes,
  599. sp->numbytes);
  600. }
  601. }
  602. static bool
  603. oldgnu_dump_header (struct tar_sparse_file *file)
  604. {
  605. off_t block_ordinal = current_block_ordinal ();
  606. union block *blk;
  607. size_t i;
  608. blk = start_header (file->stat_info);
  609. blk->header.typeflag = GNUTYPE_SPARSE;
  610. if (file->stat_info->sparse_map_avail > SPARSES_IN_OLDGNU_HEADER)
  611. blk->oldgnu_header.isextended = 1;
  612. /* Store the real file size */
  613. OFF_TO_CHARS (file->stat_info->stat.st_size, blk->oldgnu_header.realsize);
  614. /* Store the effective (shrunken) file size */
  615. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  616. i = 0;
  617. oldgnu_store_sparse_info (file, &i,
  618. blk->oldgnu_header.sp,
  619. SPARSES_IN_OLDGNU_HEADER);
  620. blk->oldgnu_header.isextended = i < file->stat_info->sparse_map_avail;
  621. finish_header (file->stat_info, blk, block_ordinal);
  622. while (i < file->stat_info->sparse_map_avail)
  623. {
  624. blk = find_next_block ();
  625. memset (blk->buffer, 0, BLOCKSIZE);
  626. oldgnu_store_sparse_info (file, &i,
  627. blk->sparse_header.sp,
  628. SPARSES_IN_SPARSE_HEADER);
  629. if (i < file->stat_info->sparse_map_avail)
  630. blk->sparse_header.isextended = 1;
  631. set_next_block_after (blk);
  632. }
  633. return true;
  634. }
  635. static struct tar_sparse_optab const oldgnu_optab = {
  636. NULL, /* No init function */
  637. NULL, /* No done function */
  638. oldgnu_sparse_member_p,
  639. oldgnu_dump_header,
  640. oldgnu_fixup_header,
  641. oldgnu_get_sparse_info,
  642. NULL, /* No scan_block function */
  643. sparse_dump_region,
  644. sparse_extract_region,
  645. };
  646. /* Star */
  647. static bool
  648. star_sparse_member_p (struct tar_sparse_file *file __attribute__ ((unused)))
  649. {
  650. return current_header->header.typeflag == GNUTYPE_SPARSE;
  651. }
  652. static bool
  653. star_fixup_header (struct tar_sparse_file *file)
  654. {
  655. /* NOTE! st_size was initialized from the header
  656. which actually contains archived size. The following fixes it */
  657. file->stat_info->archive_file_size = file->stat_info->stat.st_size;
  658. file->stat_info->stat.st_size =
  659. OFF_FROM_HEADER (current_header->star_in_header.realsize);
  660. return true;
  661. }
  662. /* Convert STAR format sparse data to internal representation */
  663. static bool
  664. star_get_sparse_info (struct tar_sparse_file *file)
  665. {
  666. size_t i;
  667. union block *h = current_header;
  668. int ext_p;
  669. enum oldgnu_add_status rc = add_ok;
  670. file->stat_info->sparse_map_avail = 0;
  671. if (h->star_in_header.prefix[0] == '\0'
  672. && h->star_in_header.sp[0].offset[10] != '\0')
  673. {
  674. /* Old star format */
  675. for (i = 0; i < SPARSES_IN_STAR_HEADER; i++)
  676. {
  677. rc = oldgnu_add_sparse (file, &h->star_in_header.sp[i]);
  678. if (rc != add_ok)
  679. break;
  680. }
  681. ext_p = h->star_in_header.isextended;
  682. }
  683. else
  684. ext_p = 1;
  685. for (; rc == add_ok && ext_p; ext_p = h->star_ext_header.isextended)
  686. {
  687. h = find_next_block ();
  688. if (!h)
  689. {
  690. ERROR ((0, 0, _("Unexpected EOF in archive")));
  691. return false;
  692. }
  693. set_next_block_after (h);
  694. for (i = 0; i < SPARSES_IN_STAR_EXT_HEADER && rc == add_ok; i++)
  695. rc = oldgnu_add_sparse (file, &h->star_ext_header.sp[i]);
  696. }
  697. if (rc == add_fail)
  698. {
  699. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  700. file->stat_info->orig_file_name));
  701. return false;
  702. }
  703. return true;
  704. }
  705. static struct tar_sparse_optab const star_optab = {
  706. NULL, /* No init function */
  707. NULL, /* No done function */
  708. star_sparse_member_p,
  709. NULL,
  710. star_fixup_header,
  711. star_get_sparse_info,
  712. NULL, /* No scan_block function */
  713. NULL, /* No dump region function */
  714. sparse_extract_region,
  715. };
  716. /* GNU PAX sparse file format. There are several versions:
  717. * 0.0
  718. The initial version of sparse format used by tar 1.14-1.15.1.
  719. The sparse file map is stored in x header:
  720. GNU.sparse.size Real size of the stored file
  721. GNU.sparse.numblocks Number of blocks in the sparse map
  722. repeat numblocks time
  723. GNU.sparse.offset Offset of the next data block
  724. GNU.sparse.numbytes Size of the next data block
  725. end repeat
  726. This has been reported as conflicting with the POSIX specs. The reason is
  727. that offsets and sizes of non-zero data blocks were stored in multiple
  728. instances of GNU.sparse.offset/GNU.sparse.numbytes variables, whereas
  729. POSIX requires the latest occurrence of the variable to override all
  730. previous occurrences.
  731. To avoid this incompatibility two following versions were introduced.
  732. * 0.1
  733. Used by tar 1.15.2 -- 1.15.91 (alpha releases).
  734. The sparse file map is stored in
  735. x header:
  736. GNU.sparse.size Real size of the stored file
  737. GNU.sparse.numblocks Number of blocks in the sparse map
  738. GNU.sparse.map Map of non-null data chunks. A string consisting
  739. of comma-separated values "offset,size[,offset,size]..."
  740. The resulting GNU.sparse.map string can be *very* long. While POSIX does not
  741. impose any limit on the length of a x header variable, this can confuse some
  742. tars.
  743. * 1.0
  744. Starting from this version, the exact sparse format version is specified
  745. explicitely in the header using the following variables:
  746. GNU.sparse.major Major version
  747. GNU.sparse.minor Minor version
  748. X header keeps the following variables:
  749. GNU.sparse.name Real file name of the sparse file
  750. GNU.sparse.realsize Real size of the stored file (corresponds to the old
  751. GNU.sparse.size variable)
  752. The name field of the ustar header is constructed using the pattern
  753. "%d/GNUSparseFile.%p/%f".
  754. The sparse map itself is stored in the file data block, preceding the actual
  755. file data. It consists of a series of octal numbers of arbitrary length,
  756. delimited by newlines. The map is padded with nulls to the nearest block
  757. boundary.
  758. The first number gives the number of entries in the map. Following are map
  759. entries, each one consisting of two numbers giving the offset and size of
  760. the data block it describes.
  761. The format is designed in such a way that non-posix aware tars and tars not
  762. supporting GNU.sparse.* keywords will extract each sparse file in its
  763. condensed form with the file map attached and will place it into a separate
  764. directory. Then, using a simple program it would be possible to expand the
  765. file to its original form even without GNU tar.
  766. Bu default, v.1.0 archives are created. To use other formats,
  767. --sparse-version option is provided. Additionally, v.0.0 can be obtained
  768. by deleting GNU.sparse.map from 0.1 format: --sparse-version 0.1
  769. --pax-option delete=GNU.sparse.map
  770. */
  771. static bool
  772. pax_sparse_member_p (struct tar_sparse_file *file)
  773. {
  774. return file->stat_info->sparse_map_avail > 0
  775. || file->stat_info->sparse_major > 0;
  776. }
  777. static bool
  778. pax_dump_header_0 (struct tar_sparse_file *file)
  779. {
  780. off_t block_ordinal = current_block_ordinal ();
  781. union block *blk;
  782. size_t i;
  783. char nbuf[UINTMAX_STRSIZE_BOUND];
  784. struct sp_array *map = file->stat_info->sparse_map;
  785. char *save_file_name = NULL;
  786. /* Store the real file size */
  787. xheader_store ("GNU.sparse.size", file->stat_info, NULL);
  788. xheader_store ("GNU.sparse.numblocks", file->stat_info, NULL);
  789. if (xheader_keyword_deleted_p ("GNU.sparse.map")
  790. || tar_sparse_minor == 0)
  791. {
  792. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  793. {
  794. xheader_store ("GNU.sparse.offset", file->stat_info, &i);
  795. xheader_store ("GNU.sparse.numbytes", file->stat_info, &i);
  796. }
  797. }
  798. else
  799. {
  800. xheader_store ("GNU.sparse.name", file->stat_info, NULL);
  801. save_file_name = file->stat_info->file_name;
  802. file->stat_info->file_name = xheader_format_name (file->stat_info,
  803. "%d/GNUSparseFile.%p/%f", 0);
  804. xheader_string_begin (&file->stat_info->xhdr);
  805. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  806. {
  807. if (i)
  808. xheader_string_add (&file->stat_info->xhdr, ",");
  809. xheader_string_add (&file->stat_info->xhdr,
  810. umaxtostr (map[i].offset, nbuf));
  811. xheader_string_add (&file->stat_info->xhdr, ",");
  812. xheader_string_add (&file->stat_info->xhdr,
  813. umaxtostr (map[i].numbytes, nbuf));
  814. }
  815. if (!xheader_string_end (&file->stat_info->xhdr,
  816. "GNU.sparse.map"))
  817. {
  818. free (file->stat_info->file_name);
  819. file->stat_info->file_name = save_file_name;
  820. return false;
  821. }
  822. }
  823. blk = start_header (file->stat_info);
  824. /* Store the effective (shrunken) file size */
  825. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  826. finish_header (file->stat_info, blk, block_ordinal);
  827. if (save_file_name)
  828. {
  829. free (file->stat_info->file_name);
  830. file->stat_info->file_name = save_file_name;
  831. }
  832. return true;
  833. }
  834. static bool
  835. pax_dump_header_1 (struct tar_sparse_file *file)
  836. {
  837. off_t block_ordinal = current_block_ordinal ();
  838. union block *blk;
  839. char *p, *q;
  840. size_t i;
  841. char nbuf[UINTMAX_STRSIZE_BOUND];
  842. off_t size = 0;
  843. struct sp_array *map = file->stat_info->sparse_map;
  844. char *save_file_name = file->stat_info->file_name;
  845. #define COPY_STRING(b,dst,src) do \
  846. { \
  847. char *endp = b->buffer + BLOCKSIZE; \
  848. char const *srcp = src; \
  849. while (*srcp) \
  850. { \
  851. if (dst == endp) \
  852. { \
  853. set_next_block_after (b); \
  854. b = find_next_block (); \
  855. dst = b->buffer; \
  856. endp = b->buffer + BLOCKSIZE; \
  857. } \
  858. *dst++ = *srcp++; \
  859. } \
  860. } while (0)
  861. /* Compute stored file size */
  862. p = umaxtostr (file->stat_info->sparse_map_avail, nbuf);
  863. size += strlen (p) + 1;
  864. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  865. {
  866. p = umaxtostr (map[i].offset, nbuf);
  867. size += strlen (p) + 1;
  868. p = umaxtostr (map[i].numbytes, nbuf);
  869. size += strlen (p) + 1;
  870. }
  871. size = (size + BLOCKSIZE - 1) / BLOCKSIZE;
  872. file->stat_info->archive_file_size += size * BLOCKSIZE;
  873. file->dumped_size += size * BLOCKSIZE;
  874. /* Store sparse file identification */
  875. xheader_store ("GNU.sparse.major", file->stat_info, NULL);
  876. xheader_store ("GNU.sparse.minor", file->stat_info, NULL);
  877. xheader_store ("GNU.sparse.name", file->stat_info, NULL);
  878. xheader_store ("GNU.sparse.realsize", file->stat_info, NULL);
  879. file->stat_info->file_name =
  880. xheader_format_name (file->stat_info, "%d/GNUSparseFile.%p/%f", 0);
  881. /* Make sure the created header name is shorter than NAME_FIELD_SIZE: */
  882. if (strlen (file->stat_info->file_name) > NAME_FIELD_SIZE)
  883. file->stat_info->file_name[NAME_FIELD_SIZE] = 0;
  884. blk = start_header (file->stat_info);
  885. /* Store the effective (shrunken) file size */
  886. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  887. finish_header (file->stat_info, blk, block_ordinal);
  888. free (file->stat_info->file_name);
  889. file->stat_info->file_name = save_file_name;
  890. blk = find_next_block ();
  891. q = blk->buffer;
  892. p = umaxtostr (file->stat_info->sparse_map_avail, nbuf);
  893. COPY_STRING (blk, q, p);
  894. COPY_STRING (blk, q, "\n");
  895. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  896. {
  897. p = umaxtostr (map[i].offset, nbuf);
  898. COPY_STRING (blk, q, p);
  899. COPY_STRING (blk, q, "\n");
  900. p = umaxtostr (map[i].numbytes, nbuf);
  901. COPY_STRING (blk, q, p);
  902. COPY_STRING (blk, q, "\n");
  903. }
  904. memset (q, 0, BLOCKSIZE - (q - blk->buffer));
  905. set_next_block_after (blk);
  906. return true;
  907. }
  908. static bool
  909. pax_dump_header (struct tar_sparse_file *file)
  910. {
  911. file->stat_info->sparse_major = tar_sparse_major;
  912. file->stat_info->sparse_minor = tar_sparse_minor;
  913. return (file->stat_info->sparse_major == 0) ?
  914. pax_dump_header_0 (file) : pax_dump_header_1 (file);
  915. }
  916. static bool
  917. decode_num (uintmax_t *num, char const *arg, uintmax_t maxval)
  918. {
  919. uintmax_t u;
  920. char *arg_lim;
  921. if (!ISDIGIT (*arg))
  922. return false;
  923. u = strtoumax (arg, &arg_lim, 10);
  924. if (! (u <= maxval && errno != ERANGE) || *arg_lim)
  925. return false;
  926. *num = u;
  927. return true;
  928. }
  929. static bool
  930. pax_decode_header (struct tar_sparse_file *file)
  931. {
  932. if (file->stat_info->sparse_major > 0)
  933. {
  934. uintmax_t u;
  935. char nbuf[UINTMAX_STRSIZE_BOUND];
  936. union block *blk;
  937. char *p;
  938. size_t i;
  939. #define COPY_BUF(b,buf,src) do \
  940. { \
  941. char *endp = b->buffer + BLOCKSIZE; \
  942. char *dst = buf; \
  943. do \
  944. { \
  945. if (dst == buf + UINTMAX_STRSIZE_BOUND -1) \
  946. { \
  947. ERROR ((0, 0, _("%s: numeric overflow in sparse archive member"), \
  948. file->stat_info->orig_file_name)); \
  949. return false; \
  950. } \
  951. if (src == endp) \
  952. { \
  953. set_next_block_after (b); \
  954. file->dumped_size += BLOCKSIZE; \
  955. b = find_next_block (); \
  956. src = b->buffer; \
  957. endp = b->buffer + BLOCKSIZE; \
  958. } \
  959. *dst = *src++; \
  960. } \
  961. while (*dst++ != '\n'); \
  962. dst[-1] = 0; \
  963. } while (0)
  964. set_next_block_after (current_header);
  965. file->dumped_size += BLOCKSIZE;
  966. blk = find_next_block ();
  967. p = blk->buffer;
  968. COPY_BUF (blk,nbuf,p);
  969. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)))
  970. {
  971. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  972. file->stat_info->orig_file_name));
  973. return false;
  974. }
  975. file->stat_info->sparse_map_size = u;
  976. file->stat_info->sparse_map = xcalloc (file->stat_info->sparse_map_size,
  977. sizeof (*file->stat_info->sparse_map));
  978. file->stat_info->sparse_map_avail = 0;
  979. for (i = 0; i < file->stat_info->sparse_map_size; i++)
  980. {
  981. struct sp_array sp;
  982. COPY_BUF (blk,nbuf,p);
  983. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (off_t)))
  984. {
  985. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  986. file->stat_info->orig_file_name));
  987. return false;
  988. }
  989. sp.offset = u;
  990. COPY_BUF (blk,nbuf,p);
  991. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (off_t)))
  992. {
  993. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  994. file->stat_info->orig_file_name));
  995. return false;
  996. }
  997. sp.numbytes = u;
  998. sparse_add_map (file->stat_info, &sp);
  999. }
  1000. set_next_block_after (blk);
  1001. }
  1002. return true;
  1003. }
  1004. static struct tar_sparse_optab const pax_optab = {
  1005. NULL, /* No init function */
  1006. NULL, /* No done function */
  1007. pax_sparse_member_p,
  1008. pax_dump_header,
  1009. NULL,
  1010. pax_decode_header,
  1011. NULL, /* No scan_block function */
  1012. sparse_dump_region,
  1013. sparse_extract_region,
  1014. };