sparse.c 33 KB

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