1 /* Delete entries from a tar archive.
3 Copyright (C) 1988, 1992, 1994, 1996, 1997, 2000, 2001, 2003 Free
4 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 2, or (at your option) any later
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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 static union block
*new_record
;
26 static int new_blocks
;
27 static bool acting_as_filter
;
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
;
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
;
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! */
48 move_archive (off_t count
)
55 struct mtop operation
;
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
))
69 && 0 <= rmtioctl (archive
, MTIOCTOP
, (char *) &operation
))
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
);
90 /* Write out the record which has been filled. If MOVE_BACK_FLAG,
91 backspace to where we started. */
93 write_record (int move_back_flag
)
95 union block
*save_record
= record_start
;
96 record_start
= new_record
;
100 archive
= STDOUT_FILENO
;
102 archive
= STDIN_FILENO
;
106 move_archive ((records_written
+ records_skipped
) - records_read
);
110 record_start
= save_record
;
114 /* Move the tape head back to where we were. */
116 if (! acting_as_filter
)
117 move_archive (records_read
- (records_written
+ records_skipped
));
124 write_recent_blocks (union block
*h
, size_t blocks
)
127 for (i
= 0; i
< blocks
; i
++)
129 new_record
[new_blocks
++] = h
[i
];
130 if (new_blocks
== blocking_factor
)
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
);
146 if (new_blocks
== blocking_factor
)
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 :-( */
158 off_t blocks_to_skip
= 0;
159 off_t blocks_to_keep
= 0;
160 int kept_blocks_in_record
;
163 open_archive (ACCESS_UPDATE
);
164 acting_as_filter
= strcmp (archive_name_array
[0], "-") == 0;
168 enum read_header status
= read_header (true);
172 case HEADER_STILL_UNREAD
:
176 if (name
= name_scan (current_stat_info
.file_name
), !name
)
183 case HEADER_SUCCESS_EXTENDED
:
184 logical_status
= status
;
187 case HEADER_ZERO_BLOCK
:
188 if (ignore_zeros_option
)
190 set_next_block_after (current_header
);
194 case HEADER_END_OF_FILE
:
195 logical_status
= HEADER_END_OF_FILE
;
199 set_next_block_after (current_header
);
200 switch (previous_status
)
202 case HEADER_STILL_UNREAD
:
203 WARN ((0, 0, _("This does not look like a tar archive")));
207 case HEADER_SUCCESS_EXTENDED
:
208 case HEADER_ZERO_BLOCK
:
209 ERROR ((0, 0, _("Skipping to next header")));
215 case HEADER_END_OF_FILE
:
221 previous_status
= status
;
223 while (logical_status
== HEADER_STILL_UNREAD
);
225 records_skipped
= records_read
- 1;
226 new_record
= xmalloc (record_size
);
228 if (logical_status
== HEADER_SUCCESS
229 || logical_status
== HEADER_SUCCESS_EXTENDED
)
231 write_archive_to_stdout
= 0;
233 /* Save away blocks before this one in this record. */
235 new_blocks
= current_block
- record_start
;
237 memcpy (new_record
, record_start
, new_blocks
* BLOCKSIZE
);
239 if (logical_status
== HEADER_SUCCESS
)
241 /* FIXME: Pheew! This is crufty code! */
242 logical_status
= HEADER_STILL_UNREAD
;
246 /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
247 "delete.c", line 223: warning: loop not entered at top
248 Reported by Bruno Haible. */
251 enum read_header status
;
253 /* Fill in a record. */
255 if (current_block
== record_end
)
257 status
= read_header (false);
259 if (extended_header
.size
)
260 xheader_decode (¤t_stat_info
);
262 if (status
== HEADER_ZERO_BLOCK
&& ignore_zeros_option
)
264 set_next_block_after (current_header
);
267 if (status
== HEADER_END_OF_FILE
|| status
== HEADER_ZERO_BLOCK
)
269 logical_status
= HEADER_END_OF_FILE
;
273 if (status
== HEADER_FAILURE
)
275 ERROR ((0, 0, _("Deleting non-header from archive")));
276 set_next_block_after (current_header
);
280 /* Found another header. */
282 if (name
= name_scan (current_stat_info
.file_name
), name
)
286 set_next_block_after (current_header
);
287 blocks_to_skip
= (current_stat_info
.stat
.st_size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
289 while (record_end
- current_block
<= blocks_to_skip
)
291 blocks_to_skip
-= (record_end
- current_block
);
294 current_block
+= blocks_to_skip
;
301 if (extended_header
.size
)
303 write_recent_bytes (extended_header
.buffer
,
304 extended_header
.size
);
308 write_recent_blocks (recent_long_name
, recent_long_name_blocks
);
309 write_recent_blocks (recent_long_link
, recent_long_link_blocks
);
311 new_record
[new_blocks
] = *current_header
;
314 = (current_stat_info
.stat
.st_size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
315 set_next_block_after (current_header
);
316 if (new_blocks
== blocking_factor
)
321 kept_blocks_in_record
= record_end
- current_block
;
322 if (kept_blocks_in_record
> blocks_to_keep
)
323 kept_blocks_in_record
= blocks_to_keep
;
325 while (blocks_to_keep
)
329 if (current_block
== record_end
)
332 current_block
= record_start
;
333 kept_blocks_in_record
= blocking_factor
;
334 if (kept_blocks_in_record
> blocks_to_keep
)
335 kept_blocks_in_record
= blocks_to_keep
;
337 count
= kept_blocks_in_record
;
338 if (blocking_factor
- new_blocks
< count
)
339 count
= blocking_factor
- new_blocks
;
344 memcpy (new_record
+ new_blocks
, current_block
, count
* BLOCKSIZE
);
346 current_block
+= count
;
347 blocks_to_keep
-= count
;
348 kept_blocks_in_record
-= count
;
350 if (new_blocks
== blocking_factor
)
356 if (logical_status
== HEADER_END_OF_FILE
)
358 /* Write the end of tape. FIXME: we can't use write_eot here,
359 as it gets confused when the input is at end of file. */
361 int total_zero_blocks
= 0;
365 int zero_blocks
= blocking_factor
- new_blocks
;
366 memset (new_record
+ new_blocks
, 0, BLOCKSIZE
* zero_blocks
);
367 total_zero_blocks
+= zero_blocks
;
368 write_record (total_zero_blocks
< 2);
370 while (total_zero_blocks
< 2);
375 if (! acting_as_filter
&& ! _isrmt (archive
))
378 int status
= write (archive
, "", 0);
380 off_t pos
= lseek (archive
, (off_t
) 0, SEEK_CUR
);
381 int status
= pos
< 0 ? -1 : ftruncate (archive
, pos
);
384 truncate_warn (archive_name_array
[0]);