Minor fixes
[tar.git] / src / delete.c
blobd59a857f5f9b663adfbdab89f39eba048e7edf80
1 /* Delete entries from a tar archive.
3 Copyright (C) 1988, 1992, 1994, 1996, 1997, 2000, 2001, 2003, 2004,
4 2005, 2006 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
9 version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 #include <system.h>
21 #include <system-ioctl.h>
23 #include "common.h"
24 #include <rmt.h>
26 static union block *new_record;
27 static int new_blocks;
28 static bool acting_as_filter;
30 /* FIXME: This module should not directly handle the following
31 variables, instead, the interface should be cleaned up. */
32 extern union block *record_start;
33 extern union block *record_end;
34 extern union block *current_block;
35 extern union block *recent_long_name;
36 extern union block *recent_long_link;
37 extern off_t records_read;
38 extern off_t records_written;
40 /* The number of records skipped at the start of the archive, when
41 passing over members that are not deleted. */
42 off_t records_skipped;
44 /* Move archive descriptor by COUNT records worth. If COUNT is
45 positive we move forward, else we move negative. If it's a tape,
46 MTIOCTOP had better work. If it's something else, we try to seek
47 on it. If we can't seek, we lose! */
48 static void
49 move_archive (off_t count)
51 if (count == 0)
52 return;
54 #ifdef MTIOCTOP
56 struct mtop operation;
58 if (count < 0
59 ? (operation.mt_op = MTBSR,
60 operation.mt_count = -count,
61 operation.mt_count == -count)
62 : (operation.mt_op = MTFSR,
63 operation.mt_count = count,
64 operation.mt_count == count))
66 if (0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
67 return;
69 if (errno == EIO
70 && 0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
71 return;
74 #endif /* MTIOCTOP */
77 off_t position0 = rmtlseek (archive, (off_t) 0, SEEK_CUR);
78 off_t increment = record_size * (off_t) count;
79 off_t position = position0 + increment;
81 if (increment / count != record_size
82 || (position < position0) != (increment < 0)
83 || (position = position < 0 ? 0 : position,
84 rmtlseek (archive, position, SEEK_SET) != position))
85 seek_error_details (archive_name_array[0], position);
87 return;
91 /* Write out the record which has been filled. If MOVE_BACK_FLAG,
92 backspace to where we started. */
93 static void
94 write_record (int move_back_flag)
96 union block *save_record = record_start;
97 record_start = new_record;
99 if (acting_as_filter)
101 archive = STDOUT_FILENO;
102 flush_write ();
103 archive = STDIN_FILENO;
105 else
107 move_archive ((records_written + records_skipped) - records_read);
108 flush_write ();
111 record_start = save_record;
113 if (move_back_flag)
115 /* Move the tape head back to where we were. */
117 if (! acting_as_filter)
118 move_archive (records_read - (records_written + records_skipped));
121 new_blocks = 0;
124 static void
125 write_recent_blocks (union block *h, size_t blocks)
127 size_t i;
128 for (i = 0; i < blocks; i++)
130 new_record[new_blocks++] = h[i];
131 if (new_blocks == blocking_factor)
132 write_record (1);
136 static void
137 write_recent_bytes (char *data, size_t bytes)
139 size_t blocks = bytes / BLOCKSIZE;
140 size_t rest = bytes - blocks * BLOCKSIZE;
142 write_recent_blocks ((union block *)data, blocks);
143 memcpy (new_record[new_blocks].buffer, data + blocks * BLOCKSIZE, rest);
144 if (rest < BLOCKSIZE)
145 memset (new_record[new_blocks].buffer + rest, 0, BLOCKSIZE - rest);
146 new_blocks++;
147 if (new_blocks == blocking_factor)
148 write_record (1);
151 void
152 delete_archive_members (void)
154 enum read_header logical_status = HEADER_STILL_UNREAD;
155 enum read_header previous_status = HEADER_STILL_UNREAD;
157 /* FIXME: Should clean the routine before cleaning these variables :-( */
158 struct name *name;
159 off_t blocks_to_skip = 0;
160 off_t blocks_to_keep = 0;
161 int kept_blocks_in_record;
163 name_gather ();
164 open_archive (ACCESS_UPDATE);
165 acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
169 enum read_header status = read_header (true);
171 switch (status)
173 case HEADER_STILL_UNREAD:
174 abort ();
176 case HEADER_SUCCESS:
177 if ((name = name_scan (current_stat_info.file_name)) == NULL)
179 skip_member ();
180 break;
182 name->found_count++;
183 if (!ISFOUND(name))
185 skip_member ();
186 break;
189 /* Fall through. */
190 case HEADER_SUCCESS_EXTENDED:
191 logical_status = status;
192 break;
194 case HEADER_ZERO_BLOCK:
195 if (ignore_zeros_option)
197 set_next_block_after (current_header);
198 break;
200 /* Fall through. */
201 case HEADER_END_OF_FILE:
202 logical_status = HEADER_END_OF_FILE;
203 break;
205 case HEADER_FAILURE:
206 set_next_block_after (current_header);
207 switch (previous_status)
209 case HEADER_STILL_UNREAD:
210 WARN ((0, 0, _("This does not look like a tar archive")));
211 /* Fall through. */
213 case HEADER_SUCCESS:
214 case HEADER_SUCCESS_EXTENDED:
215 case HEADER_ZERO_BLOCK:
216 ERROR ((0, 0, _("Skipping to next header")));
217 /* Fall through. */
219 case HEADER_FAILURE:
220 break;
222 case HEADER_END_OF_FILE:
223 abort ();
225 break;
228 previous_status = status;
230 while (logical_status == HEADER_STILL_UNREAD);
232 records_skipped = records_read - 1;
233 new_record = xmalloc (record_size);
235 if (logical_status == HEADER_SUCCESS
236 || logical_status == HEADER_SUCCESS_EXTENDED)
238 write_archive_to_stdout = false;
240 /* Save away blocks before this one in this record. */
242 new_blocks = current_block - record_start;
243 if (new_blocks)
244 memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
246 if (logical_status == HEADER_SUCCESS)
248 /* FIXME: Pheew! This is crufty code! */
249 logical_status = HEADER_STILL_UNREAD;
250 goto flush_file;
253 /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
254 "delete.c", line 223: warning: loop not entered at top
255 Reported by Bruno Haible. */
256 while (1)
258 enum read_header status;
260 /* Fill in a record. */
262 if (current_block == record_end)
263 flush_archive ();
264 status = read_header (false);
266 xheader_decode (&current_stat_info);
268 if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
270 set_next_block_after (current_header);
271 continue;
273 if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
275 logical_status = HEADER_END_OF_FILE;
276 break;
279 if (status == HEADER_FAILURE)
281 ERROR ((0, 0, _("Deleting non-header from archive")));
282 set_next_block_after (current_header);
283 continue;
286 /* Found another header. */
288 if ((name = name_scan (current_stat_info.file_name)) != NULL)
290 name->found_count++;
291 if (ISFOUND(name))
293 flush_file:
294 set_next_block_after (current_header);
295 blocks_to_skip = (current_stat_info.stat.st_size
296 + BLOCKSIZE - 1) / BLOCKSIZE;
298 while (record_end - current_block <= blocks_to_skip)
300 blocks_to_skip -= (record_end - current_block);
301 flush_archive ();
303 current_block += blocks_to_skip;
304 blocks_to_skip = 0;
305 continue;
308 /* Copy header. */
310 if (current_stat_info.xhdr.size)
312 write_recent_bytes (current_stat_info.xhdr.buffer,
313 current_stat_info.xhdr.size);
315 else
317 write_recent_blocks (recent_long_name, recent_long_name_blocks);
318 write_recent_blocks (recent_long_link, recent_long_link_blocks);
320 new_record[new_blocks] = *current_header;
321 new_blocks++;
322 blocks_to_keep
323 = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
324 set_next_block_after (current_header);
325 if (new_blocks == blocking_factor)
326 write_record (1);
328 /* Copy data. */
330 kept_blocks_in_record = record_end - current_block;
331 if (kept_blocks_in_record > blocks_to_keep)
332 kept_blocks_in_record = blocks_to_keep;
334 while (blocks_to_keep)
336 int count;
338 if (current_block == record_end)
340 flush_read ();
341 current_block = record_start;
342 kept_blocks_in_record = blocking_factor;
343 if (kept_blocks_in_record > blocks_to_keep)
344 kept_blocks_in_record = blocks_to_keep;
346 count = kept_blocks_in_record;
347 if (blocking_factor - new_blocks < count)
348 count = blocking_factor - new_blocks;
350 if (! count)
351 abort ();
353 memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE);
354 new_blocks += count;
355 current_block += count;
356 blocks_to_keep -= count;
357 kept_blocks_in_record -= count;
359 if (new_blocks == blocking_factor)
360 write_record (1);
364 if (logical_status == HEADER_END_OF_FILE)
366 /* Write the end of tape. FIXME: we can't use write_eot here,
367 as it gets confused when the input is at end of file. */
369 int total_zero_blocks = 0;
373 int zero_blocks = blocking_factor - new_blocks;
374 memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
375 total_zero_blocks += zero_blocks;
376 write_record (total_zero_blocks < 2);
378 while (total_zero_blocks < 2);
381 if (! acting_as_filter && ! _isrmt (archive))
383 if (sys_truncate (archive))
384 truncate_warn (archive_name_array[0]);
387 free (new_record);
389 close_archive ();
390 names_notfound ();