delete.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* Delete entries from a tar archive.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997, 2000, 2001, 2003 Free
  3. 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. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #include "common.h"
  17. #include "rmt.h"
  18. static union block *new_record;
  19. static int new_blocks;
  20. static bool acting_as_filter;
  21. /* FIXME: This module should not directly handle the following
  22. variables, instead, the interface should be cleaned up. */
  23. extern union block *record_start;
  24. extern union block *record_end;
  25. extern union block *current_block;
  26. extern union block *recent_long_name;
  27. extern union block *recent_long_link;
  28. extern off_t records_read;
  29. extern off_t records_written;
  30. /* The number of records skipped at the start of the archive, when
  31. passing over members that are not deleted. */
  32. static off_t records_skipped;
  33. /* Move archive descriptor by COUNT records worth. If COUNT is
  34. positive we move forward, else we move negative. If it's a tape,
  35. MTIOCTOP had better work. If it's something else, we try to seek
  36. on it. If we can't seek, we lose! */
  37. static void
  38. move_archive (off_t count)
  39. {
  40. if (count == 0)
  41. return;
  42. #ifdef MTIOCTOP
  43. {
  44. struct mtop operation;
  45. if (count < 0
  46. ? (operation.mt_op = MTBSR,
  47. operation.mt_count = -count,
  48. operation.mt_count == -count)
  49. : (operation.mt_op = MTFSR,
  50. operation.mt_count = count,
  51. operation.mt_count == count))
  52. {
  53. if (0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
  54. return;
  55. if (errno == EIO
  56. && 0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
  57. return;
  58. }
  59. }
  60. #endif /* MTIOCTOP */
  61. {
  62. off_t position0 = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  63. off_t increment = record_size * (off_t) count;
  64. off_t position = position0 + increment;
  65. if (increment / count != record_size
  66. || (position < position0) != (increment < 0)
  67. || (position = position < 0 ? 0 : position,
  68. rmtlseek (archive, position, SEEK_SET) != position))
  69. seek_error_details (archive_name_array[0], position);
  70. return;
  71. }
  72. }
  73. /* Write out the record which has been filled. If MOVE_BACK_FLAG,
  74. backspace to where we started. */
  75. static void
  76. write_record (int move_back_flag)
  77. {
  78. union block *save_record = record_start;
  79. record_start = new_record;
  80. if (acting_as_filter)
  81. {
  82. archive = STDOUT_FILENO;
  83. flush_write ();
  84. archive = STDIN_FILENO;
  85. }
  86. else
  87. {
  88. move_archive ((records_written + records_skipped) - records_read);
  89. flush_write ();
  90. }
  91. record_start = save_record;
  92. if (move_back_flag)
  93. {
  94. /* Move the tape head back to where we were. */
  95. if (! acting_as_filter)
  96. move_archive (records_read - (records_written + records_skipped));
  97. }
  98. new_blocks = 0;
  99. }
  100. static void
  101. write_recent_blocks (union block *h, size_t blocks)
  102. {
  103. size_t i;
  104. for (i = 0; i < blocks; i++)
  105. {
  106. new_record[new_blocks++] = h[i];
  107. if (new_blocks == blocking_factor)
  108. write_record (1);
  109. }
  110. }
  111. static void
  112. write_recent_bytes (char *data, size_t bytes)
  113. {
  114. size_t blocks = bytes / BLOCKSIZE;
  115. size_t rest = bytes - blocks * BLOCKSIZE;
  116. write_recent_blocks ((union block *)data, blocks);
  117. memcpy (new_record[new_blocks].buffer, data + blocks * BLOCKSIZE, rest);
  118. if (rest < BLOCKSIZE)
  119. memset (new_record[new_blocks].buffer + rest, 0, BLOCKSIZE - rest);
  120. new_blocks++;
  121. if (new_blocks == blocking_factor)
  122. write_record (1);
  123. }
  124. void
  125. delete_archive_members (void)
  126. {
  127. enum read_header logical_status = HEADER_STILL_UNREAD;
  128. enum read_header previous_status = HEADER_STILL_UNREAD;
  129. /* FIXME: Should clean the routine before cleaning these variables :-( */
  130. struct name *name;
  131. off_t blocks_to_skip = 0;
  132. off_t blocks_to_keep = 0;
  133. int kept_blocks_in_record;
  134. name_gather ();
  135. open_archive (ACCESS_UPDATE);
  136. acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
  137. do
  138. {
  139. enum read_header status = read_header (true);
  140. switch (status)
  141. {
  142. case HEADER_STILL_UNREAD:
  143. abort ();
  144. case HEADER_SUCCESS:
  145. if ((name = name_scan (current_stat_info.file_name)) == NULL)
  146. {
  147. skip_member ();
  148. break;
  149. }
  150. name->found_count++;
  151. if (!ISFOUND(name))
  152. {
  153. skip_member ();
  154. break;
  155. }
  156. /* Fall through. */
  157. case HEADER_SUCCESS_EXTENDED:
  158. logical_status = status;
  159. break;
  160. case HEADER_ZERO_BLOCK:
  161. if (ignore_zeros_option)
  162. {
  163. set_next_block_after (current_header);
  164. break;
  165. }
  166. /* Fall through. */
  167. case HEADER_END_OF_FILE:
  168. logical_status = HEADER_END_OF_FILE;
  169. break;
  170. case HEADER_FAILURE:
  171. set_next_block_after (current_header);
  172. switch (previous_status)
  173. {
  174. case HEADER_STILL_UNREAD:
  175. WARN ((0, 0, _("This does not look like a tar archive")));
  176. /* Fall through. */
  177. case HEADER_SUCCESS:
  178. case HEADER_SUCCESS_EXTENDED:
  179. case HEADER_ZERO_BLOCK:
  180. ERROR ((0, 0, _("Skipping to next header")));
  181. /* Fall through. */
  182. case HEADER_FAILURE:
  183. break;
  184. case HEADER_END_OF_FILE:
  185. abort ();
  186. }
  187. break;
  188. }
  189. previous_status = status;
  190. }
  191. while (logical_status == HEADER_STILL_UNREAD);
  192. records_skipped = records_read - 1;
  193. new_record = xmalloc (record_size);
  194. if (logical_status == HEADER_SUCCESS
  195. || logical_status == HEADER_SUCCESS_EXTENDED)
  196. {
  197. write_archive_to_stdout = 0;
  198. /* Save away blocks before this one in this record. */
  199. new_blocks = current_block - record_start;
  200. if (new_blocks)
  201. memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
  202. if (logical_status == HEADER_SUCCESS)
  203. {
  204. /* FIXME: Pheew! This is crufty code! */
  205. logical_status = HEADER_STILL_UNREAD;
  206. goto flush_file;
  207. }
  208. /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
  209. "delete.c", line 223: warning: loop not entered at top
  210. Reported by Bruno Haible. */
  211. while (1)
  212. {
  213. enum read_header status;
  214. /* Fill in a record. */
  215. if (current_block == record_end)
  216. flush_archive ();
  217. status = read_header (false);
  218. if (extended_header.size)
  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)) != 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. }
  304. if (logical_status == HEADER_END_OF_FILE)
  305. {
  306. /* Write the end of tape. FIXME: we can't use write_eot here,
  307. as it gets confused when the input is at end of file. */
  308. int total_zero_blocks = 0;
  309. do
  310. {
  311. int zero_blocks = blocking_factor - new_blocks;
  312. memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
  313. total_zero_blocks += zero_blocks;
  314. write_record (total_zero_blocks < 2);
  315. }
  316. while (total_zero_blocks < 2);
  317. }
  318. free (new_record);
  319. if (! acting_as_filter && ! _isrmt (archive))
  320. {
  321. if (sys_truncate (archive))
  322. truncate_warn (archive_name_array[0]);
  323. }
  324. close_archive ();
  325. names_notfound ();
  326. }