Copyright (C) 1988, 1992 Free Software Foundation
This file is part of GNU Tar.
GNU Tar is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
GNU Tar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Tar; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* JF implement the 'r' 'u' and 'A' options for tar. */
/* The 'A' option is my own invention: It means that the file-names are
tar files, and they should simply be appended to the end of the archive.
No attempt is made to block the reads from the args; if they're on raw
tape or something like that, it'll probably lose. . . */
int time_to_start_writing
= 0; /* We've hit the end of the old stuff,
and its time to start writing new stuff
to the tape. This involves seeking
back one block and re-writing the current
block (which has been changed). */
char *output_start
; /* Pointer to where we started to write in
the first block we write out. This is used
if we can't backspace the output and have
to null out the first part of the block */
extern void skip_file ();
extern void skip_extended_headers ();
extern union record
*head
;
extern struct stat hstat
;
struct name
*name_scan ();
/* Implement the 'r' (add files to end of archive), and 'u' (add files to
end of archive if they arent there, or are more up to date than the
version in the archive.) commands.*/
extern void dump_file ();
if (cmd_mode
== CMD_UPDATE
)
open_archive (2); /* Open for updating */
case 0: /* A bad record */
msg ("This doesn't look like a tar archive.");
msg ("Skipping to next header");
/* printf("File %s\n",head->header.name); */
/* head->header.name[NAMSIZ-1]='\0'; */
if (cmd_mode
== CMD_UPDATE
&& (name
= name_scan (current_file_name
)))
decode_header (head
, &hstat
, &head_standard
, 0);
if (stat (current_file_name
, &nstat
) < 0)
msg_perror ("can't stat %s:", current_file_name
);
if (hstat
.st_mtime
>= nstat
.st_mtime
)
if (head
->header
.isextended
)
skip_extended_headers ();
skip_file ((long) hstat
.st_size
);
time_to_start_writing
= 1;
output_start
= ar_record
->charptr
;
while (p
= name_from_list ())
if (f_confirm
&& !confirm ("add", p
))
/* Catenate file p to the archive without creating a header for it. It had
better be a tar file or the archive is screwed */
if (0 != stat (p
, &statbuf
) || (fd
= open (p
, O_RDONLY
| O_BINARY
)) < 0)
msg_perror ("can't open file %s", p
);
bytes_left
= statbuf
.st_size
;
bufsiz
= endofrecs ()->charptr
- start
->charptr
;
count
= bufsiz
% RECORDSIZE
;
bzero (start
->charptr
+ bytes_left
, (int) (RECORDSIZE
- count
));
count
= read (fd
, start
->charptr
, bufsiz
);
msg_perror ("read error at byte %ld reading %d bytes in file %s", statbuf
.st_size
- bytes_left
, bufsiz
, p
);
exit (EX_ARGSBAD
); /* FOO */
userec (start
+ (count
- 1) / RECORDSIZE
);
msg ("%s: file shrunk by %d bytes, yark!", p
, bytes_left
);
if (num
== 0 || num
== -1)
else if (c
>= ' ' && c
<= '~')
fprintf (fp
, "\\%03o", c
);
int number_of_blocks_read
= 0;
int number_of_new_records
= 0;
int number_of_records_needed
= 0;
union record
*new_block
= 0;
union record
*save_block
= 0;
int number_of_records_to_skip
= 0;
int number_of_records_to_keep
= 0;
int number_of_kept_records_in_block
;
extern int write_archive_to_stdout
;
/* fprintf(stderr,"Junk files\n"); */
msg ("This doesn't look like a tar archive.");
msg ("Skipping to next header");
/* head->header.name[NAMSIZ-1] = '\0'; */
/* fprintf(stderr,"file %s\n",head->header.name); */
if ((name
= name_scan (current_file_name
)) == (struct name
*) 0)
/* fprintf(stderr,"Skip %ld\n",(long)(hstat.st_size)); */
if (head
->header
.isextended
)
skip_extended_headers ();
skip_file ((long) (hstat
.st_size
));
/* fprintf(stderr,"Out of first loop\n"); */
if (write_archive_to_stdout
)
write_archive_to_stdout
= 0;
new_block
= (union record
*) malloc (blocksize
);
msg ("Can't allocate secondary block of %d bytes", blocksize
);
/* Save away records before this one in this block */
number_of_new_records
= ar_record
- ar_block
;
number_of_records_needed
= blocking
- number_of_new_records
;
if (number_of_new_records
)
bcopy ((void *) ar_block
, (void *) new_block
, (number_of_new_records
) * RECORDSIZE
);
/* fprintf(stderr,"Saved %d recs, need %d more\n",number_of_new_records,number_of_records_needed); */
if (head
->header
.isextended
)
skip_extended_headers ();
skip_file ((long) (hstat
.st_size
));
if (ar_record
== ar_last
)
/* fprintf(stderr,"New block\n"); */
sub_status
= read_header ();
/* fprintf(stderr,"Header type %d\n",sub_status); */
if (sub_status
== 2 && f_ignorez
)
if (sub_status
== EOF
|| sub_status
== 2)
bzero (new_block
[number_of_new_records
].charptr
, RECORDSIZE
* number_of_records_needed
);
number_of_new_records
+= number_of_records_needed
;
number_of_records_needed
= 0;
msg ("Deleting non-header from archive.");
/* Found another header. Yipee! */
/* head->header.name[NAMSIZ-1] = '\0'; */
/* fprintf(stderr,"File %s ",head->header.name); */
if (name
= name_scan (current_file_name
))
/* fprintf(stderr,"Flush it\n"); */
/* decode_header(head,&hstat,&dummy_head,0); */
number_of_records_to_skip
= (hstat
.st_size
+ RECORDSIZE
- 1) / RECORDSIZE
;
/* fprintf(stderr,"Flushing %d recs from %s\n",number_of_records_to_skip,head->header.name); */
while (ar_last
- ar_record
<= number_of_records_to_skip
)
/* fprintf(stderr,"Block: %d <= %d ",ar_last-ar_record,number_of_records_to_skip); */
number_of_records_to_skip
-= (ar_last
- ar_record
);
/* fprintf(stderr,"Block %d left\n",number_of_records_to_skip); */
ar_record
+= number_of_records_to_skip
;
/* fprintf(stderr,"Final %d\n",number_of_records_to_skip); */
number_of_records_to_skip
= 0;
new_block
[number_of_new_records
] = *head
;
number_of_records_needed
--;
number_of_records_to_keep
= (hstat
.st_size
+ RECORDSIZE
- 1) / RECORDSIZE
;
if (number_of_records_needed
== 0)
number_of_kept_records_in_block
= ar_last
- ar_record
;
if (number_of_kept_records_in_block
> number_of_records_to_keep
)
number_of_kept_records_in_block
= number_of_records_to_keep
;
/* fprintf(stderr,"Need %d kept_in %d keep %d\n",blocking,number_of_kept_records_in_block,number_of_records_to_keep); */
while (number_of_records_to_keep
)
if (ar_record
== ar_last
)
/* fprintf(stderr,"Flush. . .\n"); */
number_of_kept_records_in_block
= blocking
;
if (number_of_kept_records_in_block
> number_of_records_to_keep
)
number_of_kept_records_in_block
= number_of_records_to_keep
;
n
= number_of_kept_records_in_block
;
if (n
> number_of_records_needed
)
n
= number_of_records_needed
;
/* fprintf(stderr,"Copying %d\n",n); */
bcopy ((void *) ar_record
, (void *) (new_block
+ number_of_new_records
), n
* RECORDSIZE
);
number_of_new_records
+= n
;
number_of_records_needed
-= n
;
number_of_records_to_keep
-= n
;
number_of_kept_records_in_block
-= n
;
/* fprintf(stderr,"Now new %d need %d keep %d keep_in %d rec %d/%d\n",
number_of_new_records,number_of_records_needed,number_of_records_to_keep,
number_of_kept_records_in_block,ar_record-ar_block,ar_last-ar_block); */
if (number_of_records_needed
== 0)
/* fprintf(stderr,"Write block\n"); */
/* We've filled out a block. Write it out. */
/* Backspace back to where we started. . . */
(void) move_arch (-(number_of_blocks_read
+ 1));
/* Move the tape head back to where we were */
(void) move_arch (number_of_blocks_read
);
number_of_records_needed
= blocking
;
number_of_new_records
= 0;
/* Move archive descriptor by n blocks worth. If n is positive we move
forward, else we move negative. If its a tape, MTIOCTOP had better
work. If its something else, we try to seek on it. If we can't
if ((er
= rmtioctl (archive
, MTIOCTOP
, &t
)) >= 0)
if (errno
== EIO
&& (er
= rmtioctl (archive
, MTIOCTOP
, &t
)) >= 0)
cur
= rmtlseek (archive
, 0L, 1);
/* fprintf(stderr,"Fore to %x\n",cur); */
if (rmtlseek (archive
, cur
, 0) != cur
)
/* Lseek failed. Try a different method */
msg ("Couldn't re-position archive file.");