sparse.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. bool
  349. sparse_member_p (struct tar_stat_info *st)
  350. {
  351. struct tar_sparse_file file;
  352. if (!tar_sparse_init (&file))
  353. return false;
  354. file.stat_info = st;
  355. return tar_sparse_member_p (&file);
  356. }
  357. bool
  358. sparse_fixup_header (struct tar_stat_info *st)
  359. {
  360. struct tar_sparse_file file;
  361. if (!tar_sparse_init (&file))
  362. return false;
  363. file.stat_info = st;
  364. return tar_sparse_fixup_header (&file);
  365. }
  366. enum dump_status
  367. sparse_extract_file (int fd, struct tar_stat_info *st, off_t *size)
  368. {
  369. bool rc = true;
  370. struct tar_sparse_file file;
  371. size_t i;
  372. if (!tar_sparse_init (&file))
  373. return dump_status_not_implemented;
  374. file.stat_info = st;
  375. file.fd = fd;
  376. file.seekable = lseek (fd, 0, SEEK_SET) == 0;
  377. file.offset = 0;
  378. rc = tar_sparse_decode_header (&file);
  379. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  380. rc = tar_sparse_extract_region (&file, i);
  381. *size = file.stat_info->archive_file_size - file.dumped_size;
  382. return (tar_sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
  383. }
  384. enum dump_status
  385. sparse_skip_file (struct tar_stat_info *st)
  386. {
  387. bool rc = true;
  388. struct tar_sparse_file file;
  389. if (!tar_sparse_init (&file))
  390. return dump_status_not_implemented;
  391. file.stat_info = st;
  392. file.fd = -1;
  393. rc = tar_sparse_decode_header (&file);
  394. skip_file (file.stat_info->archive_file_size);
  395. return (tar_sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
  396. }
  397. static bool
  398. check_sparse_region (struct tar_sparse_file *file, off_t beg, off_t end)
  399. {
  400. if (!lseek_or_error (file, beg))
  401. return false;
  402. while (beg < end)
  403. {
  404. size_t bytes_read;
  405. size_t rdsize = BLOCKSIZE < end - beg ? BLOCKSIZE : end - beg;
  406. char diff_buffer[BLOCKSIZE];
  407. bytes_read = safe_read (file->fd, diff_buffer, rdsize);
  408. if (bytes_read == SAFE_READ_ERROR)
  409. {
  410. read_diag_details (file->stat_info->orig_file_name,
  411. beg,
  412. rdsize);
  413. return false;
  414. }
  415. if (!zero_block_p (diff_buffer, bytes_read))
  416. {
  417. char begbuf[INT_BUFSIZE_BOUND (off_t)];
  418. report_difference (file->stat_info,
  419. _("File fragment at %s is not a hole"),
  420. offtostr (beg, begbuf));
  421. return false;
  422. }
  423. beg += bytes_read;
  424. }
  425. return true;
  426. }
  427. static bool
  428. check_data_region (struct tar_sparse_file *file, size_t i)
  429. {
  430. size_t size_left;
  431. if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset))
  432. return false;
  433. size_left = file->stat_info->sparse_map[i].numbytes;
  434. mv_size_left (file->stat_info->archive_file_size - file->dumped_size);
  435. while (size_left > 0)
  436. {
  437. size_t bytes_read;
  438. size_t rdsize = (size_left > BLOCKSIZE) ? BLOCKSIZE : size_left;
  439. char diff_buffer[BLOCKSIZE];
  440. union block *blk = find_next_block ();
  441. if (!blk)
  442. {
  443. ERROR ((0, 0, _("Unexpected EOF in archive")));
  444. return false;
  445. }
  446. set_next_block_after (blk);
  447. bytes_read = safe_read (file->fd, diff_buffer, rdsize);
  448. if (bytes_read == SAFE_READ_ERROR)
  449. {
  450. read_diag_details (file->stat_info->orig_file_name,
  451. (file->stat_info->sparse_map[i].offset
  452. + file->stat_info->sparse_map[i].numbytes
  453. - size_left),
  454. rdsize);
  455. return false;
  456. }
  457. file->dumped_size += bytes_read;
  458. size_left -= bytes_read;
  459. mv_size_left (file->stat_info->archive_file_size - file->dumped_size);
  460. if (memcmp (blk->buffer, diff_buffer, rdsize))
  461. {
  462. report_difference (file->stat_info, _("Contents differ"));
  463. return false;
  464. }
  465. }
  466. return true;
  467. }
  468. bool
  469. sparse_diff_file (int fd, struct tar_stat_info *st)
  470. {
  471. bool rc = true;
  472. struct tar_sparse_file file;
  473. size_t i;
  474. off_t offset = 0;
  475. if (!tar_sparse_init (&file))
  476. return dump_status_not_implemented;
  477. file.stat_info = st;
  478. file.fd = fd;
  479. file.seekable = true; /* File *must* be seekable for compare to work */
  480. rc = tar_sparse_decode_header (&file);
  481. mv_begin (st);
  482. for (i = 0; rc && i < file.stat_info->sparse_map_avail; i++)
  483. {
  484. rc = check_sparse_region (&file,
  485. offset, file.stat_info->sparse_map[i].offset)
  486. && check_data_region (&file, i);
  487. offset = file.stat_info->sparse_map[i].offset
  488. + file.stat_info->sparse_map[i].numbytes;
  489. }
  490. if (!rc)
  491. skip_file (file.stat_info->archive_file_size - file.dumped_size);
  492. mv_end ();
  493. tar_sparse_done (&file);
  494. return rc;
  495. }
  496. /* Old GNU Format. The sparse file information is stored in the
  497. oldgnu_header in the following manner:
  498. The header is marked with type 'S'. Its `size' field contains
  499. the cumulative size of all non-empty blocks of the file. The
  500. actual file size is stored in `realsize' member of oldgnu_header.
  501. The map of the file is stored in a list of `struct sparse'.
  502. Each struct contains offset to the block of data and its
  503. size (both as octal numbers). The first file header contains
  504. at most 4 such structs (SPARSES_IN_OLDGNU_HEADER). If the map
  505. contains more structs, then the field `isextended' of the main
  506. header is set to 1 (binary) and the `struct sparse_header'
  507. header follows, containing at most 21 following structs
  508. (SPARSES_IN_SPARSE_HEADER). If more structs follow, `isextended'
  509. field of the extended header is set and next next extension header
  510. follows, etc... */
  511. enum oldgnu_add_status
  512. {
  513. add_ok,
  514. add_finish,
  515. add_fail
  516. };
  517. static bool
  518. oldgnu_sparse_member_p (struct tar_sparse_file *file __attribute__ ((unused)))
  519. {
  520. return current_header->header.typeflag == GNUTYPE_SPARSE;
  521. }
  522. /* Add a sparse item to the sparse file and its obstack */
  523. static enum oldgnu_add_status
  524. oldgnu_add_sparse (struct tar_sparse_file *file, struct sparse *s)
  525. {
  526. struct sp_array sp;
  527. if (s->numbytes[0] == '\0')
  528. return add_finish;
  529. sp.offset = OFF_FROM_HEADER (s->offset);
  530. sp.numbytes = SIZE_FROM_HEADER (s->numbytes);
  531. if (sp.offset < 0
  532. || file->stat_info->stat.st_size < sp.offset + sp.numbytes
  533. || file->stat_info->archive_file_size < 0)
  534. return add_fail;
  535. sparse_add_map (file->stat_info, &sp);
  536. return add_ok;
  537. }
  538. static bool
  539. oldgnu_fixup_header (struct tar_sparse_file *file)
  540. {
  541. /* NOTE! st_size was initialized from the header
  542. which actually contains archived size. The following fixes it */
  543. file->stat_info->archive_file_size = file->stat_info->stat.st_size;
  544. file->stat_info->stat.st_size =
  545. OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
  546. return true;
  547. }
  548. /* Convert old GNU format sparse data to internal representation */
  549. static bool
  550. oldgnu_get_sparse_info (struct tar_sparse_file *file)
  551. {
  552. size_t i;
  553. union block *h = current_header;
  554. int ext_p;
  555. enum oldgnu_add_status rc;
  556. file->stat_info->sparse_map_avail = 0;
  557. for (i = 0; i < SPARSES_IN_OLDGNU_HEADER; i++)
  558. {
  559. rc = oldgnu_add_sparse (file, &h->oldgnu_header.sp[i]);
  560. if (rc != add_ok)
  561. break;
  562. }
  563. for (ext_p = h->oldgnu_header.isextended;
  564. rc == add_ok && ext_p; ext_p = h->sparse_header.isextended)
  565. {
  566. h = find_next_block ();
  567. if (!h)
  568. {
  569. ERROR ((0, 0, _("Unexpected EOF in archive")));
  570. return false;
  571. }
  572. set_next_block_after (h);
  573. for (i = 0; i < SPARSES_IN_SPARSE_HEADER && rc == add_ok; i++)
  574. rc = oldgnu_add_sparse (file, &h->sparse_header.sp[i]);
  575. }
  576. if (rc == add_fail)
  577. {
  578. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  579. file->stat_info->orig_file_name));
  580. return false;
  581. }
  582. return true;
  583. }
  584. static void
  585. oldgnu_store_sparse_info (struct tar_sparse_file *file, size_t *pindex,
  586. struct sparse *sp, size_t sparse_size)
  587. {
  588. for (; *pindex < file->stat_info->sparse_map_avail
  589. && sparse_size > 0; sparse_size--, sp++, ++*pindex)
  590. {
  591. OFF_TO_CHARS (file->stat_info->sparse_map[*pindex].offset,
  592. sp->offset);
  593. SIZE_TO_CHARS (file->stat_info->sparse_map[*pindex].numbytes,
  594. sp->numbytes);
  595. }
  596. }
  597. static bool
  598. oldgnu_dump_header (struct tar_sparse_file *file)
  599. {
  600. off_t block_ordinal = current_block_ordinal ();
  601. union block *blk;
  602. size_t i;
  603. blk = start_header (file->stat_info);
  604. blk->header.typeflag = GNUTYPE_SPARSE;
  605. if (file->stat_info->sparse_map_avail > SPARSES_IN_OLDGNU_HEADER)
  606. blk->oldgnu_header.isextended = 1;
  607. /* Store the real file size */
  608. OFF_TO_CHARS (file->stat_info->stat.st_size, blk->oldgnu_header.realsize);
  609. /* Store the effective (shrunken) file size */
  610. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  611. i = 0;
  612. oldgnu_store_sparse_info (file, &i,
  613. blk->oldgnu_header.sp,
  614. SPARSES_IN_OLDGNU_HEADER);
  615. blk->oldgnu_header.isextended = i < file->stat_info->sparse_map_avail;
  616. finish_header (file->stat_info, blk, block_ordinal);
  617. while (i < file->stat_info->sparse_map_avail)
  618. {
  619. blk = find_next_block ();
  620. memset (blk->buffer, 0, BLOCKSIZE);
  621. oldgnu_store_sparse_info (file, &i,
  622. blk->sparse_header.sp,
  623. SPARSES_IN_SPARSE_HEADER);
  624. if (i < file->stat_info->sparse_map_avail)
  625. blk->sparse_header.isextended = 1;
  626. set_next_block_after (blk);
  627. }
  628. return true;
  629. }
  630. static struct tar_sparse_optab const oldgnu_optab = {
  631. NULL, /* No init function */
  632. NULL, /* No done function */
  633. oldgnu_sparse_member_p,
  634. oldgnu_dump_header,
  635. oldgnu_fixup_header,
  636. oldgnu_get_sparse_info,
  637. NULL, /* No scan_block function */
  638. sparse_dump_region,
  639. sparse_extract_region,
  640. };
  641. /* Star */
  642. static bool
  643. star_sparse_member_p (struct tar_sparse_file *file __attribute__ ((unused)))
  644. {
  645. return current_header->header.typeflag == GNUTYPE_SPARSE;
  646. }
  647. static bool
  648. star_fixup_header (struct tar_sparse_file *file)
  649. {
  650. /* NOTE! st_size was initialized from the header
  651. which actually contains archived size. The following fixes it */
  652. file->stat_info->archive_file_size = file->stat_info->stat.st_size;
  653. file->stat_info->stat.st_size =
  654. OFF_FROM_HEADER (current_header->star_in_header.realsize);
  655. return true;
  656. }
  657. /* Convert STAR format sparse data to internal representation */
  658. static bool
  659. star_get_sparse_info (struct tar_sparse_file *file)
  660. {
  661. size_t i;
  662. union block *h = current_header;
  663. int ext_p;
  664. enum oldgnu_add_status rc = add_ok;
  665. file->stat_info->sparse_map_avail = 0;
  666. if (h->star_in_header.prefix[0] == '\0'
  667. && h->star_in_header.sp[0].offset[10] != '\0')
  668. {
  669. /* Old star format */
  670. for (i = 0; i < SPARSES_IN_STAR_HEADER; i++)
  671. {
  672. rc = oldgnu_add_sparse (file, &h->star_in_header.sp[i]);
  673. if (rc != add_ok)
  674. break;
  675. }
  676. ext_p = h->star_in_header.isextended;
  677. }
  678. else
  679. ext_p = 1;
  680. for (; rc == add_ok && ext_p; ext_p = h->star_ext_header.isextended)
  681. {
  682. h = find_next_block ();
  683. if (!h)
  684. {
  685. ERROR ((0, 0, _("Unexpected EOF in archive")));
  686. return false;
  687. }
  688. set_next_block_after (h);
  689. for (i = 0; i < SPARSES_IN_STAR_EXT_HEADER && rc == add_ok; i++)
  690. rc = oldgnu_add_sparse (file, &h->star_ext_header.sp[i]);
  691. }
  692. if (rc == add_fail)
  693. {
  694. ERROR ((0, 0, _("%s: invalid sparse archive member"),
  695. file->stat_info->orig_file_name));
  696. return false;
  697. }
  698. return true;
  699. }
  700. static struct tar_sparse_optab const star_optab = {
  701. NULL, /* No init function */
  702. NULL, /* No done function */
  703. star_sparse_member_p,
  704. NULL,
  705. star_fixup_header,
  706. star_get_sparse_info,
  707. NULL, /* No scan_block function */
  708. NULL, /* No dump region function */
  709. sparse_extract_region,
  710. };
  711. /* GNU PAX sparse file format. There are several versions:
  712. * 0.0
  713. The initial version of sparse format used by tar 1.14-1.15.1.
  714. The sparse file map is stored in x header:
  715. GNU.sparse.size Real size of the stored file
  716. GNU.sparse.numblocks Number of blocks in the sparse map
  717. repeat numblocks time
  718. GNU.sparse.offset Offset of the next data block
  719. GNU.sparse.numbytes Size of the next data block
  720. end repeat
  721. This has been reported as conflicting with the POSIX specs. The reason is
  722. that offsets and sizes of non-zero data blocks were stored in multiple
  723. instances of GNU.sparse.offset/GNU.sparse.numbytes variables, whereas
  724. POSIX requires the latest occurrence of the variable to override all
  725. previous occurrences.
  726. To avoid this incompatibility two following versions were introduced.
  727. * 0.1
  728. Used by tar 1.15.2 -- 1.15.91 (alpha releases).
  729. The sparse file map is stored in
  730. x header:
  731. GNU.sparse.size Real size of the stored file
  732. GNU.sparse.numblocks Number of blocks in the sparse map
  733. GNU.sparse.map Map of non-null data chunks. A string consisting
  734. of comma-separated values "offset,size[,offset,size]..."
  735. The resulting GNU.sparse.map string can be *very* long. While POSIX does not
  736. impose any limit on the length of a x header variable, this can confuse some
  737. tars.
  738. * 1.0
  739. Starting from this version, the exact sparse format version is specified
  740. explicitely in the header using the following variables:
  741. GNU.sparse.major Major version
  742. GNU.sparse.minor Minor version
  743. X header keeps the following variables:
  744. GNU.sparse.name Real file name of the sparse file
  745. GNU.sparse.realsize Real size of the stored file (corresponds to the old
  746. GNU.sparse.size variable)
  747. The name field of the ustar header is constructed using the pattern
  748. "%d/GNUSparseFile.%p/%f".
  749. The sparse map itself is stored in the file data block, preceding the actual
  750. file data. It consists of a series of octal numbers of arbitrary length,
  751. delimited by newlines. The map is padded with nulls to the nearest block
  752. boundary.
  753. The first number gives the number of entries in the map. Following are map
  754. entries, each one consisting of two numbers giving the offset and size of
  755. the data block it describes.
  756. The format is designed in such a way that non-posix aware tars and tars not
  757. supporting GNU.sparse.* keywords will extract each sparse file in its
  758. condensed form with the file map attached and will place it into a separate
  759. directory. Then, using a simple program it would be possible to expand the
  760. file to its original form even without GNU tar.
  761. Bu default, v.1.0 archives are created. To use other formats,
  762. --sparse-version option is provided. Additionally, v.0.0 can be obtained
  763. by deleting GNU.sparse.map from 0.1 format: --sparse-version 0.1
  764. --pax-option delete=GNU.sparse.map
  765. */
  766. static bool
  767. pax_sparse_member_p (struct tar_sparse_file *file)
  768. {
  769. return file->stat_info->sparse_map_avail > 0
  770. || file->stat_info->sparse_major > 0;
  771. }
  772. static bool
  773. pax_dump_header_0 (struct tar_sparse_file *file)
  774. {
  775. off_t block_ordinal = current_block_ordinal ();
  776. union block *blk;
  777. size_t i;
  778. char nbuf[UINTMAX_STRSIZE_BOUND];
  779. struct sp_array *map = file->stat_info->sparse_map;
  780. char *save_file_name = NULL;
  781. /* Store the real file size */
  782. xheader_store ("GNU.sparse.size", file->stat_info, NULL);
  783. xheader_store ("GNU.sparse.numblocks", file->stat_info, NULL);
  784. if (xheader_keyword_deleted_p ("GNU.sparse.map")
  785. || tar_sparse_minor == 0)
  786. {
  787. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  788. {
  789. xheader_store ("GNU.sparse.offset", file->stat_info, &i);
  790. xheader_store ("GNU.sparse.numbytes", file->stat_info, &i);
  791. }
  792. }
  793. else
  794. {
  795. xheader_store ("GNU.sparse.name", file->stat_info, NULL);
  796. save_file_name = file->stat_info->file_name;
  797. file->stat_info->file_name = xheader_format_name (file->stat_info,
  798. "%d/GNUSparseFile.%p/%f", 0);
  799. xheader_string_begin ();
  800. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  801. {
  802. if (i)
  803. xheader_string_add (",");
  804. xheader_string_add (umaxtostr (map[i].offset, nbuf));
  805. xheader_string_add (",");
  806. xheader_string_add (umaxtostr (map[i].numbytes, nbuf));
  807. }
  808. if (!xheader_string_end ("GNU.sparse.map"))
  809. {
  810. free (file->stat_info->file_name);
  811. file->stat_info->file_name = save_file_name;
  812. return false;
  813. }
  814. }
  815. blk = start_header (file->stat_info);
  816. /* Store the effective (shrunken) file size */
  817. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  818. finish_header (file->stat_info, blk, block_ordinal);
  819. if (save_file_name)
  820. {
  821. free (file->stat_info->file_name);
  822. file->stat_info->file_name = save_file_name;
  823. }
  824. return true;
  825. }
  826. static bool
  827. pax_dump_header_1 (struct tar_sparse_file *file)
  828. {
  829. off_t block_ordinal = current_block_ordinal ();
  830. union block *blk;
  831. char *p, *q;
  832. size_t i;
  833. char nbuf[UINTMAX_STRSIZE_BOUND];
  834. off_t size = 0;
  835. struct sp_array *map = file->stat_info->sparse_map;
  836. char *save_file_name = file->stat_info->file_name;
  837. #define COPY_STRING(b,dst,src) do \
  838. { \
  839. char *endp = b->buffer + BLOCKSIZE; \
  840. char *srcp = src; \
  841. while (*srcp) \
  842. { \
  843. if (dst == endp) \
  844. { \
  845. set_next_block_after (b); \
  846. b = find_next_block (); \
  847. dst = b->buffer; \
  848. endp = b->buffer + BLOCKSIZE; \
  849. } \
  850. *dst++ = *srcp++; \
  851. } \
  852. } while (0)
  853. /* Compute stored file size */
  854. p = umaxtostr (file->stat_info->sparse_map_avail, nbuf);
  855. size += strlen (p) + 1;
  856. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  857. {
  858. p = umaxtostr (map[i].offset, nbuf);
  859. size += strlen (p) + 1;
  860. p = umaxtostr (map[i].numbytes, nbuf);
  861. size += strlen (p) + 1;
  862. }
  863. size = (size + BLOCKSIZE - 1) / BLOCKSIZE;
  864. file->stat_info->archive_file_size += size * BLOCKSIZE;
  865. /* Store sparse file identification */
  866. xheader_store ("GNU.sparse.major", file->stat_info, NULL);
  867. xheader_store ("GNU.sparse.minor", file->stat_info, NULL);
  868. xheader_store ("GNU.sparse.name", file->stat_info, NULL);
  869. xheader_store ("GNU.sparse.realsize", file->stat_info, NULL);
  870. file->stat_info->file_name = xheader_format_name (file->stat_info,
  871. "%d/GNUSparseFile.%p/%f", 0);
  872. blk = start_header (file->stat_info);
  873. /* Store the effective (shrunken) file size */
  874. OFF_TO_CHARS (file->stat_info->archive_file_size, blk->header.size);
  875. finish_header (file->stat_info, blk, block_ordinal);
  876. free (file->stat_info->file_name);
  877. file->stat_info->file_name = save_file_name;
  878. blk = find_next_block ();
  879. q = blk->buffer;
  880. p = umaxtostr (file->stat_info->sparse_map_avail, nbuf);
  881. COPY_STRING (blk, q, p);
  882. COPY_STRING (blk, q, "\n");
  883. for (i = 0; i < file->stat_info->sparse_map_avail; i++)
  884. {
  885. p = umaxtostr (map[i].offset, nbuf);
  886. COPY_STRING (blk, q, p);
  887. COPY_STRING (blk, q, "\n");
  888. p = umaxtostr (map[i].numbytes, nbuf);
  889. COPY_STRING (blk, q, p);
  890. COPY_STRING (blk, q, "\n");
  891. }
  892. memset (q, 0, BLOCKSIZE - (q - blk->buffer));
  893. set_next_block_after (blk);
  894. return true;
  895. }
  896. static bool
  897. pax_dump_header (struct tar_sparse_file *file)
  898. {
  899. file->stat_info->sparse_major = tar_sparse_major;
  900. file->stat_info->sparse_minor = tar_sparse_minor;
  901. return (file->stat_info->sparse_major == 0) ?
  902. pax_dump_header_0 (file) : pax_dump_header_1 (file);
  903. }
  904. static bool
  905. decode_num (uintmax_t *num, char const *arg, uintmax_t maxval)
  906. {
  907. uintmax_t u;
  908. char *arg_lim;
  909. if (!ISDIGIT (*arg))
  910. return false;
  911. u = strtoumax (arg, &arg_lim, 10);
  912. if (! (u <= maxval && errno != ERANGE) || *arg_lim)
  913. return false;
  914. *num = u;
  915. return true;
  916. }
  917. static bool
  918. pax_decode_header (struct tar_sparse_file *file)
  919. {
  920. if (file->stat_info->sparse_major > 0)
  921. {
  922. uintmax_t u;
  923. char nbuf[UINTMAX_STRSIZE_BOUND];
  924. union block *blk;
  925. char *p;
  926. size_t i;
  927. #define COPY_BUF(b,buf,src) do \
  928. { \
  929. char *endp = b->buffer + BLOCKSIZE; \
  930. char *dst = buf; \
  931. do \
  932. { \
  933. if (dst == buf + UINTMAX_STRSIZE_BOUND -1) \
  934. { \
  935. ERROR ((0, 0, _("%s: numeric overflow in sparse archive member"), \
  936. file->stat_info->orig_file_name)); \
  937. return false; \
  938. } \
  939. if (src == endp) \
  940. { \
  941. set_next_block_after (b); \
  942. b = find_next_block (); \
  943. src = b->buffer; \
  944. endp = b->buffer + BLOCKSIZE; \
  945. } \
  946. *dst = *src++; \
  947. } \
  948. while (*dst++ != '\n'); \
  949. dst[-1] = 0; \
  950. } while (0)
  951. set_next_block_after (current_header);
  952. blk = find_next_block ();
  953. p = blk->buffer;
  954. COPY_BUF (blk,nbuf,p);
  955. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)))
  956. {
  957. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  958. file->stat_info->orig_file_name));
  959. return false;
  960. }
  961. file->stat_info->sparse_map_size = u;
  962. file->stat_info->sparse_map = xcalloc (file->stat_info->sparse_map_size,
  963. sizeof (*file->stat_info->sparse_map));
  964. file->stat_info->sparse_map_avail = 0;
  965. for (i = 0; i < file->stat_info->sparse_map_size; i++)
  966. {
  967. struct sp_array sp;
  968. COPY_BUF (blk,nbuf,p);
  969. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (off_t)))
  970. {
  971. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  972. file->stat_info->orig_file_name));
  973. return false;
  974. }
  975. sp.offset = u;
  976. COPY_BUF (blk,nbuf,p);
  977. if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)))
  978. {
  979. ERROR ((0, 0, _("%s: malformed sparse archive member"),
  980. file->stat_info->orig_file_name));
  981. return false;
  982. }
  983. sp.numbytes = u;
  984. sparse_add_map (file->stat_info, &sp);
  985. }
  986. set_next_block_after (blk);
  987. }
  988. return true;
  989. }
  990. static struct tar_sparse_optab const pax_optab = {
  991. NULL, /* No init function */
  992. NULL, /* No done function */
  993. pax_sparse_member_p,
  994. pax_dump_header,
  995. NULL,
  996. pax_decode_header,
  997. NULL, /* No scan_block function */
  998. sparse_dump_region,
  999. sparse_extract_region,
  1000. };