delete.c 9.7 KB

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