1 /* Delete entries from 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 Place - Suite 330, Boston, MA 02111-1307, USA. */
26 static union block *new_record = NULL;
27 static union block *save_record = NULL;
28 static int records_read = 0;
29 static int new_blocks = 0;
30 static int blocks_needed = 0;
32 /* FIXME: This module should not directly handle the following three
33 variables, instead, this should be done in buffer.c only. */
34 extern union block *record_start;
35 extern union block *record_end;
36 extern union block *current_block;
38 /*-------------------------------------------------------------------------.
39 | Move archive descriptor by COUNT records worth. If COUNT is positive we |
40 | move forward, else we move negative. If its a tape, MTIOCTOP had better |
41 | work. If its something else, we try to seek on it. If we can't seek, |
43 `-------------------------------------------------------------------------*/
46 move_archive (int count)
50 struct mtop operation;
55 operation.mt_op = MTFSR;
56 operation.mt_count = count;
60 operation.mt_op = MTBSR;
61 operation.mt_count = -count;
64 if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
69 if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
76 off_t position = rmtlseek (archive, 0L, 1);
78 position += record_size * count;
80 if (rmtlseek (archive, position, 0) != position)
81 FATAL_ERROR ((0, 0, _("Could not re-position archive file")));
87 /*----------------------------------------------------------------.
88 | Write out the record which has been filled. If MOVE_BACK_FLAG, |
89 | backspace to where we started. |
90 `----------------------------------------------------------------*/
93 write_record (int move_back_flag)
95 save_record = record_start;
96 record_start = new_record;
106 move_archive (-(records_read + 1));
110 record_start = save_record;
114 /* Move the tape head back to where we were. */
116 if (archive != STDIN)
117 move_archive (records_read);
122 blocks_needed = blocking_factor;
131 delete_archive_members (void)
133 enum read_header logical_status = HEADER_STILL_UNREAD;
134 enum read_header previous_status = HEADER_STILL_UNREAD;
136 /* FIXME: Should clean the routine before cleaning these variables :-( */
138 int blocks_to_skip = 0;
139 int blocks_to_keep = 0;
140 int kept_blocks_in_record;
143 open_archive (ACCESS_UPDATE);
145 while (logical_status == HEADER_STILL_UNREAD)
147 enum read_header status = read_header ();
151 case HEADER_STILL_UNREAD:
155 if (name = name_scan (current_file_name), !name)
157 set_next_block_after (current_header);
158 if (current_header->oldgnu_header.isextended)
159 skip_extended_headers ();
160 skip_file ((long) (current_stat.st_size));
164 logical_status = HEADER_SUCCESS;
167 case HEADER_ZERO_BLOCK:
168 case HEADER_END_OF_FILE:
169 logical_status = HEADER_END_OF_FILE;
173 set_next_block_after (current_header);
174 switch (previous_status)
176 case HEADER_STILL_UNREAD:
177 WARN ((0, 0, _("This does not look like a tar archive")));
181 case HEADER_ZERO_BLOCK:
182 ERROR ((0, 0, _("Skipping to next header")));
188 case HEADER_END_OF_FILE:
194 previous_status = status;
197 if (logical_status != HEADER_SUCCESS)
205 write_archive_to_stdout = 0;
206 new_record = (union block *) xmalloc ((size_t) record_size);
208 /* Save away blocks before this one in this record. */
210 new_blocks = current_block - record_start;
211 blocks_needed = blocking_factor - new_blocks;
213 memcpy ((void *) new_record, (void *) record_start,
214 (size_t) (new_blocks * BLOCKSIZE));
217 /* FIXME: Old code, before the goto was inserted. To be redesigned. */
218 set_next_block_after (current_header);
219 if (current_header->oldgnu_header.isextended)
220 skip_extended_headers ();
221 skip_file ((long) (current_stat.st_size));
223 logical_status = HEADER_STILL_UNREAD;
226 /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
227 "delete.c", line 223: warning: loop not entered at top
228 Reported by Bruno Haible. */
231 enum read_header status;
233 /* Fill in a record. */
235 if (current_block == record_end)
240 status = read_header ();
242 if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
244 set_next_block_after (current_header);
247 if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
249 logical_status = HEADER_END_OF_FILE;
250 memset (new_record[new_blocks].buffer, 0,
251 (size_t) (BLOCKSIZE * blocks_needed));
252 new_blocks += blocks_needed;
258 if (status == HEADER_FAILURE)
260 ERROR ((0, 0, _("Deleting non-header from archive")));
261 set_next_block_after (current_header);
265 /* Found another header. */
267 if (name = name_scan (current_file_name), name)
271 set_next_block_after (current_header);
272 blocks_to_skip = (current_stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
274 while (record_end - current_block <= blocks_to_skip)
276 blocks_to_skip -= (record_end - current_block);
280 current_block += blocks_to_skip;
287 new_record[new_blocks] = *current_header;
291 = (current_stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
292 set_next_block_after (current_header);
293 if (blocks_needed == 0)
298 kept_blocks_in_record = record_end - current_block;
299 if (kept_blocks_in_record > blocks_to_keep)
300 kept_blocks_in_record = blocks_to_keep;
302 while (blocks_to_keep)
306 if (current_block == record_end)
310 current_block = record_start;
311 kept_blocks_in_record = blocking_factor;
312 if (kept_blocks_in_record > blocks_to_keep)
313 kept_blocks_in_record = blocks_to_keep;
315 count = kept_blocks_in_record;
316 if (count > blocks_needed)
317 count = blocks_needed;
319 memcpy ((void *) (new_record + new_blocks),
320 (void *) current_block,
321 (size_t) (count * BLOCKSIZE));
323 blocks_needed -= count;
324 current_block += count;
325 blocks_to_keep -= count;
326 kept_blocks_in_record -= count;
328 if (blocks_needed == 0)