+ ssize_t status;
+
+ status = _flush_write ();
+ if (status != record_size)
+ archive_write_error (status);
+ else
+ {
+ records_written++;
+ bytes_written += status;
+ }
+}
+
+\f
+/* GNU flush functions. These support multi-volume and archive labels in
+ GNU and PAX archive formats. */
+
+static void
+_gnu_flush_read (void)
+{
+ size_t status; /* result from system call */
+
+ checkpoint_run (false);
+
+ /* Clear the count of errors. This only applies to a single call to
+ flush_read. */
+
+ read_error_count = 0; /* clear error count */
+
+ if (write_archive_to_stdout && record_start_block != 0)
+ {
+ archive = STDOUT_FILENO;
+ status = sys_write_archive_buffer ();
+ archive = STDIN_FILENO;
+ if (status != record_size)
+ archive_write_error (status);
+ }
+
+ multi_volume_sync ();
+
+ for (;;)
+ {
+ status = rmtread (archive, record_start->buffer, record_size);
+ if (status == record_size)
+ {
+ records_read++;
+ return;
+ }
+
+ /* The condition below used to include
+ || (status > 0 && !read_full_records)
+ This is incorrect since even if new_volume() succeeds, the
+ subsequent call to rmtread will overwrite the chunk of data
+ already read in the buffer, so the processing will fail */
+ if ((status == 0
+ || (status == SAFE_READ_ERROR && errno == ENOSPC))
+ && multi_volume_option)
+ {
+ while (!try_new_volume ())
+ ;
+ if (current_block == record_end)
+ /* Necessary for blocking_factor == 1 */
+ flush_archive();
+ return;
+ }
+ else if (status == SAFE_READ_ERROR)
+ {
+ archive_read_error ();
+ continue;
+ }
+ break;
+ }
+ short_read (status);
+}
+
+static void
+gnu_flush_read (void)
+{
+ flush_read_ptr = simple_flush_read; /* Avoid recursion */
+ _gnu_flush_read ();
+ flush_read_ptr = gnu_flush_read;
+}
+
+static void
+_gnu_flush_write (size_t buffer_level)
+{
+ ssize_t status;
+ union block *header;
+ char *copy_ptr;
+ size_t copy_size;
+ size_t bufsize;
+ tarlong wrt;
+
+ status = _flush_write ();
+ if (status != record_size && !multi_volume_option)
+ archive_write_error (status);
+ else
+ {
+ if (status)
+ records_written++;
+ bytes_written += status;
+ }
+
+ if (status == record_size)
+ {
+ multi_volume_sync ();
+ return;
+ }
+
+ if (status % BLOCKSIZE)
+ {
+ ERROR ((0, 0, _("write did not end on a block boundary")));
+ archive_write_error (status);
+ }
+
+ /* In multi-volume mode. */
+ /* ENXIO is for the UNIX PC. */
+ if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
+ archive_write_error (status);
+
+ real_s_sizeleft -= status;
+ if (!new_volume (ACCESS_WRITE))
+ return;
+
+ tar_stat_destroy (&dummy);
+
+ increase_volume_number ();
+ prev_written += bytes_written;
+ bytes_written = 0;
+
+ copy_ptr = record_start->buffer + status;
+ copy_size = buffer_level - status;
+
+ /* Switch to the next buffer */
+ record_index = !record_index;
+ init_buffer ();
+
+ if (volume_label_option)
+ add_volume_label ();
+
+ if (real_s_name)
+ add_multi_volume_header ();
+
+ write_extended (true, &dummy, find_next_block ());
+ tar_stat_destroy (&dummy);
+
+ if (real_s_name)
+ add_chunk_header ();
+ wrt = bytes_written;
+ header = find_next_block ();
+ bufsize = available_space_after (header);
+ while (bufsize < copy_size)
+ {
+ memcpy (header->buffer, copy_ptr, bufsize);
+ copy_ptr += bufsize;
+ copy_size -= bufsize;
+ set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
+ header = find_next_block ();
+ bufsize = available_space_after (header);
+ }
+ memcpy (header->buffer, copy_ptr, copy_size);
+ memset (header->buffer + copy_size, 0, bufsize - copy_size);
+ set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
+ if (multi_volume_option && wrt < bytes_written)
+ {
+ /* The value of bytes_written has changed while moving data;
+ that means that flush_archive was executed at least once in
+ between, and, as a consequence, copy_size bytes were not written
+ to disk. We need to update sizeleft variables to compensate for
+ that. */
+ save_sizeleft += copy_size;
+ multi_volume_sync ();
+ }
+ find_next_block ();
+}
+
+static void
+gnu_flush_write (size_t buffer_level)
+{
+ flush_write_ptr = simple_flush_write; /* Avoid recursion */
+ _gnu_flush_write (buffer_level);
+ flush_write_ptr = gnu_flush_write;
+}
+
+void
+flush_read ()
+{
+ flush_read_ptr ();
+}
+
+void
+flush_write ()
+{
+ flush_write_ptr (record_size);
+}
+
+void
+open_archive (enum access_mode wanted_access)
+{
+ flush_read_ptr = gnu_flush_read;
+ flush_write_ptr = gnu_flush_write;
+
+ _open_archive (wanted_access);
+ switch (wanted_access)
+ {
+ case ACCESS_READ:
+ case ACCESS_UPDATE:
+ if (volume_label_option)
+ match_volume_label ();
+ break;
+
+ case ACCESS_WRITE:
+ records_written = 0;
+ if (volume_label_option)
+ write_volume_label ();
+ break;
+ }
+ set_volume_start_time ();