- int err; /* Result from system call */
- int left; /* Bytes left */
- char *more; /* Pointer to next byte to read */
-
- /*
- * Clear the count of errors. This only applies to a single
- * call to fl_read. We leave read_error_flag alone; it is
- * only turned off by higher level software.
- */
- r_error_count = 0; /* Clear error count */
-
- /*
- * If we are about to wipe out a record that
- * somebody needs to keep, copy it out to a holding
- * area and adjust somebody's pointer to it.
- */
- if (save_rec &&
- *save_rec >= ar_record &&
- *save_rec < ar_last) {
- record_save_area = **save_rec;
- *save_rec = &record_save_area;
- }
- if(write_archive_to_stdout && baserec!=0) {
- err=rmtwrite(1, ar_block->charptr, blocksize);
- if(err!=blocksize)
- writeerror(err);
+ if (time_to_start_writing || access_mode == ACCESS_WRITE)
+ {
+ flush_archive ();
+ if (current_block > record_start)
+ flush_archive ();
+ }
+
+ sys_drain_input_pipe ();
+
+ compute_duration ();
+ if (verify_option)
+ verify_volume ();
+
+ if (rmtclose (archive) != 0)
+ close_warn (*archive_name_cursor);
+
+ sys_wait_for_child (child_pid);
+
+ tar_stat_destroy (¤t_stat_info);
+ if (save_name)
+ free (save_name);
+ if (real_s_name)
+ free (real_s_name);
+ free (record_buffer[0]);
+ free (record_buffer[1]);
+}
+
+/* Called to initialize the global volume number. */
+void
+init_volume_number (void)
+{
+ FILE *file = fopen (volno_file_option, "r");
+
+ if (file)
+ {
+ if (fscanf (file, "%d", &global_volno) != 1
+ || global_volno < 0)
+ FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
+ quotearg_colon (volno_file_option)));
+ if (ferror (file))
+ read_error (volno_file_option);
+ if (fclose (file) != 0)
+ close_error (volno_file_option);
+ }
+ else if (errno != ENOENT)
+ open_error (volno_file_option);
+}
+
+/* Called to write out the closing global volume number. */
+void
+closeout_volume_number (void)
+{
+ FILE *file = fopen (volno_file_option, "w");
+
+ if (file)
+ {
+ fprintf (file, "%d\n", global_volno);
+ if (ferror (file))
+ write_error (volno_file_option);
+ if (fclose (file) != 0)
+ close_error (volno_file_option);
+ }
+ else
+ open_error (volno_file_option);
+}
+
+\f
+static void
+increase_volume_number ()
+{
+ global_volno++;
+ if (global_volno < 0)
+ FATAL_ERROR ((0, 0, _("Volume number overflow")));
+ volno++;
+}
+
+/* We've hit the end of the old volume. Close it and open the next one.
+ Return nonzero on success.
+*/
+static bool
+new_volume (enum access_mode mode)
+{
+ static FILE *read_file;
+ static int looped;
+
+ if (!read_file && !info_script_option)
+ /* FIXME: if fopen is used, it will never be closed. */
+ read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
+
+ if (now_verifying)
+ return false;
+ if (verify_option)
+ verify_volume ();
+
+ assign_string (&volume_label, NULL);
+ assign_string (&continued_file_name, NULL);
+ continued_file_size = continued_file_offset = 0;
+
+ if (rmtclose (archive) != 0)
+ close_warn (*archive_name_cursor);
+
+ archive_name_cursor++;
+ if (archive_name_cursor == archive_name_array + archive_names)
+ {
+ archive_name_cursor = archive_name_array;
+ looped = 1;
+ }
+
+ tryagain:
+ if (looped)
+ {
+ /* We have to prompt from now on. */
+
+ if (info_script_option)
+ {
+ if (volno_file_option)
+ closeout_volume_number ();
+ if (system (info_script_option) != 0)
+ FATAL_ERROR ((0, 0, _("%s command failed"),
+ quote (info_script_option)));