4
0

buffer.c 39 KB

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