]> Dogcows Code - chaz/tar/blob - src/delete.c
Revamp initial name collection functions to ensure proper argument ordering.
[chaz/tar] / src / delete.c
1 /* Delete entries from a tar archive.
2
3 Copyright 1988, 1992, 1994, 1996-1997, 2000-2001, 2003-2006, 2010,
4 2013 Free Software Foundation, Inc.
5
6 This file is part of GNU tar.
7
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <system.h>
22 #include <system-ioctl.h>
23
24 #include "common.h"
25 #include <rmt.h>
26
27 static union block *new_record;
28 static int new_blocks;
29 static bool acting_as_filter;
30
31 /* FIXME: This module should not directly handle the following
32 variables, instead, the interface should be cleaned up. */
33 extern union block *record_start;
34 extern union block *record_end;
35 extern union block *current_block;
36 extern union block *recent_long_name;
37 extern union block *recent_long_link;
38 extern off_t records_read;
39
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;
43
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)
50 {
51 if (count == 0)
52 return;
53
54 #ifdef MTIOCTOP
55 {
56 struct mtop operation;
57
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))
65 {
66 if (0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
67 return;
68
69 if (errno == EIO
70 && 0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
71 return;
72 }
73 }
74 #endif /* MTIOCTOP */
75
76 {
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;
80
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);
86
87 return;
88 }
89 }
90
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)
95 {
96 union block *save_record = record_start;
97 record_start = new_record;
98
99 if (acting_as_filter)
100 {
101 archive = STDOUT_FILENO;
102 flush_write ();
103 archive = STDIN_FILENO;
104 }
105 else
106 {
107 move_archive ((records_written + records_skipped) - records_read);
108 flush_write ();
109 }
110
111 record_start = save_record;
112
113 if (move_back_flag)
114 {
115 /* Move the tape head back to where we were. */
116
117 if (! acting_as_filter)
118 move_archive (records_read - (records_written + records_skipped));
119 }
120
121 new_blocks = 0;
122 }
123
124 static void
125 write_recent_blocks (union block *h, size_t blocks)
126 {
127 size_t i;
128 for (i = 0; i < blocks; i++)
129 {
130 new_record[new_blocks++] = h[i];
131 if (new_blocks == blocking_factor)
132 write_record (1);
133 }
134 }
135
136 static void
137 write_recent_bytes (char *data, size_t bytes)
138 {
139 size_t blocks = bytes / BLOCKSIZE;
140 size_t rest = bytes - blocks * BLOCKSIZE;
141
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);
149 }
150
151 void
152 delete_archive_members (void)
153 {
154 enum read_header logical_status = HEADER_STILL_UNREAD;
155 enum read_header previous_status = HEADER_STILL_UNREAD;
156
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;
162
163 name_gather ();
164 open_archive (ACCESS_UPDATE);
165 acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
166
167 do
168 {
169 enum read_header status = read_header (&current_header,
170 &current_stat_info,
171 read_header_x_raw);
172
173 switch (status)
174 {
175 case HEADER_STILL_UNREAD:
176 abort ();
177
178 case HEADER_SUCCESS:
179 if ((name = name_scan (current_stat_info.file_name)) == NULL)
180 {
181 skip_member ();
182 break;
183 }
184 name->found_count++;
185 if (!ISFOUND(name))
186 {
187 skip_member ();
188 break;
189 }
190
191 /* Fall through. */
192 case HEADER_SUCCESS_EXTENDED:
193 logical_status = status;
194 break;
195
196 case HEADER_ZERO_BLOCK:
197 if (ignore_zeros_option)
198 {
199 set_next_block_after (current_header);
200 break;
201 }
202 /* Fall through. */
203 case HEADER_END_OF_FILE:
204 logical_status = HEADER_END_OF_FILE;
205 break;
206
207 case HEADER_FAILURE:
208 set_next_block_after (current_header);
209 switch (previous_status)
210 {
211 case HEADER_STILL_UNREAD:
212 WARN ((0, 0, _("This does not look like a tar archive")));
213 /* Fall through. */
214
215 case HEADER_SUCCESS:
216 case HEADER_SUCCESS_EXTENDED:
217 case HEADER_ZERO_BLOCK:
218 ERROR ((0, 0, _("Skipping to next header")));
219 /* Fall through. */
220
221 case HEADER_FAILURE:
222 break;
223
224 case HEADER_END_OF_FILE:
225 abort ();
226 }
227 break;
228 }
229
230 previous_status = status;
231 }
232 while (logical_status == HEADER_STILL_UNREAD);
233
234 records_skipped = records_read - 1;
235 new_record = xmalloc (record_size);
236
237 if (logical_status == HEADER_SUCCESS
238 || logical_status == HEADER_SUCCESS_EXTENDED)
239 {
240 write_archive_to_stdout = false;
241
242 /* Save away blocks before this one in this record. */
243
244 new_blocks = current_block - record_start;
245 if (new_blocks)
246 memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
247
248 if (logical_status == HEADER_SUCCESS)
249 {
250 /* FIXME: Pheew! This is crufty code! */
251 logical_status = HEADER_STILL_UNREAD;
252 goto flush_file;
253 }
254
255 /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
256 "delete.c", line 223: warning: loop not entered at top
257 Reported by Bruno Haible. */
258 while (1)
259 {
260 enum read_header status;
261
262 /* Fill in a record. */
263
264 if (current_block == record_end)
265 flush_archive ();
266 status = read_header (&current_header, &current_stat_info,
267 read_header_auto);
268
269 xheader_decode (&current_stat_info);
270
271 if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
272 {
273 set_next_block_after (current_header);
274 continue;
275 }
276 if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
277 {
278 logical_status = HEADER_END_OF_FILE;
279 break;
280 }
281
282 if (status == HEADER_FAILURE)
283 {
284 ERROR ((0, 0, _("Deleting non-header from archive")));
285 set_next_block_after (current_header);
286 continue;
287 }
288
289 /* Found another header. */
290
291 if ((name = name_scan (current_stat_info.file_name)) != NULL)
292 {
293 name->found_count++;
294 if (ISFOUND(name))
295 {
296 flush_file:
297 set_next_block_after (current_header);
298 blocks_to_skip = (current_stat_info.stat.st_size
299 + BLOCKSIZE - 1) / BLOCKSIZE;
300
301 while (record_end - current_block <= blocks_to_skip)
302 {
303 blocks_to_skip -= (record_end - current_block);
304 flush_archive ();
305 }
306 current_block += blocks_to_skip;
307 blocks_to_skip = 0;
308 continue;
309 }
310 }
311 /* Copy header. */
312
313 if (current_stat_info.xhdr.size)
314 {
315 write_recent_bytes (current_stat_info.xhdr.buffer,
316 current_stat_info.xhdr.size);
317 }
318 else
319 {
320 write_recent_blocks (recent_long_name, recent_long_name_blocks);
321 write_recent_blocks (recent_long_link, recent_long_link_blocks);
322 }
323 new_record[new_blocks] = *current_header;
324 new_blocks++;
325 blocks_to_keep
326 = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
327 set_next_block_after (current_header);
328 if (new_blocks == blocking_factor)
329 write_record (1);
330
331 /* Copy data. */
332
333 kept_blocks_in_record = record_end - current_block;
334 if (kept_blocks_in_record > blocks_to_keep)
335 kept_blocks_in_record = blocks_to_keep;
336
337 while (blocks_to_keep)
338 {
339 int count;
340
341 if (current_block == record_end)
342 {
343 flush_read ();
344 current_block = record_start;
345 kept_blocks_in_record = blocking_factor;
346 if (kept_blocks_in_record > blocks_to_keep)
347 kept_blocks_in_record = blocks_to_keep;
348 }
349 count = kept_blocks_in_record;
350 if (blocking_factor - new_blocks < count)
351 count = blocking_factor - new_blocks;
352
353 if (! count)
354 abort ();
355
356 memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE);
357 new_blocks += count;
358 current_block += count;
359 blocks_to_keep -= count;
360 kept_blocks_in_record -= count;
361
362 if (new_blocks == blocking_factor)
363 write_record (1);
364 }
365 }
366
367 if (logical_status == HEADER_END_OF_FILE)
368 {
369 /* Write the end of tape. FIXME: we can't use write_eot here,
370 as it gets confused when the input is at end of file. */
371
372 int total_zero_blocks = 0;
373
374 do
375 {
376 int zero_blocks = blocking_factor - new_blocks;
377 memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
378 total_zero_blocks += zero_blocks;
379 write_record (total_zero_blocks < 2);
380 }
381 while (total_zero_blocks < 2);
382 }
383
384 if (! acting_as_filter && ! _isrmt (archive))
385 {
386 if (sys_truncate (archive))
387 truncate_warn (archive_name_array[0]);
388 }
389 }
390 free (new_record);
391
392 close_archive ();
393 names_notfound ();
394 }
This page took 0.049169 seconds and 4 git commands to generate.