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