]> Dogcows Code - chaz/tar/blob - src/buffer.c
Fail if archive comes from a terminal.
[chaz/tar] / src / buffer.c
1 /* Buffer management for tar.
2
3 Copyright 1988, 1992-1994, 1996-1997, 1999-2010, 2013-2014 Free
4 Software 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 /* Refuse to read archive from a tty.
693 Do not fail if the tar's output goes directly to tty because such
694 behavior would go against GNU Coding Standards:
695 http://lists.gnu.org/archive/html/bug-tar/2014-03/msg00042.html */
696 if (strcmp (archive_name_array[0], "-") == 0
697 && wanted_access == ACCESS_READ && isatty (STDIN_FILENO))
698 FATAL_ERROR ((0, 0,
699 _("Refusing to read archive contents from terminal "
700 "(missing -f option?)")));
701
702 read_full_records = read_full_records_option;
703
704 records_read = 0;
705
706 if (use_compress_program_option)
707 {
708 switch (wanted_access)
709 {
710 case ACCESS_READ:
711 child_pid = sys_child_open_for_uncompress ();
712 read_full_records = true;
713 record_end = record_start; /* set up for 1st record = # 0 */
714 break;
715
716 case ACCESS_WRITE:
717 child_pid = sys_child_open_for_compress ();
718 break;
719
720 case ACCESS_UPDATE:
721 abort (); /* Should not happen */
722 break;
723 }
724
725 if (!index_file_name
726 && wanted_access == ACCESS_WRITE
727 && strcmp (archive_name_array[0], "-") == 0)
728 stdlis = stderr;
729 }
730 else if (strcmp (archive_name_array[0], "-") == 0)
731 {
732 read_full_records = true; /* could be a pipe, be safe */
733 if (verify_option)
734 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
735
736 switch (wanted_access)
737 {
738 case ACCESS_READ:
739 {
740 bool shortfile;
741 enum compress_type type;
742
743 archive = STDIN_FILENO;
744 type = check_compressed_archive (&shortfile);
745 if (type != ct_tar && type != ct_none)
746 FATAL_ERROR ((0, 0,
747 _("Archive is compressed. Use %s option"),
748 compress_option (type)));
749 if (shortfile)
750 ERROR ((0, 0, _("This does not look like a tar archive")));
751 }
752 break;
753
754 case ACCESS_WRITE:
755 archive = STDOUT_FILENO;
756 if (!index_file_name)
757 stdlis = stderr;
758 break;
759
760 case ACCESS_UPDATE:
761 archive = STDIN_FILENO;
762 write_archive_to_stdout = true;
763 record_end = record_start; /* set up for 1st record = # 0 */
764 if (!index_file_name)
765 stdlis = stderr;
766 break;
767 }
768 }
769 else
770 switch (wanted_access)
771 {
772 case ACCESS_READ:
773 archive = open_compressed_archive ();
774 if (archive >= 0)
775 guess_seekable_archive ();
776 break;
777
778 case ACCESS_WRITE:
779 if (backup_option)
780 {
781 maybe_backup_file (archive_name_array[0], 1);
782 backed_up_flag = 1;
783 }
784 if (verify_option)
785 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
786 MODE_RW, rsh_command_option);
787 else
788 archive = rmtcreat (archive_name_array[0], MODE_RW,
789 rsh_command_option);
790 break;
791
792 case ACCESS_UPDATE:
793 archive = rmtopen (archive_name_array[0],
794 O_RDWR | O_CREAT | O_BINARY,
795 MODE_RW, rsh_command_option);
796
797 switch (check_compressed_archive (NULL))
798 {
799 case ct_none:
800 case ct_tar:
801 break;
802
803 default:
804 FATAL_ERROR ((0, 0,
805 _("Cannot update compressed archives")));
806 }
807 break;
808 }
809
810 if (archive < 0
811 || (! _isrmt (archive) && !sys_get_archive_stat ()))
812 {
813 int saved_errno = errno;
814
815 if (backed_up_flag)
816 undo_last_backup ();
817 errno = saved_errno;
818 open_fatal (archive_name_array[0]);
819 }
820
821 sys_detect_dev_null_output ();
822 sys_save_archive_dev_ino ();
823 SET_BINARY_MODE (archive);
824
825 switch (wanted_access)
826 {
827 case ACCESS_READ:
828 find_next_block (); /* read it in, check for EOF */
829 break;
830
831 case ACCESS_UPDATE:
832 case ACCESS_WRITE:
833 records_written = 0;
834 break;
835 }
836 }
837
838 /* Perform a write to flush the buffer. */
839 static ssize_t
840 _flush_write (void)
841 {
842 ssize_t status;
843
844 checkpoint_run (true);
845 if (tape_length_option && tape_length_option <= bytes_written)
846 {
847 errno = ENOSPC;
848 status = 0;
849 }
850 else if (dev_null_output)
851 status = record_size;
852 else
853 status = sys_write_archive_buffer ();
854
855 if (status && multi_volume_option && !inhibit_map)
856 {
857 struct bufmap *map = bufmap_locate (status);
858 if (map)
859 {
860 size_t delta = status - map->start * BLOCKSIZE;
861 if (delta > map->sizeleft)
862 delta = map->sizeleft;
863 map->sizeleft -= delta;
864 if (map->sizeleft == 0)
865 map = map->next;
866 bufmap_reset (map, map ? (- map->start) : 0);
867 }
868 }
869 return status;
870 }
871
872 /* Handle write errors on the archive. Write errors are always fatal.
873 Hitting the end of a volume does not cause a write error unless the
874 write was the first record of the volume. */
875 void
876 archive_write_error (ssize_t status)
877 {
878 /* It might be useful to know how much was written before the error
879 occurred. */
880 if (totals_option)
881 {
882 int e = errno;
883 print_total_stats ();
884 errno = e;
885 }
886
887 write_fatal_details (*archive_name_cursor, status, record_size);
888 }
889
890 /* Handle read errors on the archive. If the read should be retried,
891 return to the caller. */
892 void
893 archive_read_error (void)
894 {
895 read_error (*archive_name_cursor);
896
897 if (record_start_block == 0)
898 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
899
900 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
901 then give up on reading the archive. */
902
903 if (read_error_count++ > READ_ERROR_MAX)
904 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
905 return;
906 }
907
908 static bool
909 archive_is_dev (void)
910 {
911 struct stat st;
912
913 if (fstat (archive, &st))
914 {
915 stat_diag (*archive_name_cursor);
916 return false;
917 }
918 return S_ISBLK (st.st_mode) || S_ISCHR (st.st_mode);
919 }
920
921 static void
922 short_read (size_t status)
923 {
924 size_t left; /* bytes left */
925 char *more; /* pointer to next byte to read */
926
927 more = record_start->buffer + status;
928 left = record_size - status;
929
930 if (left && left % BLOCKSIZE == 0
931 && (warning_option & WARN_RECORD_SIZE)
932 && record_start_block == 0 && status != 0
933 && archive_is_dev ())
934 {
935 unsigned long rsize = status / BLOCKSIZE;
936 WARN ((0, 0,
937 ngettext ("Record size = %lu block",
938 "Record size = %lu blocks",
939 rsize),
940 rsize));
941 }
942
943 while (left % BLOCKSIZE != 0
944 || (left && status && read_full_records))
945 {
946 if (status)
947 while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
948 archive_read_error ();
949
950 if (status == 0)
951 break;
952
953 if (! read_full_records)
954 {
955 unsigned long rest = record_size - left;
956
957 FATAL_ERROR ((0, 0,
958 ngettext ("Unaligned block (%lu byte) in archive",
959 "Unaligned block (%lu bytes) in archive",
960 rest),
961 rest));
962 }
963
964 left -= status;
965 more += status;
966 }
967
968 record_end = record_start + (record_size - left) / BLOCKSIZE;
969 records_read++;
970 }
971
972 /* Flush the current buffer to/from the archive. */
973 void
974 flush_archive (void)
975 {
976 size_t buffer_level = current_block->buffer - record_start->buffer;
977 record_start_block += record_end - record_start;
978 current_block = record_start;
979 record_end = record_start + blocking_factor;
980
981 if (access_mode == ACCESS_READ && time_to_start_writing)
982 {
983 access_mode = ACCESS_WRITE;
984 time_to_start_writing = false;
985 backspace_output ();
986 }
987
988 switch (access_mode)
989 {
990 case ACCESS_READ:
991 flush_read ();
992 break;
993
994 case ACCESS_WRITE:
995 flush_write_ptr (buffer_level);
996 break;
997
998 case ACCESS_UPDATE:
999 abort ();
1000 }
1001 }
1002
1003 /* Backspace the archive descriptor by one record worth. If it's a
1004 tape, MTIOCTOP will work. If it's something else, try to seek on
1005 it. If we can't seek, we lose! */
1006 static void
1007 backspace_output (void)
1008 {
1009 #ifdef MTIOCTOP
1010 {
1011 struct mtop operation;
1012
1013 operation.mt_op = MTBSR;
1014 operation.mt_count = 1;
1015 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1016 return;
1017 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1018 return;
1019 }
1020 #endif
1021
1022 {
1023 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
1024
1025 /* Seek back to the beginning of this record and start writing there. */
1026
1027 position -= record_size;
1028 if (position < 0)
1029 position = 0;
1030 if (rmtlseek (archive, position, SEEK_SET) != position)
1031 {
1032 /* Lseek failed. Try a different method. */
1033
1034 WARN ((0, 0,
1035 _("Cannot backspace archive file; it may be unreadable without -i")));
1036
1037 /* Replace the first part of the record with NULs. */
1038
1039 if (record_start->buffer != output_start)
1040 memset (record_start->buffer, 0,
1041 output_start - record_start->buffer);
1042 }
1043 }
1044 }
1045
1046 off_t
1047 seek_archive (off_t size)
1048 {
1049 off_t start = current_block_ordinal ();
1050 off_t offset;
1051 off_t nrec, nblk;
1052 off_t skipped = (blocking_factor - (current_block - record_start))
1053 * BLOCKSIZE;
1054
1055 if (size <= skipped)
1056 return 0;
1057
1058 /* Compute number of records to skip */
1059 nrec = (size - skipped) / record_size;
1060 if (nrec == 0)
1061 return 0;
1062 offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
1063 if (offset < 0)
1064 return offset;
1065
1066 if (offset % record_size)
1067 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1068
1069 /* Convert to number of records */
1070 offset /= BLOCKSIZE;
1071 /* Compute number of skipped blocks */
1072 nblk = offset - start;
1073
1074 /* Update buffering info */
1075 records_read += nblk / blocking_factor;
1076 record_start_block = offset - blocking_factor;
1077 current_block = record_end;
1078
1079 return nblk;
1080 }
1081
1082 /* Close the archive file. */
1083 void
1084 close_archive (void)
1085 {
1086 if (time_to_start_writing || access_mode == ACCESS_WRITE)
1087 {
1088 flush_archive ();
1089 if (current_block > record_start)
1090 flush_archive ();
1091 }
1092
1093 compute_duration ();
1094 if (verify_option)
1095 verify_volume ();
1096
1097 if (rmtclose (archive) != 0)
1098 close_error (*archive_name_cursor);
1099
1100 sys_wait_for_child (child_pid, hit_eof);
1101
1102 tar_stat_destroy (&current_stat_info);
1103 free (record_buffer[0]);
1104 free (record_buffer[1]);
1105 bufmap_free (NULL);
1106 }
1107
1108 /* Called to initialize the global volume number. */
1109 void
1110 init_volume_number (void)
1111 {
1112 FILE *file = fopen (volno_file_option, "r");
1113
1114 if (file)
1115 {
1116 if (fscanf (file, "%d", &global_volno) != 1
1117 || global_volno < 0)
1118 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1119 quotearg_colon (volno_file_option)));
1120 if (ferror (file))
1121 read_error (volno_file_option);
1122 if (fclose (file) != 0)
1123 close_error (volno_file_option);
1124 }
1125 else if (errno != ENOENT)
1126 open_error (volno_file_option);
1127 }
1128
1129 /* Called to write out the closing global volume number. */
1130 void
1131 closeout_volume_number (void)
1132 {
1133 FILE *file = fopen (volno_file_option, "w");
1134
1135 if (file)
1136 {
1137 fprintf (file, "%d\n", global_volno);
1138 if (ferror (file))
1139 write_error (volno_file_option);
1140 if (fclose (file) != 0)
1141 close_error (volno_file_option);
1142 }
1143 else
1144 open_error (volno_file_option);
1145 }
1146
1147 \f
1148 static void
1149 increase_volume_number (void)
1150 {
1151 global_volno++;
1152 if (global_volno < 0)
1153 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1154 volno++;
1155 }
1156
1157 static void
1158 change_tape_menu (FILE *read_file)
1159 {
1160 char *input_buffer = NULL;
1161 size_t size = 0;
1162 bool stop = false;
1163
1164 while (!stop)
1165 {
1166 fputc ('\007', stderr);
1167 fprintf (stderr,
1168 _("Prepare volume #%d for %s and hit return: "),
1169 global_volno + 1, quote (*archive_name_cursor));
1170 fflush (stderr);
1171
1172 if (getline (&input_buffer, &size, read_file) <= 0)
1173 {
1174 WARN ((0, 0, _("EOF where user reply was expected")));
1175
1176 if (subcommand_option != EXTRACT_SUBCOMMAND
1177 && subcommand_option != LIST_SUBCOMMAND
1178 && subcommand_option != DIFF_SUBCOMMAND)
1179 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1180
1181 fatal_exit ();
1182 }
1183
1184 if (input_buffer[0] == '\n'
1185 || input_buffer[0] == 'y'
1186 || input_buffer[0] == 'Y')
1187 break;
1188
1189 switch (input_buffer[0])
1190 {
1191 case '?':
1192 {
1193 fprintf (stderr, _("\
1194 n name Give a new file name for the next (and subsequent) volume(s)\n\
1195 q Abort tar\n\
1196 y or newline Continue operation\n"));
1197 if (!restrict_option)
1198 fprintf (stderr, _(" ! Spawn a subshell\n"));
1199 fprintf (stderr, _(" ? Print this list\n"));
1200 }
1201 break;
1202
1203 case 'q':
1204 /* Quit. */
1205
1206 WARN ((0, 0, _("No new volume; exiting.\n")));
1207
1208 if (subcommand_option != EXTRACT_SUBCOMMAND
1209 && subcommand_option != LIST_SUBCOMMAND
1210 && subcommand_option != DIFF_SUBCOMMAND)
1211 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1212
1213 fatal_exit ();
1214
1215 case 'n':
1216 /* Get new file name. */
1217
1218 {
1219 char *name;
1220 char *cursor;
1221
1222 for (name = input_buffer + 1;
1223 *name == ' ' || *name == '\t';
1224 name++)
1225 ;
1226
1227 for (cursor = name; *cursor && *cursor != '\n'; cursor++)
1228 ;
1229 *cursor = '\0';
1230
1231 if (name[0])
1232 {
1233 /* FIXME: the following allocation is never reclaimed. */
1234 *archive_name_cursor = xstrdup (name);
1235 stop = true;
1236 }
1237 else
1238 fprintf (stderr, "%s",
1239 _("File name not specified. Try again.\n"));
1240 }
1241 break;
1242
1243 case '!':
1244 if (!restrict_option)
1245 {
1246 sys_spawn_shell ();
1247 break;
1248 }
1249 /* FALL THROUGH */
1250
1251 default:
1252 fprintf (stderr, _("Invalid input. Type ? for help.\n"));
1253 }
1254 }
1255 free (input_buffer);
1256 }
1257
1258 /* We've hit the end of the old volume. Close it and open the next one.
1259 Return nonzero on success.
1260 */
1261 static bool
1262 new_volume (enum access_mode mode)
1263 {
1264 static FILE *read_file;
1265 static int looped;
1266 int prompt;
1267
1268 if (!read_file && !info_script_option)
1269 /* FIXME: if fopen is used, it will never be closed. */
1270 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
1271
1272 if (now_verifying)
1273 return false;
1274 if (verify_option)
1275 verify_volume ();
1276
1277 assign_string (&volume_label, NULL);
1278 assign_string (&continued_file_name, NULL);
1279 continued_file_size = continued_file_offset = 0;
1280 current_block = record_start;
1281
1282 if (rmtclose (archive) != 0)
1283 close_error (*archive_name_cursor);
1284
1285 archive_name_cursor++;
1286 if (archive_name_cursor == archive_name_array + archive_names)
1287 {
1288 archive_name_cursor = archive_name_array;
1289 looped = 1;
1290 }
1291 prompt = looped;
1292
1293 tryagain:
1294 if (prompt)
1295 {
1296 /* We have to prompt from now on. */
1297
1298 if (info_script_option)
1299 {
1300 if (volno_file_option)
1301 closeout_volume_number ();
1302 if (sys_exec_info_script (archive_name_cursor, global_volno+1))
1303 FATAL_ERROR ((0, 0, _("%s command failed"),
1304 quote (info_script_option)));
1305 }
1306 else
1307 change_tape_menu (read_file);
1308 }
1309
1310 if (strcmp (archive_name_cursor[0], "-") == 0)
1311 {
1312 read_full_records = true;
1313 archive = STDIN_FILENO;
1314 }
1315 else if (verify_option)
1316 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1317 rsh_command_option);
1318 else
1319 switch (mode)
1320 {
1321 case ACCESS_READ:
1322 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1323 rsh_command_option);
1324 guess_seekable_archive ();
1325 break;
1326
1327 case ACCESS_WRITE:
1328 if (backup_option)
1329 maybe_backup_file (*archive_name_cursor, 1);
1330 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1331 rsh_command_option);
1332 break;
1333
1334 case ACCESS_UPDATE:
1335 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1336 rsh_command_option);
1337 break;
1338 }
1339
1340 if (archive < 0)
1341 {
1342 open_warn (*archive_name_cursor);
1343 if (!verify_option && mode == ACCESS_WRITE && backup_option)
1344 undo_last_backup ();
1345 prompt = 1;
1346 goto tryagain;
1347 }
1348
1349 SET_BINARY_MODE (archive);
1350
1351 return true;
1352 }
1353
1354 static bool
1355 read_header0 (struct tar_stat_info *info)
1356 {
1357 enum read_header rc;
1358
1359 tar_stat_init (info);
1360 rc = read_header (&current_header, info, read_header_auto);
1361 if (rc == HEADER_SUCCESS)
1362 {
1363 set_next_block_after (current_header);
1364 return true;
1365 }
1366 ERROR ((0, 0, _("This does not look like a tar archive")));
1367 return false;
1368 }
1369
1370 static bool
1371 try_new_volume (void)
1372 {
1373 size_t status;
1374 union block *header;
1375 enum access_mode acc;
1376
1377 switch (subcommand_option)
1378 {
1379 case APPEND_SUBCOMMAND:
1380 case CAT_SUBCOMMAND:
1381 case UPDATE_SUBCOMMAND:
1382 acc = ACCESS_UPDATE;
1383 break;
1384
1385 default:
1386 acc = ACCESS_READ;
1387 break;
1388 }
1389
1390 if (!new_volume (acc))
1391 return true;
1392
1393 while ((status = rmtread (archive, record_start->buffer, record_size))
1394 == SAFE_READ_ERROR)
1395 archive_read_error ();
1396
1397 if (status != record_size)
1398 short_read (status);
1399
1400 header = find_next_block ();
1401 if (!header)
1402 return false;
1403
1404 switch (header->header.typeflag)
1405 {
1406 case XGLTYPE:
1407 {
1408 tar_stat_init (&dummy);
1409 if (read_header (&header, &dummy, read_header_x_global)
1410 != HEADER_SUCCESS_EXTENDED)
1411 {
1412 ERROR ((0, 0, _("This does not look like a tar archive")));
1413 return false;
1414 }
1415
1416 xheader_decode (&dummy); /* decodes values from the global header */
1417 tar_stat_destroy (&dummy);
1418
1419 /* The initial global header must be immediately followed by
1420 an extended PAX header for the first member in this volume.
1421 However, in some cases tar may split volumes in the middle
1422 of a PAX header. This is incorrect, and should be fixed
1423 in the future versions. In the meantime we must be
1424 prepared to correctly list and extract such archives.
1425
1426 If this happens, the following call to read_header returns
1427 HEADER_FAILURE, which is ignored.
1428
1429 See also tests/multiv07.at */
1430
1431 switch (read_header (&header, &dummy, read_header_auto))
1432 {
1433 case HEADER_SUCCESS:
1434 set_next_block_after (header);
1435 break;
1436
1437 case HEADER_FAILURE:
1438 break;
1439
1440 default:
1441 ERROR ((0, 0, _("This does not look like a tar archive")));
1442 return false;
1443 }
1444 break;
1445 }
1446
1447 case GNUTYPE_VOLHDR:
1448 if (!read_header0 (&dummy))
1449 return false;
1450 tar_stat_destroy (&dummy);
1451 assign_string (&volume_label, current_header->header.name);
1452 set_next_block_after (header);
1453 header = find_next_block ();
1454 if (header->header.typeflag != GNUTYPE_MULTIVOL)
1455 break;
1456 /* FALL THROUGH */
1457
1458 case GNUTYPE_MULTIVOL:
1459 if (!read_header0 (&dummy))
1460 return false;
1461 tar_stat_destroy (&dummy);
1462 assign_string (&continued_file_name, current_header->header.name);
1463 continued_file_size =
1464 UINTMAX_FROM_HEADER (current_header->header.size);
1465 continued_file_offset =
1466 UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
1467 break;
1468
1469 default:
1470 break;
1471 }
1472
1473 if (bufmap_head)
1474 {
1475 uintmax_t s;
1476 if (!continued_file_name
1477 || strcmp (continued_file_name, bufmap_head->file_name))
1478 {
1479 if ((archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
1480 && strlen (bufmap_head->file_name) >= NAME_FIELD_SIZE
1481 && strncmp (continued_file_name, bufmap_head->file_name,
1482 NAME_FIELD_SIZE) == 0)
1483 WARN ((0, 0,
1484 _("%s is possibly continued on this volume: header contains truncated name"),
1485 quote (bufmap_head->file_name)));
1486 else
1487 {
1488 WARN ((0, 0, _("%s is not continued on this volume"),
1489 quote (bufmap_head->file_name)));
1490 return false;
1491 }
1492 }
1493
1494 s = continued_file_size + continued_file_offset;
1495
1496 if (bufmap_head->sizetotal != s || s < continued_file_offset)
1497 {
1498 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1499 char s1buf[UINTMAX_STRSIZE_BOUND];
1500 char s2buf[UINTMAX_STRSIZE_BOUND];
1501
1502 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1503 quote (continued_file_name),
1504 STRINGIFY_BIGINT (bufmap_head->sizetotal, totsizebuf),
1505 STRINGIFY_BIGINT (continued_file_size, s1buf),
1506 STRINGIFY_BIGINT (continued_file_offset, s2buf)));
1507 return false;
1508 }
1509
1510 if (bufmap_head->sizetotal - bufmap_head->sizeleft !=
1511 continued_file_offset)
1512 {
1513 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1514 char s1buf[UINTMAX_STRSIZE_BOUND];
1515 char s2buf[UINTMAX_STRSIZE_BOUND];
1516
1517 WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
1518 STRINGIFY_BIGINT (bufmap_head->sizetotal, totsizebuf),
1519 STRINGIFY_BIGINT (bufmap_head->sizeleft, s1buf),
1520 STRINGIFY_BIGINT (continued_file_offset, s2buf)));
1521
1522 return false;
1523 }
1524 }
1525
1526 increase_volume_number ();
1527 return true;
1528 }
1529
1530 \f
1531 #define VOLUME_TEXT " Volume "
1532 #define VOLUME_TEXT_LEN (sizeof VOLUME_TEXT - 1)
1533
1534 char *
1535 drop_volume_label_suffix (const char *label)
1536 {
1537 const char *p;
1538 size_t len = strlen (label);
1539
1540 if (len < 1)
1541 return NULL;
1542
1543 for (p = label + len - 1; p > label && isdigit ((unsigned char) *p); p--)
1544 ;
1545 if (p > label && p - (VOLUME_TEXT_LEN - 1) > label)
1546 {
1547 p -= VOLUME_TEXT_LEN - 1;
1548 if (memcmp (p, VOLUME_TEXT, VOLUME_TEXT_LEN) == 0)
1549 {
1550 char *s = xmalloc ((len = p - label) + 1);
1551 memcpy (s, label, len);
1552 s[len] = 0;
1553 return s;
1554 }
1555 }
1556
1557 return NULL;
1558 }
1559
1560 /* Check LABEL against the volume label, seen as a globbing
1561 pattern. Return true if the pattern matches. In case of failure,
1562 retry matching a volume sequence number before giving up in
1563 multi-volume mode. */
1564 static bool
1565 check_label_pattern (const char *label)
1566 {
1567 char *string;
1568 bool result = false;
1569
1570 if (fnmatch (volume_label_option, label, 0) == 0)
1571 return true;
1572
1573 if (!multi_volume_option)
1574 return false;
1575
1576 string = drop_volume_label_suffix (label);
1577 if (string)
1578 {
1579 result = fnmatch (string, volume_label_option, 0) == 0;
1580 free (string);
1581 }
1582 return result;
1583 }
1584
1585 /* Check if the next block contains a volume label and if this matches
1586 the one given in the command line */
1587 static void
1588 match_volume_label (void)
1589 {
1590 if (!volume_label)
1591 {
1592 union block *label = find_next_block ();
1593
1594 if (!label)
1595 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1596 quote (volume_label_option)));
1597 if (label->header.typeflag == GNUTYPE_VOLHDR)
1598 {
1599 if (memchr (label->header.name, '\0', sizeof label->header.name))
1600 assign_string (&volume_label, label->header.name);
1601 else
1602 {
1603 volume_label = xmalloc (sizeof (label->header.name) + 1);
1604 memcpy (volume_label, label->header.name,
1605 sizeof (label->header.name));
1606 volume_label[sizeof (label->header.name)] = 0;
1607 }
1608 }
1609 else if (label->header.typeflag == XGLTYPE)
1610 {
1611 struct tar_stat_info st;
1612 tar_stat_init (&st);
1613 xheader_read (&st.xhdr, label,
1614 OFF_FROM_HEADER (label->header.size));
1615 xheader_decode (&st);
1616 tar_stat_destroy (&st);
1617 }
1618 }
1619
1620 if (!volume_label)
1621 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1622 quote (volume_label_option)));
1623
1624 if (!check_label_pattern (volume_label))
1625 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1626 quote_n (0, volume_label),
1627 quote_n (1, volume_label_option)));
1628 }
1629
1630 /* Mark the archive with volume label STR. */
1631 static void
1632 _write_volume_label (const char *str)
1633 {
1634 if (archive_format == POSIX_FORMAT)
1635 xheader_store ("GNU.volume.label", &dummy, str);
1636 else
1637 {
1638 union block *label = find_next_block ();
1639
1640 memset (label, 0, BLOCKSIZE);
1641
1642 strcpy (label->header.name, str);
1643 assign_string (&current_stat_info.file_name,
1644 label->header.name);
1645 current_stat_info.had_trailing_slash =
1646 strip_trailing_slashes (current_stat_info.file_name);
1647
1648 label->header.typeflag = GNUTYPE_VOLHDR;
1649 TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
1650 finish_header (&current_stat_info, label, -1);
1651 set_next_block_after (label);
1652 }
1653 }
1654
1655 #define VOL_SUFFIX "Volume"
1656
1657 /* Add a volume label to a part of multi-volume archive */
1658 static void
1659 add_volume_label (void)
1660 {
1661 char buf[UINTMAX_STRSIZE_BOUND];
1662 char *p = STRINGIFY_BIGINT (volno, buf);
1663 char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
1664 + strlen (p) + 2);
1665 sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
1666 _write_volume_label (s);
1667 free (s);
1668 }
1669
1670 static void
1671 add_chunk_header (struct bufmap *map)
1672 {
1673 if (archive_format == POSIX_FORMAT)
1674 {
1675 off_t block_ordinal;
1676 union block *blk;
1677 struct tar_stat_info st;
1678
1679 memset (&st, 0, sizeof st);
1680 st.orig_file_name = st.file_name = map->file_name;
1681 st.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
1682 st.stat.st_uid = getuid ();
1683 st.stat.st_gid = getgid ();
1684 st.orig_file_name = xheader_format_name (&st,
1685 "%d/GNUFileParts.%p/%f.%n",
1686 volno);
1687 st.file_name = st.orig_file_name;
1688 st.archive_file_size = st.stat.st_size = map->sizeleft;
1689
1690 block_ordinal = current_block_ordinal ();
1691 blk = start_header (&st);
1692 if (!blk)
1693 abort (); /* FIXME */
1694 finish_header (&st, blk, block_ordinal);
1695 free (st.orig_file_name);
1696 }
1697 }
1698
1699
1700 /* Add a volume label to the current archive */
1701 static void
1702 write_volume_label (void)
1703 {
1704 if (multi_volume_option)
1705 add_volume_label ();
1706 else
1707 _write_volume_label (volume_label_option);
1708 }
1709
1710 /* Write GNU multi-volume header */
1711 static void
1712 gnu_add_multi_volume_header (struct bufmap *map)
1713 {
1714 int tmp;
1715 union block *block = find_next_block ();
1716
1717 if (strlen (map->file_name) > NAME_FIELD_SIZE)
1718 WARN ((0, 0,
1719 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1720 quotearg_colon (map->file_name)));
1721
1722 memset (block, 0, BLOCKSIZE);
1723
1724 strncpy (block->header.name, map->file_name, NAME_FIELD_SIZE);
1725 block->header.typeflag = GNUTYPE_MULTIVOL;
1726
1727 OFF_TO_CHARS (map->sizeleft, block->header.size);
1728 OFF_TO_CHARS (map->sizetotal - map->sizeleft,
1729 block->oldgnu_header.offset);
1730
1731 tmp = verbose_option;
1732 verbose_option = 0;
1733 finish_header (&current_stat_info, block, -1);
1734 verbose_option = tmp;
1735 set_next_block_after (block);
1736 }
1737
1738 /* Add a multi volume header to the current archive. The exact header format
1739 depends on the archive format. */
1740 static void
1741 add_multi_volume_header (struct bufmap *map)
1742 {
1743 if (archive_format == POSIX_FORMAT)
1744 {
1745 off_t d = map->sizetotal - map->sizeleft;
1746 xheader_store ("GNU.volume.filename", &dummy, map->file_name);
1747 xheader_store ("GNU.volume.size", &dummy, &map->sizeleft);
1748 xheader_store ("GNU.volume.offset", &dummy, &d);
1749 }
1750 else
1751 gnu_add_multi_volume_header (map);
1752 }
1753
1754 \f
1755 /* Low-level flush functions */
1756
1757 /* Simple flush read (no multi-volume or label extensions) */
1758 static void
1759 simple_flush_read (void)
1760 {
1761 size_t status; /* result from system call */
1762
1763 checkpoint_run (false);
1764
1765 /* Clear the count of errors. This only applies to a single call to
1766 flush_read. */
1767
1768 read_error_count = 0; /* clear error count */
1769
1770 if (write_archive_to_stdout && record_start_block != 0)
1771 {
1772 archive = STDOUT_FILENO;
1773 status = sys_write_archive_buffer ();
1774 archive = STDIN_FILENO;
1775 if (status != record_size)
1776 archive_write_error (status);
1777 }
1778
1779 for (;;)
1780 {
1781 status = rmtread (archive, record_start->buffer, record_size);
1782 if (status == record_size)
1783 {
1784 records_read++;
1785 return;
1786 }
1787 if (status == SAFE_READ_ERROR)
1788 {
1789 archive_read_error ();
1790 continue; /* try again */
1791 }
1792 break;
1793 }
1794 short_read (status);
1795 }
1796
1797 /* Simple flush write (no multi-volume or label extensions) */
1798 static void
1799 simple_flush_write (size_t level __attribute__((unused)))
1800 {
1801 ssize_t status;
1802
1803 status = _flush_write ();
1804 if (status != record_size)
1805 archive_write_error (status);
1806 else
1807 {
1808 records_written++;
1809 bytes_written += status;
1810 }
1811 }
1812
1813 \f
1814 /* GNU flush functions. These support multi-volume and archive labels in
1815 GNU and PAX archive formats. */
1816
1817 static void
1818 _gnu_flush_read (void)
1819 {
1820 size_t status; /* result from system call */
1821
1822 checkpoint_run (false);
1823
1824 /* Clear the count of errors. This only applies to a single call to
1825 flush_read. */
1826
1827 read_error_count = 0; /* clear error count */
1828
1829 if (write_archive_to_stdout && record_start_block != 0)
1830 {
1831 archive = STDOUT_FILENO;
1832 status = sys_write_archive_buffer ();
1833 archive = STDIN_FILENO;
1834 if (status != record_size)
1835 archive_write_error (status);
1836 }
1837
1838 for (;;)
1839 {
1840 status = rmtread (archive, record_start->buffer, record_size);
1841 if (status == record_size)
1842 {
1843 records_read++;
1844 return;
1845 }
1846
1847 /* The condition below used to include
1848 || (status > 0 && !read_full_records)
1849 This is incorrect since even if new_volume() succeeds, the
1850 subsequent call to rmtread will overwrite the chunk of data
1851 already read in the buffer, so the processing will fail */
1852 if ((status == 0
1853 || (status == SAFE_READ_ERROR && errno == ENOSPC))
1854 && multi_volume_option)
1855 {
1856 while (!try_new_volume ())
1857 ;
1858 if (current_block == record_end)
1859 /* Necessary for blocking_factor == 1 */
1860 flush_archive();
1861 return;
1862 }
1863 else if (status == SAFE_READ_ERROR)
1864 {
1865 archive_read_error ();
1866 continue;
1867 }
1868 break;
1869 }
1870 short_read (status);
1871 }
1872
1873 static void
1874 gnu_flush_read (void)
1875 {
1876 flush_read_ptr = simple_flush_read; /* Avoid recursion */
1877 _gnu_flush_read ();
1878 flush_read_ptr = gnu_flush_read;
1879 }
1880
1881 static void
1882 _gnu_flush_write (size_t buffer_level)
1883 {
1884 ssize_t status;
1885 union block *header;
1886 char *copy_ptr;
1887 size_t copy_size;
1888 size_t bufsize;
1889 struct bufmap *map;
1890
1891 status = _flush_write ();
1892 if (status != record_size && !multi_volume_option)
1893 archive_write_error (status);
1894 else
1895 {
1896 if (status)
1897 records_written++;
1898 bytes_written += status;
1899 }
1900
1901 if (status == record_size)
1902 {
1903 return;
1904 }
1905
1906 map = bufmap_locate (status);
1907
1908 if (status % BLOCKSIZE)
1909 {
1910 ERROR ((0, 0, _("write did not end on a block boundary")));
1911 archive_write_error (status);
1912 }
1913
1914 /* In multi-volume mode. */
1915 /* ENXIO is for the UNIX PC. */
1916 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
1917 archive_write_error (status);
1918
1919 if (!new_volume (ACCESS_WRITE))
1920 return;
1921
1922 tar_stat_destroy (&dummy);
1923
1924 increase_volume_number ();
1925 prev_written += bytes_written;
1926 bytes_written = 0;
1927
1928 copy_ptr = record_start->buffer + status;
1929 copy_size = buffer_level - status;
1930
1931 /* Switch to the next buffer */
1932 record_index = !record_index;
1933 init_buffer ();
1934
1935 inhibit_map = 1;
1936
1937 if (volume_label_option)
1938 add_volume_label ();
1939
1940 if (map)
1941 add_multi_volume_header (map);
1942
1943 write_extended (true, &dummy, find_next_block ());
1944 tar_stat_destroy (&dummy);
1945
1946 if (map)
1947 add_chunk_header (map);
1948 header = find_next_block ();
1949 bufmap_reset (map, header - record_start);
1950 bufsize = available_space_after (header);
1951 inhibit_map = 0;
1952 while (bufsize < copy_size)
1953 {
1954 memcpy (header->buffer, copy_ptr, bufsize);
1955 copy_ptr += bufsize;
1956 copy_size -= bufsize;
1957 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
1958 header = find_next_block ();
1959 bufsize = available_space_after (header);
1960 }
1961 memcpy (header->buffer, copy_ptr, copy_size);
1962 memset (header->buffer + copy_size, 0, bufsize - copy_size);
1963 set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
1964 find_next_block ();
1965 }
1966
1967 static void
1968 gnu_flush_write (size_t buffer_level)
1969 {
1970 flush_write_ptr = simple_flush_write; /* Avoid recursion */
1971 _gnu_flush_write (buffer_level);
1972 flush_write_ptr = gnu_flush_write;
1973 }
1974
1975 void
1976 flush_read (void)
1977 {
1978 flush_read_ptr ();
1979 }
1980
1981 void
1982 flush_write (void)
1983 {
1984 flush_write_ptr (record_size);
1985 }
1986
1987 void
1988 open_archive (enum access_mode wanted_access)
1989 {
1990 flush_read_ptr = gnu_flush_read;
1991 flush_write_ptr = gnu_flush_write;
1992
1993 _open_archive (wanted_access);
1994 switch (wanted_access)
1995 {
1996 case ACCESS_READ:
1997 case ACCESS_UPDATE:
1998 if (volume_label_option)
1999 match_volume_label ();
2000 break;
2001
2002 case ACCESS_WRITE:
2003 records_written = 0;
2004 if (volume_label_option)
2005 write_volume_label ();
2006 break;
2007 }
2008 set_volume_start_time ();
2009 }
This page took 0.125138 seconds and 4 git commands to generate.