sparse.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /* Functions for dealing with sparse files
  2. Copyright (C) 2003, 2004, 2005, 2006 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. if (i < file->stat_info->sparse_map_avail)
  633. blk->sparse_header.isextended = 1;
  634. set_next_block_after (blk);
  635. }
  636. return true;
  637. }
  638. static struct tar_sparse_optab const oldgnu_optab = {
  639. NULL, /* No init function */
  640. NULL, /* No done function */
  641. oldgnu_sparse_member_p,
  642. oldgnu_dump_header,
  643. oldgnu_fixup_header,
  644. oldgnu_get_sparse_info,
  645. NULL, /* No scan_block function */
  646. sparse_dump_region,
  647. sparse_extract_region,
  648. };
  649. /* Star */
  650. static bool
  651. star_sparse_member_p (struct tar_sparse_file *file __attribute__ ((unused)))
  652. {
  653. return current_header->header.typeflag == GNUTYPE_SPARSE;
  654. }
  655. static bool
  656. star_fixup_header (struct tar_sparse_file *file)
  657. {
  658. /* NOTE! st_size was initialized from the header
  659. which actually contains archived size. The following fixes it */
  660. file->stat_info->archive_file_size = file->stat_info->stat.st_size;
  661. file->stat_info->stat.st_size =
  662. OFF_FROM_HEADER (current_header->star_in_header.realsize);
  663. return true;
  664. }
  665. /* Convert STAR format sparse data to internal representation */
  666. static bool
  667. star_get_sparse_info (struct tar_sparse_file *file)
  668. {
  669. size_t i;
  670. union block *h = current_header;
  671. int ext_p;
  672. enum oldgnu_add_status rc = add_ok;
  673. file->stat_info->sparse_map_avail = 0;
  674. if (h->star_in_header.prefix[0] == '\0'
  675. && h->star_in_header.sp[0].offset[10] != '\0')
  676. {
  677. /* Old star format */
  678. for (i = 0; i < SPARSES_IN_STAR_HEADER; i++)
  679. {
  680. rc = oldgnu_add_sparse (file, &h->star_in_header.sp[i]);
  681. if (rc != add_ok)
  682. break;
  683. }
  684. ext_p = h->star_in_header.isextended;
  685. }
  686. else
  687. ext_p = 1;
  688. for (; rc == add_ok && ext_p; ext_p = h->star_ext_header.isextended)
  689. {
  690. h = find_next_block ();
  691. if (!h)
  692. {
  693. ERROR ((0, 0, _("Unexpected EOF in archive")));
  694. return false;
  695. }
  696. set_next_block_after (h);
  697. for (i = 0; i < SPARSES_IN_STAR_EXT_HEADER && rc == add_ok; i++)
  698. rc = oldgnu_add_sparse (file, &h->star_ext_header.sp[i]);
  699. }
  700. if (rc == add_fail)
  701. {
  702. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  703. file->stat_info->orig_file_name));
  704. return false;
  705. }
  706. return true;
  707. }
  708. static struct tar_sparse_optab const star_optab = {
  709. NULL, /* No init function */
  710. NULL, /* No done function */
  711. star_sparse_member_p,
  712. NULL,
  713. star_fixup_header,
  714. star_get_sparse_info,
  715. NULL, /* No scan_block function */
  716. NULL, /* No dump region function */
  717. sparse_extract_region,
  718. };
  719. /* GNU PAX sparse file format. There are several versions:
  720. * 0.0
  721. The initial version of sparse format used by tar 1.14-1.15.1.
  722. The sparse file map is stored in x header:
  723. GNU.sparse.size Real size of the stored file
  724. GNU.sparse.numblocks Number of blocks in the sparse map
  725. repeat numblocks time
  726. GNU.sparse.offset Offset of the next data block
  727. GNU.sparse.numbytes Size of the next data block
  728. end repeat
  729. This has been reported as conflicting with the POSIX specs. The reason is
  730. that offsets and sizes of non-zero data blocks were stored in multiple
  731. instances of GNU.sparse.offset/GNU.sparse.numbytes variables, whereas
  732. POSIX requires the latest occurrence of the variable to override all
  733. previous occurrences.
  734. To avoid this incompatibility two following versions were introduced.
  735. * 0.1
  736. Used by tar 1.15.2 -- 1.15.91 (alpha releases).
  737. The sparse file map is stored in
  738. x header:
  739. GNU.sparse.size Real size of the stored file
  740. GNU.sparse.numblocks Number of blocks in the sparse map
  741. GNU.sparse.map Map of non-null data chunks. A string consisting
  742. of comma-separated values "offset,size[,offset,size]..."
  743. The resulting GNU.sparse.map string can be *very* long. While POSIX does not
  744. impose any limit on the length of a x header variable, this can confuse some
  745. tars.
  746. * 1.0
  747. Starting from this version, the exact sparse format version is specified explicitely
  748. in the header using the following variables:
  749. GNU.sparse.major Major version
  750. GNU.sparse.minor Minor version
  751. X header keeps the following variables:
  752. GNU.sparse.name Real file name of the sparse file
  753. GNU.sparse.realsize Real size of the stored file (corresponds to the old
  754. GNU.sparse.size variable)
  755. The name field of the ustar header is constructed using the pattern
  756. "%d/GNUSparseFile.%p/%f".
  757. The sparse map itself is stored in the file data block, preceding the actual
  758. file data. It consists of a series of octal numbers of arbitrary length, delimited
  759. by newlines. The map is padded with nulls to the nearest block boundary.
  760. The first number gives the number of entries in the map. Following are map entries,
  761. each one consisting of two numbers giving the offset and size of the
  762. data block it describes.
  763. The format is designed in such a way that non-posix aware tars and tars not
  764. supporting GNU.sparse.* keywords will extract each sparse file in its condensed
  765. form with the file map attached and will place it into a separate directory.
  766. Then, using a simple program it would be possible to expand the file to its
  767. original form even without GNU tar.
  768. Bu default, v.1.0 archives are created. To use other formats, --sparse-version
  769. option is provided. Additionally, v.0.0 can be obtained by deleting GNU.sparse.map
  770. from 0.1 format: --sparse-version 0.1 --pax-option delete=GNU.sparse.map
  771. */
  772. static bool
  773. pax_sparse_member_p (struct tar_sparse_file *file)
  774. {
  775. return file->stat_info->sparse_map_avail > 0
  776. || file->stat_info->sparse_major > 0;
  777. }
  778. static bool
  779. pax_dump_header_0 (struct tar_sparse_file *file)
  780. {
  781. off_t block_ordinal = current_block_ordinal ();
  782. union block *blk;
  783. size_t i;
  784. char nbuf[UINTMAX_STRSIZE_BOUND];
  785. struct sp_array *map = file->stat_info->sparse_map;
  786. char *save_file_name = NULL;
  787. /* Store the real file size */
  788. xheader_store ("GNU.sparse.size", file->stat_info, NULL);
  789. xheader_store ("GNU.sparse.numblocks", file->stat_info, NULL);
  790. if (xheader_keyword_deleted_p ("GNU.sparse.map")
  791. || tar_sparse_minor == 0)
  792. {
  793. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  794. {
  795. xheader_store ("GNU.sparse.offset", file->stat_info, &i);
  796. xheader_store ("GNU.sparse.numbytes", file->stat_info, &i);
  797. }
  798. }
  799. else
  800. {
  801. xheader_store ("GNU.sparse.name", file->stat_info, NULL);
  802. save_file_name = file->stat_info->file_name;
  803. file->stat_info->file_name = xheader_format_name (file->stat_info,
  804. "%d/GNUSparseFile.%p/%f", 0);
  805. xheader_string_begin ();
  806. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  807. {
  808. if (i)
  809. xheader_string_add (",");
  810. xheader_string_add (umaxtostr (map[i].offset, nbuf));
  811. xheader_string_add (",");
  812. xheader_string_add (umaxtostr (map[i].numbytes, nbuf));
  813. }
  814. xheader_string_end ("GNU.sparse.map");
  815. }
  816. blk = start_header (file->stat_info);
  817. /* Store the effective (shrunken) file size */
  818. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  819. finish_header (file->stat_info, blk, block_ordinal);
  820. if (save_file_name)
  821. {
  822. free (file->stat_info->file_name);
  823. file->stat_info->file_name = save_file_name;
  824. }
  825. return true;
  826. }
  827. static bool
  828. pax_dump_header_1 (struct tar_sparse_file *file)
  829. {
  830. off_t block_ordinal = current_block_ordinal ();
  831. union block *blk;
  832. char *p, *q;
  833. size_t i;
  834. char nbuf[UINTMAX_STRSIZE_BOUND];
  835. off_t size = 0;
  836. struct sp_array *map = file->stat_info->sparse_map;
  837. char *save_file_name = file->stat_info->file_name;
  838. #define COPY_STRING(b,dst,src) do \
  839. { \
  840. char *endp = b->buffer + BLOCKSIZE; \
  841. char *srcp = src; \
  842. while (*srcp) \
  843. { \
  844. if (dst == endp) \
  845. { \
  846. set_next_block_after (b); \
  847. b = find_next_block (); \
  848. dst = b->buffer; \
  849. endp = b->buffer + BLOCKSIZE; \
  850. } \
  851. *dst++ = *srcp++; \
  852. } \
  853. } while (0)
  854. /* Compute stored file size */
  855. p = umaxtostr (file->stat_info->sparse_map_avail, nbuf);
  856. size += strlen (p) + 1;
  857. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  858. {
  859. p = umaxtostr (map[i].offset, nbuf);
  860. size += strlen (p) + 1;
  861. p = umaxtostr (map[i].numbytes, nbuf);
  862. size += strlen (p) + 1;
  863. }
  864. size = (size + BLOCKSIZE - 1) / BLOCKSIZE;
  865. file->stat_info->archive_file_size += size * BLOCKSIZE;
  866. /* Store sparse file identification */
  867. xheader_store ("GNU.sparse.major", file->stat_info, NULL);
  868. xheader_store ("GNU.sparse.minor", file->stat_info, NULL);
  869. xheader_store ("GNU.sparse.name", file->stat_info, NULL);
  870. xheader_store ("GNU.sparse.realsize", file->stat_info, NULL);
  871. file->stat_info->file_name = xheader_format_name (file->stat_info,
  872. "%d/GNUSparseFile.%p/%f", 0);
  873. blk = start_header (file->stat_info);
  874. /* Store the effective (shrunken) file size */
  875. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  876. finish_header (file->stat_info, blk, block_ordinal);
  877. free (file->stat_info->file_name);
  878. file->stat_info->file_name = save_file_name;
  879. blk = find_next_block ();
  880. q = blk->buffer;
  881. p = umaxtostr (file->stat_info->sparse_map_avail, nbuf);
  882. COPY_STRING (blk, q, p);
  883. COPY_STRING (blk, q, "\n");
  884. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  885. {
  886. p = umaxtostr (map[i].offset, nbuf);
  887. COPY_STRING (blk, q, p);
  888. COPY_STRING (blk, q, "\n");
  889. p = umaxtostr (map[i].numbytes, nbuf);
  890. COPY_STRING (blk, q, p);
  891. COPY_STRING (blk, q, "\n");
  892. }
  893. memset (q, 0, BLOCKSIZE - (q - blk->buffer));
  894. set_next_block_after (blk);
  895. return true;
  896. }
  897. static bool
  898. pax_dump_header (struct tar_sparse_file *file)
  899. {
  900. file->stat_info->sparse_major = tar_sparse_major;
  901. file->stat_info->sparse_minor = tar_sparse_minor;
  902. if (file->stat_info->sparse_major == 0)
  903. pax_dump_header_0 (file);
  904. else
  905. pax_dump_header_1 (file);
  906. }
  907. static bool
  908. decode_num (uintmax_t *num, char const *arg, uintmax_t maxval)
  909. {
  910. uintmax_t u;
  911. char *arg_lim;
  912. if (!ISDIGIT (*arg))
  913. return false;
  914. u = strtoumax (arg, &arg_lim, 10);
  915. if (! (u <= maxval && errno != ERANGE) || *arg_lim)
  916. return false;
  917. *num = u;
  918. return true;
  919. }
  920. static bool
  921. pax_decode_header (struct tar_sparse_file *file)
  922. {
  923. if (file->stat_info->sparse_major > 0)
  924. {
  925. uintmax_t u;
  926. char nbuf[UINTMAX_STRSIZE_BOUND];
  927. union block *blk;
  928. char *p;
  929. size_t i;
  930. #define COPY_BUF(b,buf,src) do \
  931. { \
  932. char *endp = b->buffer + BLOCKSIZE; \
  933. char *dst = buf; \
  934. do \
  935. { \
  936. if (dst == buf + UINTMAX_STRSIZE_BOUND -1) \
  937. { \
  938. ERROR ((0, 0, _("%s: numeric overflow in sparse archive member"), \
  939. file->stat_info->orig_file_name)); \
  940. return false; \
  941. } \
  942. if (src == endp) \
  943. { \
  944. set_next_block_after (b); \
  945. b = find_next_block (); \
  946. src = b->buffer; \
  947. endp = b->buffer + BLOCKSIZE; \
  948. } \
  949. *dst = *src++; \
  950. } \
  951. while (*dst++ != '\n'); \
  952. dst[-1] = 0; \
  953. } while (0)
  954. set_next_block_after (current_header);
  955. blk = find_next_block ();
  956. p = blk->buffer;
  957. COPY_BUF (blk,nbuf,p);
  958. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)))
  959. {
  960. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  961. file->stat_info->orig_file_name));
  962. return false;
  963. }
  964. file->stat_info->sparse_map_size = u;
  965. file->stat_info->sparse_map = xcalloc (file->stat_info->sparse_map_size,
  966. sizeof (*file->stat_info->sparse_map));
  967. file->stat_info->sparse_map_avail = 0;
  968. for (i = 0; i < file->stat_info->sparse_map_size; i++)
  969. {
  970. struct sp_array sp;
  971. COPY_BUF (blk,nbuf,p);
  972. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (off_t)))
  973. {
  974. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  975. file->stat_info->orig_file_name));
  976. return false;
  977. }
  978. sp.offset = u;
  979. COPY_BUF (blk,nbuf,p);
  980. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)))
  981. {
  982. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  983. file->stat_info->orig_file_name));
  984. return false;
  985. }
  986. sp.numbytes = u;
  987. sparse_add_map (file->stat_info, &sp);
  988. }
  989. set_next_block_after (blk);
  990. }
  991. return true;
  992. }
  993. static struct tar_sparse_optab const pax_optab = {
  994. NULL, /* No init function */
  995. NULL, /* No done function */
  996. pax_sparse_member_p,
  997. pax_dump_header,
  998. NULL,
  999. pax_decode_header,
  1000. NULL, /* No scan_block function */
  1001. sparse_dump_region,
  1002. sparse_extract_region,
  1003. };