Don't apply file transformations to volume names.
[tar.git] / src / delete.c
bloba0a6d67c0b76033b09761fc7ad33ae1b741910c3
1 /* Delete entries from a tar archive.
3 Copyright (C) 1988, 1992, 1994, 1996, 1997, 2000, 2001, 2003, 2004,
4 2005, 2006, 2010 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;
39 /* The number of records skipped at the start of the archive, when
40 passing over members that are not deleted. */
41 off_t records_skipped;
43 /* Move archive descriptor by COUNT records worth. If COUNT is
44 positive we move forward, else we move negative. If it's a tape,
45 MTIOCTOP had better work. If it's something else, we try to seek
46 on it. If we can't seek, we lose! */
47 static void
48 move_archive (off_t count)
50 if (count == 0)
51 return;
53 #ifdef MTIOCTOP
55 struct mtop operation;
57 if (count < 0
58 ? (operation.mt_op = MTBSR,
59 operation.mt_count = -count,
60 operation.mt_count == -count)
61 : (operation.mt_op = MTFSR,
62 operation.mt_count = count,
63 operation.mt_count == count))
65 if (0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
66 return;
68 if (errno == EIO
69 && 0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
70 return;
73 #endif /* MTIOCTOP */
76 off_t position0 = rmtlseek (archive, (off_t) 0, SEEK_CUR);
77 off_t increment = record_size * (off_t) count;
78 off_t position = position0 + increment;
80 if (increment / count != record_size
81 || (position < position0) != (increment < 0)
82 || (position = position < 0 ? 0 : position,
83 rmtlseek (archive, position, SEEK_SET) != position))
84 seek_error_details (archive_name_array[0], position);
86 return;
90 /* Write out the record which has been filled. If MOVE_BACK_FLAG,
91 backspace to where we started. */
92 static void
93 write_record (int move_back_flag)
95 union block *save_record = record_start;
96 record_start = new_record;
98 if (acting_as_filter)
100 archive = STDOUT_FILENO;
101 flush_write ();
102 archive = STDIN_FILENO;
104 else
106 move_archive ((records_written + records_skipped) - records_read);
107 flush_write ();
110 record_start = save_record;
112 if (move_back_flag)
114 /* Move the tape head back to where we were. */
116 if (! acting_as_filter)
117 move_archive (records_read - (records_written + records_skipped));
120 new_blocks = 0;
123 static void
124 write_recent_blocks (union block *h, size_t blocks)
126 size_t i;
127 for (i = 0; i < blocks; i++)
129 new_record[new_blocks++] = h[i];
130 if (new_blocks == blocking_factor)
131 write_record (1);
135 static void
136 write_recent_bytes (char *data, size_t bytes)
138 size_t blocks = bytes / BLOCKSIZE;
139 size_t rest = bytes - blocks * BLOCKSIZE;
141 write_recent_blocks ((union block *)data, blocks);
142 memcpy (new_record[new_blocks].buffer, data + blocks * BLOCKSIZE, rest);
143 if (rest < BLOCKSIZE)
144 memset (new_record[new_blocks].buffer + rest, 0, BLOCKSIZE - rest);
145 new_blocks++;
146 if (new_blocks == blocking_factor)
147 write_record (1);
150 void
151 delete_archive_members (void)
153 enum read_header logical_status = HEADER_STILL_UNREAD;
154 enum read_header previous_status = HEADER_STILL_UNREAD;
156 /* FIXME: Should clean the routine before cleaning these variables :-( */
157 struct name *name;
158 off_t blocks_to_skip = 0;
159 off_t blocks_to_keep = 0;
160 int kept_blocks_in_record;
162 name_gather ();
163 open_archive (ACCESS_UPDATE);
164 acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
168 enum read_header status = read_header (&current_header,
169 &current_stat_info,
170 read_header_x_raw);
172 switch (status)
174 case HEADER_STILL_UNREAD:
175 abort ();
177 case HEADER_SUCCESS:
178 if ((name = name_scan (current_stat_info.file_name)) == NULL)
180 skip_member ();
181 break;
183 name->found_count++;
184 if (!ISFOUND(name))
186 skip_member ();
187 break;
190 /* Fall through. */
191 case HEADER_SUCCESS_EXTENDED:
192 logical_status = status;
193 break;
195 case HEADER_ZERO_BLOCK:
196 if (ignore_zeros_option)
198 set_next_block_after (current_header);
199 break;
201 /* Fall through. */
202 case HEADER_END_OF_FILE:
203 logical_status = HEADER_END_OF_FILE;
204 break;
206 case HEADER_FAILURE:
207 set_next_block_after (current_header);
208 switch (previous_status)
210 case HEADER_STILL_UNREAD:
211 WARN ((0, 0, _("This does not look like a tar archive")));
212 /* Fall through. */
214 case HEADER_SUCCESS:
215 case HEADER_SUCCESS_EXTENDED:
216 case HEADER_ZERO_BLOCK:
217 ERROR ((0, 0, _("Skipping to next header")));
218 /* Fall through. */
220 case HEADER_FAILURE:
221 break;
223 case HEADER_END_OF_FILE:
224 abort ();
226 break;
229 previous_status = status;
231 while (logical_status == HEADER_STILL_UNREAD);
233 records_skipped = records_read - 1;
234 new_record = xmalloc (record_size);
236 if (logical_status == HEADER_SUCCESS
237 || logical_status == HEADER_SUCCESS_EXTENDED)
239 write_archive_to_stdout = false;
241 /* Save away blocks before this one in this record. */
243 new_blocks = current_block - record_start;
244 if (new_blocks)
245 memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
247 if (logical_status == HEADER_SUCCESS)
249 /* FIXME: Pheew! This is crufty code! */
250 logical_status = HEADER_STILL_UNREAD;
251 goto flush_file;
254 /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
255 "delete.c", line 223: warning: loop not entered at top
256 Reported by Bruno Haible. */
257 while (1)
259 enum read_header status;
261 /* Fill in a record. */
263 if (current_block == record_end)
264 flush_archive ();
265 status = read_header (&current_header, &current_stat_info,
266 read_header_auto);
268 xheader_decode (&current_stat_info);
270 if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
272 set_next_block_after (current_header);
273 continue;
275 if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
277 logical_status = HEADER_END_OF_FILE;
278 break;
281 if (status == HEADER_FAILURE)
283 ERROR ((0, 0, _("Deleting non-header from archive")));
284 set_next_block_after (current_header);
285 continue;
288 /* Found another header. */
290 if ((name = name_scan (current_stat_info.file_name)) != NULL)
292 name->found_count++;
293 if (ISFOUND(name))
295 flush_file:
296 set_next_block_after (current_header);
297 blocks_to_skip = (current_stat_info.stat.st_size
298 + BLOCKSIZE - 1) / BLOCKSIZE;
300 while (record_end - current_block <= blocks_to_skip)
302 blocks_to_skip -= (record_end - current_block);
303 flush_archive ();
305 current_block += blocks_to_skip;
306 blocks_to_skip = 0;
307 continue;
310 /* Copy header. */
312 if (current_stat_info.xhdr.size)
314 write_recent_bytes (current_stat_info.xhdr.buffer,
315 current_stat_info.xhdr.size);
317 else
319 write_recent_blocks (recent_long_name, recent_long_name_blocks);
320 write_recent_blocks (recent_long_link, recent_long_link_blocks);
322 new_record[new_blocks] = *current_header;
323 new_blocks++;
324 blocks_to_keep
325 = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
326 set_next_block_after (current_header);
327 if (new_blocks == blocking_factor)
328 write_record (1);
330 /* Copy data. */
332 kept_blocks_in_record = record_end - current_block;
333 if (kept_blocks_in_record > blocks_to_keep)
334 kept_blocks_in_record = blocks_to_keep;
336 while (blocks_to_keep)
338 int count;
340 if (current_block == record_end)
342 flush_read ();
343 current_block = record_start;
344 kept_blocks_in_record = blocking_factor;
345 if (kept_blocks_in_record > blocks_to_keep)
346 kept_blocks_in_record = blocks_to_keep;
348 count = kept_blocks_in_record;
349 if (blocking_factor - new_blocks < count)
350 count = blocking_factor - new_blocks;
352 if (! count)
353 abort ();
355 memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE);
356 new_blocks += count;
357 current_block += count;
358 blocks_to_keep -= count;
359 kept_blocks_in_record -= count;
361 if (new_blocks == blocking_factor)
362 write_record (1);
366 if (logical_status == HEADER_END_OF_FILE)
368 /* Write the end of tape. FIXME: we can't use write_eot here,
369 as it gets confused when the input is at end of file. */
371 int total_zero_blocks = 0;
375 int zero_blocks = blocking_factor - new_blocks;
376 memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
377 total_zero_blocks += zero_blocks;
378 write_record (total_zero_blocks < 2);
380 while (total_zero_blocks < 2);
383 if (! acting_as_filter && ! _isrmt (archive))
385 if (sys_truncate (archive))
386 truncate_warn (archive_name_array[0]);
389 free (new_record);
391 close_archive ();
392 names_notfound ();