delete.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Delete entries from a tar archive.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997, 2000, 2001, 2003, 2004,
  3. 2005, 2006 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <system.h>
  16. #include <system-ioctl.h>
  17. #include "common.h"
  18. #include <rmt.h>
  19. static union block *new_record;
  20. static int new_blocks;
  21. static bool acting_as_filter;
  22. /* FIXME: This module should not directly handle the following
  23. variables, instead, the interface should be cleaned up. */
  24. extern union block *record_start;
  25. extern union block *record_end;
  26. extern union block *current_block;
  27. extern union block *recent_long_name;
  28. extern union block *recent_long_link;
  29. extern off_t records_read;
  30. extern off_t records_written;
  31. /* The number of records skipped at the start of the archive, when
  32. passing over members that are not deleted. */
  33. off_t records_skipped;
  34. /* Move archive descriptor by COUNT records worth. If COUNT is
  35. positive we move forward, else we move negative. If it's a tape,
  36. MTIOCTOP had better work. If it's something else, we try to seek
  37. on it. If we can't seek, we lose! */
  38. static void
  39. move_archive (off_t count)
  40. {
  41. if (count == 0)
  42. return;
  43. #ifdef MTIOCTOP
  44. {
  45. struct mtop operation;
  46. if (count < 0
  47. ? (operation.mt_op = MTBSR,
  48. operation.mt_count = -count,
  49. operation.mt_count == -count)
  50. : (operation.mt_op = MTFSR,
  51. operation.mt_count = count,
  52. operation.mt_count == count))
  53. {
  54. if (0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
  55. return;
  56. if (errno == EIO
  57. && 0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
  58. return;
  59. }
  60. }
  61. #endif /* MTIOCTOP */
  62. {
  63. off_t position0 = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  64. off_t increment = record_size * (off_t) count;
  65. off_t position = position0 + increment;
  66. if (increment / count != record_size
  67. || (position < position0) != (increment < 0)
  68. || (position = position < 0 ? 0 : position,
  69. rmtlseek (archive, position, SEEK_SET) != position))
  70. seek_error_details (archive_name_array[0], position);
  71. return;
  72. }
  73. }
  74. /* Write out the record which has been filled. If MOVE_BACK_FLAG,
  75. backspace to where we started. */
  76. static void
  77. write_record (int move_back_flag)
  78. {
  79. union block *save_record = record_start;
  80. record_start = new_record;
  81. if (acting_as_filter)
  82. {
  83. archive = STDOUT_FILENO;
  84. flush_write ();
  85. archive = STDIN_FILENO;
  86. }
  87. else
  88. {
  89. move_archive ((records_written + records_skipped) - records_read);
  90. flush_write ();
  91. }
  92. record_start = save_record;
  93. if (move_back_flag)
  94. {
  95. /* Move the tape head back to where we were. */
  96. if (! acting_as_filter)
  97. move_archive (records_read - (records_written + records_skipped));
  98. }
  99. new_blocks = 0;
  100. }
  101. static void
  102. write_recent_blocks (union block *h, size_t blocks)
  103. {
  104. size_t i;
  105. for (i = 0; i < blocks; i++)
  106. {
  107. new_record[new_blocks++] = h[i];
  108. if (new_blocks == blocking_factor)
  109. write_record (1);
  110. }
  111. }
  112. static void
  113. write_recent_bytes (char *data, size_t bytes)
  114. {
  115. size_t blocks = bytes / BLOCKSIZE;
  116. size_t rest = bytes - blocks * BLOCKSIZE;
  117. write_recent_blocks ((union block *)data, blocks);
  118. memcpy (new_record[new_blocks].buffer, data + blocks * BLOCKSIZE, rest);
  119. if (rest < BLOCKSIZE)
  120. memset (new_record[new_blocks].buffer + rest, 0, BLOCKSIZE - rest);
  121. new_blocks++;
  122. if (new_blocks == blocking_factor)
  123. write_record (1);
  124. }
  125. void
  126. delete_archive_members (void)
  127. {
  128. enum read_header logical_status = HEADER_STILL_UNREAD;
  129. enum read_header previous_status = HEADER_STILL_UNREAD;
  130. /* FIXME: Should clean the routine before cleaning these variables :-( */
  131. struct name *name;
  132. off_t blocks_to_skip = 0;
  133. off_t blocks_to_keep = 0;
  134. int kept_blocks_in_record;
  135. name_gather ();
  136. open_archive (ACCESS_UPDATE);
  137. acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
  138. do
  139. {
  140. enum read_header status = read_header (true);
  141. switch (status)
  142. {
  143. case HEADER_STILL_UNREAD:
  144. abort ();
  145. case HEADER_SUCCESS:
  146. if ((name = name_scan (current_stat_info.file_name, false)) == NULL)
  147. {
  148. skip_member ();
  149. break;
  150. }
  151. name->found_count++;
  152. if (!ISFOUND(name))
  153. {
  154. skip_member ();
  155. break;
  156. }
  157. /* Fall through. */
  158. case HEADER_SUCCESS_EXTENDED:
  159. logical_status = status;
  160. break;
  161. case HEADER_ZERO_BLOCK:
  162. if (ignore_zeros_option)
  163. {
  164. set_next_block_after (current_header);
  165. break;
  166. }
  167. /* Fall through. */
  168. case HEADER_END_OF_FILE:
  169. logical_status = HEADER_END_OF_FILE;
  170. break;
  171. case HEADER_FAILURE:
  172. set_next_block_after (current_header);
  173. switch (previous_status)
  174. {
  175. case HEADER_STILL_UNREAD:
  176. WARN ((0, 0, _("This does not look like a tar archive")));
  177. /* Fall through. */
  178. case HEADER_SUCCESS:
  179. case HEADER_SUCCESS_EXTENDED:
  180. case HEADER_ZERO_BLOCK:
  181. ERROR ((0, 0, _("Skipping to next header")));
  182. /* Fall through. */
  183. case HEADER_FAILURE:
  184. break;
  185. case HEADER_END_OF_FILE:
  186. abort ();
  187. }
  188. break;
  189. }
  190. previous_status = status;
  191. }
  192. while (logical_status == HEADER_STILL_UNREAD);
  193. records_skipped = records_read - 1;
  194. new_record = xmalloc (record_size);
  195. if (logical_status == HEADER_SUCCESS
  196. || logical_status == HEADER_SUCCESS_EXTENDED)
  197. {
  198. write_archive_to_stdout = false;
  199. /* Save away blocks before this one in this record. */
  200. new_blocks = current_block - record_start;
  201. if (new_blocks)
  202. memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
  203. if (logical_status == HEADER_SUCCESS)
  204. {
  205. /* FIXME: Pheew! This is crufty code! */
  206. logical_status = HEADER_STILL_UNREAD;
  207. goto flush_file;
  208. }
  209. /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
  210. "delete.c", line 223: warning: loop not entered at top
  211. Reported by Bruno Haible. */
  212. while (1)
  213. {
  214. enum read_header status;
  215. /* Fill in a record. */
  216. if (current_block == record_end)
  217. flush_archive ();
  218. status = read_header (false);
  219. xheader_decode (&current_stat_info);
  220. if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
  221. {
  222. set_next_block_after (current_header);
  223. continue;
  224. }
  225. if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
  226. {
  227. logical_status = HEADER_END_OF_FILE;
  228. break;
  229. }
  230. if (status == HEADER_FAILURE)
  231. {
  232. ERROR ((0, 0, _("Deleting non-header from archive")));
  233. set_next_block_after (current_header);
  234. continue;
  235. }
  236. /* Found another header. */
  237. if ((name = name_scan (current_stat_info.file_name, false)) != NULL)
  238. {
  239. name->found_count++;
  240. if (ISFOUND(name))
  241. {
  242. flush_file:
  243. set_next_block_after (current_header);
  244. blocks_to_skip = (current_stat_info.stat.st_size
  245. + BLOCKSIZE - 1) / BLOCKSIZE;
  246. while (record_end - current_block <= blocks_to_skip)
  247. {
  248. blocks_to_skip -= (record_end - current_block);
  249. flush_archive ();
  250. }
  251. current_block += blocks_to_skip;
  252. blocks_to_skip = 0;
  253. continue;
  254. }
  255. }
  256. /* Copy header. */
  257. if (extended_header.size)
  258. {
  259. write_recent_bytes (extended_header.buffer,
  260. extended_header.size);
  261. }
  262. else
  263. {
  264. write_recent_blocks (recent_long_name, recent_long_name_blocks);
  265. write_recent_blocks (recent_long_link, recent_long_link_blocks);
  266. }
  267. new_record[new_blocks] = *current_header;
  268. new_blocks++;
  269. blocks_to_keep
  270. = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
  271. set_next_block_after (current_header);
  272. if (new_blocks == blocking_factor)
  273. write_record (1);
  274. /* Copy data. */
  275. kept_blocks_in_record = record_end - current_block;
  276. if (kept_blocks_in_record > blocks_to_keep)
  277. kept_blocks_in_record = blocks_to_keep;
  278. while (blocks_to_keep)
  279. {
  280. int count;
  281. if (current_block == record_end)
  282. {
  283. flush_read ();
  284. current_block = record_start;
  285. kept_blocks_in_record = blocking_factor;
  286. if (kept_blocks_in_record > blocks_to_keep)
  287. kept_blocks_in_record = blocks_to_keep;
  288. }
  289. count = kept_blocks_in_record;
  290. if (blocking_factor - new_blocks < count)
  291. count = blocking_factor - new_blocks;
  292. if (! count)
  293. abort ();
  294. memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE);
  295. new_blocks += count;
  296. current_block += count;
  297. blocks_to_keep -= count;
  298. kept_blocks_in_record -= count;
  299. if (new_blocks == blocking_factor)
  300. write_record (1);
  301. }
  302. }
  303. if (logical_status == HEADER_END_OF_FILE)
  304. {
  305. /* Write the end of tape. FIXME: we can't use write_eot here,
  306. as it gets confused when the input is at end of file. */
  307. int total_zero_blocks = 0;
  308. do
  309. {
  310. int zero_blocks = blocking_factor - new_blocks;
  311. memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
  312. total_zero_blocks += zero_blocks;
  313. write_record (total_zero_blocks < 2);
  314. }
  315. while (total_zero_blocks < 2);
  316. }
  317. if (! acting_as_filter && ! _isrmt (archive))
  318. {
  319. if (sys_truncate (archive))
  320. truncate_warn (archive_name_array[0]);
  321. }
  322. }
  323. free (new_record);
  324. close_archive ();
  325. names_notfound ();
  326. }