]> Dogcows Code - chaz/tar/blob - src/delete.c
Use current_stat_info
[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 void
136 delete_archive_members (void)
137 {
138 enum read_header logical_status = HEADER_STILL_UNREAD;
139 enum read_header previous_status = HEADER_STILL_UNREAD;
140
141 /* FIXME: Should clean the routine before cleaning these variables :-( */
142 struct name *name;
143 off_t blocks_to_skip = 0;
144 off_t blocks_to_keep = 0;
145 int kept_blocks_in_record;
146
147 name_gather ();
148 open_archive (ACCESS_UPDATE);
149 acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
150
151 do
152 {
153 enum read_header status = read_header (1);
154
155 switch (status)
156 {
157 case HEADER_STILL_UNREAD:
158 abort ();
159
160 case HEADER_SUCCESS:
161 if (name = name_scan (current_stat_info.file_name), !name)
162 {
163 skip_member ();
164 break;
165 }
166 name->found = 1;
167 /* Fall through. */
168 case HEADER_SUCCESS_EXTENDED:
169 logical_status = status;
170 break;
171
172 case HEADER_ZERO_BLOCK:
173 if (ignore_zeros_option)
174 {
175 set_next_block_after (current_header);
176 break;
177 }
178 /* Fall through. */
179 case HEADER_END_OF_FILE:
180 logical_status = HEADER_END_OF_FILE;
181 break;
182
183 case HEADER_FAILURE:
184 set_next_block_after (current_header);
185 switch (previous_status)
186 {
187 case HEADER_STILL_UNREAD:
188 WARN ((0, 0, _("This does not look like a tar archive")));
189 /* Fall through. */
190
191 case HEADER_SUCCESS:
192 case HEADER_ZERO_BLOCK:
193 ERROR ((0, 0, _("Skipping to next header")));
194 /* Fall through. */
195
196 case HEADER_FAILURE:
197 break;
198
199 case HEADER_END_OF_FILE:
200 abort ();
201 }
202 break;
203 }
204
205 previous_status = status;
206 }
207 while (logical_status == HEADER_STILL_UNREAD);
208
209 records_skipped = records_read - 1;
210 new_record = xmalloc (record_size);
211
212 if (logical_status == HEADER_SUCCESS
213 || logical_status == HEADER_SUCCESS_EXTENDED)
214 {
215 write_archive_to_stdout = 0;
216
217 /* Save away blocks before this one in this record. */
218
219 new_blocks = current_block - record_start;
220 if (new_blocks)
221 memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
222
223 if (logical_status == HEADER_SUCCESS)
224 {
225 /* FIXME: Pheew! This is crufty code! */
226 logical_status = HEADER_STILL_UNREAD;
227 goto flush_file;
228 }
229
230 /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
231 "delete.c", line 223: warning: loop not entered at top
232 Reported by Bruno Haible. */
233 while (1)
234 {
235 enum read_header status;
236
237 /* Fill in a record. */
238
239 if (current_block == record_end)
240 flush_archive ();
241 status = read_header (0);
242
243 if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
244 {
245 set_next_block_after (current_header);
246 continue;
247 }
248 if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
249 {
250 logical_status = HEADER_END_OF_FILE;
251 break;
252 }
253
254 if (status == HEADER_FAILURE)
255 {
256 ERROR ((0, 0, _("Deleting non-header from archive")));
257 set_next_block_after (current_header);
258 continue;
259 }
260
261 /* Found another header. */
262
263 if (name = name_scan (current_stat_info.file_name), name)
264 {
265 name->found = 1;
266 flush_file:
267 set_next_block_after (current_header);
268 blocks_to_skip = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
269
270 while (record_end - current_block <= blocks_to_skip)
271 {
272 blocks_to_skip -= (record_end - current_block);
273 flush_archive ();
274 }
275 current_block += blocks_to_skip;
276 blocks_to_skip = 0;
277 continue;
278 }
279
280 /* Copy header. */
281
282 write_recent_blocks (recent_long_name, recent_long_name_blocks);
283 write_recent_blocks (recent_long_link, recent_long_link_blocks);
284 new_record[new_blocks] = *current_header;
285 new_blocks++;
286 blocks_to_keep
287 = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
288 set_next_block_after (current_header);
289 if (new_blocks == blocking_factor)
290 write_record (1);
291
292 /* Copy data. */
293
294 kept_blocks_in_record = record_end - current_block;
295 if (kept_blocks_in_record > blocks_to_keep)
296 kept_blocks_in_record = blocks_to_keep;
297
298 while (blocks_to_keep)
299 {
300 int count;
301
302 if (current_block == record_end)
303 {
304 flush_read ();
305 current_block = record_start;
306 kept_blocks_in_record = blocking_factor;
307 if (kept_blocks_in_record > blocks_to_keep)
308 kept_blocks_in_record = blocks_to_keep;
309 }
310 count = kept_blocks_in_record;
311 if (blocking_factor - new_blocks < count)
312 count = blocking_factor - new_blocks;
313
314 if (! count)
315 abort ();
316
317 memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE);
318 new_blocks += count;
319 current_block += count;
320 blocks_to_keep -= count;
321 kept_blocks_in_record -= count;
322
323 if (new_blocks == blocking_factor)
324 write_record (1);
325 }
326 }
327 }
328
329 if (logical_status == HEADER_END_OF_FILE)
330 {
331 /* Write the end of tape. FIXME: we can't use write_eot here,
332 as it gets confused when the input is at end of file. */
333
334 int total_zero_blocks = 0;
335
336 do
337 {
338 int zero_blocks = blocking_factor - new_blocks;
339 memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
340 total_zero_blocks += zero_blocks;
341 write_record (total_zero_blocks < 2);
342 }
343 while (total_zero_blocks < 2);
344 }
345
346 free (new_record);
347
348 if (! acting_as_filter && ! _isrmt (archive))
349 {
350 #if MSDOS
351 int status = write (archive, "", 0);
352 #else
353 off_t pos = lseek (archive, (off_t) 0, SEEK_CUR);
354 int status = pos < 0 ? -1 : ftruncate (archive, pos);
355 #endif
356 if (status != 0)
357 truncate_warn (archive_name_array[0]);
358 }
359
360 close_archive ();
361 names_notfound ();
362 }
This page took 0.048235 seconds and 5 git commands to generate.