buffer.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /* Buffer management for tar.
  2. Copyright 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Written by John Gilmore, on 1985-08-25. */
  15. #include <system.h>
  16. #include <system-ioctl.h>
  17. #include <signal.h>
  18. #include <closeout.h>
  19. #include <fnmatch.h>
  20. #include <human.h>
  21. #include <quotearg.h>
  22. #include <verify.h>
  23. #include "common.h"
  24. #include <rmt.h>
  25. /* Number of retries before giving up on read. */
  26. #define READ_ERROR_MAX 10
  27. /* Variables. */
  28. static tarlong prev_written; /* bytes written on previous volumes */
  29. static tarlong bytes_written; /* bytes written on this volume */
  30. static void *record_buffer[2]; /* allocated memory */
  31. static union block *record_buffer_aligned[2];
  32. static int record_index;
  33. /* FIXME: The following variables should ideally be static to this
  34. module. However, this cannot be done yet. The cleanup continues! */
  35. union block *record_start; /* start of record of archive */
  36. union block *record_end; /* last+1 block of archive record */
  37. union block *current_block; /* current block of archive */
  38. enum access_mode access_mode; /* how do we handle the archive */
  39. off_t records_read; /* number of records read from this archive */
  40. off_t records_written; /* likewise, for records written */
  41. extern off_t records_skipped; /* number of records skipped at the start
  42. of the archive, defined in delete.c */
  43. static off_t record_start_block; /* block ordinal at record_start */
  44. /* Where we write list messages (not errors, not interactions) to. */
  45. FILE *stdlis;
  46. static void backspace_output (void);
  47. /* PID of child program, if compress_option or remote archive access. */
  48. static pid_t child_pid;
  49. /* Error recovery stuff */
  50. static int read_error_count;
  51. /* Have we hit EOF yet? */
  52. static bool hit_eof;
  53. static bool read_full_records = false;
  54. /* We're reading, but we just read the last block and it's time to update.
  55. Declared in update.c
  56. FIXME: Either eliminate it or move it to common.h.
  57. */
  58. extern bool time_to_start_writing;
  59. bool write_archive_to_stdout;
  60. static void (*flush_write_ptr) (size_t);
  61. static void (*flush_read_ptr) (void);
  62. char *volume_label;
  63. char *continued_file_name;
  64. uintmax_t continued_file_size;
  65. uintmax_t continued_file_offset;
  66. static int volno = 1; /* which volume of a multi-volume tape we're
  67. on */
  68. static int global_volno = 1; /* volume number to print in external
  69. messages */
  70. bool write_archive_to_stdout;
  71. /* Multi-volume tracking support */
  72. /* When creating a multi-volume archive, each 'bufmap' represents
  73. a member stored (perhaps partly) in the current record buffer.
  74. Bufmaps are form a single-linked list in chronological order.
  75. After flushing the record to the output media, all bufmaps that
  76. represent fully written members are removed from the list, the
  77. nblocks and sizeleft values in the bufmap_head and start values
  78. in all remaining bufmaps are updated. The information stored
  79. in bufmap_head is used to form the volume header.
  80. When reading from a multi-volume archive, the list degrades to a
  81. single element, which keeps information about the member currently
  82. being read. In that case the sizeleft member is updated explicitly
  83. from the extractor code by calling the mv_size_left function. The
  84. information from bufmap_head is compared with the volume header data
  85. to ensure that subsequent volumes are fed in the right order.
  86. */
  87. struct bufmap
  88. {
  89. struct bufmap *next; /* Pointer to the next map entry */
  90. size_t start; /* Offset of the first data block */
  91. char *file_name; /* Name of the stored file */
  92. off_t sizetotal; /* Size of the stored file */
  93. off_t sizeleft; /* Size left to read/write */
  94. size_t nblocks; /* Number of blocks written since reset */
  95. };
  96. static struct bufmap *bufmap_head, *bufmap_tail;
  97. /* This variable, when set, inhibits updating the bufmap chain after
  98. a write. This is necessary when writing extended POSIX headers. */
  99. static int inhibit_map;
  100. void
  101. mv_begin_write (const char *file_name, off_t totsize, off_t sizeleft)
  102. {
  103. if (multi_volume_option)
  104. {
  105. struct bufmap *bp = xmalloc (sizeof bp[0]);
  106. if (bufmap_tail)
  107. bufmap_tail->next = bp;
  108. else
  109. bufmap_head = bp;
  110. bufmap_tail = bp;
  111. bp->next = NULL;
  112. bp->start = current_block - record_start;
  113. bp->file_name = xstrdup (file_name);
  114. bp->sizetotal = totsize;
  115. bp->sizeleft = sizeleft;
  116. bp->nblocks = 0;
  117. }
  118. }
  119. static struct bufmap *
  120. bufmap_locate (size_t off)
  121. {
  122. struct bufmap *map;
  123. for (map = bufmap_head; map; map = map->next)
  124. {
  125. if (!map->next || off < map->next->start * BLOCKSIZE)
  126. break;
  127. }
  128. return map;
  129. }
  130. static void
  131. bufmap_free (struct bufmap *mark)
  132. {
  133. struct bufmap *map;
  134. for (map = bufmap_head; map && map != mark; )
  135. {
  136. struct bufmap *next = map->next;
  137. free (map->file_name);
  138. free (map);
  139. map = next;
  140. }
  141. bufmap_head = map;
  142. if (!bufmap_head)
  143. bufmap_tail = bufmap_head;
  144. }
  145. static void
  146. bufmap_reset (struct bufmap *map, ssize_t fixup)
  147. {
  148. bufmap_free (map);
  149. if (map)
  150. {
  151. for (; map; map = map->next)
  152. {
  153. map->start += fixup;
  154. map->nblocks = 0;
  155. }
  156. }
  157. }
  158. static struct tar_stat_info dummy;
  159. void
  160. buffer_write_global_xheader (void)
  161. {
  162. xheader_write_global (&dummy.xhdr);
  163. }
  164. void
  165. mv_begin_read (struct tar_stat_info *st)
  166. {
  167. mv_begin_write (st->orig_file_name, st->stat.st_size, st->stat.st_size);
  168. }
  169. void
  170. mv_end (void)
  171. {
  172. if (multi_volume_option)
  173. bufmap_free (NULL);
  174. }
  175. void
  176. mv_size_left (off_t size)
  177. {
  178. if (bufmap_head)
  179. bufmap_head->sizeleft = size;
  180. }
  181. /* Functions. */
  182. void
  183. clear_read_error_count (void)
  184. {
  185. read_error_count = 0;
  186. }
  187. /* Time-related functions */
  188. static double duration;
  189. void
  190. set_start_time (void)
  191. {
  192. gettime (&start_time);
  193. volume_start_time = start_time;
  194. last_stat_time = start_time;
  195. }
  196. static void
  197. set_volume_start_time (void)
  198. {
  199. gettime (&volume_start_time);
  200. last_stat_time = volume_start_time;
  201. }
  202. double
  203. compute_duration (void)
  204. {
  205. struct timespec now;
  206. gettime (&now);
  207. duration += ((now.tv_sec - last_stat_time.tv_sec)
  208. + (now.tv_nsec - last_stat_time.tv_nsec) / 1e9);
  209. gettime (&last_stat_time);
  210. return duration;
  211. }
  212. /* Compression detection */
  213. enum compress_type {
  214. ct_none, /* Unknown compression type */
  215. ct_tar, /* Plain tar file */
  216. ct_compress,
  217. ct_gzip,
  218. ct_bzip2,
  219. ct_lzip,
  220. ct_lzma,
  221. ct_lzop,
  222. ct_xz,
  223. ct_zstd
  224. };
  225. static enum compress_type archive_compression_type = ct_none;
  226. struct zip_magic
  227. {
  228. enum compress_type type;
  229. size_t length;
  230. char const *magic;
  231. };
  232. struct zip_program
  233. {
  234. enum compress_type type;
  235. char const *program;
  236. char const *option;
  237. };
  238. static struct zip_magic const magic[] = {
  239. { ct_none, 0, 0 },
  240. { ct_tar, 0, 0 },
  241. { ct_compress, 2, "\037\235" },
  242. { ct_gzip, 2, "\037\213" },
  243. { ct_bzip2, 3, "BZh" },
  244. { ct_lzip, 4, "LZIP" },
  245. { ct_lzma, 6, "\xFFLZMA" },
  246. { ct_lzop, 4, "\211LZO" },
  247. { ct_xz, 6, "\xFD" "7zXZ" },
  248. { ct_zstd, 4, "\x28\xB5\x2F\xFD" },
  249. };
  250. #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
  251. static struct zip_program zip_program[] = {
  252. { ct_compress, COMPRESS_PROGRAM, "-Z" },
  253. { ct_compress, GZIP_PROGRAM, "-z" },
  254. { ct_gzip, GZIP_PROGRAM, "-z" },
  255. { ct_bzip2, BZIP2_PROGRAM, "-j" },
  256. { ct_bzip2, "lbzip2", "-j" },
  257. { ct_lzip, LZIP_PROGRAM, "--lzip" },
  258. { ct_lzma, LZMA_PROGRAM, "--lzma" },
  259. { ct_lzma, XZ_PROGRAM, "-J" },
  260. { ct_lzop, LZOP_PROGRAM, "--lzop" },
  261. { ct_xz, XZ_PROGRAM, "-J" },
  262. { ct_zstd, ZSTD_PROGRAM, "--zstd" },
  263. };
  264. enum { n_zip_programs = sizeof zip_program / sizeof *zip_program };
  265. static struct zip_program const *
  266. find_zip_program (enum compress_type type, int *pstate)
  267. {
  268. int i;
  269. for (i = *pstate; i < n_zip_programs; i++)
  270. {
  271. if (zip_program[i].type == type)
  272. {
  273. *pstate = i + 1;
  274. return zip_program + i;
  275. }
  276. }
  277. *pstate = i;
  278. return NULL;
  279. }
  280. const char *
  281. first_decompress_program (int *pstate)
  282. {
  283. struct zip_program const *zp;
  284. *pstate = n_zip_programs;
  285. if (use_compress_program_option)
  286. return use_compress_program_option;
  287. if (archive_compression_type == ct_none)
  288. return NULL;
  289. *pstate = 0;
  290. zp = find_zip_program (archive_compression_type, pstate);
  291. return zp ? zp->program : NULL;
  292. }
  293. const char *
  294. next_decompress_program (int *pstate)
  295. {
  296. struct zip_program const *zp;
  297. zp = find_zip_program (archive_compression_type, pstate);
  298. return zp ? zp->program : NULL;
  299. }
  300. static const char *
  301. compress_option (enum compress_type type)
  302. {
  303. struct zip_program const *zp;
  304. int i = 0;
  305. zp = find_zip_program (type, &i);
  306. return zp ? zp->option : NULL;
  307. }
  308. /* Check if the file ARCHIVE is a compressed archive. */
  309. static enum compress_type
  310. check_compressed_archive (bool *pshort)
  311. {
  312. struct zip_magic const *p;
  313. bool sfr;
  314. bool temp;
  315. if (!pshort)
  316. pshort = &temp;
  317. /* Prepare global data needed for find_next_block: */
  318. record_end = record_start; /* set up for 1st record = # 0 */
  319. sfr = read_full_records;
  320. read_full_records = true; /* Suppress fatal error on reading a partial
  321. record */
  322. *pshort = find_next_block () == 0;
  323. /* Restore global values */
  324. read_full_records = sfr;
  325. if (record_start != record_end /* no files smaller than BLOCKSIZE */
  326. && (strcmp (record_start->header.magic, TMAGIC) == 0
  327. || strcmp (record_start->buffer + offsetof (struct posix_header,
  328. magic),
  329. OLDGNU_MAGIC) == 0)
  330. && tar_checksum (record_start, true) == HEADER_SUCCESS)
  331. /* Probably a valid header */
  332. return ct_tar;
  333. for (p = magic + 2; p < magic + NMAGIC; p++)
  334. if (memcmp (record_start->buffer, p->magic, p->length) == 0)
  335. return p->type;
  336. return ct_none;
  337. }
  338. /* Guess if the archive is seekable. */
  339. static void
  340. guess_seekable_archive (void)
  341. {
  342. struct stat st;
  343. if (subcommand_option == DELETE_SUBCOMMAND)
  344. {
  345. /* The current code in delete.c is based on the assumption that
  346. skip_member() reads all data from the archive. So, we should
  347. make sure it won't use seeks. On the other hand, the same code
  348. depends on the ability to backspace a record in the archive,
  349. so setting seekable_archive to false is technically incorrect.
  350. However, it is tested only in skip_member(), so it's not a
  351. problem. */
  352. seekable_archive = false;
  353. }
  354. if (seek_option != -1)
  355. {
  356. seekable_archive = !!seek_option;
  357. return;
  358. }
  359. if (!multi_volume_option && !use_compress_program_option
  360. && fstat (archive, &st) == 0)
  361. seekable_archive = S_ISREG (st.st_mode);
  362. else
  363. seekable_archive = false;
  364. }
  365. /* Open an archive named archive_name_array[0]. Detect if it is
  366. a compressed archive of known type and use corresponding decompression
  367. program if so */
  368. static int
  369. open_compressed_archive (void)
  370. {
  371. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  372. MODE_RW, rsh_command_option);
  373. if (archive == -1)
  374. return archive;
  375. if (!multi_volume_option)
  376. {
  377. if (!use_compress_program_option)
  378. {
  379. bool shortfile;
  380. enum compress_type type = check_compressed_archive (&shortfile);
  381. switch (type)
  382. {
  383. case ct_tar:
  384. if (shortfile)
  385. ERROR ((0, 0, _("This does not look like a tar archive")));
  386. return archive;
  387. case ct_none:
  388. if (shortfile)
  389. ERROR ((0, 0, _("This does not look like a tar archive")));
  390. set_compression_program_by_suffix (archive_name_array[0], NULL);
  391. if (!use_compress_program_option)
  392. return archive;
  393. break;
  394. default:
  395. archive_compression_type = type;
  396. break;
  397. }
  398. }
  399. /* FD is not needed any more */
  400. rmtclose (archive);
  401. hit_eof = false; /* It might have been set by find_next_block in
  402. check_compressed_archive */
  403. /* Open compressed archive */
  404. child_pid = sys_child_open_for_uncompress ();
  405. read_full_records = true;
  406. }
  407. records_read = 0;
  408. record_end = record_start; /* set up for 1st record = # 0 */
  409. return archive;
  410. }
  411. static int
  412. print_stats (FILE *fp, const char *text, tarlong numbytes)
  413. {
  414. char abbr[LONGEST_HUMAN_READABLE + 1];
  415. char rate[LONGEST_HUMAN_READABLE + 1];
  416. int n = 0;
  417. int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
  418. if (text && text[0])
  419. n += fprintf (fp, "%s: ", gettext (text));
  420. return n + fprintf (fp, TARLONG_FORMAT " (%s, %s/s)",
  421. numbytes,
  422. human_readable (numbytes, abbr, human_opts, 1, 1),
  423. (0 < duration && numbytes / duration < (uintmax_t) -1
  424. ? human_readable (numbytes / duration, rate, human_opts, 1, 1)
  425. : "?"));
  426. }
  427. /* Format totals to file FP. FORMATS is an array of strings to output
  428. before each data item (bytes read, written, deleted, in that order).
  429. EOR is a delimiter to output after each item (used only if deleting
  430. from the archive), EOL is a delimiter to add at the end of the output
  431. line. */
  432. int
  433. format_total_stats (FILE *fp, char const *const *formats, int eor, int eol)
  434. {
  435. int n;
  436. switch (subcommand_option)
  437. {
  438. case CREATE_SUBCOMMAND:
  439. case CAT_SUBCOMMAND:
  440. case UPDATE_SUBCOMMAND:
  441. case APPEND_SUBCOMMAND:
  442. n = print_stats (fp, formats[TF_WRITE],
  443. prev_written + bytes_written);
  444. break;
  445. case DELETE_SUBCOMMAND:
  446. {
  447. char buf[UINTMAX_STRSIZE_BOUND];
  448. n = print_stats (fp, formats[TF_READ],
  449. records_read * record_size);
  450. fputc (eor, fp);
  451. n++;
  452. n += print_stats (fp, formats[TF_WRITE],
  453. prev_written + bytes_written);
  454. fputc (eor, fp);
  455. n++;
  456. if (formats[TF_DELETED] && formats[TF_DELETED][0])
  457. n += fprintf (fp, "%s: ", gettext (formats[TF_DELETED]));
  458. n += fprintf (fp, "%s",
  459. STRINGIFY_BIGINT ((records_read - records_skipped)
  460. * record_size
  461. - (prev_written + bytes_written), buf));
  462. }
  463. break;
  464. case EXTRACT_SUBCOMMAND:
  465. case LIST_SUBCOMMAND:
  466. case DIFF_SUBCOMMAND:
  467. n = print_stats (fp, _(formats[TF_READ]),
  468. records_read * record_size);
  469. break;
  470. default:
  471. abort ();
  472. }
  473. if (eol)
  474. {
  475. fputc (eol, fp);
  476. n++;
  477. }
  478. return n;
  479. }
  480. static char const *const default_total_format[] = {
  481. N_("Total bytes read"),
  482. /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
  483. N_("Total bytes written"),
  484. N_("Total bytes deleted")
  485. };
  486. void
  487. print_total_stats (void)
  488. {
  489. format_total_stats (stderr, default_total_format, '\n', '\n');
  490. }
  491. /* Compute and return the block ordinal at current_block. */
  492. off_t
  493. current_block_ordinal (void)
  494. {
  495. return record_start_block + (current_block - record_start);
  496. }
  497. /* If the EOF flag is set, reset it, as well as current_block, etc. */
  498. void
  499. reset_eof (void)
  500. {
  501. if (hit_eof)
  502. {
  503. hit_eof = false;
  504. current_block = record_start;
  505. record_end = record_start + blocking_factor;
  506. access_mode = ACCESS_WRITE;
  507. }
  508. }
  509. /* Return the location of the next available input or output block.
  510. Return zero for EOF. Once we have returned zero, we just keep returning
  511. it, to avoid accidentally going on to the next file on the tape. */
  512. union block *
  513. find_next_block (void)
  514. {
  515. if (current_block == record_end)
  516. {
  517. if (hit_eof)
  518. return 0;
  519. flush_archive ();
  520. if (current_block == record_end)
  521. {
  522. hit_eof = true;
  523. return 0;
  524. }
  525. }
  526. return current_block;
  527. }
  528. /* Indicate that we have used all blocks up thru BLOCK. */
  529. void
  530. set_next_block_after (union block *block)
  531. {
  532. while (block >= current_block)
  533. current_block++;
  534. /* Do *not* flush the archive here. If we do, the same argument to
  535. set_next_block_after could mean the next block (if the input record
  536. is exactly one block long), which is not what is intended. */
  537. if (current_block > record_end)
  538. abort ();
  539. }
  540. /* Return the number of bytes comprising the space between POINTER
  541. through the end of the current buffer of blocks. This space is
  542. available for filling with data, or taking data from. POINTER is
  543. usually (but not always) the result of previous find_next_block call. */
  544. size_t
  545. available_space_after (union block *pointer)
  546. {
  547. return record_end->buffer - pointer->buffer;
  548. }
  549. /* Close file having descriptor FD, and abort if close unsuccessful. */
  550. void
  551. xclose (int fd)
  552. {
  553. if (close (fd) != 0)
  554. close_error (_("(pipe)"));
  555. }
  556. static void
  557. init_buffer (void)
  558. {
  559. if (! record_buffer_aligned[record_index])
  560. record_buffer_aligned[record_index] =
  561. page_aligned_alloc (&record_buffer[record_index], record_size);
  562. record_start = record_buffer_aligned[record_index];
  563. current_block = record_start;
  564. record_end = record_start + blocking_factor;
  565. }
  566. static void
  567. check_tty (enum access_mode mode)
  568. {
  569. /* Refuse to read archive from and write it to a tty. */
  570. if (strcmp (archive_name_array[0], "-") == 0
  571. && isatty (mode == ACCESS_READ ? STDIN_FILENO : STDOUT_FILENO))
  572. {
  573. FATAL_ERROR ((0, 0,
  574. mode == ACCESS_READ
  575. ? _("Refusing to read archive contents from terminal "
  576. "(missing -f option?)")
  577. : _("Refusing to write archive contents to terminal "
  578. "(missing -f option?)")));
  579. }
  580. }
  581. /* Open an archive file. The argument specifies whether we are
  582. reading or writing, or both. */
  583. static void
  584. _open_archive (enum access_mode wanted_access)
  585. {
  586. int backed_up_flag = 0;
  587. if (record_size == 0)
  588. FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
  589. if (archive_names == 0)
  590. FATAL_ERROR ((0, 0, _("No archive name given")));
  591. tar_stat_destroy (&current_stat_info);
  592. record_index = 0;
  593. init_buffer ();
  594. /* When updating the archive, we start with reading. */
  595. access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
  596. check_tty (access_mode);
  597. read_full_records = read_full_records_option;
  598. records_read = 0;
  599. if (use_compress_program_option)
  600. {
  601. switch (wanted_access)
  602. {
  603. case ACCESS_READ:
  604. child_pid = sys_child_open_for_uncompress ();
  605. read_full_records = true;
  606. record_end = record_start; /* set up for 1st record = # 0 */
  607. break;
  608. case ACCESS_WRITE:
  609. child_pid = sys_child_open_for_compress ();
  610. break;
  611. case ACCESS_UPDATE:
  612. abort (); /* Should not happen */
  613. break;
  614. }
  615. if (!index_file_name
  616. && wanted_access == ACCESS_WRITE
  617. && strcmp (archive_name_array[0], "-") == 0)
  618. stdlis = stderr;
  619. }
  620. else if (strcmp (archive_name_array[0], "-") == 0)
  621. {
  622. read_full_records = true; /* could be a pipe, be safe */
  623. if (verify_option)
  624. FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
  625. switch (wanted_access)
  626. {
  627. case ACCESS_READ:
  628. {
  629. bool shortfile;
  630. enum compress_type type;
  631. archive = STDIN_FILENO;
  632. type = check_compressed_archive (&shortfile);
  633. if (type != ct_tar && type != ct_none)
  634. FATAL_ERROR ((0, 0,
  635. _("Archive is compressed. Use %s option"),
  636. compress_option (type)));
  637. if (shortfile)
  638. ERROR ((0, 0, _("This does not look like a tar archive")));
  639. }
  640. break;
  641. case ACCESS_WRITE:
  642. archive = STDOUT_FILENO;
  643. if (!index_file_name)
  644. stdlis = stderr;
  645. break;
  646. case ACCESS_UPDATE:
  647. archive = STDIN_FILENO;
  648. write_archive_to_stdout = true;
  649. record_end = record_start; /* set up for 1st record = # 0 */
  650. if (!index_file_name)
  651. stdlis = stderr;
  652. break;
  653. }
  654. }
  655. else
  656. switch (wanted_access)
  657. {
  658. case ACCESS_READ:
  659. archive = open_compressed_archive ();
  660. if (archive >= 0)
  661. guess_seekable_archive ();
  662. break;
  663. case ACCESS_WRITE:
  664. if (backup_option)
  665. {
  666. maybe_backup_file (archive_name_array[0], 1);
  667. backed_up_flag = 1;
  668. }
  669. if (verify_option)
  670. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  671. MODE_RW, rsh_command_option);
  672. else
  673. archive = rmtcreat (archive_name_array[0], MODE_RW,
  674. rsh_command_option);
  675. break;
  676. case ACCESS_UPDATE:
  677. archive = rmtopen (archive_name_array[0],
  678. O_RDWR | O_CREAT | O_BINARY,
  679. MODE_RW, rsh_command_option);
  680. switch (check_compressed_archive (NULL))
  681. {
  682. case ct_none:
  683. case ct_tar:
  684. break;
  685. default:
  686. FATAL_ERROR ((0, 0,
  687. _("Cannot update compressed archives")));
  688. }
  689. break;
  690. }
  691. if (archive < 0
  692. || (! _isrmt (archive) && !sys_get_archive_stat ()))
  693. {
  694. int saved_errno = errno;
  695. if (backed_up_flag)
  696. undo_last_backup ();
  697. errno = saved_errno;
  698. open_fatal (archive_name_array[0]);
  699. }
  700. sys_detect_dev_null_output ();
  701. sys_save_archive_dev_ino ();
  702. SET_BINARY_MODE (archive);
  703. switch (wanted_access)
  704. {
  705. case ACCESS_READ:
  706. find_next_block (); /* read it in, check for EOF */
  707. break;
  708. case ACCESS_UPDATE:
  709. case ACCESS_WRITE:
  710. records_written = 0;
  711. break;
  712. }
  713. }
  714. /* Perform a write to flush the buffer. */
  715. static ssize_t
  716. _flush_write (void)
  717. {
  718. ssize_t status;
  719. checkpoint_run (true);
  720. if (tape_length_option && tape_length_option <= bytes_written)
  721. {
  722. errno = ENOSPC;
  723. status = 0;
  724. }
  725. else if (dev_null_output)
  726. status = record_size;
  727. else
  728. status = sys_write_archive_buffer ();
  729. if (status && multi_volume_option && !inhibit_map)
  730. {
  731. struct bufmap *map = bufmap_locate (status);
  732. if (map)
  733. {
  734. size_t delta = status - map->start * BLOCKSIZE;
  735. ssize_t diff;
  736. map->nblocks += delta / BLOCKSIZE;
  737. if (delta > map->sizeleft)
  738. delta = map->sizeleft;
  739. map->sizeleft -= delta;
  740. if (map->sizeleft == 0)
  741. {
  742. diff = map->start + map->nblocks;
  743. map = map->next;
  744. }
  745. else
  746. diff = map->start;
  747. bufmap_reset (map, - diff);
  748. }
  749. }
  750. return status;
  751. }
  752. /* Handle write errors on the archive. Write errors are always fatal.
  753. Hitting the end of a volume does not cause a write error unless the
  754. write was the first record of the volume. */
  755. void
  756. archive_write_error (ssize_t status)
  757. {
  758. /* It might be useful to know how much was written before the error
  759. occurred. */
  760. if (totals_option)
  761. {
  762. int e = errno;
  763. print_total_stats ();
  764. errno = e;
  765. }
  766. write_fatal_details (*archive_name_cursor, status, record_size);
  767. }
  768. /* Handle read errors on the archive. If the read should be retried,
  769. return to the caller. */
  770. void
  771. archive_read_error (void)
  772. {
  773. read_error (*archive_name_cursor);
  774. if (record_start_block == 0)
  775. FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
  776. /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
  777. then give up on reading the archive. */
  778. if (read_error_count++ > READ_ERROR_MAX)
  779. FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
  780. return;
  781. }
  782. static bool
  783. archive_is_dev (void)
  784. {
  785. struct stat st;
  786. if (fstat (archive, &st))
  787. {
  788. stat_diag (*archive_name_cursor);
  789. return false;
  790. }
  791. return S_ISBLK (st.st_mode) || S_ISCHR (st.st_mode);
  792. }
  793. static void
  794. short_read (size_t status)
  795. {
  796. size_t left; /* bytes left */
  797. char *more; /* pointer to next byte to read */
  798. more = record_start->buffer + status;
  799. left = record_size - status;
  800. if (left && left % BLOCKSIZE == 0
  801. && (warning_option & WARN_RECORD_SIZE)
  802. && record_start_block == 0 && status != 0
  803. && archive_is_dev ())
  804. {
  805. unsigned long rsize = status / BLOCKSIZE;
  806. WARN ((0, 0,
  807. ngettext ("Record size = %lu block",
  808. "Record size = %lu blocks",
  809. rsize),
  810. rsize));
  811. }
  812. while (left % BLOCKSIZE != 0
  813. || (left && status && read_full_records))
  814. {
  815. if (status)
  816. while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
  817. archive_read_error ();
  818. if (status == 0)
  819. break;
  820. if (! read_full_records)
  821. {
  822. unsigned long rest = record_size - left;
  823. FATAL_ERROR ((0, 0,
  824. ngettext ("Unaligned block (%lu byte) in archive",
  825. "Unaligned block (%lu bytes) in archive",
  826. rest),
  827. rest));
  828. }
  829. left -= status;
  830. more += status;
  831. }
  832. record_end = record_start + (record_size - left) / BLOCKSIZE;
  833. records_read++;
  834. }
  835. /* Flush the current buffer to/from the archive. */
  836. void
  837. flush_archive (void)
  838. {
  839. size_t buffer_level;
  840. if (access_mode == ACCESS_READ && time_to_start_writing)
  841. {
  842. access_mode = ACCESS_WRITE;
  843. time_to_start_writing = false;
  844. backspace_output ();
  845. if (record_end - record_start < blocking_factor)
  846. {
  847. memset (record_end, 0,
  848. (blocking_factor - (record_end - record_start))
  849. * BLOCKSIZE);
  850. record_end = record_start + blocking_factor;
  851. return;
  852. }
  853. }
  854. buffer_level = current_block->buffer - record_start->buffer;
  855. record_start_block += record_end - record_start;
  856. current_block = record_start;
  857. record_end = record_start + blocking_factor;
  858. switch (access_mode)
  859. {
  860. case ACCESS_READ:
  861. flush_read ();
  862. break;
  863. case ACCESS_WRITE:
  864. flush_write_ptr (buffer_level);
  865. break;
  866. case ACCESS_UPDATE:
  867. abort ();
  868. }
  869. }
  870. /* Backspace the archive descriptor by one record worth. If it's a
  871. tape, MTIOCTOP will work. If it's something else, try to seek on
  872. it. If we can't seek, we lose! */
  873. static void
  874. backspace_output (void)
  875. {
  876. #ifdef MTIOCTOP
  877. {
  878. struct mtop operation;
  879. operation.mt_op = MTBSR;
  880. operation.mt_count = 1;
  881. if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  882. return;
  883. if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  884. return;
  885. }
  886. #endif
  887. {
  888. off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  889. /* Seek back to the beginning of this record and start writing there. */
  890. position -= record_end->buffer - record_start->buffer;
  891. if (position < 0)
  892. position = 0;
  893. if (rmtlseek (archive, position, SEEK_SET) != position)
  894. {
  895. /* Lseek failed. Try a different method. */
  896. WARN ((0, 0,
  897. _("Cannot backspace archive file; it may be unreadable without -i")));
  898. /* Replace the first part of the record with NULs. */
  899. if (record_start->buffer != output_start)
  900. memset (record_start->buffer, 0,
  901. output_start - record_start->buffer);
  902. }
  903. }
  904. }
  905. off_t
  906. seek_archive (off_t size)
  907. {
  908. off_t start = current_block_ordinal ();
  909. off_t offset;
  910. off_t nrec, nblk;
  911. off_t skipped = (blocking_factor - (current_block - record_start))
  912. * BLOCKSIZE;
  913. if (size <= skipped)
  914. return 0;
  915. /* Compute number of records to skip */
  916. nrec = (size - skipped) / record_size;
  917. if (nrec == 0)
  918. return 0;
  919. offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
  920. if (offset < 0)
  921. return offset;
  922. if (offset % record_size)
  923. FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
  924. /* Convert to number of records */
  925. offset /= BLOCKSIZE;
  926. /* Compute number of skipped blocks */
  927. nblk = offset - start;
  928. /* Update buffering info */
  929. records_read += nblk / blocking_factor;
  930. record_start_block = offset - blocking_factor;
  931. current_block = record_end;
  932. return nblk;
  933. }
  934. /* Close the archive file. */
  935. void
  936. close_archive (void)
  937. {
  938. if (time_to_start_writing || access_mode == ACCESS_WRITE)
  939. {
  940. do
  941. flush_archive ();
  942. while (current_block > record_start);
  943. }
  944. compute_duration ();
  945. if (verify_option)
  946. verify_volume ();
  947. if (rmtclose (archive) != 0)
  948. close_error (*archive_name_cursor);
  949. sys_wait_for_child (child_pid, hit_eof);
  950. tar_stat_destroy (&current_stat_info);
  951. free (record_buffer[0]);
  952. free (record_buffer[1]);
  953. bufmap_free (NULL);
  954. }
  955. void
  956. write_fatal_details (char const *name, ssize_t status, size_t size)
  957. {
  958. write_error_details (name, status, size);
  959. if (rmtclose (archive) != 0)
  960. close_error (*archive_name_cursor);
  961. sys_wait_for_child (child_pid, false);
  962. fatal_exit ();
  963. }
  964. /* Called to initialize the global volume number. */
  965. void
  966. init_volume_number (void)
  967. {
  968. FILE *file = fopen (volno_file_option, "r");
  969. if (file)
  970. {
  971. if (fscanf (file, "%d", &global_volno) != 1
  972. || global_volno < 0)
  973. FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
  974. quotearg_colon (volno_file_option)));
  975. if (ferror (file))
  976. read_error (volno_file_option);
  977. if (fclose (file) != 0)
  978. close_error (volno_file_option);
  979. }
  980. else if (errno != ENOENT)
  981. open_error (volno_file_option);
  982. }
  983. /* Called to write out the closing global volume number. */
  984. void
  985. closeout_volume_number (void)
  986. {
  987. FILE *file = fopen (volno_file_option, "w");
  988. if (file)
  989. {
  990. fprintf (file, "%d\n", global_volno);
  991. if (ferror (file))
  992. write_error (volno_file_option);
  993. if (fclose (file) != 0)
  994. close_error (volno_file_option);
  995. }
  996. else
  997. open_error (volno_file_option);
  998. }
  999. static void
  1000. increase_volume_number (void)
  1001. {
  1002. global_volno++;
  1003. if (global_volno < 0)
  1004. FATAL_ERROR ((0, 0, _("Volume number overflow")));
  1005. volno++;
  1006. }
  1007. static void
  1008. change_tape_menu (FILE *read_file)
  1009. {
  1010. char *input_buffer = NULL;
  1011. size_t size = 0;
  1012. bool stop = false;
  1013. while (!stop)
  1014. {
  1015. fputc ('\007', stderr);
  1016. fprintf (stderr,
  1017. _("Prepare volume #%d for %s and hit return: "),
  1018. global_volno + 1, quote (*archive_name_cursor));
  1019. fflush (stderr);
  1020. if (getline (&input_buffer, &size, read_file) <= 0)
  1021. {
  1022. WARN ((0, 0, _("EOF where user reply was expected")));
  1023. if (subcommand_option != EXTRACT_SUBCOMMAND
  1024. && subcommand_option != LIST_SUBCOMMAND
  1025. && subcommand_option != DIFF_SUBCOMMAND)
  1026. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  1027. fatal_exit ();
  1028. }
  1029. if (input_buffer[0] == '\n'
  1030. || input_buffer[0] == 'y'
  1031. || input_buffer[0] == 'Y')
  1032. break;
  1033. switch (input_buffer[0])
  1034. {
  1035. case '?':
  1036. {
  1037. fprintf (stderr, _("\
  1038. n name Give a new file name for the next (and subsequent) volume(s)\n\
  1039. q Abort tar\n\
  1040. y or newline Continue operation\n"));
  1041. if (!restrict_option)
  1042. fprintf (stderr, _(" ! Spawn a subshell\n"));
  1043. fprintf (stderr, _(" ? Print this list\n"));
  1044. }
  1045. break;
  1046. case 'q':
  1047. /* Quit. */
  1048. WARN ((0, 0, _("No new volume; exiting.\n")));
  1049. if (subcommand_option != EXTRACT_SUBCOMMAND
  1050. && subcommand_option != LIST_SUBCOMMAND
  1051. && subcommand_option != DIFF_SUBCOMMAND)
  1052. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  1053. fatal_exit ();
  1054. case 'n':
  1055. /* Get new file name. */
  1056. {
  1057. char *name;
  1058. char *cursor;
  1059. for (name = input_buffer + 1;
  1060. *name == ' ' || *name == '\t';
  1061. name++)
  1062. ;
  1063. for (cursor = name; *cursor && *cursor != '\n'; cursor++)
  1064. ;
  1065. *cursor = '\0';
  1066. if (name[0])
  1067. {
  1068. /* FIXME: the following allocation is never reclaimed. */
  1069. *archive_name_cursor = xstrdup (name);
  1070. stop = true;
  1071. }
  1072. else
  1073. fprintf (stderr, "%s",
  1074. _("File name not specified. Try again.\n"));
  1075. }
  1076. break;
  1077. case '!':
  1078. if (!restrict_option)
  1079. {
  1080. sys_spawn_shell ();
  1081. break;
  1082. }
  1083. FALLTHROUGH;
  1084. default:
  1085. fprintf (stderr, _("Invalid input. Type ? for help.\n"));
  1086. }
  1087. }
  1088. free (input_buffer);
  1089. }
  1090. /* We've hit the end of the old volume. Close it and open the next one.
  1091. Return nonzero on success.
  1092. */
  1093. static bool
  1094. new_volume (enum access_mode mode)
  1095. {
  1096. static FILE *read_file;
  1097. static int looped;
  1098. int prompt;
  1099. if (!read_file && !info_script_option)
  1100. /* FIXME: if fopen is used, it will never be closed. */
  1101. read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
  1102. if (now_verifying)
  1103. return false;
  1104. if (verify_option)
  1105. verify_volume ();
  1106. assign_null (&volume_label);
  1107. assign_null (&continued_file_name);
  1108. continued_file_size = continued_file_offset = 0;
  1109. current_block = record_start;
  1110. if (rmtclose (archive) != 0)
  1111. close_error (*archive_name_cursor);
  1112. archive_name_cursor++;
  1113. if (archive_name_cursor == archive_name_array + archive_names)
  1114. {
  1115. archive_name_cursor = archive_name_array;
  1116. looped = 1;
  1117. }
  1118. prompt = looped;
  1119. tryagain:
  1120. if (prompt)
  1121. {
  1122. /* We have to prompt from now on. */
  1123. if (info_script_option)
  1124. {
  1125. if (volno_file_option)
  1126. closeout_volume_number ();
  1127. if (sys_exec_info_script (archive_name_cursor, global_volno+1))
  1128. FATAL_ERROR ((0, 0, _("%s command failed"),
  1129. quote (info_script_option)));
  1130. }
  1131. else
  1132. change_tape_menu (read_file);
  1133. }
  1134. if (strcmp (archive_name_cursor[0], "-") == 0)
  1135. {
  1136. read_full_records = true;
  1137. archive = STDIN_FILENO;
  1138. }
  1139. else if (verify_option)
  1140. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  1141. rsh_command_option);
  1142. else
  1143. switch (mode)
  1144. {
  1145. case ACCESS_READ:
  1146. archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
  1147. rsh_command_option);
  1148. guess_seekable_archive ();
  1149. break;
  1150. case ACCESS_WRITE:
  1151. if (backup_option)
  1152. maybe_backup_file (*archive_name_cursor, 1);
  1153. archive = rmtcreat (*archive_name_cursor, MODE_RW,
  1154. rsh_command_option);
  1155. break;
  1156. case ACCESS_UPDATE:
  1157. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  1158. rsh_command_option);
  1159. break;
  1160. }
  1161. if (archive < 0)
  1162. {
  1163. open_warn (*archive_name_cursor);
  1164. if (!verify_option && mode == ACCESS_WRITE && backup_option)
  1165. undo_last_backup ();
  1166. prompt = 1;
  1167. goto tryagain;
  1168. }
  1169. SET_BINARY_MODE (archive);
  1170. return true;
  1171. }
  1172. static bool
  1173. read_header0 (struct tar_stat_info *info)
  1174. {
  1175. enum read_header rc;
  1176. tar_stat_init (info);
  1177. rc = read_header (&current_header, info, read_header_auto);
  1178. if (rc == HEADER_SUCCESS)
  1179. {
  1180. set_next_block_after (current_header);
  1181. return true;
  1182. }
  1183. ERROR ((0, 0, _("This does not look like a tar archive")));
  1184. return false;
  1185. }
  1186. static bool
  1187. try_new_volume (void)
  1188. {
  1189. size_t status;
  1190. union block *header;
  1191. enum access_mode acc;
  1192. switch (subcommand_option)
  1193. {
  1194. case APPEND_SUBCOMMAND:
  1195. case CAT_SUBCOMMAND:
  1196. case UPDATE_SUBCOMMAND:
  1197. acc = ACCESS_UPDATE;
  1198. break;
  1199. default:
  1200. acc = ACCESS_READ;
  1201. break;
  1202. }
  1203. if (!new_volume (acc))
  1204. return true;
  1205. while ((status = rmtread (archive, record_start->buffer, record_size))
  1206. == SAFE_READ_ERROR)
  1207. archive_read_error ();
  1208. if (status != record_size)
  1209. short_read (status);
  1210. header = find_next_block ();
  1211. if (!header)
  1212. {
  1213. WARN ((0, 0, _("This does not look like a tar archive")));
  1214. return false;
  1215. }
  1216. switch (header->header.typeflag)
  1217. {
  1218. case XGLTYPE:
  1219. {
  1220. tar_stat_init (&dummy);
  1221. if (read_header (&header, &dummy, read_header_x_global)
  1222. != HEADER_SUCCESS_EXTENDED)
  1223. {
  1224. WARN ((0, 0, _("This does not look like a tar archive")));
  1225. return false;
  1226. }
  1227. xheader_decode (&dummy); /* decodes values from the global header */
  1228. tar_stat_destroy (&dummy);
  1229. /* The initial global header must be immediately followed by
  1230. an extended PAX header for the first member in this volume.
  1231. However, in some cases tar may split volumes in the middle
  1232. of a PAX header. This is incorrect, and should be fixed
  1233. in the future versions. In the meantime we must be
  1234. prepared to correctly list and extract such archives.
  1235. If this happens, the following call to read_header returns
  1236. HEADER_FAILURE, which is ignored.
  1237. See also tests/multiv07.at */
  1238. switch (read_header (&header, &dummy, read_header_auto))
  1239. {
  1240. case HEADER_SUCCESS:
  1241. set_next_block_after (header);
  1242. break;
  1243. case HEADER_FAILURE:
  1244. break;
  1245. default:
  1246. WARN ((0, 0, _("This does not look like a tar archive")));
  1247. return false;
  1248. }
  1249. break;
  1250. }
  1251. case GNUTYPE_VOLHDR:
  1252. if (!read_header0 (&dummy))
  1253. return false;
  1254. tar_stat_destroy (&dummy);
  1255. ASSIGN_STRING_N (&volume_label, current_header->header.name);
  1256. set_next_block_after (header);
  1257. header = find_next_block ();
  1258. if (! (header && header->header.typeflag == GNUTYPE_MULTIVOL))
  1259. break;
  1260. FALLTHROUGH;
  1261. case GNUTYPE_MULTIVOL:
  1262. if (!read_header0 (&dummy))
  1263. return false;
  1264. tar_stat_destroy (&dummy);
  1265. ASSIGN_STRING_N (&continued_file_name, current_header->header.name);
  1266. continued_file_size =
  1267. UINTMAX_FROM_HEADER (current_header->header.size);
  1268. continued_file_offset =
  1269. UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
  1270. break;
  1271. default:
  1272. break;
  1273. }
  1274. if (bufmap_head)
  1275. {
  1276. uintmax_t s;
  1277. if (!continued_file_name)
  1278. {
  1279. WARN ((0, 0, _("%s is not continued on this volume"),
  1280. quote (bufmap_head->file_name)));
  1281. return false;
  1282. }
  1283. if (strcmp (continued_file_name, bufmap_head->file_name))
  1284. {
  1285. if ((archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
  1286. && strlen (bufmap_head->file_name) >= NAME_FIELD_SIZE
  1287. && strncmp (continued_file_name, bufmap_head->file_name,
  1288. NAME_FIELD_SIZE) == 0)
  1289. WARN ((0, 0,
  1290. _("%s is possibly continued on this volume: header contains truncated name"),
  1291. quote (bufmap_head->file_name)));
  1292. else
  1293. {
  1294. WARN ((0, 0, _("%s is not continued on this volume"),
  1295. quote (bufmap_head->file_name)));
  1296. return false;
  1297. }
  1298. }
  1299. s = continued_file_size + continued_file_offset;
  1300. if (bufmap_head->sizetotal != s || s < continued_file_offset)
  1301. {
  1302. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  1303. char s1buf[UINTMAX_STRSIZE_BOUND];
  1304. char s2buf[UINTMAX_STRSIZE_BOUND];
  1305. WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
  1306. quote (continued_file_name),
  1307. STRINGIFY_BIGINT (bufmap_head->sizetotal, totsizebuf),
  1308. STRINGIFY_BIGINT (continued_file_size, s1buf),
  1309. STRINGIFY_BIGINT (continued_file_offset, s2buf)));
  1310. return false;
  1311. }
  1312. if (bufmap_head->sizetotal - bufmap_head->sizeleft !=
  1313. continued_file_offset)
  1314. {
  1315. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  1316. char s1buf[UINTMAX_STRSIZE_BOUND];
  1317. char s2buf[UINTMAX_STRSIZE_BOUND];
  1318. WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
  1319. STRINGIFY_BIGINT (bufmap_head->sizetotal, totsizebuf),
  1320. STRINGIFY_BIGINT (bufmap_head->sizeleft, s1buf),
  1321. STRINGIFY_BIGINT (continued_file_offset, s2buf)));
  1322. return false;
  1323. }
  1324. }
  1325. increase_volume_number ();
  1326. return true;
  1327. }
  1328. #define VOLUME_TEXT " Volume "
  1329. #define VOLUME_TEXT_LEN (sizeof VOLUME_TEXT - 1)
  1330. char *
  1331. drop_volume_label_suffix (const char *label)
  1332. {
  1333. const char *p;
  1334. size_t len = strlen (label);
  1335. if (len < 1)
  1336. return NULL;
  1337. for (p = label + len - 1; p > label && isdigit ((unsigned char) *p); p--)
  1338. ;
  1339. if (p > label && p - (VOLUME_TEXT_LEN - 1) > label)
  1340. {
  1341. p -= VOLUME_TEXT_LEN - 1;
  1342. if (memcmp (p, VOLUME_TEXT, VOLUME_TEXT_LEN) == 0)
  1343. {
  1344. char *s = xmalloc ((len = p - label) + 1);
  1345. memcpy (s, label, len);
  1346. s[len] = 0;
  1347. return s;
  1348. }
  1349. }
  1350. return NULL;
  1351. }
  1352. /* Check LABEL against the volume label, seen as a globbing
  1353. pattern. Return true if the pattern matches. In case of failure,
  1354. retry matching a volume sequence number before giving up in
  1355. multi-volume mode. */
  1356. static bool
  1357. check_label_pattern (const char *label)
  1358. {
  1359. char *string;
  1360. bool result = false;
  1361. if (fnmatch (volume_label_option, label, 0) == 0)
  1362. return true;
  1363. if (!multi_volume_option)
  1364. return false;
  1365. string = drop_volume_label_suffix (label);
  1366. if (string)
  1367. {
  1368. result = fnmatch (string, volume_label_option, 0) == 0;
  1369. free (string);
  1370. }
  1371. return result;
  1372. }
  1373. /* Check if the next block contains a volume label and if this matches
  1374. the one given in the command line */
  1375. static void
  1376. match_volume_label (void)
  1377. {
  1378. if (!volume_label)
  1379. {
  1380. union block *label = find_next_block ();
  1381. if (!label)
  1382. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  1383. quote (volume_label_option)));
  1384. if (label->header.typeflag == GNUTYPE_VOLHDR)
  1385. {
  1386. ASSIGN_STRING_N (&volume_label, label->header.name);
  1387. }
  1388. else if (label->header.typeflag == XGLTYPE)
  1389. {
  1390. struct tar_stat_info st;
  1391. tar_stat_init (&st);
  1392. xheader_read (&st.xhdr, label,
  1393. OFF_FROM_HEADER (label->header.size));
  1394. xheader_decode (&st);
  1395. tar_stat_destroy (&st);
  1396. }
  1397. }
  1398. if (!volume_label)
  1399. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  1400. quote (volume_label_option)));
  1401. if (!check_label_pattern (volume_label))
  1402. FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
  1403. quote_n (0, volume_label),
  1404. quote_n (1, volume_label_option)));
  1405. }
  1406. /* Mark the archive with volume label STR. */
  1407. static void
  1408. _write_volume_label (const char *str)
  1409. {
  1410. if (archive_format == POSIX_FORMAT)
  1411. xheader_store ("GNU.volume.label", &dummy, str);
  1412. else
  1413. {
  1414. union block *label = find_next_block ();
  1415. assume (label);
  1416. memset (label, 0, BLOCKSIZE);
  1417. strcpy (label->header.name, str);
  1418. assign_string (&current_stat_info.file_name, label->header.name);
  1419. current_stat_info.had_trailing_slash =
  1420. strip_trailing_slashes (current_stat_info.file_name);
  1421. label->header.typeflag = GNUTYPE_VOLHDR;
  1422. TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
  1423. finish_header (&current_stat_info, label, -1);
  1424. set_next_block_after (label);
  1425. }
  1426. }
  1427. #define VOL_SUFFIX "Volume"
  1428. /* Add a volume label to a part of multi-volume archive */
  1429. static void
  1430. add_volume_label (void)
  1431. {
  1432. char buf[UINTMAX_STRSIZE_BOUND];
  1433. char *p = STRINGIFY_BIGINT (volno, buf);
  1434. char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
  1435. + strlen (p) + 2);
  1436. sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
  1437. _write_volume_label (s);
  1438. free (s);
  1439. }
  1440. static void
  1441. add_chunk_header (struct bufmap *map)
  1442. {
  1443. if (archive_format == POSIX_FORMAT)
  1444. {
  1445. union block *blk;
  1446. struct tar_stat_info st;
  1447. memset (&st, 0, sizeof st);
  1448. st.orig_file_name = st.file_name = map->file_name;
  1449. st.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
  1450. st.stat.st_uid = getuid ();
  1451. st.stat.st_gid = getgid ();
  1452. st.orig_file_name = xheader_format_name (&st,
  1453. "%d/GNUFileParts/%f.%n",
  1454. volno);
  1455. st.file_name = st.orig_file_name;
  1456. st.archive_file_size = st.stat.st_size = map->sizeleft;
  1457. blk = start_header (&st);
  1458. if (!blk)
  1459. abort (); /* FIXME */
  1460. simple_finish_header (write_extended (false, &st, blk));
  1461. free (st.orig_file_name);
  1462. }
  1463. }
  1464. /* Add a volume label to the current archive */
  1465. static void
  1466. write_volume_label (void)
  1467. {
  1468. if (multi_volume_option)
  1469. add_volume_label ();
  1470. else
  1471. _write_volume_label (volume_label_option);
  1472. }
  1473. /* Write GNU multi-volume header */
  1474. static void
  1475. gnu_add_multi_volume_header (struct bufmap *map)
  1476. {
  1477. int tmp;
  1478. union block *block = find_next_block ();
  1479. size_t len = strlen (map->file_name);
  1480. if (len > NAME_FIELD_SIZE)
  1481. {
  1482. WARN ((0, 0,
  1483. _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
  1484. quotearg_colon (map->file_name)));
  1485. len = NAME_FIELD_SIZE;
  1486. }
  1487. memset (block, 0, BLOCKSIZE);
  1488. memcpy (block->header.name, map->file_name, len);
  1489. block->header.typeflag = GNUTYPE_MULTIVOL;
  1490. OFF_TO_CHARS (map->sizeleft, block->header.size);
  1491. OFF_TO_CHARS (map->sizetotal - map->sizeleft,
  1492. block->oldgnu_header.offset);
  1493. tmp = verbose_option;
  1494. verbose_option = 0;
  1495. finish_header (&current_stat_info, block, -1);
  1496. verbose_option = tmp;
  1497. set_next_block_after (block);
  1498. }
  1499. /* Add a multi volume header to the current archive. The exact header format
  1500. depends on the archive format. */
  1501. static void
  1502. add_multi_volume_header (struct bufmap *map)
  1503. {
  1504. if (archive_format == POSIX_FORMAT)
  1505. {
  1506. off_t d = map->sizetotal - map->sizeleft;
  1507. xheader_store ("GNU.volume.filename", &dummy, map->file_name);
  1508. xheader_store ("GNU.volume.size", &dummy, &map->sizeleft);
  1509. xheader_store ("GNU.volume.offset", &dummy, &d);
  1510. }
  1511. else
  1512. gnu_add_multi_volume_header (map);
  1513. }
  1514. /* Low-level flush functions */
  1515. /* Simple flush read (no multi-volume or label extensions) */
  1516. static void
  1517. simple_flush_read (void)
  1518. {
  1519. size_t status; /* result from system call */
  1520. checkpoint_run (false);
  1521. /* Clear the count of errors. This only applies to a single call to
  1522. flush_read. */
  1523. read_error_count = 0; /* clear error count */
  1524. if (write_archive_to_stdout && record_start_block != 0)
  1525. {
  1526. archive = STDOUT_FILENO;
  1527. status = sys_write_archive_buffer ();
  1528. archive = STDIN_FILENO;
  1529. if (status != record_size)
  1530. archive_write_error (status);
  1531. }
  1532. for (;;)
  1533. {
  1534. status = rmtread (archive, record_start->buffer, record_size);
  1535. if (status == record_size)
  1536. {
  1537. records_read++;
  1538. return;
  1539. }
  1540. if (status == SAFE_READ_ERROR)
  1541. {
  1542. archive_read_error ();
  1543. continue; /* try again */
  1544. }
  1545. break;
  1546. }
  1547. short_read (status);
  1548. }
  1549. /* Simple flush write (no multi-volume or label extensions) */
  1550. static void
  1551. simple_flush_write (size_t level MAYBE_UNUSED)
  1552. {
  1553. ssize_t status;
  1554. status = _flush_write ();
  1555. if (status != record_size)
  1556. archive_write_error (status);
  1557. else
  1558. {
  1559. records_written++;
  1560. bytes_written += status;
  1561. }
  1562. }
  1563. /* GNU flush functions. These support multi-volume and archive labels in
  1564. GNU and PAX archive formats. */
  1565. static void
  1566. _gnu_flush_read (void)
  1567. {
  1568. size_t status; /* result from system call */
  1569. checkpoint_run (false);
  1570. /* Clear the count of errors. This only applies to a single call to
  1571. flush_read. */
  1572. read_error_count = 0; /* clear error count */
  1573. if (write_archive_to_stdout && record_start_block != 0)
  1574. {
  1575. archive = STDOUT_FILENO;
  1576. status = sys_write_archive_buffer ();
  1577. archive = STDIN_FILENO;
  1578. if (status != record_size)
  1579. archive_write_error (status);
  1580. }
  1581. for (;;)
  1582. {
  1583. status = rmtread (archive, record_start->buffer, record_size);
  1584. if (status == record_size)
  1585. {
  1586. records_read++;
  1587. return;
  1588. }
  1589. /* The condition below used to include
  1590. || (status > 0 && !read_full_records)
  1591. This is incorrect since even if new_volume() succeeds, the
  1592. subsequent call to rmtread will overwrite the chunk of data
  1593. already read in the buffer, so the processing will fail */
  1594. if ((status == 0
  1595. || (status == SAFE_READ_ERROR && errno == ENOSPC))
  1596. && multi_volume_option)
  1597. {
  1598. while (!try_new_volume ())
  1599. ;
  1600. if (current_block == record_end)
  1601. /* Necessary for blocking_factor == 1 */
  1602. flush_archive();
  1603. return;
  1604. }
  1605. else if (status == SAFE_READ_ERROR)
  1606. {
  1607. archive_read_error ();
  1608. continue;
  1609. }
  1610. break;
  1611. }
  1612. short_read (status);
  1613. }
  1614. static void
  1615. gnu_flush_read (void)
  1616. {
  1617. flush_read_ptr = simple_flush_read; /* Avoid recursion */
  1618. _gnu_flush_read ();
  1619. flush_read_ptr = gnu_flush_read;
  1620. }
  1621. static void
  1622. _gnu_flush_write (size_t buffer_level)
  1623. {
  1624. ssize_t status;
  1625. union block *header;
  1626. char *copy_ptr;
  1627. size_t copy_size;
  1628. size_t bufsize;
  1629. struct bufmap *map;
  1630. status = _flush_write ();
  1631. if (status != record_size && !multi_volume_option)
  1632. archive_write_error (status);
  1633. else
  1634. {
  1635. if (status)
  1636. records_written++;
  1637. bytes_written += status;
  1638. }
  1639. if (status == record_size)
  1640. {
  1641. return;
  1642. }
  1643. map = bufmap_locate (status);
  1644. if (status % BLOCKSIZE)
  1645. {
  1646. ERROR ((0, 0, _("write did not end on a block boundary")));
  1647. archive_write_error (status);
  1648. }
  1649. /* In multi-volume mode. */
  1650. /* ENXIO is for the UNIX PC. */
  1651. if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  1652. archive_write_error (status);
  1653. if (!new_volume (ACCESS_WRITE))
  1654. return;
  1655. tar_stat_destroy (&dummy);
  1656. increase_volume_number ();
  1657. prev_written += bytes_written;
  1658. bytes_written = 0;
  1659. copy_ptr = record_start->buffer + status;
  1660. copy_size = buffer_level - status;
  1661. /* Switch to the next buffer */
  1662. record_index = !record_index;
  1663. init_buffer ();
  1664. inhibit_map = 1;
  1665. if (volume_label_option)
  1666. add_volume_label ();
  1667. if (map)
  1668. add_multi_volume_header (map);
  1669. write_extended (true, &dummy, find_next_block ());
  1670. tar_stat_destroy (&dummy);
  1671. if (map)
  1672. add_chunk_header (map);
  1673. header = find_next_block ();
  1674. bufmap_reset (map, header - record_start);
  1675. bufsize = available_space_after (header);
  1676. inhibit_map = 0;
  1677. while (bufsize < copy_size)
  1678. {
  1679. memcpy (header->buffer, copy_ptr, bufsize);
  1680. copy_ptr += bufsize;
  1681. copy_size -= bufsize;
  1682. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  1683. header = find_next_block ();
  1684. bufsize = available_space_after (header);
  1685. }
  1686. memcpy (header->buffer, copy_ptr, copy_size);
  1687. memset (header->buffer + copy_size, 0, bufsize - copy_size);
  1688. set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
  1689. find_next_block ();
  1690. }
  1691. static void
  1692. gnu_flush_write (size_t buffer_level)
  1693. {
  1694. flush_write_ptr = simple_flush_write; /* Avoid recursion */
  1695. _gnu_flush_write (buffer_level);
  1696. flush_write_ptr = gnu_flush_write;
  1697. }
  1698. void
  1699. flush_read (void)
  1700. {
  1701. flush_read_ptr ();
  1702. }
  1703. void
  1704. flush_write (void)
  1705. {
  1706. flush_write_ptr (record_size);
  1707. }
  1708. void
  1709. open_archive (enum access_mode wanted_access)
  1710. {
  1711. flush_read_ptr = gnu_flush_read;
  1712. flush_write_ptr = gnu_flush_write;
  1713. _open_archive (wanted_access);
  1714. switch (wanted_access)
  1715. {
  1716. case ACCESS_READ:
  1717. case ACCESS_UPDATE:
  1718. if (volume_label_option)
  1719. match_volume_label ();
  1720. break;
  1721. case ACCESS_WRITE:
  1722. records_written = 0;
  1723. if (volume_label_option)
  1724. write_volume_label ();
  1725. break;
  1726. }
  1727. set_volume_start_time ();
  1728. }