delete.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* Delete entries from a tar archive.
  2. Copyright 1988-2021 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 <system-ioctl.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. /* The number of records skipped at the start of the archive, when
  30. passing over members that are not deleted. */
  31. off_t records_skipped;
  32. /* Move archive descriptor by COUNT records worth. If COUNT is
  33. positive we move forward, else we move negative. If it's a tape,
  34. MTIOCTOP had better work. If it's something else, we try to seek
  35. on it. If we can't seek, we lose! */
  36. static void
  37. move_archive (off_t count)
  38. {
  39. if (count == 0)
  40. return;
  41. #ifdef MTIOCTOP
  42. {
  43. struct mtop operation;
  44. if (count < 0
  45. ? (operation.mt_op = MTBSR,
  46. operation.mt_count = -count,
  47. operation.mt_count == -count)
  48. : (operation.mt_op = MTFSR,
  49. operation.mt_count = count,
  50. operation.mt_count == count))
  51. {
  52. if (0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
  53. return;
  54. if (errno == EIO
  55. && 0 <= rmtioctl (archive, MTIOCTOP, (char *) &operation))
  56. return;
  57. }
  58. }
  59. #endif /* MTIOCTOP */
  60. {
  61. off_t position0 = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  62. off_t increment = record_size * (off_t) count;
  63. off_t position = position0 + increment;
  64. if (increment / count != record_size
  65. || (position < position0) != (increment < 0)
  66. || (position = position < 0 ? 0 : position,
  67. rmtlseek (archive, position, SEEK_SET) != position))
  68. seek_error_details (archive_name_array[0], position);
  69. return;
  70. }
  71. }
  72. /* Write out the record which has been filled. If MOVE_BACK_FLAG,
  73. backspace to where we started. */
  74. static void
  75. write_record (int move_back_flag)
  76. {
  77. union block *save_record = record_start;
  78. record_start = new_record;
  79. if (acting_as_filter)
  80. {
  81. archive = STDOUT_FILENO;
  82. flush_write ();
  83. archive = STDIN_FILENO;
  84. }
  85. else
  86. {
  87. move_archive ((records_written + records_skipped) - records_read);
  88. flush_write ();
  89. }
  90. record_start = save_record;
  91. if (move_back_flag)
  92. {
  93. /* Move the tape head back to where we were. */
  94. if (! acting_as_filter)
  95. move_archive (records_read - (records_written + records_skipped));
  96. }
  97. new_blocks = 0;
  98. }
  99. static void
  100. write_recent_blocks (union block *h, size_t blocks)
  101. {
  102. size_t i;
  103. for (i = 0; i < blocks; i++)
  104. {
  105. new_record[new_blocks++] = h[i];
  106. if (new_blocks == blocking_factor)
  107. write_record (1);
  108. }
  109. }
  110. static void
  111. write_recent_bytes (char *data, size_t bytes)
  112. {
  113. size_t blocks = bytes / BLOCKSIZE;
  114. size_t rest = bytes - blocks * BLOCKSIZE;
  115. write_recent_blocks ((union block *)data, blocks);
  116. memcpy (new_record[new_blocks].buffer, data + blocks * BLOCKSIZE, rest);
  117. if (rest < BLOCKSIZE)
  118. memset (new_record[new_blocks].buffer + rest, 0, BLOCKSIZE - rest);
  119. new_blocks++;
  120. if (new_blocks == blocking_factor)
  121. write_record (1);
  122. }
  123. static inline void
  124. flush_file (void)
  125. {
  126. off_t blocks_to_skip;
  127. set_next_block_after (current_header);
  128. blocks_to_skip = (current_stat_info.stat.st_size
  129. + BLOCKSIZE - 1) / BLOCKSIZE;
  130. while (record_end - current_block <= blocks_to_skip)
  131. {
  132. blocks_to_skip -= (record_end - current_block);
  133. flush_archive ();
  134. }
  135. current_block += blocks_to_skip;
  136. }
  137. void
  138. delete_archive_members (void)
  139. {
  140. enum read_header logical_status = HEADER_STILL_UNREAD;
  141. enum read_header previous_status = HEADER_STILL_UNREAD;
  142. /* FIXME: Should clean the routine before cleaning these variables :-( */
  143. struct name *name;
  144. off_t blocks_to_keep = 0;
  145. int kept_blocks_in_record;
  146. name_gather ();
  147. open_archive (ACCESS_UPDATE);
  148. acting_as_filter = strcmp (archive_name_array[0], "-") == 0;
  149. /* Skip to the first member that matches the name list. */
  150. do
  151. {
  152. enum read_header status = read_header (&current_header,
  153. &current_stat_info,
  154. read_header_x_raw);
  155. switch (status)
  156. {
  157. case HEADER_STILL_UNREAD:
  158. abort ();
  159. case HEADER_SUCCESS:
  160. if ((name = name_scan (current_stat_info.file_name)) == NULL)
  161. {
  162. skip_member ();
  163. break;
  164. }
  165. name->found_count++;
  166. if (!ISFOUND (name))
  167. {
  168. skip_member ();
  169. break;
  170. }
  171. FALLTHROUGH;
  172. case HEADER_SUCCESS_EXTENDED:
  173. logical_status = status;
  174. break;
  175. case HEADER_ZERO_BLOCK:
  176. if (ignore_zeros_option)
  177. {
  178. set_next_block_after (current_header);
  179. break;
  180. }
  181. FALLTHROUGH;
  182. case HEADER_END_OF_FILE:
  183. logical_status = HEADER_END_OF_FILE;
  184. break;
  185. case HEADER_FAILURE:
  186. set_next_block_after (current_header);
  187. switch (previous_status)
  188. {
  189. case HEADER_STILL_UNREAD:
  190. WARN ((0, 0, _("This does not look like a tar archive")));
  191. FALLTHROUGH;
  192. case HEADER_SUCCESS:
  193. case HEADER_SUCCESS_EXTENDED:
  194. case HEADER_ZERO_BLOCK:
  195. ERROR ((0, 0, _("Skipping to next header")));
  196. FALLTHROUGH;
  197. case HEADER_FAILURE:
  198. break;
  199. case HEADER_END_OF_FILE:
  200. abort ();
  201. }
  202. break;
  203. }
  204. previous_status = status;
  205. }
  206. while (logical_status == HEADER_STILL_UNREAD);
  207. records_skipped = records_read - 1;
  208. new_record = xmalloc (record_size);
  209. if (logical_status == HEADER_SUCCESS
  210. || logical_status == HEADER_SUCCESS_EXTENDED)
  211. {
  212. write_archive_to_stdout = false;
  213. /* Save away blocks before this one in this record. */
  214. new_blocks = current_block - record_start;
  215. if (new_blocks)
  216. memcpy (new_record, record_start, new_blocks * BLOCKSIZE);
  217. if (logical_status == HEADER_SUCCESS)
  218. {
  219. logical_status = HEADER_STILL_UNREAD;
  220. flush_file ();
  221. }
  222. /* Skip matching members and move the rest up the archive. */
  223. while (logical_status != HEADER_END_OF_FILE)
  224. {
  225. enum read_header status;
  226. /* Fill in a record. */
  227. if (current_block == record_end)
  228. flush_archive ();
  229. status = read_header (&current_header, &current_stat_info,
  230. read_header_auto);
  231. switch (status)
  232. {
  233. case HEADER_STILL_UNREAD:
  234. case HEADER_SUCCESS_EXTENDED:
  235. abort ();
  236. case HEADER_SUCCESS:
  237. /* Found another header. */
  238. xheader_decode (&current_stat_info);
  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. break;
  246. }
  247. }
  248. /* Copy header. */
  249. if (current_stat_info.xhdr.size)
  250. {
  251. write_recent_bytes (current_stat_info.xhdr.buffer,
  252. current_stat_info.xhdr.size);
  253. }
  254. else
  255. {
  256. write_recent_blocks (recent_long_name,
  257. recent_long_name_blocks);
  258. write_recent_blocks (recent_long_link,
  259. recent_long_link_blocks);
  260. }
  261. new_record[new_blocks] = *current_header;
  262. new_blocks++;
  263. blocks_to_keep
  264. = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE;
  265. set_next_block_after (current_header);
  266. if (new_blocks == blocking_factor)
  267. write_record (1);
  268. /* Copy data. */
  269. kept_blocks_in_record = record_end - current_block;
  270. if (kept_blocks_in_record > blocks_to_keep)
  271. kept_blocks_in_record = blocks_to_keep;
  272. while (blocks_to_keep)
  273. {
  274. int count;
  275. if (current_block == record_end)
  276. {
  277. flush_read ();
  278. current_block = record_start;
  279. kept_blocks_in_record = blocking_factor;
  280. if (kept_blocks_in_record > blocks_to_keep)
  281. kept_blocks_in_record = blocks_to_keep;
  282. }
  283. count = kept_blocks_in_record;
  284. if (blocking_factor - new_blocks < count)
  285. count = blocking_factor - new_blocks;
  286. if (! count)
  287. abort ();
  288. memcpy (new_record + new_blocks, current_block,
  289. count * BLOCKSIZE);
  290. new_blocks += count;
  291. current_block += count;
  292. blocks_to_keep -= count;
  293. kept_blocks_in_record -= count;
  294. if (new_blocks == blocking_factor)
  295. write_record (1);
  296. }
  297. break;
  298. case HEADER_ZERO_BLOCK:
  299. if (ignore_zeros_option)
  300. set_next_block_after (current_header);
  301. else
  302. logical_status = HEADER_END_OF_FILE;
  303. break;
  304. case HEADER_END_OF_FILE:
  305. logical_status = HEADER_END_OF_FILE;
  306. break;
  307. case HEADER_FAILURE:
  308. ERROR ((0, 0, _("Deleting non-header from archive")));
  309. set_next_block_after (current_header);
  310. break;
  311. default:
  312. abort ();
  313. }
  314. tar_stat_destroy (&current_stat_info);
  315. }
  316. if (logical_status == HEADER_END_OF_FILE)
  317. {
  318. /* Write the end of tape. FIXME: we can't use write_eot here,
  319. as it gets confused when the input is at end of file. */
  320. int total_zero_blocks = 0;
  321. do
  322. {
  323. int zero_blocks = blocking_factor - new_blocks;
  324. memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks);
  325. total_zero_blocks += zero_blocks;
  326. write_record (total_zero_blocks < 2);
  327. }
  328. while (total_zero_blocks < 2);
  329. }
  330. if (! acting_as_filter && ! _isrmt (archive))
  331. {
  332. if (sys_truncate (archive))
  333. truncate_warn (archive_name_array[0]);
  334. }
  335. }
  336. free (new_record);
  337. close_archive ();
  338. names_notfound ();
  339. }