buffer.c 49 KB

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