1 /* Update a tar archive.
2 Copyright (C) 1988, 1992, 1994, 1996, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any later
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Implement the 'r', 'u' and 'A' options for tar. 'A' means that the
19 file names are tar files, and they should simply be appended to the end
20 of the archive. No attempt is made to record the reads from the args; if
21 they're on raw tape or something like that, it'll probably lose... */
26 /* FIXME: This module should not directly handle the following variable,
27 instead, this should be done in buffer.c only. */
28 extern union block
*current_block
;
30 /* We've hit the end of the old stuff, and its time to start writing new
31 stuff to the tape. This involves seeking back one record and
32 re-writing the current record (which has been changed). */
33 int time_to_start_writing
= 0;
35 /* Pointer to where we started to write in the first record we write out.
36 This is used if we can't backspace the output and have to null out the
37 first part of the record. */
40 /*------------------------------------------------------------------------.
41 | Catenate file PATH to the archive without creating a header for it. It |
42 | had better be a tar file or the archive is screwed. |
43 `------------------------------------------------------------------------*/
46 append_file (char *path
)
49 struct stat stat_data
;
52 if (stat (path
, &stat_data
) != 0
53 || (handle
= open (path
, O_RDONLY
| O_BINARY
), handle
< 0))
55 ERROR ((0, errno
, _("Cannot open file %s"), path
));
59 bytes_left
= stat_data
.st_size
;
61 while (bytes_left
> 0)
63 union block
*start
= find_next_block ();
64 size_t buffer_size
= available_space_after (start
);
67 if (bytes_left
< buffer_size
)
69 buffer_size
= bytes_left
;
70 status
= buffer_size
% BLOCKSIZE
;
72 memset (start
->buffer
+ bytes_left
, 0,
73 (size_t) (BLOCKSIZE
- status
));
76 status
= full_read (handle
, start
->buffer
, buffer_size
);
79 char buf
[UINTMAX_STRSIZE_BOUND
];
80 FATAL_ERROR ((0, errno
,
81 _("Read error at byte %s reading %lu bytes in file %s"),
82 STRINGIFY_BIGINT (stat_data
.st_size
- bytes_left
, buf
),
83 (unsigned long) buffer_size
, path
));
87 set_next_block_after (start
+ (status
- 1) / BLOCKSIZE
);
89 if (status
!= buffer_size
)
91 char buf
[UINTMAX_STRSIZE_BOUND
];
92 FATAL_ERROR ((0, 0, _("%s: File shrunk by %s bytes, (yark!)"),
93 path
, STRINGIFY_BIGINT (bytes_left
, buf
)));
100 /*-----------------------------------------------------------------------.
101 | Implement the 'r' (add files to end of archive), and 'u' (add files to |
102 | end of archive if they arent there, or are more up to date than the |
103 | version in the archive.) commands. |
104 `-----------------------------------------------------------------------*/
107 update_archive (void)
109 enum read_header previous_status
= HEADER_STILL_UNREAD
;
113 if (subcommand_option
== UPDATE_SUBCOMMAND
)
115 open_archive (ACCESS_UPDATE
);
119 enum read_header status
= read_header ();
123 case HEADER_STILL_UNREAD
:
130 if (subcommand_option
== UPDATE_SUBCOMMAND
131 && (name
= name_scan (current_file_name
), name
))
133 struct stat stat_data
;
134 enum archive_format unused
;
136 decode_header (current_header
, ¤t_stat
, &unused
, 0);
137 if (stat (current_file_name
, &stat_data
) < 0)
138 ERROR ((0, errno
, _("Cannot stat %s"), current_file_name
));
139 else if (current_stat
.st_mtime
>= stat_data
.st_mtime
)
142 set_next_block_after (current_header
);
143 if (current_header
->oldgnu_header
.isextended
)
144 skip_extended_headers ();
145 skip_file (current_stat
.st_size
);
149 case HEADER_ZERO_BLOCK
:
150 current_block
= current_header
;
154 case HEADER_END_OF_FILE
:
159 set_next_block_after (current_header
);
160 switch (previous_status
)
162 case HEADER_STILL_UNREAD
:
163 WARN ((0, 0, _("This does not look like a tar archive")));
167 case HEADER_ZERO_BLOCK
:
168 ERROR ((0, 0, _("Skipping to next header")));
174 case HEADER_END_OF_FILE
:
180 previous_status
= status
;
184 time_to_start_writing
= 1;
185 output_start
= current_block
->buffer
;
190 while (path
= name_from_list (), path
)
192 if (interactive_option
&& !confirm ("add", path
))
194 if (subcommand_option
== CAT_SUBCOMMAND
)
197 dump_file (path
, (dev_t
) -1, 1);