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