delete.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Delete entries from a tar archive.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "system.h"
  15. #define STDIN 0
  16. #define STDOUT 1
  17. #include "common.h"
  18. #include "rmt.h"
  19. static union block *new_record = NULL;
  20. static union block *save_record = NULL;
  21. static int records_read = 0;
  22. static int new_blocks = 0;
  23. static int blocks_needed = 0;
  24. /* FIXME: This module should not directly handle the following three
  25. variables, instead, this should be done in buffer.c only. */
  26. extern union block *record_start;
  27. extern union block *record_end;
  28. extern union block *current_block;
  29. /*-------------------------------------------------------------------------.
  30. | Move archive descriptor by COUNT records worth. If COUNT is positive we |
  31. | move forward, else we move negative. If its a tape, MTIOCTOP had better |
  32. | work. If its something else, we try to seek on it. If we can't seek, |
  33. | we loose! |
  34. `-------------------------------------------------------------------------*/
  35. static void
  36. move_archive (int count)
  37. {
  38. #ifdef MTIOCTOP
  39. {
  40. struct mtop operation;
  41. int status;
  42. if (count > 0)
  43. {
  44. operation.mt_op = MTFSR;
  45. operation.mt_count = count;
  46. }
  47. else
  48. {
  49. operation.mt_op = MTBSR;
  50. operation.mt_count = -count;
  51. }
  52. if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
  53. status >= 0)
  54. return;
  55. if (errno == EIO)
  56. if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
  57. status >= 0)
  58. return;
  59. }
  60. #endif /* MTIOCTOP */
  61. {
  62. off_t position = rmtlseek (archive, 0L, 1);
  63. position += record_size * count;
  64. if (rmtlseek (archive, position, 0) != position)
  65. FATAL_ERROR ((0, 0, _("Could not re-position archive file")));
  66. return;
  67. }
  68. }
  69. /*----------------------------------------------------------------.
  70. | Write out the record which has been filled. If MOVE_BACK_FLAG, |
  71. | backspace to where we started. |
  72. `----------------------------------------------------------------*/
  73. static void
  74. write_record (int move_back_flag)
  75. {
  76. save_record = record_start;
  77. record_start = new_record;
  78. if (archive == STDIN)
  79. {
  80. archive = STDOUT;
  81. flush_write ();
  82. archive = STDIN;
  83. }
  84. else
  85. {
  86. move_archive (-(records_read + 1));
  87. flush_write ();
  88. }
  89. record_start = save_record;
  90. if (move_back_flag)
  91. {
  92. /* Move the tape head back to where we were. */
  93. if (archive != STDIN)
  94. move_archive (records_read);
  95. records_read--;
  96. }
  97. blocks_needed = blocking_factor;
  98. new_blocks = 0;
  99. }
  100. /*---.
  101. | ? |
  102. `---*/
  103. void
  104. delete_archive_members (void)
  105. {
  106. enum read_header logical_status = HEADER_STILL_UNREAD;
  107. enum read_header previous_status = HEADER_STILL_UNREAD;
  108. /* FIXME: Should clean the routine before cleaning these variables :-( */
  109. struct name *name;
  110. int blocks_to_skip = 0;
  111. int blocks_to_keep = 0;
  112. int kept_blocks_in_record;
  113. name_gather ();
  114. open_archive (ACCESS_UPDATE);
  115. while (logical_status == HEADER_STILL_UNREAD)
  116. {
  117. enum read_header status = read_header ();
  118. switch (status)
  119. {
  120. case HEADER_STILL_UNREAD:
  121. abort ();
  122. case HEADER_SUCCESS:
  123. if (name = name_scan (current_file_name), !name)
  124. {
  125. set_next_block_after (current_header);
  126. if (current_header->oldgnu_header.isextended)
  127. skip_extended_headers ();
  128. skip_file ((long) (current_stat.st_size));
  129. break;
  130. }
  131. name->found = 1;
  132. logical_status = HEADER_SUCCESS;
  133. break;
  134. case HEADER_ZERO_BLOCK:
  135. case HEADER_END_OF_FILE:
  136. logical_status = HEADER_END_OF_FILE;
  137. break;
  138. case HEADER_FAILURE:
  139. set_next_block_after (current_header);
  140. switch (previous_status)
  141. {
  142. case HEADER_STILL_UNREAD:
  143. WARN ((0, 0, _("This does not look like a tar archive")));
  144. /* Fall through. */
  145. case HEADER_SUCCESS:
  146. case HEADER_ZERO_BLOCK:
  147. ERROR ((0, 0, _("Skipping to next header")));
  148. /* Fall through. */
  149. case HEADER_FAILURE:
  150. break;
  151. case HEADER_END_OF_FILE:
  152. abort ();
  153. }
  154. break;
  155. }
  156. previous_status = status;
  157. }
  158. if (logical_status != HEADER_SUCCESS)
  159. {
  160. write_eot ();
  161. close_archive ();
  162. names_notfound ();
  163. return;
  164. }
  165. write_archive_to_stdout = 0;
  166. new_record = (union block *) xmalloc ((size_t) record_size);
  167. /* Save away blocks before this one in this record. */
  168. new_blocks = current_block - record_start;
  169. blocks_needed = blocking_factor - new_blocks;
  170. if (new_blocks)
  171. memcpy ((void *) new_record, (void *) record_start,
  172. (size_t) (new_blocks * BLOCKSIZE));
  173. #if 0
  174. /* FIXME: Old code, before the goto was inserted. To be redesigned. */
  175. set_next_block_after (current_header);
  176. if (current_header->oldgnu_header.isextended)
  177. skip_extended_headers ();
  178. skip_file ((long) (current_stat.st_size));
  179. #endif
  180. logical_status = HEADER_STILL_UNREAD;
  181. goto flush_file;
  182. /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
  183. "delete.c", line 223: warning: loop not entered at top
  184. Reported by Bruno Haible. */
  185. while (1)
  186. {
  187. enum read_header status;
  188. /* Fill in a record. */
  189. if (current_block == record_end)
  190. {
  191. flush_archive ();
  192. records_read++;
  193. }
  194. status = read_header ();
  195. if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
  196. {
  197. set_next_block_after (current_header);
  198. continue;
  199. }
  200. if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
  201. {
  202. logical_status = HEADER_END_OF_FILE;
  203. memset (new_record[new_blocks].buffer, 0,
  204. (size_t) (BLOCKSIZE * blocks_needed));
  205. new_blocks += blocks_needed;
  206. blocks_needed = 0;
  207. write_record (0);
  208. break;
  209. }
  210. if (status == HEADER_FAILURE)
  211. {
  212. ERROR ((0, 0, _("Deleting non-header from archive")));
  213. set_next_block_after (current_header);
  214. continue;
  215. }
  216. /* Found another header. */
  217. if (name = name_scan (current_file_name), name)
  218. {
  219. name->found = 1;
  220. flush_file:
  221. set_next_block_after (current_header);
  222. blocks_to_skip = (current_stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
  223. while (record_end - current_block <= blocks_to_skip)
  224. {
  225. blocks_to_skip -= (record_end - current_block);
  226. flush_archive ();
  227. records_read++;
  228. }
  229. current_block += blocks_to_skip;
  230. blocks_to_skip = 0;
  231. continue;
  232. }
  233. /* Copy header. */
  234. new_record[new_blocks] = *current_header;
  235. new_blocks++;
  236. blocks_needed--;
  237. blocks_to_keep
  238. = (current_stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
  239. set_next_block_after (current_header);
  240. if (blocks_needed == 0)
  241. write_record (1);
  242. /* Copy data. */
  243. kept_blocks_in_record = record_end - current_block;
  244. if (kept_blocks_in_record > blocks_to_keep)
  245. kept_blocks_in_record = blocks_to_keep;
  246. while (blocks_to_keep)
  247. {
  248. int count;
  249. if (current_block == record_end)
  250. {
  251. flush_read ();
  252. records_read++;
  253. current_block = record_start;
  254. kept_blocks_in_record = blocking_factor;
  255. if (kept_blocks_in_record > blocks_to_keep)
  256. kept_blocks_in_record = blocks_to_keep;
  257. }
  258. count = kept_blocks_in_record;
  259. if (count > blocks_needed)
  260. count = blocks_needed;
  261. memcpy ((void *) (new_record + new_blocks),
  262. (void *) current_block,
  263. (size_t) (count * BLOCKSIZE));
  264. new_blocks += count;
  265. blocks_needed -= count;
  266. current_block += count;
  267. blocks_to_keep -= count;
  268. kept_blocks_in_record -= count;
  269. if (blocks_needed == 0)
  270. write_record (1);
  271. }
  272. }
  273. write_eot ();
  274. close_archive ();
  275. names_notfound ();
  276. }