delete.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Delete entries from a tar archive.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997, 2000, 2001, 2003, 2004,
  3. 2005, 2006, 2010 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 3, 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. /* The number of records skipped at the start of the archive, when
  31. passing over members that are not deleted. */
  32. 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 (&current_header,
  140. &current_stat_info,
  141. read_header_x_raw);
  142. switch (status)
  143. {
  144. case HEADER_STILL_UNREAD:
  145. abort ();
  146. case HEADER_SUCCESS:
  147. if ((name = name_scan (current_stat_info.file_name)) == NULL)
  148. {
  149. skip_member ();
  150. break;
  151. }
  152. name->found_count++;
  153. if (!ISFOUND(name))
  154. {
  155. skip_member ();
  156. break;
  157. }
  158. /* Fall through. */
  159. case HEADER_SUCCESS_EXTENDED:
  160. logical_status = status;
  161. break;
  162. case HEADER_ZERO_BLOCK:
  163. if (ignore_zeros_option)
  164. {
  165. set_next_block_after (current_header);
  166. break;
  167. }
  168. /* Fall through. */
  169. case HEADER_END_OF_FILE:
  170. logical_status = HEADER_END_OF_FILE;
  171. break;
  172. case HEADER_FAILURE:
  173. set_next_block_after (current_header);
  174. switch (previous_status)
  175. {
  176. case HEADER_STILL_UNREAD:
  177. WARN ((0, 0, _("This does not look like a tar archive")));
  178. /* Fall through. */
  179. case HEADER_SUCCESS:
  180. case HEADER_SUCCESS_EXTENDED:
  181. case HEADER_ZERO_BLOCK:
  182. ERROR ((0, 0, _("Skipping to next header")));
  183. /* Fall through. */
  184. case HEADER_FAILURE:
  185. break;
  186. case HEADER_END_OF_FILE:
  187. abort ();
  188. }
  189. break;
  190. }
  191. previous_status = status;
  192. }
  193. while (logical_status == HEADER_STILL_UNREAD);
  194. records_skipped = records_read - 1;
  195. new_record = xmalloc (record_size);
  196. if (logical_status == HEADER_SUCCESS
  197. || logical_status == HEADER_SUCCESS_EXTENDED)
  198. {
  199. write_archive_to_stdout = false;
  200. /* Save away blocks before this one in this record. */
  201. new_blocks = current_block - record_start;
  202. if (new_blocks)
  203. memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
  204. if (logical_status == HEADER_SUCCESS)
  205. {
  206. /* FIXME: Pheew! This is crufty code! */
  207. logical_status = HEADER_STILL_UNREAD;
  208. goto flush_file;
  209. }
  210. /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says:
  211. "delete.c", line 223: warning: loop not entered at top
  212. Reported by Bruno Haible. */
  213. while (1)
  214. {
  215. enum read_header status;
  216. /* Fill in a record. */
  217. if (current_block == record_end)
  218. flush_archive ();
  219. status = read_header (&current_header, &current_stat_info,
  220. read_header_auto);
  221. xheader_decode (&current_stat_info);
  222. if (status == HEADER_ZERO_BLOCK && ignore_zeros_option)
  223. {
  224. set_next_block_after (current_header);
  225. continue;
  226. }
  227. if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK)
  228. {
  229. logical_status = HEADER_END_OF_FILE;
  230. break;
  231. }
  232. if (status == HEADER_FAILURE)
  233. {
  234. ERROR ((0, 0, _("Deleting non-header from archive")));
  235. set_next_block_after (current_header);
  236. continue;
  237. }
  238. /* Found another header. */
  239. if ((name = name_scan (current_stat_info.file_name)) != NULL)
  240. {
  241. name->found_count++;
  242. if (ISFOUND(name))
  243. {
  244. flush_file:
  245. set_next_block_after (current_header);
  246. blocks_to_skip = (current_stat_info.stat.st_size
  247. + BLOCKSIZE - 1) / BLOCKSIZE;
  248. while (record_end - current_block <= blocks_to_skip)
  249. {
  250. blocks_to_skip -= (record_end - current_block);
  251. flush_archive ();
  252. }
  253. current_block += blocks_to_skip;
  254. blocks_to_skip = 0;
  255. continue;
  256. }
  257. }
  258. /* Copy header. */
  259. if (current_stat_info.xhdr.size)
  260. {
  261. write_recent_bytes (current_stat_info.xhdr.buffer,
  262. current_stat_info.xhdr.size);
  263. }
  264. else
  265. {
  266. write_recent_blocks (recent_long_name, recent_long_name_blocks);
  267. write_recent_blocks (recent_long_link, recent_long_link_blocks);
  268. }
  269. new_record[new_blocks] = *current_header;
  270. new_blocks++;
  271. blocks_to_keep
  272. = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
  273. set_next_block_after (current_header);
  274. if (new_blocks == blocking_factor)
  275. write_record (1);
  276. /* Copy data. */
  277. kept_blocks_in_record = record_end - current_block;
  278. if (kept_blocks_in_record > blocks_to_keep)
  279. kept_blocks_in_record = blocks_to_keep;
  280. while (blocks_to_keep)
  281. {
  282. int count;
  283. if (current_block == record_end)
  284. {
  285. flush_read ();
  286. current_block = record_start;
  287. kept_blocks_in_record = blocking_factor;
  288. if (kept_blocks_in_record > blocks_to_keep)
  289. kept_blocks_in_record = blocks_to_keep;
  290. }
  291. count = kept_blocks_in_record;
  292. if (blocking_factor - new_blocks < count)
  293. count = blocking_factor - new_blocks;
  294. if (! count)
  295. abort ();
  296. memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE);
  297. new_blocks += count;
  298. current_block += count;
  299. blocks_to_keep -= count;
  300. kept_blocks_in_record -= count;
  301. if (new_blocks == blocking_factor)
  302. write_record (1);
  303. }
  304. }
  305. if (logical_status == HEADER_END_OF_FILE)
  306. {
  307. /* Write the end of tape. FIXME: we can't use write_eot here,
  308. as it gets confused when the input is at end of file. */
  309. int total_zero_blocks = 0;
  310. do
  311. {
  312. int zero_blocks = blocking_factor - new_blocks;
  313. memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
  314. total_zero_blocks += zero_blocks;
  315. write_record (total_zero_blocks < 2);
  316. }
  317. while (total_zero_blocks < 2);
  318. }
  319. if (! acting_as_filter && ! _isrmt (archive))
  320. {
  321. if (sys_truncate (archive))
  322. truncate_warn (archive_name_array[0]);
  323. }
  324. }
  325. free (new_record);
  326. close_archive ();
  327. names_notfound ();
  328. }