delete.c 9.9 KB

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