]> Dogcows Code - chaz/tar/blob - src/buffer.c
(base_64_digits): New constant.
[chaz/tar] / src / buffer.c
1 /* Buffer management for tar.
2 Copyright (C) 1988, 92, 93, 94, 96, 97, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-08-25.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
8 version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 /* Enable GNU extensions in fnmatch.h. */
20 #ifndef _GNU_SOURCE
21 # define _GNU_SOURCE 1
22 #endif
23
24 #include "system.h"
25
26 #include <signal.h>
27 #include <time.h>
28 time_t time ();
29
30 #if MSDOS
31 # include <process.h>
32 #endif
33
34 #if XENIX
35 # include <sys/inode.h>
36 #endif
37
38 #include <fnmatch.h>
39
40 #include "common.h"
41 #include "rmt.h"
42
43 #define DEBUG_FORK 0 /* if nonzero, childs are born stopped */
44
45 #define PREAD 0 /* read file descriptor from pipe() */
46 #define PWRITE 1 /* write file descriptor from pipe() */
47
48 /* Number of retries before giving up on read. */
49 #define READ_ERROR_MAX 10
50
51 /* Globbing pattern to append to volume label if initial match failed. */
52 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
53 \f
54 /* Variables. */
55
56 static tarlong prev_written; /* bytes written on previous volumes */
57 static tarlong bytes_written; /* bytes written on this volume */
58
59 /* FIXME: The following four variables should ideally be static to this
60 module. However, this cannot be done yet, as update.c uses the first
61 three a lot, and compare.c uses the fourth. The cleanup continues! */
62
63 union block *record_start; /* start of record of archive */
64 union block *record_end; /* last+1 block of archive record */
65 union block *current_block; /* current block of archive */
66 enum access_mode access_mode; /* how do we handle the archive */
67 static struct stat archive_stat; /* stat block for archive file */
68
69 static off_t record_start_block; /* block ordinal at record_start */
70
71 /* Where we write list messages (not errors, not interactions) to. Stdout
72 unless we're writing a pipe, in which case stderr. */
73 FILE *stdlis;
74
75 static void backspace_output PARAMS ((void));
76 static int new_volume PARAMS ((enum access_mode));
77 static void write_error PARAMS ((ssize_t));
78 static void read_error PARAMS ((void));
79
80 #if !MSDOS
81 /* Obnoxious test to see if dimwit is trying to dump the archive. */
82 dev_t ar_dev;
83 ino_t ar_ino;
84 #endif
85
86 /* PID of child program, if compress_option or remote archive access. */
87 static pid_t child_pid;
88
89 /* Error recovery stuff */
90 static int read_error_count;
91
92 /* Have we hit EOF yet? */
93 static int hit_eof;
94
95 /* Checkpointing counter */
96 static int checkpoint;
97
98 /* We're reading, but we just read the last block and its time to update. */
99 /* As least EXTERN like this one as possible. FIXME! */
100 extern int time_to_start_writing;
101
102 int file_to_switch_to = -1; /* if remote update, close archive, and use
103 this descriptor to write to */
104
105 static int volno = 1; /* which volume of a multi-volume tape we're
106 on */
107 static int global_volno = 1; /* volume number to print in external
108 messages */
109
110 /* The pointer save_name, which is set in function dump_file() of module
111 create.c, points to the original long filename instead of the new,
112 shorter mangled name that is set in start_header() of module create.c.
113 The pointer save_name is only used in multi-volume mode when the file
114 being processed is non-sparse; if a file is split between volumes, the
115 save_name is used in generating the LF_MULTIVOL record on the second
116 volume. (From Pierce Cantrell, 1991-08-13.) */
117
118 char *save_name; /* name of the file we are currently writing */
119 off_t save_totsize; /* total size of file we are writing, only
120 valid if save_name is non NULL */
121 off_t save_sizeleft; /* where we are in the file we are writing,
122 only valid if save_name is nonzero */
123
124 int write_archive_to_stdout = 0;
125
126 /* Used by flush_read and flush_write to store the real info about saved
127 names. */
128 static char *real_s_name = NULL;
129 static off_t real_s_totsize;
130 static off_t real_s_sizeleft;
131 \f
132 /* Functions. */
133
134 #if DEBUG_FORK
135
136 static pid_t
137 myfork (void)
138 {
139 pid_t result = fork();
140
141 if (result == 0)
142 kill (getpid (), SIGSTOP);
143 return result;
144 }
145
146 # define fork myfork
147
148 #endif /* DEBUG FORK */
149
150 void
151 print_total_written (void)
152 {
153 fprintf (stderr, _("Total bytes written: "));
154 fprintf (stderr, TARLONG_FORMAT, prev_written + bytes_written);
155 fprintf (stderr, "\n");
156 }
157
158 /*--------------------------------------------------------.
159 | Compute and return the block ordinal at current_block. |
160 `--------------------------------------------------------*/
161
162 off_t
163 current_block_ordinal (void)
164 {
165 return record_start_block + (current_block - record_start);
166 }
167
168 /*------------------------------------------------------------------.
169 | If the EOF flag is set, reset it, as well as current_block, etc. |
170 `------------------------------------------------------------------*/
171
172 void
173 reset_eof (void)
174 {
175 if (hit_eof)
176 {
177 hit_eof = 0;
178 current_block = record_start;
179 record_end = record_start + blocking_factor;
180 access_mode = ACCESS_WRITE;
181 }
182 }
183
184 /*-------------------------------------------------------------------------.
185 | Return the location of the next available input or output block. |
186 | Return NULL for EOF. Once we have returned NULL, we just keep returning |
187 | it, to avoid accidentally going on to the next file on the tape. |
188 `-------------------------------------------------------------------------*/
189
190 union block *
191 find_next_block (void)
192 {
193 if (current_block == record_end)
194 {
195 if (hit_eof)
196 return NULL;
197 flush_archive ();
198 if (current_block == record_end)
199 {
200 hit_eof = 1;
201 return NULL;
202 }
203 }
204 return current_block;
205 }
206
207 /*------------------------------------------------------.
208 | Indicate that we have used all blocks up thru BLOCK. |
209 | |
210 | FIXME: should the arg have an off-by-1? |
211 `------------------------------------------------------*/
212
213 void
214 set_next_block_after (union block *block)
215 {
216 while (block >= current_block)
217 current_block++;
218
219 /* Do *not* flush the archive here. If we do, the same argument to
220 set_next_block_after could mean the next block (if the input record
221 is exactly one block long), which is not what is intended. */
222
223 if (current_block > record_end)
224 abort ();
225 }
226
227 /*------------------------------------------------------------------------.
228 | Return the number of bytes comprising the space between POINTER through |
229 | the end of the current buffer of blocks. This space is available for |
230 | filling with data, or taking data from. POINTER is usually (but not |
231 | always) the result previous find_next_block call. |
232 `------------------------------------------------------------------------*/
233
234 size_t
235 available_space_after (union block *pointer)
236 {
237 return record_end->buffer - pointer->buffer;
238 }
239
240 /*------------------------------------------------------------------.
241 | Close file having descriptor FD, and abort if close unsucessful. |
242 `------------------------------------------------------------------*/
243
244 static void
245 xclose (int fd)
246 {
247 if (close (fd) < 0)
248 FATAL_ERROR ((0, errno, _("Cannot close file #%d"), fd));
249 }
250
251 /*-----------------------------------------------------------------------.
252 | Duplicate file descriptor FROM into becoming INTO, or else, issue |
253 | MESSAGE. INTO is closed first and has to be the next available slot. |
254 `-----------------------------------------------------------------------*/
255
256 static void
257 xdup2 (int from, int into, const char *message)
258 {
259 if (from != into)
260 {
261 int status = close (into);
262
263 if (status < 0 && errno != EBADF)
264 FATAL_ERROR ((0, errno, _("Cannot close descriptor %d"), into));
265 status = dup (from);
266 if (status != into)
267 FATAL_ERROR ((0, errno, _("Cannot properly duplicate %s"), message));
268 xclose (from);
269 }
270 }
271
272 #if MSDOS
273
274 /*-------------------------------------------------------.
275 | Set ARCHIVE for writing, then compressing an archive. |
276 `-------------------------------------------------------*/
277
278 static void
279 child_open_for_compress (void)
280 {
281 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
282 }
283
284 /*---------------------------------------------------------.
285 | Set ARCHIVE for uncompressing, then reading an archive. |
286 `---------------------------------------------------------*/
287
288 static void
289 child_open_for_uncompress (void)
290 {
291 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
292 }
293
294 #else /* not MSDOS */
295
296 /*---------------------------------------------------------------------.
297 | Return nonzero if NAME is the name of a regular file, or if the file |
298 | does not exist (so it would be created as a regular file). |
299 `---------------------------------------------------------------------*/
300
301 static int
302 is_regular_file (const char *name)
303 {
304 struct stat stbuf;
305
306 if (stat (name, &stbuf) == 0)
307 return S_ISREG (stbuf.st_mode);
308 else
309 return errno == ENOENT;
310 }
311
312 static ssize_t
313 write_archive_buffer (void)
314 {
315 ssize_t status;
316 ssize_t written = 0;
317
318 while (0 <= (status = rmtwrite (archive, record_start->buffer + written,
319 record_size - written)))
320 {
321 written += status;
322 if (written == record_size
323 || _isrmt (archive) || ! S_ISFIFO (archive_stat.st_mode))
324 break;
325 }
326
327 return written ? written : status;
328 }
329
330 /*-------------------------------------------------------.
331 | Set ARCHIVE for writing, then compressing an archive. |
332 `-------------------------------------------------------*/
333
334 static void
335 child_open_for_compress (void)
336 {
337 int parent_pipe[2];
338 int child_pipe[2];
339 pid_t grandchild_pid;
340
341 if (pipe (parent_pipe) < 0)
342 FATAL_ERROR ((0, errno, _("Cannot open pipe")));
343
344 child_pid = fork ();
345 if (child_pid < 0)
346 FATAL_ERROR ((0, errno, _("Cannot fork")));
347
348 if (child_pid > 0)
349 {
350 /* The parent tar is still here! Just clean up. */
351
352 archive = parent_pipe[PWRITE];
353 xclose (parent_pipe[PREAD]);
354 return;
355 }
356
357 /* The new born child tar is here! */
358
359 program_name = _("tar (child)");
360
361 xclose (parent_pipe[PWRITE]);
362 xdup2 (parent_pipe[PREAD], STDIN_FILENO, _("(child) Pipe to stdin"));
363
364 /* Check if we need a grandchild tar. This happens only if either:
365 a) we are writing stdout: to force reblocking;
366 b) the file is to be accessed by rmt: compressor doesn't know how;
367 c) the file is not a plain file. */
368
369 if (strcmp (archive_name_array[0], "-") != 0
370 && !_remdev (archive_name_array[0])
371 && is_regular_file (archive_name_array[0]))
372 {
373 if (backup_option)
374 maybe_backup_file (archive_name_array[0], 1);
375
376 /* We don't need a grandchild tar. Open the archive and launch the
377 compressor. */
378
379 archive = creat (archive_name_array[0], MODE_RW);
380 if (archive < 0)
381 {
382 int saved_errno = errno;
383
384 if (backup_option)
385 undo_last_backup ();
386 FATAL_ERROR ((0, saved_errno, _("Cannot open archive %s"),
387 archive_name_array[0]));
388 }
389 xdup2 (archive, STDOUT_FILENO, _("Archive to stdout"));
390 execlp (use_compress_program_option, use_compress_program_option,
391 (char *) 0);
392 FATAL_ERROR ((0, errno, _("Cannot exec %s"),
393 use_compress_program_option));
394 }
395
396 /* We do need a grandchild tar. */
397
398 if (pipe (child_pipe) < 0)
399 FATAL_ERROR ((0, errno, _("Cannot open pipe")));
400
401 grandchild_pid = fork ();
402 if (grandchild_pid < 0)
403 FATAL_ERROR ((0, errno, _("Child cannot fork")));
404
405 if (grandchild_pid > 0)
406 {
407 /* The child tar is still here! Launch the compressor. */
408
409 xdup2 (child_pipe[PWRITE], STDOUT_FILENO,
410 _("((child)) Pipe to stdout"));
411 xclose (child_pipe[PREAD]);
412 execlp (use_compress_program_option, use_compress_program_option,
413 (char *) 0);
414 FATAL_ERROR ((0, errno, _("Cannot exec %s"),
415 use_compress_program_option));
416 }
417
418 /* The new born grandchild tar is here! */
419
420 program_name = _("tar (grandchild)");
421
422 /* Prepare for reblocking the data from the compressor into the archive. */
423
424 xdup2 (child_pipe[PREAD], STDIN_FILENO, _("(grandchild) Pipe to stdin"));
425 xclose (child_pipe[PWRITE]);
426
427 if (strcmp (archive_name_array[0], "-") == 0)
428 archive = STDOUT_FILENO;
429 else
430 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
431 if (archive < 0)
432 FATAL_ERROR ((0, errno, _("Cannot open archive %s"),
433 archive_name_array[0]));
434
435 /* Let's read out of the stdin pipe and write an archive. */
436
437 while (1)
438 {
439 ssize_t status = 0;
440 char *cursor;
441 size_t length;
442
443 /* Assemble a record. */
444
445 for (length = 0, cursor = record_start->buffer;
446 length < record_size;
447 length += status, cursor += status)
448 {
449 size_t size = record_size - length;
450
451 if (size < BLOCKSIZE)
452 size = BLOCKSIZE;
453 status = safe_read (STDIN_FILENO, cursor, size);
454 if (status <= 0)
455 break;
456 }
457
458 if (status < 0)
459 FATAL_ERROR ((0, errno, _("Cannot read from compression program")));
460
461 /* Copy the record. */
462
463 if (status == 0)
464 {
465 /* We hit the end of the file. Write last record at
466 full length, as the only role of the grandchild is
467 doing proper reblocking. */
468
469 if (length > 0)
470 {
471 memset (record_start->buffer + length, 0, record_size - length);
472 status = write_archive_buffer ();
473 if (status != record_size)
474 write_error (status);
475 }
476
477 /* There is nothing else to read, break out. */
478 break;
479 }
480
481 status = write_archive_buffer ();
482 if (status != record_size)
483 write_error (status);
484 }
485
486 #if 0
487 close_archive ();
488 #endif
489 exit (exit_status);
490 }
491
492 /*---------------------------------------------------------.
493 | Set ARCHIVE for uncompressing, then reading an archive. |
494 `---------------------------------------------------------*/
495
496 static void
497 child_open_for_uncompress (void)
498 {
499 int parent_pipe[2];
500 int child_pipe[2];
501 pid_t grandchild_pid;
502
503 if (pipe (parent_pipe) < 0)
504 FATAL_ERROR ((0, errno, _("Cannot open pipe")));
505
506 child_pid = fork ();
507 if (child_pid < 0)
508 FATAL_ERROR ((0, errno, _("Cannot fork")));
509
510 if (child_pid > 0)
511 {
512 /* The parent tar is still here! Just clean up. */
513
514 read_full_records_option = 1;
515 archive = parent_pipe[PREAD];
516 xclose (parent_pipe[PWRITE]);
517 return;
518 }
519
520 /* The new born child tar is here! */
521
522 program_name = _("tar (child)");
523
524 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO, _("(child) Pipe to stdout"));
525 xclose (parent_pipe[PREAD]);
526
527 /* Check if we need a grandchild tar. This happens only if either:
528 a) we're reading stdin: to force unblocking;
529 b) the file is to be accessed by rmt: compressor doesn't know how;
530 c) the file is not a plain file. */
531
532 if (strcmp (archive_name_array[0], "-") != 0
533 && !_remdev (archive_name_array[0])
534 && is_regular_file (archive_name_array[0]))
535 {
536 /* We don't need a grandchild tar. Open the archive and lauch the
537 uncompressor. */
538
539 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
540 if (archive < 0)
541 FATAL_ERROR ((0, errno, _("Cannot open archive %s"),
542 archive_name_array[0]));
543 xdup2 (archive, STDIN_FILENO, _("Archive to stdin"));
544 execlp (use_compress_program_option, use_compress_program_option,
545 "-d", (char *) 0);
546 FATAL_ERROR ((0, errno, _("Cannot exec %s"),
547 use_compress_program_option));
548 }
549
550 /* We do need a grandchild tar. */
551
552 if (pipe (child_pipe) < 0)
553 FATAL_ERROR ((0, errno, _("Cannot open pipe")));
554
555 grandchild_pid = fork ();
556 if (grandchild_pid < 0)
557 FATAL_ERROR ((0, errno, _("Child cannot fork")));
558
559 if (grandchild_pid > 0)
560 {
561 /* The child tar is still here! Launch the uncompressor. */
562
563 xdup2 (child_pipe[PREAD], STDIN_FILENO, _("((child)) Pipe to stdin"));
564 xclose (child_pipe[PWRITE]);
565 execlp (use_compress_program_option, use_compress_program_option,
566 "-d", (char *) 0);
567 FATAL_ERROR ((0, errno, _("Cannot exec %s"),
568 use_compress_program_option));
569 }
570
571 /* The new born grandchild tar is here! */
572
573 program_name = _("tar (grandchild)");
574
575 /* Prepare for unblocking the data from the archive into the uncompressor. */
576
577 xdup2 (child_pipe[PWRITE], STDOUT_FILENO, _("(grandchild) Pipe to stdout"));
578 xclose (child_pipe[PREAD]);
579
580 if (strcmp (archive_name_array[0], "-") == 0)
581 archive = STDIN_FILENO;
582 else
583 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
584 MODE_RW, rsh_command_option);
585 if (archive < 0)
586 FATAL_ERROR ((0, errno, _("Cannot open archive %s"),
587 archive_name_array[0]));
588
589 /* Let's read the archive and pipe it into stdout. */
590
591 while (1)
592 {
593 char *cursor;
594 size_t maximum;
595 size_t count;
596 ssize_t status;
597
598 read_error_count = 0;
599
600 error_loop:
601 status = rmtread (archive, record_start->buffer, record_size);
602 if (status < 0)
603 {
604 read_error ();
605 goto error_loop;
606 }
607 if (status == 0)
608 break;
609 cursor = record_start->buffer;
610 maximum = status;
611 while (maximum)
612 {
613 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
614 status = full_write (STDOUT_FILENO, cursor, count);
615 if (status < 0)
616 FATAL_ERROR ((0, errno, _("\
617 Cannot write to compression program")));
618
619 if (status != count)
620 {
621 ERROR ((0, 0, _("\
622 Write to compression program short %lu bytes"),
623 (unsigned long) (count - status)));
624 count = status;
625 }
626
627 cursor += count;
628 maximum -= count;
629 }
630 }
631
632 #if 0
633 close_archive ();
634 #endif
635 exit (exit_status);
636 }
637
638 #endif /* not MSDOS */
639
640 /*--------------------------------------------------------------------------.
641 | Check the LABEL block against the volume label, seen as a globbing |
642 | pattern. Return true if the pattern matches. In case of failure, retry |
643 | matching a volume sequence number before giving up in multi-volume mode. |
644 `--------------------------------------------------------------------------*/
645
646 static int
647 check_label_pattern (union block *label)
648 {
649 char *string;
650 int result;
651
652 if (fnmatch (volume_label_option, label->header.name, 0) == 0)
653 return 1;
654
655 if (!multi_volume_option)
656 return 0;
657
658 string = xmalloc (strlen (volume_label_option)
659 + sizeof VOLUME_LABEL_APPEND + 1);
660 strcpy (string, volume_label_option);
661 strcat (string, VOLUME_LABEL_APPEND);
662 result = fnmatch (string, label->header.name, 0) == 0;
663 free (string);
664 return result;
665 }
666
667 /*------------------------------------------------------------------------.
668 | Open an archive file. The argument specifies whether we are reading or |
669 | writing, or both. |
670 `------------------------------------------------------------------------*/
671
672 void
673 open_archive (enum access_mode access)
674 {
675 int backed_up_flag = 0;
676
677 stdlis = to_stdout_option ? stderr : stdout;
678
679 if (record_size == 0)
680 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
681
682 if (archive_names == 0)
683 FATAL_ERROR ((0, 0, _("No archive name given")));
684
685 current_file_name = NULL;
686 current_link_name = NULL;
687
688 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
689 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
690 size that is reasonable to allocate a buffer. In the GNU system, there
691 is no fixed limit. The only correct thing to do is to use dynamic
692 allocation. (Roland McGrath) */
693
694 if (!real_s_name)
695 real_s_name = (char *) xmalloc (PATH_MAX);
696 /* FIXME: real_s_name is never freed. */
697
698 save_name = NULL;
699
700 if (multi_volume_option)
701 {
702 record_start
703 = (union block *) valloc (record_size + (2 * BLOCKSIZE));
704 if (record_start)
705 record_start += 2;
706 }
707 else
708 record_start = (union block *) valloc (record_size);
709 if (!record_start)
710 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
711 blocking_factor));
712
713 current_block = record_start;
714 record_end = record_start + blocking_factor;
715 /* When updating the archive, we start with reading. */
716 access_mode = access == ACCESS_UPDATE ? ACCESS_READ : access;
717
718 if (multi_volume_option && verify_option)
719 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
720
721 if (use_compress_program_option)
722 {
723 if (multi_volume_option)
724 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
725 if (verify_option)
726 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
727
728 switch (access)
729 {
730 case ACCESS_READ:
731 child_open_for_uncompress ();
732 break;
733
734 case ACCESS_WRITE:
735 child_open_for_compress ();
736 break;
737
738 case ACCESS_UPDATE:
739 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
740 break;
741 }
742
743 if (access == ACCESS_WRITE && strcmp (archive_name_array[0], "-") == 0)
744 stdlis = stderr;
745 }
746 else if (strcmp (archive_name_array[0], "-") == 0)
747 {
748 read_full_records_option = 1; /* could be a pipe, be safe */
749 if (verify_option)
750 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
751
752 switch (access)
753 {
754 case ACCESS_READ:
755 archive = STDIN_FILENO;
756 break;
757
758 case ACCESS_WRITE:
759 archive = STDOUT_FILENO;
760 stdlis = stderr;
761 break;
762
763 case ACCESS_UPDATE:
764 archive = STDIN_FILENO;
765 stdlis = stderr;
766 write_archive_to_stdout = 1;
767 break;
768 }
769 }
770 else if (verify_option)
771 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
772 MODE_RW, rsh_command_option);
773 else
774 switch (access)
775 {
776 case ACCESS_READ:
777 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
778 MODE_RW, rsh_command_option);
779 break;
780
781 case ACCESS_WRITE:
782 if (backup_option)
783 {
784 maybe_backup_file (archive_name_array[0], 1);
785 backed_up_flag = 1;
786 }
787 archive = rmtcreat (archive_name_array[0], MODE_RW,
788 rsh_command_option);
789 break;
790
791 case ACCESS_UPDATE:
792 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
793 MODE_RW, rsh_command_option);
794 break;
795 }
796
797 if (archive < 0
798 || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
799 {
800 int saved_errno = errno;
801
802 if (backed_up_flag)
803 undo_last_backup ();
804 FATAL_ERROR ((0, saved_errno, "%s", archive_name_array[0]));
805 }
806
807 #if !MSDOS
808
809 /* Detect if outputting to "/dev/null". */
810 {
811 static char const dev_null[] = "/dev/null";
812 struct stat dev_null_stat;
813
814 dev_null_output =
815 (strcmp (archive_name_array[0], dev_null) == 0
816 || (! _isrmt (archive)
817 && S_ISCHR (archive_stat.st_mode)
818 && stat (dev_null, &dev_null_stat) == 0
819 && S_ISCHR (dev_null_stat.st_mode)
820 && archive_stat.st_rdev == dev_null_stat.st_rdev));
821 }
822
823 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
824 {
825 ar_dev = archive_stat.st_dev;
826 ar_ino = archive_stat.st_ino;
827 }
828 else
829 ar_dev = 0;
830
831 #endif /* not MSDOS */
832
833 #if MSDOS
834 setmode (archive, O_BINARY);
835 #endif
836
837 switch (access)
838 {
839 case ACCESS_READ:
840 case ACCESS_UPDATE:
841 record_end = record_start; /* set up for 1st record = # 0 */
842 find_next_block (); /* read it in, check for EOF */
843
844 if (volume_label_option)
845 {
846 union block *label = find_next_block ();
847
848 if (!label)
849 FATAL_ERROR ((0, 0, _("Archive not labelled to match `%s'"),
850 volume_label_option));
851 if (!check_label_pattern (label))
852 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
853 label->header.name, volume_label_option));
854 }
855 break;
856
857 case ACCESS_WRITE:
858 if (volume_label_option)
859 {
860 memset ((void *) record_start, 0, BLOCKSIZE);
861 if (multi_volume_option)
862 sprintf (record_start->header.name, "%s Volume 1",
863 volume_label_option);
864 else
865 strcpy (record_start->header.name, volume_label_option);
866
867 assign_string (&current_file_name, record_start->header.name);
868
869 record_start->header.typeflag = GNUTYPE_VOLHDR;
870 TIME_TO_CHARS (time (0), record_start->header.mtime);
871 finish_header (record_start);
872 #if 0
873 current_block++;
874 #endif
875 }
876 break;
877 }
878 }
879
880 /*--------------------------------------.
881 | Perform a write to flush the buffer. |
882 `--------------------------------------*/
883
884 void
885 flush_write (void)
886 {
887 int copy_back;
888 ssize_t status;
889
890 if (checkpoint_option && !(++checkpoint % 10))
891 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
892
893 if (tape_length_option && tape_length_option <= bytes_written)
894 {
895 errno = ENOSPC; /* FIXME: errno should be read-only */
896 status = 0;
897 }
898 else if (dev_null_output)
899 status = record_size;
900 else
901 status = write_archive_buffer ();
902 if (status != record_size && !multi_volume_option)
903 write_error (status);
904
905 if (status > 0)
906 bytes_written += status;
907
908 if (status == record_size)
909 {
910 if (multi_volume_option)
911 {
912 char *cursor;
913
914 if (!save_name)
915 {
916 real_s_name[0] = '\0';
917 real_s_totsize = 0;
918 real_s_sizeleft = 0;
919 return;
920 }
921
922 cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
923 while (*cursor == '/')
924 cursor++;
925
926 strcpy (real_s_name, cursor);
927 real_s_totsize = save_totsize;
928 real_s_sizeleft = save_sizeleft;
929 }
930 return;
931 }
932
933 /* We're multivol. Panic if we didn't get the right kind of response. */
934
935 /* ENXIO is for the UNIX PC. */
936 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
937 write_error (status);
938
939 /* If error indicates a short write, we just move to the next tape. */
940
941 if (!new_volume (ACCESS_WRITE))
942 return;
943
944 if (totals_option)
945 prev_written += bytes_written;
946 bytes_written = 0;
947
948 if (volume_label_option && real_s_name[0])
949 {
950 copy_back = 2;
951 record_start -= 2;
952 }
953 else if (volume_label_option || real_s_name[0])
954 {
955 copy_back = 1;
956 record_start--;
957 }
958 else
959 copy_back = 0;
960
961 if (volume_label_option)
962 {
963 memset ((void *) record_start, 0, BLOCKSIZE);
964 sprintf (record_start->header.name, "%s Volume %d", volume_label_option, volno);
965 TIME_TO_CHARS (time (0), record_start->header.mtime);
966 record_start->header.typeflag = GNUTYPE_VOLHDR;
967 finish_header (record_start);
968 }
969
970 if (real_s_name[0])
971 {
972 int tmp;
973
974 if (volume_label_option)
975 record_start++;
976
977 memset ((void *) record_start, 0, BLOCKSIZE);
978
979 /* FIXME: Michael P Urban writes: [a long name file] is being written
980 when a new volume rolls around [...] Looks like the wrong value is
981 being preserved in real_s_name, though. */
982
983 strcpy (record_start->header.name, real_s_name);
984 record_start->header.typeflag = GNUTYPE_MULTIVOL;
985 OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
986 OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
987 record_start->oldgnu_header.offset);
988 tmp = verbose_option;
989 verbose_option = 0;
990 finish_header (record_start);
991 verbose_option = tmp;
992
993 if (volume_label_option)
994 record_start--;
995 }
996
997 status = write_archive_buffer ();
998 if (status != record_size)
999 write_error (status);
1000
1001 bytes_written += status;
1002
1003 if (copy_back)
1004 {
1005 record_start += copy_back;
1006 memcpy ((void *) current_block,
1007 (void *) (record_start + blocking_factor - copy_back),
1008 (size_t) (copy_back * BLOCKSIZE));
1009 current_block += copy_back;
1010
1011 if (real_s_sizeleft >= copy_back * BLOCKSIZE)
1012 real_s_sizeleft -= copy_back * BLOCKSIZE;
1013 else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
1014 real_s_name[0] = '\0';
1015 else
1016 {
1017 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
1018
1019 while (*cursor == '/')
1020 cursor++;
1021
1022 strcpy (real_s_name, cursor);
1023 real_s_sizeleft = save_sizeleft;
1024 real_s_totsize = save_totsize;
1025 }
1026 copy_back = 0;
1027 }
1028 }
1029
1030 /*---------------------------------------------------------------------.
1031 | Handle write errors on the archive. Write errors are always fatal. |
1032 | Hitting the end of a volume does not cause a write error unless the |
1033 | write was the first record of the volume. |
1034 `---------------------------------------------------------------------*/
1035
1036 static void
1037 write_error (ssize_t status)
1038 {
1039 int saved_errno = errno;
1040
1041 /* It might be useful to know how much was written before the error
1042 occured. Beware that mere printing maybe change errno value. */
1043 if (totals_option)
1044 print_total_written ();
1045
1046 if (status < 0)
1047 FATAL_ERROR ((0, saved_errno, _("Cannot write to %s"),
1048 *archive_name_cursor));
1049 else
1050 FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
1051 (unsigned long) status, (unsigned long) record_size,
1052 *archive_name_cursor));
1053 }
1054
1055 /*-------------------------------------------------------------------.
1056 | Handle read errors on the archive. If the read should be retried, |
1057 | returns to the caller. |
1058 `-------------------------------------------------------------------*/
1059
1060 static void
1061 read_error (void)
1062 {
1063 WARN ((0, errno, _("Read error on %s"), *archive_name_cursor));
1064
1065 if (record_start_block == 0)
1066 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1067
1068 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1069 then give up on reading the archive. */
1070
1071 if (read_error_count++ > READ_ERROR_MAX)
1072 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1073 return;
1074 }
1075
1076 /*-------------------------------------.
1077 | Perform a read to flush the buffer. |
1078 `-------------------------------------*/
1079
1080 void
1081 flush_read (void)
1082 {
1083 ssize_t status; /* result from system call */
1084 size_t left; /* bytes left */
1085 char *more; /* pointer to next byte to read */
1086
1087 if (checkpoint_option && !(++checkpoint % 10))
1088 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1089
1090 /* Clear the count of errors. This only applies to a single call to
1091 flush_read. */
1092
1093 read_error_count = 0; /* clear error count */
1094
1095 if (write_archive_to_stdout && record_start_block != 0)
1096 {
1097 status = write_archive_buffer ();
1098 if (status != record_size)
1099 write_error (status);
1100 }
1101 if (multi_volume_option)
1102 {
1103 if (save_name)
1104 {
1105 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
1106
1107 while (*cursor == '/')
1108 cursor++;
1109
1110 strcpy (real_s_name, cursor);
1111 real_s_sizeleft = save_sizeleft;
1112 real_s_totsize = save_totsize;
1113 }
1114 else
1115 {
1116 real_s_name[0] = '\0';
1117 real_s_totsize = 0;
1118 real_s_sizeleft = 0;
1119 }
1120 }
1121
1122 error_loop:
1123 status = rmtread (archive, record_start->buffer, record_size);
1124 if (status == record_size)
1125 return;
1126
1127 if ((status == 0
1128 || (status < 0 && errno == ENOSPC)
1129 || (status > 0 && !read_full_records_option))
1130 && multi_volume_option)
1131 {
1132 union block *cursor;
1133
1134 try_volume:
1135 switch (subcommand_option)
1136 {
1137 case APPEND_SUBCOMMAND:
1138 case CAT_SUBCOMMAND:
1139 case UPDATE_SUBCOMMAND:
1140 if (!new_volume (ACCESS_UPDATE))
1141 return;
1142 break;
1143
1144 default:
1145 if (!new_volume (ACCESS_READ))
1146 return;
1147 break;
1148 }
1149
1150 vol_error:
1151 status = rmtread (archive, record_start->buffer, record_size);
1152 if (status < 0)
1153 {
1154 read_error ();
1155 goto vol_error;
1156 }
1157 if (status != record_size)
1158 goto short_read;
1159
1160 cursor = record_start;
1161
1162 if (cursor->header.typeflag == GNUTYPE_VOLHDR)
1163 {
1164 if (volume_label_option)
1165 {
1166 if (!check_label_pattern (cursor))
1167 {
1168 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1169 cursor->header.name, volume_label_option));
1170 volno--;
1171 global_volno--;
1172 goto try_volume;
1173 }
1174 }
1175 if (verbose_option)
1176 fprintf (stdlis, _("Reading %s\n"), cursor->header.name);
1177 cursor++;
1178 }
1179 else if (volume_label_option)
1180 WARN ((0, 0, _("WARNING: No volume header")));
1181
1182 if (real_s_name[0])
1183 {
1184 uintmax_t s1, s2;
1185 if (cursor->header.typeflag != GNUTYPE_MULTIVOL
1186 || strcmp (cursor->header.name, real_s_name))
1187 {
1188 WARN ((0, 0, _("%s is not continued on this volume"),
1189 real_s_name));
1190 volno--;
1191 global_volno--;
1192 goto try_volume;
1193 }
1194 s1 = UINTMAX_FROM_CHARS (cursor->header.size);
1195 s2 = UINTMAX_FROM_CHARS (cursor->oldgnu_header.offset);
1196 if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
1197 {
1198 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1199 char s1buf[UINTMAX_STRSIZE_BOUND];
1200 char s2buf[UINTMAX_STRSIZE_BOUND];
1201
1202 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1203 cursor->header.name,
1204 STRINGIFY_BIGINT (save_totsize, totsizebuf),
1205 STRINGIFY_BIGINT (s1, s1buf),
1206 STRINGIFY_BIGINT (s2, s2buf)));
1207 volno--;
1208 global_volno--;
1209 goto try_volume;
1210 }
1211 if (real_s_totsize - real_s_sizeleft
1212 != OFF_FROM_CHARS (cursor->oldgnu_header.offset))
1213 {
1214 WARN ((0, 0, _("This volume is out of sequence")));
1215 volno--;
1216 global_volno--;
1217 goto try_volume;
1218 }
1219 cursor++;
1220 }
1221 current_block = cursor;
1222 return;
1223 }
1224 else if (status < 0)
1225 {
1226 read_error ();
1227 goto error_loop; /* try again */
1228 }
1229
1230 short_read:
1231 more = record_start->buffer + status;
1232 left = record_size - status;
1233
1234 again:
1235 if (left % BLOCKSIZE == 0)
1236 {
1237 /* FIXME: for size=0, multi-volume support. On the first record, warn
1238 about the problem. */
1239
1240 if (!read_full_records_option && verbose_option
1241 && record_start_block == 0 && status > 0)
1242 WARN ((0, 0, _("Record size = %lu blocks"),
1243 (unsigned long) (status / BLOCKSIZE)));
1244
1245 record_end = record_start + (record_size - left) / BLOCKSIZE;
1246
1247 return;
1248 }
1249 if (read_full_records_option)
1250 {
1251 /* User warned us about this. Fix up. */
1252
1253 if (left > 0)
1254 {
1255 error2loop:
1256 status = rmtread (archive, more, left);
1257 if (status < 0)
1258 {
1259 read_error ();
1260 goto error2loop; /* try again */
1261 }
1262 if (status == 0)
1263 FATAL_ERROR ((0, 0, _("Archive %s EOF not on block boundary"),
1264 *archive_name_cursor));
1265 left -= status;
1266 more += status;
1267 goto again;
1268 }
1269 }
1270 else
1271 FATAL_ERROR ((0, 0, _("Only read %lu bytes from archive %s"),
1272 (unsigned long) status, *archive_name_cursor));
1273 }
1274
1275 /*-----------------------------------------------.
1276 | Flush the current buffer to/from the archive. |
1277 `-----------------------------------------------*/
1278
1279 void
1280 flush_archive (void)
1281 {
1282 record_start_block += record_end - record_start;
1283 current_block = record_start;
1284 record_end = record_start + blocking_factor;
1285
1286 if (access_mode == ACCESS_READ && time_to_start_writing)
1287 {
1288 access_mode = ACCESS_WRITE;
1289 time_to_start_writing = 0;
1290
1291 if (file_to_switch_to >= 0)
1292 {
1293 int status = rmtclose (archive);
1294
1295 if (status < 0)
1296 WARN ((0, errno, _("WARNING: Cannot close %s (%d, %d)"),
1297 *archive_name_cursor, archive, status));
1298
1299 archive = file_to_switch_to;
1300 }
1301 else
1302 backspace_output ();
1303 }
1304
1305 switch (access_mode)
1306 {
1307 case ACCESS_READ:
1308 flush_read ();
1309 break;
1310
1311 case ACCESS_WRITE:
1312 flush_write ();
1313 break;
1314
1315 case ACCESS_UPDATE:
1316 abort ();
1317 }
1318 }
1319
1320 /*-------------------------------------------------------------------------.
1321 | Backspace the archive descriptor by one record worth. If its a tape, |
1322 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1323 | can't seek, we lose! |
1324 `-------------------------------------------------------------------------*/
1325
1326 static void
1327 backspace_output (void)
1328 {
1329 #ifdef MTIOCTOP
1330 {
1331 struct mtop operation;
1332
1333 operation.mt_op = MTBSR;
1334 operation.mt_count = 1;
1335 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1336 return;
1337 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1338 return;
1339 }
1340 #endif
1341
1342 {
1343 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
1344
1345 /* Seek back to the beginning of this record and start writing there. */
1346
1347 position -= record_size;
1348 if (rmtlseek (archive, position, SEEK_SET) != position)
1349 {
1350 /* Lseek failed. Try a different method. */
1351
1352 WARN ((0, 0, _("\
1353 Could not backspace archive file; it may be unreadable without -i")));
1354
1355 /* Replace the first part of the record with NULs. */
1356
1357 if (record_start->buffer != output_start)
1358 memset (record_start->buffer, 0,
1359 (size_t) (output_start - record_start->buffer));
1360 }
1361 }
1362 }
1363
1364 /*-------------------------.
1365 | Close the archive file. |
1366 `-------------------------*/
1367
1368 void
1369 close_archive (void)
1370 {
1371 if (time_to_start_writing || access_mode == ACCESS_WRITE)
1372 flush_archive ();
1373
1374 #if !MSDOS
1375
1376 /* Manage to fully drain a pipe we might be reading, so to not break it on
1377 the producer after the EOF block. FIXME: one of these days, GNU tar
1378 might become clever enough to just stop working, once there is no more
1379 work to do, we might have to revise this area in such time. */
1380
1381 if (access_mode == ACCESS_READ
1382 && ! _isrmt (archive)
1383 && S_ISFIFO (archive_stat.st_mode))
1384 while (rmtread (archive, record_start->buffer, record_size) > 0)
1385 continue;
1386 #endif
1387
1388 if (! _isrmt (archive) && subcommand_option == DELETE_SUBCOMMAND)
1389 {
1390 #if MSDOS
1391 int status = write (archive, "", 0);
1392 #else
1393 off_t pos = lseek (archive, (off_t) 0, SEEK_CUR);
1394 int status = pos < 0 ? -1 : ftruncate (archive, pos);
1395 #endif
1396 if (status != 0)
1397 WARN ((0, errno, _("WARNING: Cannot truncate %s"),
1398 *archive_name_cursor));
1399 }
1400 if (verify_option)
1401 verify_volume ();
1402
1403 {
1404 int status = rmtclose (archive);
1405
1406 if (status < 0)
1407 WARN ((0, errno, _("WARNING: Cannot close %s (%d, %d)"),
1408 *archive_name_cursor, archive, status));
1409 }
1410
1411 #if !MSDOS
1412
1413 if (child_pid)
1414 {
1415 WAIT_T wait_status;
1416 pid_t child;
1417
1418 /* Loop waiting for the right child to die, or for no more kids. */
1419
1420 while ((child = wait (&wait_status), child != child_pid)
1421 && child != -1)
1422 continue;
1423
1424 if (child != -1)
1425 {
1426 if (WIFSIGNALED (wait_status)
1427 #if 0
1428 && !WIFSTOPPED (wait_status)
1429 #endif
1430 )
1431 {
1432 /* SIGPIPE is OK, everything else is a problem. */
1433
1434 if (WTERMSIG (wait_status) != SIGPIPE)
1435 ERROR ((0, 0, _("Child died with signal %d%s"),
1436 WTERMSIG (wait_status),
1437 WCOREDUMP (wait_status) ? _(" (core dumped)") : ""));
1438 }
1439 else
1440 {
1441 /* Child voluntarily terminated -- but why? /bin/sh returns
1442 SIGPIPE + 128 if its child, then do nothing. */
1443
1444 if (WEXITSTATUS (wait_status) != (SIGPIPE + 128)
1445 && WEXITSTATUS (wait_status))
1446 ERROR ((0, 0, _("Child returned status %d"),
1447 WEXITSTATUS (wait_status)));
1448 }
1449 }
1450 }
1451 #endif /* !MSDOS */
1452
1453 if (current_file_name)
1454 free (current_file_name);
1455 if (current_link_name)
1456 free (current_link_name);
1457 if (save_name)
1458 free (save_name);
1459 free (multi_volume_option ? record_start - 2 : record_start);
1460 }
1461
1462 /*------------------------------------------------.
1463 | Called to initialize the global volume number. |
1464 `------------------------------------------------*/
1465
1466 void
1467 init_volume_number (void)
1468 {
1469 FILE *file = fopen (volno_file_option, "r");
1470
1471 if (file)
1472 {
1473 fscanf (file, "%d", &global_volno);
1474 if (fclose (file) == EOF)
1475 ERROR ((0, errno, "%s", volno_file_option));
1476 }
1477 else if (errno != ENOENT)
1478 ERROR ((0, errno, "%s", volno_file_option));
1479 }
1480
1481 /*-------------------------------------------------------.
1482 | Called to write out the closing global volume number. |
1483 `-------------------------------------------------------*/
1484
1485 void
1486 closeout_volume_number (void)
1487 {
1488 FILE *file = fopen (volno_file_option, "w");
1489
1490 if (file)
1491 {
1492 fprintf (file, "%d\n", global_volno);
1493 if (fclose (file) == EOF)
1494 ERROR ((0, errno, "%s", volno_file_option));
1495 }
1496 else
1497 ERROR ((0, errno, "%s", volno_file_option));
1498 }
1499
1500 /*-----------------------------------------------------------------------.
1501 | We've hit the end of the old volume. Close it and open the next one. |
1502 | Return nonzero on success. |
1503 `-----------------------------------------------------------------------*/
1504
1505 static int
1506 new_volume (enum access_mode access)
1507 {
1508 static FILE *read_file = NULL;
1509 static int looped = 0;
1510
1511 int status;
1512
1513 if (!read_file && !info_script_option)
1514 /* FIXME: if fopen is used, it will never be closed. */
1515 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
1516
1517 if (now_verifying)
1518 return 0;
1519 if (verify_option)
1520 verify_volume ();
1521
1522 if (status = rmtclose (archive), status < 0)
1523 WARN ((0, errno, _("WARNING: Cannot close %s (%d, %d)"),
1524 *archive_name_cursor, archive, status));
1525
1526 global_volno++;
1527 volno++;
1528 archive_name_cursor++;
1529 if (archive_name_cursor == archive_name_array + archive_names)
1530 {
1531 archive_name_cursor = archive_name_array;
1532 looped = 1;
1533 }
1534
1535 tryagain:
1536 if (looped)
1537 {
1538 /* We have to prompt from now on. */
1539
1540 if (info_script_option)
1541 {
1542 if (volno_file_option)
1543 closeout_volume_number ();
1544 system (info_script_option);
1545 }
1546 else
1547 while (1)
1548 {
1549 char input_buffer[80];
1550
1551 fputc ('\007', stderr);
1552 fprintf (stderr,
1553 _("Prepare volume #%d for %s and hit return: "),
1554 global_volno, *archive_name_cursor);
1555 fflush (stderr);
1556
1557 if (fgets (input_buffer, sizeof (input_buffer), read_file) == 0)
1558 {
1559 fprintf (stderr, _("EOF where user reply was expected"));
1560
1561 if (subcommand_option != EXTRACT_SUBCOMMAND
1562 && subcommand_option != LIST_SUBCOMMAND
1563 && subcommand_option != DIFF_SUBCOMMAND)
1564 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1565
1566 apply_delayed_set_stat ();
1567 exit (TAREXIT_FAILURE);
1568 }
1569 if (input_buffer[0] == '\n'
1570 || input_buffer[0] == 'y'
1571 || input_buffer[0] == 'Y')
1572 break;
1573
1574 switch (input_buffer[0])
1575 {
1576 case '?':
1577 {
1578 fprintf (stderr, _("\
1579 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1580 q Abort tar\n\
1581 ! Spawn a subshell\n\
1582 ? Print this list\n"));
1583 }
1584 break;
1585
1586 case 'q':
1587 /* Quit. */
1588
1589 fprintf (stdlis, _("No new volume; exiting.\n"));
1590
1591 if (subcommand_option != EXTRACT_SUBCOMMAND
1592 && subcommand_option != LIST_SUBCOMMAND
1593 && subcommand_option != DIFF_SUBCOMMAND)
1594 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1595
1596 apply_delayed_set_stat ();
1597 exit (TAREXIT_FAILURE);
1598
1599 case 'n':
1600 /* Get new file name. */
1601
1602 {
1603 char *name = &input_buffer[1];
1604 char *cursor;
1605
1606 while (*name == ' ' || *name == '\t')
1607 name++;
1608 cursor = name;
1609 while (*cursor && *cursor != '\n')
1610 cursor++;
1611 *cursor = '\0';
1612
1613 /* FIXME: the following allocation is never reclaimed. */
1614 *archive_name_cursor = xstrdup (name);
1615 }
1616 break;
1617
1618 case '!':
1619 #if MSDOS
1620 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
1621 #else /* not MSDOS */
1622 switch (fork ())
1623 {
1624 case -1:
1625 WARN ((0, errno, _("Cannot fork!")));
1626 break;
1627
1628 case 0:
1629 {
1630 const char *shell = getenv ("SHELL");
1631
1632 if (shell == NULL)
1633 shell = "/bin/sh";
1634 execlp (shell, "-sh", "-i", 0);
1635 FATAL_ERROR ((0, errno, _("Cannot exec a shell %s"),
1636 shell));
1637 }
1638
1639 default:
1640 {
1641 WAIT_T wait_status;
1642
1643 wait (&wait_status);
1644 }
1645 break;
1646 }
1647
1648 /* FIXME: I'm not sure if that's all that has to be done
1649 here. (jk) */
1650
1651 #endif /* not MSDOS */
1652 break;
1653 }
1654 }
1655 }
1656
1657 if (verify_option)
1658 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1659 rsh_command_option);
1660 else
1661 switch (access)
1662 {
1663 case ACCESS_READ:
1664 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1665 rsh_command_option);
1666 break;
1667
1668 case ACCESS_WRITE:
1669 if (backup_option)
1670 maybe_backup_file (*archive_name_cursor, 1);
1671 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1672 rsh_command_option);
1673 break;
1674
1675 case ACCESS_UPDATE:
1676 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1677 rsh_command_option);
1678 break;
1679 }
1680
1681 if (archive < 0)
1682 {
1683 WARN ((0, errno, _("Cannot open %s"), *archive_name_cursor));
1684 if (!verify_option && access == ACCESS_WRITE && backup_option)
1685 undo_last_backup ();
1686 goto tryagain;
1687 }
1688
1689 #if MSDOS
1690 setmode (archive, O_BINARY);
1691 #endif
1692
1693 return 1;
1694 }
This page took 0.105987 seconds and 4 git commands to generate.