buffer.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  1. /* Buffer management for tar.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
  4. Written by John Gilmore, on 1985-08-25.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <system.h>
  17. #include <system-ioctl.h>
  18. #include <signal.h>
  19. #include <closeout.h>
  20. #include <fnmatch.h>
  21. #include <getline.h>
  22. #include <human.h>
  23. #include <quotearg.h>
  24. #include "common.h"
  25. #include <rmt.h>
  26. /* Number of retries before giving up on read. */
  27. #define READ_ERROR_MAX 10
  28. /* Globbing pattern to append to volume label if initial match failed. */
  29. #define VOLUME_LABEL_APPEND " Volume [1-9]*"
  30. /* Variables. */
  31. static tarlong prev_written; /* bytes written on previous volumes */
  32. static tarlong bytes_written; /* bytes written on this volume */
  33. static void *record_buffer[2]; /* allocated memory */
  34. union block *record_buffer_aligned[2];
  35. static int record_index;
  36. /* FIXME: The following variables should ideally be static to this
  37. module. However, this cannot be done yet. The cleanup continues! */
  38. union block *record_start; /* start of record of archive */
  39. union block *record_end; /* last+1 block of archive record */
  40. union block *current_block; /* current block of archive */
  41. enum access_mode access_mode; /* how do we handle the archive */
  42. off_t records_read; /* number of records read from this archive */
  43. off_t records_written; /* likewise, for records written */
  44. extern off_t records_skipped; /* number of records skipped at the start
  45. of the archive, defined in delete.c */
  46. static off_t record_start_block; /* block ordinal at record_start */
  47. /* Where we write list messages (not errors, not interactions) to. */
  48. FILE *stdlis;
  49. static void backspace_output (void);
  50. /* PID of child program, if compress_option or remote archive access. */
  51. static pid_t child_pid;
  52. /* Error recovery stuff */
  53. static int read_error_count;
  54. /* Have we hit EOF yet? */
  55. static bool hit_eof;
  56. /* Checkpointing counter */
  57. static unsigned checkpoint;
  58. static bool read_full_records = false;
  59. /* We're reading, but we just read the last block and it's time to update.
  60. Declared in update.c
  61. As least EXTERN like this one as possible. (?? --gray)
  62. FIXME: Either eliminate it or move it to common.h.
  63. */
  64. extern bool time_to_start_writing;
  65. bool write_archive_to_stdout;
  66. void (*flush_write_ptr) (size_t);
  67. void (*flush_read_ptr) (void);
  68. char *volume_label;
  69. char *continued_file_name;
  70. uintmax_t continued_file_size;
  71. uintmax_t continued_file_offset;
  72. static int volno = 1; /* which volume of a multi-volume tape we're
  73. on */
  74. static int global_volno = 1; /* volume number to print in external
  75. messages */
  76. bool write_archive_to_stdout;
  77. /* Used by flush_read and flush_write to store the real info about saved
  78. names. */
  79. static char *real_s_name;
  80. static off_t real_s_totsize;
  81. static off_t real_s_sizeleft;
  82. /* Multi-volume tracking support */
  83. static char *save_name; /* name of the file we are currently writing */
  84. static off_t save_totsize; /* total size of file we are writing, only
  85. valid if save_name is nonzero */
  86. static off_t save_sizeleft; /* where we are in the file we are writing,
  87. only valid if save_name is nonzero */
  88. void
  89. mv_begin (struct tar_stat_info *st)
  90. {
  91. if (multi_volume_option)
  92. {
  93. assign_string (&save_name, st->orig_file_name);
  94. save_totsize = save_sizeleft = st->stat.st_size;
  95. }
  96. }
  97. void
  98. mv_end ()
  99. {
  100. if (multi_volume_option)
  101. assign_string (&save_name, 0);
  102. }
  103. void
  104. mv_total_size (off_t size)
  105. {
  106. save_totsize = size;
  107. }
  108. void
  109. mv_size_left (off_t size)
  110. {
  111. save_sizeleft = size;
  112. }
  113. /* Functions. */
  114. void
  115. clear_read_error_count (void)
  116. {
  117. read_error_count = 0;
  118. }
  119. /* Time-related functions */
  120. double duration;
  121. void
  122. set_start_time ()
  123. {
  124. gettime (&start_time);
  125. volume_start_time = start_time;
  126. last_stat_time = start_time;
  127. }
  128. void
  129. set_volume_start_time ()
  130. {
  131. gettime (&volume_start_time);
  132. last_stat_time = volume_start_time;
  133. }
  134. void
  135. compute_duration ()
  136. {
  137. struct timespec now;
  138. gettime (&now);
  139. duration += ((now.tv_sec - last_stat_time.tv_sec)
  140. + (now.tv_nsec - last_stat_time.tv_nsec) / 1e9);
  141. gettime (&last_stat_time);
  142. }
  143. /* Compression detection */
  144. enum compress_type {
  145. ct_none,
  146. ct_compress,
  147. ct_gzip,
  148. ct_bzip2
  149. };
  150. struct zip_magic
  151. {
  152. enum compress_type type;
  153. size_t length;
  154. char *magic;
  155. char *program;
  156. char *option;
  157. };
  158. static struct zip_magic const magic[] = {
  159. { ct_none, },
  160. { ct_compress, 2, "\037\235", "compress", "-Z" },
  161. { ct_gzip, 2, "\037\213", "gzip", "-z" },
  162. { ct_bzip2, 3, "BZh", "bzip2", "-j" },
  163. };
  164. #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
  165. #define compress_option(t) magic[t].option
  166. #define compress_program(t) magic[t].program
  167. /* Check if the file ARCHIVE is a compressed archive. */
  168. enum compress_type
  169. check_compressed_archive ()
  170. {
  171. struct zip_magic const *p;
  172. bool sfr;
  173. /* Prepare global data needed for find_next_block: */
  174. record_end = record_start; /* set up for 1st record = # 0 */
  175. sfr = read_full_records;
  176. read_full_records = true; /* Suppress fatal error on reading a partial
  177. record */
  178. find_next_block ();
  179. /* Restore global values */
  180. read_full_records = sfr;
  181. if (tar_checksum (record_start, true) == HEADER_SUCCESS)
  182. /* Probably a valid header */
  183. return ct_none;
  184. for (p = magic + 1; p < magic + NMAGIC; p++)
  185. if (memcmp (record_start->buffer, p->magic, p->length) == 0)
  186. return p->type;
  187. return ct_none;
  188. }
  189. /* Open an archive named archive_name_array[0]. Detect if it is
  190. a compressed archive of known type and use corresponding decompression
  191. program if so */
  192. int
  193. open_compressed_archive ()
  194. {
  195. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  196. MODE_RW, rsh_command_option);
  197. if (archive == -1)
  198. return archive;
  199. if (!multi_volume_option)
  200. {
  201. enum compress_type type = check_compressed_archive ();
  202. if (type == ct_none)
  203. return archive;
  204. /* FD is not needed any more */
  205. rmtclose (archive);
  206. hit_eof = false; /* It might have been set by find_next_block in
  207. check_compressed_archive */
  208. /* Open compressed archive */
  209. use_compress_program_option = compress_program (type);
  210. child_pid = sys_child_open_for_uncompress ();
  211. read_full_records = true;
  212. }
  213. records_read = 0;
  214. record_end = record_start; /* set up for 1st record = # 0 */
  215. return archive;
  216. }
  217. static void
  218. print_stats (FILE *fp, const char *text, tarlong numbytes)
  219. {
  220. char bytes[sizeof (tarlong) * CHAR_BIT];
  221. char abbr[LONGEST_HUMAN_READABLE + 1];
  222. char rate[LONGEST_HUMAN_READABLE + 1];
  223. int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
  224. sprintf (bytes, TARLONG_FORMAT, numbytes);
  225. fprintf (fp, "%s: %s (%s, %s/s)\n",
  226. text, bytes,
  227. human_readable (numbytes, abbr, human_opts, 1, 1),
  228. (0 < duration && numbytes / duration < (uintmax_t) -1
  229. ? human_readable (numbytes / duration, rate, human_opts, 1, 1)
  230. : "?"));
  231. }
  232. void
  233. print_total_stats ()
  234. {
  235. switch (subcommand_option)
  236. {
  237. case CREATE_SUBCOMMAND:
  238. case CAT_SUBCOMMAND:
  239. case UPDATE_SUBCOMMAND:
  240. case APPEND_SUBCOMMAND:
  241. /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
  242. print_stats (stderr, _("Total bytes written"),
  243. prev_written + bytes_written);
  244. break;
  245. case DELETE_SUBCOMMAND:
  246. {
  247. char buf[UINTMAX_STRSIZE_BOUND];
  248. print_stats (stderr, _("Total bytes read"),
  249. records_read * record_size);
  250. print_stats (stderr, _("Total bytes written"),
  251. prev_written + bytes_written);
  252. fprintf (stderr, _("Total bytes deleted: %s\n"),
  253. STRINGIFY_BIGINT ((records_read - records_skipped)
  254. * record_size
  255. - (prev_written + bytes_written), buf));
  256. }
  257. break;
  258. case EXTRACT_SUBCOMMAND:
  259. case LIST_SUBCOMMAND:
  260. case DIFF_SUBCOMMAND:
  261. print_stats (stderr, _("Total bytes read"),
  262. records_read * record_size);
  263. break;
  264. default:
  265. abort ();
  266. }
  267. }
  268. /* Compute and return the block ordinal at current_block. */
  269. off_t
  270. current_block_ordinal (void)
  271. {
  272. return record_start_block + (current_block - record_start);
  273. }
  274. /* If the EOF flag is set, reset it, as well as current_block, etc. */
  275. void
  276. reset_eof (void)
  277. {
  278. if (hit_eof)
  279. {
  280. hit_eof = false;
  281. current_block = record_start;
  282. record_end = record_start + blocking_factor;
  283. access_mode = ACCESS_WRITE;
  284. }
  285. }
  286. /* Return the location of the next available input or output block.
  287. Return zero for EOF. Once we have returned zero, we just keep returning
  288. it, to avoid accidentally going on to the next file on the tape. */
  289. union block *
  290. find_next_block (void)
  291. {
  292. if (current_block == record_end)
  293. {
  294. if (hit_eof)
  295. return 0;
  296. flush_archive ();
  297. if (current_block == record_end)
  298. {
  299. hit_eof = true;
  300. return 0;
  301. }
  302. }
  303. return current_block;
  304. }
  305. /* Indicate that we have used all blocks up thru BLOCK. */
  306. void
  307. set_next_block_after (union block *block)
  308. {
  309. while (block >= current_block)
  310. current_block++;
  311. /* Do *not* flush the archive here. If we do, the same argument to
  312. set_next_block_after could mean the next block (if the input record
  313. is exactly one block long), which is not what is intended. */
  314. if (current_block > record_end)
  315. abort ();
  316. }
  317. /* Return the number of bytes comprising the space between POINTER
  318. through the end of the current buffer of blocks. This space is
  319. available for filling with data, or taking data from. POINTER is
  320. usually (but not always) the result of previous find_next_block call. */
  321. size_t
  322. available_space_after (union block *pointer)
  323. {
  324. return record_end->buffer - pointer->buffer;
  325. }
  326. /* Close file having descriptor FD, and abort if close unsuccessful. */
  327. void
  328. xclose (int fd)
  329. {
  330. if (close (fd) != 0)
  331. close_error (_("(pipe)"));
  332. }
  333. static void
  334. init_buffer ()
  335. {
  336. if (! record_buffer_aligned[record_index])
  337. record_buffer_aligned[record_index] =
  338. page_aligned_alloc (&record_buffer[record_index], record_size);
  339. record_start = record_buffer_aligned[record_index];
  340. current_block = record_start;
  341. record_end = record_start + blocking_factor;
  342. }
  343. /* Open an archive file. The argument specifies whether we are
  344. reading or writing, or both. */
  345. static void
  346. _open_archive (enum access_mode wanted_access)
  347. {
  348. int backed_up_flag = 0;
  349. if (record_size == 0)
  350. FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
  351. if (archive_names == 0)
  352. FATAL_ERROR ((0, 0, _("No archive name given")));
  353. tar_stat_destroy (&current_stat_info);
  354. save_name = 0;
  355. real_s_name = 0;
  356. record_index = 0;
  357. init_buffer ();
  358. /* When updating the archive, we start with reading. */
  359. access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
  360. read_full_records = read_full_records_option;
  361. records_read = 0;
  362. if (use_compress_program_option)
  363. {
  364. switch (wanted_access)
  365. {
  366. case ACCESS_READ:
  367. child_pid = sys_child_open_for_uncompress ();
  368. read_full_records = true;
  369. record_end = record_start; /* set up for 1st record = # 0 */
  370. break;
  371. case ACCESS_WRITE:
  372. child_pid = sys_child_open_for_compress ();
  373. break;
  374. case ACCESS_UPDATE:
  375. abort (); /* Should not happen */
  376. break;
  377. }
  378. }
  379. else if (strcmp (archive_name_array[0], "-") == 0)
  380. {
  381. read_full_records = true; /* could be a pipe, be safe */
  382. if (verify_option)
  383. FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
  384. switch (wanted_access)
  385. {
  386. case ACCESS_READ:
  387. {
  388. enum compress_type type;
  389. archive = STDIN_FILENO;
  390. type = check_compressed_archive ();
  391. if (type != ct_none)
  392. FATAL_ERROR ((0, 0,
  393. _("Archive is compressed. Use %s option"),
  394. compress_option (type)));
  395. }
  396. break;
  397. case ACCESS_WRITE:
  398. archive = STDOUT_FILENO;
  399. break;
  400. case ACCESS_UPDATE:
  401. archive = STDIN_FILENO;
  402. write_archive_to_stdout = true;
  403. record_end = record_start; /* set up for 1st record = # 0 */
  404. break;
  405. }
  406. }
  407. else if (verify_option)
  408. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  409. MODE_RW, rsh_command_option);
  410. else
  411. switch (wanted_access)
  412. {
  413. case ACCESS_READ:
  414. archive = open_compressed_archive ();
  415. break;
  416. case ACCESS_WRITE:
  417. if (backup_option)
  418. {
  419. maybe_backup_file (archive_name_array[0], 1);
  420. backed_up_flag = 1;
  421. }
  422. archive = rmtcreat (archive_name_array[0], MODE_RW,
  423. rsh_command_option);
  424. break;
  425. case ACCESS_UPDATE:
  426. archive = rmtopen (archive_name_array[0],
  427. O_RDWR | O_CREAT | O_BINARY,
  428. MODE_RW, rsh_command_option);
  429. if (check_compressed_archive () != ct_none)
  430. FATAL_ERROR ((0, 0,
  431. _("Cannot update compressed archives")));
  432. break;
  433. }
  434. if (archive < 0
  435. || (! _isrmt (archive) && !sys_get_archive_stat ()))
  436. {
  437. int saved_errno = errno;
  438. if (backed_up_flag)
  439. undo_last_backup ();
  440. errno = saved_errno;
  441. open_fatal (archive_name_array[0]);
  442. }
  443. sys_detect_dev_null_output ();
  444. sys_save_archive_dev_ino ();
  445. SET_BINARY_MODE (archive);
  446. switch (wanted_access)
  447. {
  448. case ACCESS_READ:
  449. find_next_block (); /* read it in, check for EOF */
  450. break;
  451. case ACCESS_UPDATE:
  452. case ACCESS_WRITE:
  453. records_written = 0;
  454. break;
  455. }
  456. }
  457. static void
  458. do_checkpoint (bool write)
  459. {
  460. if (checkpoint_option && !(++checkpoint % checkpoint_option))
  461. {
  462. switch (checkpoint_style)
  463. {
  464. case checkpoint_dot:
  465. fputc ('.', stdlis);
  466. fflush (stdlis);
  467. break;
  468. case checkpoint_text:
  469. if (write)
  470. /* TRANSLATORS: This is a ``checkpoint of write operation'',
  471. *not* ``Writing a checkpoint''.
  472. E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
  473. *not* ``Escribiendo un punto de comprobaci@'on'' */
  474. WARN ((0, 0, _("Write checkpoint %u"), checkpoint));
  475. else
  476. /* TRANSLATORS: This is a ``checkpoint of read operation'',
  477. *not* ``Reading a checkpoint''.
  478. E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
  479. *not* ``Leyendo un punto de comprobaci@'on'' */
  480. WARN ((0, 0, _("Read checkpoint %u"), checkpoint));
  481. break;
  482. }
  483. }
  484. }
  485. /* Perform a write to flush the buffer. */
  486. ssize_t
  487. _flush_write (void)
  488. {
  489. ssize_t status;
  490. do_checkpoint (true);
  491. if (tape_length_option && tape_length_option <= bytes_written)
  492. {
  493. errno = ENOSPC;
  494. status = 0;
  495. }
  496. else if (dev_null_output)
  497. status = record_size;
  498. else
  499. status = sys_write_archive_buffer ();
  500. return status;
  501. }
  502. /* Handle write errors on the archive. Write errors are always fatal.
  503. Hitting the end of a volume does not cause a write error unless the
  504. write was the first record of the volume. */
  505. void
  506. archive_write_error (ssize_t status)
  507. {
  508. /* It might be useful to know how much was written before the error
  509. occurred. */
  510. if (totals_option)
  511. {
  512. int e = errno;
  513. print_total_stats ();
  514. errno = e;
  515. }
  516. write_fatal_details (*archive_name_cursor, status, record_size);
  517. }
  518. /* Handle read errors on the archive. If the read should be retried,
  519. return to the caller. */
  520. void
  521. archive_read_error (void)
  522. {
  523. read_error (*archive_name_cursor);
  524. if (record_start_block == 0)
  525. FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
  526. /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
  527. then give up on reading the archive. */
  528. if (read_error_count++ > READ_ERROR_MAX)
  529. FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
  530. return;
  531. }
  532. static void
  533. short_read (size_t status)
  534. {
  535. size_t left; /* bytes left */
  536. char *more; /* pointer to next byte to read */
  537. more = record_start->buffer + status;
  538. left = record_size - status;
  539. while (left % BLOCKSIZE != 0
  540. || (left && status && read_full_records))
  541. {
  542. if (status)
  543. while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
  544. archive_read_error ();
  545. if (status == 0)
  546. break;
  547. if (! read_full_records)
  548. {
  549. unsigned long rest = record_size - left;
  550. FATAL_ERROR ((0, 0,
  551. ngettext ("Unaligned block (%lu byte) in archive",
  552. "Unaligned block (%lu bytes) in archive",
  553. rest),
  554. rest));
  555. }
  556. /* User warned us about this. Fix up. */
  557. left -= status;
  558. more += status;
  559. }
  560. /* FIXME: for size=0, multi-volume support. On the first record, warn
  561. about the problem. */
  562. if (!read_full_records && verbose_option > 1
  563. && record_start_block == 0 && status != 0)
  564. {
  565. unsigned long rsize = (record_size - left) / BLOCKSIZE;
  566. WARN ((0, 0,
  567. ngettext ("Record size = %lu block",
  568. "Record size = %lu blocks",
  569. rsize),
  570. rsize));
  571. }
  572. record_end = record_start + (record_size - left) / BLOCKSIZE;
  573. records_read++;
  574. }
  575. /* Flush the current buffer to/from the archive. */
  576. void
  577. flush_archive (void)
  578. {
  579. size_t buffer_level = current_block->buffer - record_start->buffer;
  580. record_start_block += record_end - record_start;
  581. current_block = record_start;
  582. record_end = record_start + blocking_factor;
  583. if (access_mode == ACCESS_READ && time_to_start_writing)
  584. {
  585. access_mode = ACCESS_WRITE;
  586. time_to_start_writing = false;
  587. backspace_output ();
  588. }
  589. switch (access_mode)
  590. {
  591. case ACCESS_READ:
  592. flush_read ();
  593. break;
  594. case ACCESS_WRITE:
  595. flush_write_ptr (buffer_level);
  596. break;
  597. case ACCESS_UPDATE:
  598. abort ();
  599. }
  600. }
  601. /* Backspace the archive descriptor by one record worth. If it's a
  602. tape, MTIOCTOP will work. If it's something else, try to seek on
  603. it. If we can't seek, we lose! */
  604. static void
  605. backspace_output (void)
  606. {
  607. #ifdef MTIOCTOP
  608. {
  609. struct mtop operation;
  610. operation.mt_op = MTBSR;
  611. operation.mt_count = 1;
  612. if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  613. return;
  614. if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  615. return;
  616. }
  617. #endif
  618. {
  619. off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  620. /* Seek back to the beginning of this record and start writing there. */
  621. position -= record_size;
  622. if (position < 0)
  623. position = 0;
  624. if (rmtlseek (archive, position, SEEK_SET) != position)
  625. {
  626. /* Lseek failed. Try a different method. */
  627. WARN ((0, 0,
  628. _("Cannot backspace archive file; it may be unreadable without -i")));
  629. /* Replace the first part of the record with NULs. */
  630. if (record_start->buffer != output_start)
  631. memset (record_start->buffer, 0,
  632. output_start - record_start->buffer);
  633. }
  634. }
  635. }
  636. off_t
  637. seek_archive (off_t size)
  638. {
  639. off_t start = current_block_ordinal ();
  640. off_t offset;
  641. off_t nrec, nblk;
  642. off_t skipped = (blocking_factor - (current_block - record_start));
  643. size -= skipped * BLOCKSIZE;
  644. if (size < record_size)
  645. return 0;
  646. /* FIXME: flush? */
  647. /* Compute number of records to skip */
  648. nrec = size / record_size;
  649. offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
  650. if (offset < 0)
  651. return offset;
  652. if (offset % record_size)
  653. FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
  654. /* Convert to number of records */
  655. offset /= BLOCKSIZE;
  656. /* Compute number of skipped blocks */
  657. nblk = offset - start;
  658. /* Update buffering info */
  659. records_read += nblk / blocking_factor;
  660. record_start_block = offset - blocking_factor;
  661. current_block = record_end;
  662. return nblk;
  663. }
  664. /* Close the archive file. */
  665. void
  666. close_archive (void)
  667. {
  668. if (time_to_start_writing || access_mode == ACCESS_WRITE)
  669. {
  670. flush_archive ();
  671. if (current_block > record_start)
  672. flush_archive ();
  673. }
  674. sys_drain_input_pipe ();
  675. compute_duration ();
  676. if (verify_option)
  677. verify_volume ();
  678. if (rmtclose (archive) != 0)
  679. close_warn (*archive_name_cursor);
  680. sys_wait_for_child (child_pid);
  681. tar_stat_destroy (&current_stat_info);
  682. if (save_name)
  683. free (save_name);
  684. if (real_s_name)
  685. free (real_s_name);
  686. free (record_buffer[0]);
  687. free (record_buffer[1]);
  688. }
  689. /* Called to initialize the global volume number. */
  690. void
  691. init_volume_number (void)
  692. {
  693. FILE *file = fopen (volno_file_option, "r");
  694. if (file)
  695. {
  696. if (fscanf (file, "%d", &global_volno) != 1
  697. || global_volno < 0)
  698. FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
  699. quotearg_colon (volno_file_option)));
  700. if (ferror (file))
  701. read_error (volno_file_option);
  702. if (fclose (file) != 0)
  703. close_error (volno_file_option);
  704. }
  705. else if (errno != ENOENT)
  706. open_error (volno_file_option);
  707. }
  708. /* Called to write out the closing global volume number. */
  709. void
  710. closeout_volume_number (void)
  711. {
  712. FILE *file = fopen (volno_file_option, "w");
  713. if (file)
  714. {
  715. fprintf (file, "%d\n", global_volno);
  716. if (ferror (file))
  717. write_error (volno_file_option);
  718. if (fclose (file) != 0)
  719. close_error (volno_file_option);
  720. }
  721. else
  722. open_error (volno_file_option);
  723. }
  724. static void
  725. increase_volume_number ()
  726. {
  727. global_volno++;
  728. if (global_volno < 0)
  729. FATAL_ERROR ((0, 0, _("Volume number overflow")));
  730. volno++;
  731. }
  732. void
  733. change_tape_menu (FILE *read_file)
  734. {
  735. char *input_buffer = NULL;
  736. size_t size = 0;
  737. bool stop = false;
  738. while (!stop)
  739. {
  740. fputc ('\007', stderr);
  741. fprintf (stderr,
  742. _("Prepare volume #%d for %s and hit return: "),
  743. global_volno + 1, quote (*archive_name_cursor));
  744. fflush (stderr);
  745. if (getline (&input_buffer, &size, read_file) <= 0)
  746. {
  747. WARN ((0, 0, _("EOF where user reply was expected")));
  748. if (subcommand_option != EXTRACT_SUBCOMMAND
  749. && subcommand_option != LIST_SUBCOMMAND
  750. && subcommand_option != DIFF_SUBCOMMAND)
  751. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  752. fatal_exit ();
  753. }
  754. if (input_buffer[0] == '\n'
  755. || input_buffer[0] == 'y'
  756. || input_buffer[0] == 'Y')
  757. break;
  758. switch (input_buffer[0])
  759. {
  760. case '?':
  761. {
  762. fprintf (stderr, _("\
  763. n name Give a new file name for the next (and subsequent) volume(s)\n\
  764. q Abort tar\n\
  765. y or newline Continue operation\n"));
  766. if (!restrict_option)
  767. fprintf (stderr, _(" ! Spawn a subshell\n"));
  768. fprintf (stderr, _(" ? Print this list\n"));
  769. }
  770. break;
  771. case 'q':
  772. /* Quit. */
  773. WARN ((0, 0, _("No new volume; exiting.\n")));
  774. if (subcommand_option != EXTRACT_SUBCOMMAND
  775. && subcommand_option != LIST_SUBCOMMAND
  776. && subcommand_option != DIFF_SUBCOMMAND)
  777. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  778. fatal_exit ();
  779. case 'n':
  780. /* Get new file name. */
  781. {
  782. char *name;
  783. char *cursor;
  784. for (name = input_buffer + 1;
  785. *name == ' ' || *name == '\t';
  786. name++)
  787. ;
  788. for (cursor = name; *cursor && *cursor != '\n'; cursor++)
  789. ;
  790. *cursor = '\0';
  791. if (name[0])
  792. {
  793. /* FIXME: the following allocation is never reclaimed. */
  794. *archive_name_cursor = xstrdup (name);
  795. stop = true;
  796. }
  797. else
  798. fprintf (stderr, "%s",
  799. _("File name not specified. Try again.\n"));
  800. }
  801. break;
  802. case '!':
  803. if (!restrict_option)
  804. {
  805. sys_spawn_shell ();
  806. break;
  807. }
  808. /* FALL THROUGH */
  809. default:
  810. fprintf (stderr, _("Invalid input. Type ? for help.\n"));
  811. }
  812. }
  813. free (input_buffer);
  814. }
  815. /* We've hit the end of the old volume. Close it and open the next one.
  816. Return nonzero on success.
  817. */
  818. static bool
  819. new_volume (enum access_mode mode)
  820. {
  821. static FILE *read_file;
  822. static int looped;
  823. int prompt;
  824. if (!read_file && !info_script_option)
  825. /* FIXME: if fopen is used, it will never be closed. */
  826. read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
  827. if (now_verifying)
  828. return false;
  829. if (verify_option)
  830. verify_volume ();
  831. assign_string (&volume_label, NULL);
  832. assign_string (&continued_file_name, NULL);
  833. continued_file_size = continued_file_offset = 0;
  834. if (rmtclose (archive) != 0)
  835. close_warn (*archive_name_cursor);
  836. archive_name_cursor++;
  837. if (archive_name_cursor == archive_name_array + archive_names)
  838. {
  839. archive_name_cursor = archive_name_array;
  840. looped = 1;
  841. }
  842. prompt = looped;
  843. tryagain:
  844. if (prompt)
  845. {
  846. /* We have to prompt from now on. */
  847. if (info_script_option)
  848. {
  849. if (volno_file_option)
  850. closeout_volume_number ();
  851. if (sys_exec_info_script (archive_name_cursor, global_volno+1))
  852. FATAL_ERROR ((0, 0, _("%s command failed"),
  853. quote (info_script_option)));
  854. }
  855. else
  856. change_tape_menu (read_file);
  857. }
  858. if (strcmp (archive_name_cursor[0], "-") == 0)
  859. {
  860. read_full_records = true;
  861. archive = STDIN_FILENO;
  862. }
  863. else if (verify_option)
  864. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  865. rsh_command_option);
  866. else
  867. switch (mode)
  868. {
  869. case ACCESS_READ:
  870. archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
  871. rsh_command_option);
  872. break;
  873. case ACCESS_WRITE:
  874. if (backup_option)
  875. maybe_backup_file (*archive_name_cursor, 1);
  876. archive = rmtcreat (*archive_name_cursor, MODE_RW,
  877. rsh_command_option);
  878. break;
  879. case ACCESS_UPDATE:
  880. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  881. rsh_command_option);
  882. break;
  883. }
  884. if (archive < 0)
  885. {
  886. open_warn (*archive_name_cursor);
  887. if (!verify_option && mode == ACCESS_WRITE && backup_option)
  888. undo_last_backup ();
  889. prompt = 1;
  890. goto tryagain;
  891. }
  892. SET_BINARY_MODE (archive);
  893. return true;
  894. }
  895. static bool
  896. read_header0 (struct tar_stat_info *info)
  897. {
  898. enum read_header rc;
  899. tar_stat_init (info);
  900. rc = read_header_primitive (false, info);
  901. if (rc == HEADER_SUCCESS)
  902. {
  903. set_next_block_after (current_header);
  904. return true;
  905. }
  906. ERROR ((0, 0, _("This does not look like a tar archive")));
  907. return false;
  908. }
  909. bool
  910. try_new_volume ()
  911. {
  912. size_t status;
  913. union block *header;
  914. struct tar_stat_info dummy;
  915. switch (subcommand_option)
  916. {
  917. case APPEND_SUBCOMMAND:
  918. case CAT_SUBCOMMAND:
  919. case UPDATE_SUBCOMMAND:
  920. if (!new_volume (ACCESS_UPDATE))
  921. return true;
  922. break;
  923. default:
  924. if (!new_volume (ACCESS_READ))
  925. return true;
  926. break;
  927. }
  928. while ((status = rmtread (archive, record_start->buffer, record_size))
  929. == SAFE_READ_ERROR)
  930. archive_read_error ();
  931. if (status != record_size)
  932. short_read (status);
  933. header = find_next_block ();
  934. if (!header)
  935. return false;
  936. switch (header->header.typeflag)
  937. {
  938. case XGLTYPE:
  939. {
  940. if (!read_header0 (&dummy))
  941. return false;
  942. xheader_decode (&dummy); /* decodes values from the global header */
  943. tar_stat_destroy (&dummy);
  944. if (!real_s_name)
  945. {
  946. /* We have read the extended header of the first member in
  947. this volume. Put it back, so next read_header works as
  948. expected. */
  949. current_block = record_start;
  950. }
  951. break;
  952. }
  953. case GNUTYPE_VOLHDR:
  954. if (!read_header0 (&dummy))
  955. return false;
  956. tar_stat_destroy (&dummy);
  957. assign_string (&volume_label, current_header->header.name);
  958. set_next_block_after (header);
  959. header = find_next_block ();
  960. if (header->header.typeflag != GNUTYPE_MULTIVOL)
  961. break;
  962. /* FALL THROUGH */
  963. case GNUTYPE_MULTIVOL:
  964. if (!read_header0 (&dummy))
  965. return false;
  966. tar_stat_destroy (&dummy);
  967. assign_string (&continued_file_name, current_header->header.name);
  968. continued_file_size =
  969. UINTMAX_FROM_HEADER (current_header->header.size);
  970. continued_file_offset =
  971. UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
  972. break;
  973. default:
  974. break;
  975. }
  976. if (real_s_name)
  977. {
  978. uintmax_t s;
  979. if (!continued_file_name
  980. || strcmp (continued_file_name, real_s_name))
  981. {
  982. if ((archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
  983. && strlen (real_s_name) >= NAME_FIELD_SIZE
  984. && strncmp (continued_file_name, real_s_name,
  985. NAME_FIELD_SIZE) == 0)
  986. WARN ((0, 0,
  987. _("%s is possibly continued on this volume: header contains truncated name"),
  988. quote (real_s_name)));
  989. else
  990. {
  991. WARN ((0, 0, _("%s is not continued on this volume"),
  992. quote (real_s_name)));
  993. return false;
  994. }
  995. }
  996. s = continued_file_size + continued_file_offset;
  997. if (real_s_totsize != s || s < continued_file_offset)
  998. {
  999. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  1000. char s1buf[UINTMAX_STRSIZE_BOUND];
  1001. char s2buf[UINTMAX_STRSIZE_BOUND];
  1002. WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
  1003. quote (continued_file_name),
  1004. STRINGIFY_BIGINT (save_totsize, totsizebuf),
  1005. STRINGIFY_BIGINT (continued_file_size, s1buf),
  1006. STRINGIFY_BIGINT (continued_file_offset, s2buf)));
  1007. return false;
  1008. }
  1009. if (real_s_totsize - real_s_sizeleft != continued_file_offset)
  1010. {
  1011. WARN ((0, 0, _("This volume is out of sequence")));
  1012. return false;
  1013. }
  1014. }
  1015. increase_volume_number ();
  1016. return true;
  1017. }
  1018. /* Check the LABEL block against the volume label, seen as a globbing
  1019. pattern. Return true if the pattern matches. In case of failure,
  1020. retry matching a volume sequence number before giving up in
  1021. multi-volume mode. */
  1022. static bool
  1023. check_label_pattern (union block *label)
  1024. {
  1025. char *string;
  1026. bool result;
  1027. if (! memchr (label->header.name, '\0', sizeof label->header.name))
  1028. return false;
  1029. if (fnmatch (volume_label_option, label->header.name, 0) == 0)
  1030. return true;
  1031. if (!multi_volume_option)
  1032. return false;
  1033. string = xmalloc (strlen (volume_label_option)
  1034. + sizeof VOLUME_LABEL_APPEND + 1);
  1035. strcpy (string, volume_label_option);
  1036. strcat (string, VOLUME_LABEL_APPEND);
  1037. result = fnmatch (string, label->header.name, 0) == 0;
  1038. free (string);
  1039. return result;
  1040. }
  1041. /* Check if the next block contains a volume label and if this matches
  1042. the one given in the command line */
  1043. static void
  1044. match_volume_label (void)
  1045. {
  1046. union block *label = find_next_block ();
  1047. if (!label)
  1048. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  1049. quote (volume_label_option)));
  1050. if (!check_label_pattern (label))
  1051. FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
  1052. quote_n (0, label->header.name),
  1053. quote_n (1, volume_label_option)));
  1054. }
  1055. /* Mark the archive with volume label STR. */
  1056. static void
  1057. _write_volume_label (const char *str)
  1058. {
  1059. if (archive_format == POSIX_FORMAT)
  1060. xheader_store ("GNU.volume.label", NULL, str);
  1061. else
  1062. {
  1063. union block *label = find_next_block ();
  1064. memset (label, 0, BLOCKSIZE);
  1065. strcpy (label->header.name, volume_label_option);
  1066. assign_string (&current_stat_info.file_name,
  1067. label->header.name);
  1068. current_stat_info.had_trailing_slash =
  1069. strip_trailing_slashes (current_stat_info.file_name);
  1070. label->header.typeflag = GNUTYPE_VOLHDR;
  1071. TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
  1072. finish_header (&current_stat_info, label, -1);
  1073. set_next_block_after (label);
  1074. }
  1075. }
  1076. #define VOL_SUFFIX "Volume"
  1077. /* Add a volume label to a part of multi-volume archive */
  1078. static void
  1079. add_volume_label (void)
  1080. {
  1081. char buf[UINTMAX_STRSIZE_BOUND];
  1082. char *p = STRINGIFY_BIGINT (volno, buf);
  1083. char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
  1084. + strlen (p) + 2);
  1085. sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
  1086. _write_volume_label (s);
  1087. free (s);
  1088. }
  1089. static void
  1090. add_chunk_header ()
  1091. {
  1092. if (archive_format == POSIX_FORMAT)
  1093. {
  1094. off_t block_ordinal;
  1095. union block *blk;
  1096. struct tar_stat_info st;
  1097. static size_t real_s_part_no; /* FIXME */
  1098. real_s_part_no++;
  1099. memset (&st, 0, sizeof st);
  1100. st.orig_file_name = st.file_name = real_s_name;
  1101. st.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
  1102. st.stat.st_uid = getuid ();
  1103. st.stat.st_gid = getgid ();
  1104. st.orig_file_name = xheader_format_name (&st,
  1105. "%d/GNUFileParts.%p/%f.%n",
  1106. real_s_part_no);
  1107. st.file_name = st.orig_file_name;
  1108. st.archive_file_size = st.stat.st_size = real_s_sizeleft;
  1109. block_ordinal = current_block_ordinal ();
  1110. blk = start_header (&st);
  1111. if (!blk)
  1112. abort (); /* FIXME */
  1113. finish_header (&st, blk, block_ordinal);
  1114. free (st.orig_file_name);
  1115. }
  1116. }
  1117. /* Add a volume label to the current archive */
  1118. static void
  1119. write_volume_label (void)
  1120. {
  1121. if (multi_volume_option)
  1122. add_volume_label ();
  1123. else
  1124. _write_volume_label (volume_label_option);
  1125. }
  1126. /* Write GNU multi-volume header */
  1127. static void
  1128. gnu_add_multi_volume_header (void)
  1129. {
  1130. int tmp;
  1131. union block *block = find_next_block ();
  1132. if (strlen (real_s_name) > NAME_FIELD_SIZE)
  1133. WARN ((0, 0,
  1134. _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
  1135. quotearg_colon (real_s_name)));
  1136. memset (block, 0, BLOCKSIZE);
  1137. /* FIXME: Michael P Urban writes: [a long name file] is being written
  1138. when a new volume rolls around [...] Looks like the wrong value is
  1139. being preserved in real_s_name, though. */
  1140. strncpy (block->header.name, real_s_name, NAME_FIELD_SIZE);
  1141. block->header.typeflag = GNUTYPE_MULTIVOL;
  1142. OFF_TO_CHARS (real_s_sizeleft, block->header.size);
  1143. OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
  1144. block->oldgnu_header.offset);
  1145. tmp = verbose_option;
  1146. verbose_option = 0;
  1147. finish_header (&current_stat_info, block, -1);
  1148. verbose_option = tmp;
  1149. set_next_block_after (block);
  1150. }
  1151. /* Add a multi volume header to the current archive. The exact header format
  1152. depends on the archive format. */
  1153. static void
  1154. add_multi_volume_header (void)
  1155. {
  1156. if (archive_format == POSIX_FORMAT)
  1157. {
  1158. off_t d = real_s_totsize - real_s_sizeleft;
  1159. xheader_store ("GNU.volume.filename", NULL, real_s_name);
  1160. xheader_store ("GNU.volume.size", NULL, &real_s_sizeleft);
  1161. xheader_store ("GNU.volume.offset", NULL, &d);
  1162. }
  1163. else
  1164. gnu_add_multi_volume_header ();
  1165. }
  1166. /* Synchronize multi-volume globals */
  1167. static void
  1168. multi_volume_sync ()
  1169. {
  1170. if (multi_volume_option)
  1171. {
  1172. if (save_name)
  1173. {
  1174. assign_string (&real_s_name,
  1175. safer_name_suffix (save_name, false,
  1176. absolute_names_option));
  1177. real_s_totsize = save_totsize;
  1178. real_s_sizeleft = save_sizeleft;
  1179. }
  1180. else
  1181. {
  1182. assign_string (&real_s_name, 0);
  1183. real_s_totsize = 0;
  1184. real_s_sizeleft = 0;
  1185. }
  1186. }
  1187. }
  1188. /* Low-level flush functions */
  1189. /* Simple flush read (no multi-volume or label extensions) */
  1190. static void
  1191. simple_flush_read (void)
  1192. {
  1193. size_t status; /* result from system call */
  1194. do_checkpoint (false);
  1195. /* Clear the count of errors. This only applies to a single call to
  1196. flush_read. */
  1197. read_error_count = 0; /* clear error count */
  1198. if (write_archive_to_stdout && record_start_block != 0)
  1199. {
  1200. archive = STDOUT_FILENO;
  1201. status = sys_write_archive_buffer ();
  1202. archive = STDIN_FILENO;
  1203. if (status != record_size)
  1204. archive_write_error (status);
  1205. }
  1206. for (;;)
  1207. {
  1208. status = rmtread (archive, record_start->buffer, record_size);
  1209. if (status == record_size)
  1210. {
  1211. records_read++;
  1212. return;
  1213. }
  1214. if (status == SAFE_READ_ERROR)
  1215. {
  1216. archive_read_error ();
  1217. continue; /* try again */
  1218. }
  1219. break;
  1220. }
  1221. short_read (status);
  1222. }
  1223. /* Simple flush write (no multi-volume or label extensions) */
  1224. static void
  1225. simple_flush_write (size_t level __attribute__((unused)))
  1226. {
  1227. ssize_t status;
  1228. status = _flush_write ();
  1229. if (status != record_size)
  1230. archive_write_error (status);
  1231. else
  1232. {
  1233. records_written++;
  1234. bytes_written += status;
  1235. }
  1236. }
  1237. /* GNU flush functions. These support multi-volume and archive labels in
  1238. GNU and PAX archive formats. */
  1239. static void
  1240. _gnu_flush_read (void)
  1241. {
  1242. size_t status; /* result from system call */
  1243. do_checkpoint (false);
  1244. /* Clear the count of errors. This only applies to a single call to
  1245. flush_read. */
  1246. read_error_count = 0; /* clear error count */
  1247. if (write_archive_to_stdout && record_start_block != 0)
  1248. {
  1249. archive = STDOUT_FILENO;
  1250. status = sys_write_archive_buffer ();
  1251. archive = STDIN_FILENO;
  1252. if (status != record_size)
  1253. archive_write_error (status);
  1254. }
  1255. multi_volume_sync ();
  1256. for (;;)
  1257. {
  1258. status = rmtread (archive, record_start->buffer, record_size);
  1259. if (status == record_size)
  1260. {
  1261. records_read++;
  1262. return;
  1263. }
  1264. /* The condition below used to include
  1265. || (status > 0 && !read_full_records)
  1266. This is incorrect since even if new_volume() succeeds, the
  1267. subsequent call to rmtread will overwrite the chunk of data
  1268. already read in the buffer, so the processing will fail */
  1269. if ((status == 0
  1270. || (status == SAFE_READ_ERROR && errno == ENOSPC))
  1271. && multi_volume_option)
  1272. {
  1273. while (!try_new_volume ())
  1274. ;
  1275. return;
  1276. }
  1277. else if (status == SAFE_READ_ERROR)
  1278. {
  1279. archive_read_error ();
  1280. continue;
  1281. }
  1282. break;
  1283. }
  1284. short_read (status);
  1285. }
  1286. static void
  1287. gnu_flush_read (void)
  1288. {
  1289. flush_read_ptr = simple_flush_read; /* Avoid recursion */
  1290. _gnu_flush_read ();
  1291. flush_read_ptr = gnu_flush_read;
  1292. }
  1293. static void
  1294. _gnu_flush_write (size_t buffer_level)
  1295. {
  1296. ssize_t status;
  1297. union block *header;
  1298. char *copy_ptr;
  1299. size_t copy_size;
  1300. size_t bufsize;
  1301. status = _flush_write ();
  1302. if (status != record_size && !multi_volume_option)
  1303. archive_write_error (status);
  1304. else
  1305. {
  1306. records_written++;
  1307. bytes_written += status;
  1308. }
  1309. if (status == record_size)
  1310. {
  1311. multi_volume_sync ();
  1312. return;
  1313. }
  1314. /* In multi-volume mode. */
  1315. /* ENXIO is for the UNIX PC. */
  1316. if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  1317. archive_write_error (status);
  1318. if (!new_volume (ACCESS_WRITE))
  1319. return;
  1320. xheader_destroy (&extended_header);
  1321. increase_volume_number ();
  1322. prev_written += bytes_written;
  1323. bytes_written = 0;
  1324. copy_ptr = record_start->buffer + status;
  1325. copy_size = buffer_level - status;
  1326. /* Switch to the next buffer */
  1327. record_index = !record_index;
  1328. init_buffer ();
  1329. if (volume_label_option)
  1330. add_volume_label ();
  1331. if (real_s_name)
  1332. add_multi_volume_header ();
  1333. write_extended (true, NULL, find_next_block ());
  1334. if (real_s_name)
  1335. add_chunk_header ();
  1336. header = find_next_block ();
  1337. bufsize = available_space_after (header);
  1338. while (bufsize < copy_size)
  1339. {
  1340. memcpy (header->buffer, copy_ptr, bufsize);
  1341. copy_ptr += bufsize;
  1342. copy_size -= bufsize;
  1343. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  1344. header = find_next_block ();
  1345. bufsize = available_space_after (header);
  1346. }
  1347. memcpy (header->buffer, copy_ptr, copy_size);
  1348. memset (header->buffer + copy_size, 0, bufsize - copy_size);
  1349. set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
  1350. find_next_block ();
  1351. }
  1352. static void
  1353. gnu_flush_write (size_t buffer_level)
  1354. {
  1355. flush_write_ptr = simple_flush_write; /* Avoid recursion */
  1356. _gnu_flush_write (buffer_level);
  1357. flush_write_ptr = gnu_flush_write;
  1358. }
  1359. void
  1360. flush_read ()
  1361. {
  1362. flush_read_ptr ();
  1363. }
  1364. void
  1365. flush_write ()
  1366. {
  1367. flush_write_ptr (record_size);
  1368. }
  1369. void
  1370. open_archive (enum access_mode wanted_access)
  1371. {
  1372. flush_read_ptr = gnu_flush_read;
  1373. flush_write_ptr = gnu_flush_write;
  1374. _open_archive (wanted_access);
  1375. switch (wanted_access)
  1376. {
  1377. case ACCESS_READ:
  1378. if (volume_label_option)
  1379. match_volume_label ();
  1380. break;
  1381. case ACCESS_WRITE:
  1382. records_written = 0;
  1383. if (volume_label_option)
  1384. write_volume_label ();
  1385. break;
  1386. default:
  1387. break;
  1388. }
  1389. set_volume_start_time ();
  1390. }