read.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*-
  2. * Copyright 2006-2025 Tarsnap Backup Inc.
  3. * All rights reserved.
  4. *
  5. * Portions of the file below are covered by the following license:
  6. *
  7. * Copyright (c) 2003-2007 Tim Kientzle
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "bsdtar_platform.h"
  31. __FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.40 2008/08/21 06:41:14 kientzle Exp $");
  32. #ifdef HAVE_SYS_TYPES_H
  33. #include <sys/types.h>
  34. #endif
  35. #ifdef HAVE_SYS_PARAM_H
  36. #include <sys/param.h>
  37. #endif
  38. #ifdef HAVE_SYS_STAT_H
  39. #include <sys/stat.h>
  40. #endif
  41. #ifdef HAVE_ERRNO_H
  42. #include <errno.h>
  43. #endif
  44. #ifdef HAVE_GRP_H
  45. #include <grp.h>
  46. #endif
  47. #if HAVE_INTTYPES_H
  48. #include <inttypes.h>
  49. #endif
  50. #ifdef HAVE_LIMITS_H
  51. #include <limits.h>
  52. #endif
  53. #ifdef HAVE_PWD_H
  54. #include <pwd.h>
  55. #endif
  56. #include <signal.h>
  57. #include <stdio.h>
  58. #ifdef HAVE_STDLIB_H
  59. #include <stdlib.h>
  60. #endif
  61. #ifdef HAVE_STRING_H
  62. #include <string.h>
  63. #endif
  64. #ifdef HAVE_TIME_H
  65. #include <time.h>
  66. #endif
  67. #ifdef HAVE_UNISTD_H
  68. #include <unistd.h>
  69. #endif
  70. #include "bsdtar.h"
  71. #include "archive_multitape.h"
  72. #include "print_separator.h"
  73. static void read_archive(struct bsdtar *bsdtar, char mode);
  74. void
  75. tarsnap_mode_t(struct bsdtar *bsdtar)
  76. {
  77. read_archive(bsdtar, 't');
  78. unmatched_inclusions_warn(bsdtar, "Not found in archive");
  79. }
  80. void
  81. tarsnap_mode_x(struct bsdtar *bsdtar)
  82. {
  83. /* We want to catch SIGINFO and SIGUSR1. */
  84. siginfo_init(bsdtar);
  85. read_archive(bsdtar, 'x');
  86. unmatched_inclusions_warn(bsdtar, "Not found in archive");
  87. /* Restore old SIGINFO + SIGUSR1 handlers. */
  88. siginfo_done(bsdtar);
  89. }
  90. static void
  91. progress_func(void * cookie)
  92. {
  93. struct bsdtar * bsdtar = cookie;
  94. siginfo_printinfo(bsdtar, 0, 0);
  95. }
  96. /*
  97. * Should we skip over this file if given --resume-extract?
  98. * 0: skip it. 1: don't skip. -1: error.
  99. */
  100. static int
  101. check_skip_file(const char * filename, const struct stat * archive_st)
  102. {
  103. struct stat file_st;
  104. /* Get info about the file on disk. */
  105. if (stat(filename, &file_st) == -1) {
  106. if (errno == ENOENT)
  107. goto noskip;
  108. goto err0;
  109. }
  110. /*
  111. * Compare file size and mtime (seconds). Some filesystems don't have
  112. * sub-second timestamp precision, so comparing the full timespecs
  113. * would produce a lot of false negatives.
  114. */
  115. if (file_st.st_size != archive_st->st_size)
  116. goto noskip;
  117. #ifdef POSIXFAIL_STAT_ST_MTIM
  118. /* POSIX Issue 7. */
  119. if (file_st.st_mtim.tv_sec != archive_st->st_mtim.tv_sec)
  120. goto noskip;
  121. #else
  122. /* POSIX Issue 6 and below: use time_t st_mtime instead of st_mtim. */
  123. if (file_st.st_mtime != archive_st->st_mtime)
  124. goto noskip;
  125. #endif
  126. /* Skip file. */
  127. return (0);
  128. noskip:
  129. /* Don't skip. */
  130. return (1);
  131. err0:
  132. /* Failure! */
  133. return (-1);
  134. }
  135. /*
  136. * Handle 'x' and 't' modes.
  137. */
  138. static void
  139. read_archive(struct bsdtar *bsdtar, char mode)
  140. {
  141. FILE *out;
  142. struct archive *a;
  143. struct archive_entry *entry;
  144. const struct stat *st;
  145. int r;
  146. while (*bsdtar->argv) {
  147. include(bsdtar, *bsdtar->argv);
  148. bsdtar->argv++;
  149. }
  150. if (bsdtar->names_from_file != NULL)
  151. include_from_file(bsdtar, bsdtar->names_from_file);
  152. if ((a = archive_read_new()) == NULL) {
  153. bsdtar_warnc(bsdtar, ENOMEM, "Cannot allocate memory");
  154. goto err0;
  155. }
  156. archive_read_support_compression_none(a);
  157. archive_read_support_format_tar(a);
  158. if (archive_read_open_multitape(a, bsdtar->machinenum,
  159. bsdtar->tapenames[0]) == NULL) {
  160. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  161. goto err1;
  162. }
  163. do_chdir(bsdtar);
  164. if (mode == 'x') {
  165. /* Set an extract callback so that we can handle SIGINFO. */
  166. archive_read_extract_set_progress_callback(a, progress_func,
  167. bsdtar);
  168. }
  169. if (mode == 'x' && bsdtar->option_chroot) {
  170. #if HAVE_CHROOT
  171. if (chroot(".") != 0) {
  172. bsdtar_warnc(bsdtar, errno, "Can't chroot to \".\"");
  173. goto err1;
  174. }
  175. #else
  176. bsdtar_warnc(bsdtar, 0,
  177. "chroot isn't supported on this platform");
  178. goto err1;
  179. #endif
  180. }
  181. for (;;) {
  182. /* Support --fast-read option */
  183. if (bsdtar->option_fast_read &&
  184. unmatched_inclusions(bsdtar) == 0)
  185. break;
  186. r = archive_read_next_header(a, &entry);
  187. if (r == ARCHIVE_EOF)
  188. break;
  189. if (r < ARCHIVE_OK)
  190. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  191. if (r <= ARCHIVE_WARN)
  192. bsdtar->return_value = 1;
  193. if (r == ARCHIVE_RETRY) {
  194. /* Retryable error: try again */
  195. bsdtar_warnc(bsdtar, 0, "Retrying...");
  196. continue;
  197. }
  198. if (r == ARCHIVE_FATAL)
  199. break;
  200. if (bsdtar->option_numeric_owner) {
  201. archive_entry_set_uname(entry, NULL);
  202. archive_entry_set_gname(entry, NULL);
  203. }
  204. /*
  205. * Exclude entries that are too old.
  206. */
  207. st = archive_entry_stat(entry);
  208. if (bsdtar->newer_ctime_sec > 0) {
  209. if (st->st_ctime < bsdtar->newer_ctime_sec)
  210. continue; /* Too old, skip it. */
  211. if (st->st_ctime == bsdtar->newer_ctime_sec
  212. && ARCHIVE_STAT_CTIME_NANOS(st)
  213. <= bsdtar->newer_ctime_nsec)
  214. continue; /* Too old, skip it. */
  215. }
  216. if (bsdtar->newer_mtime_sec > 0) {
  217. if (st->st_mtime < bsdtar->newer_mtime_sec)
  218. continue; /* Too old, skip it. */
  219. if (st->st_mtime == bsdtar->newer_mtime_sec
  220. && ARCHIVE_STAT_MTIME_NANOS(st)
  221. <= bsdtar->newer_mtime_nsec)
  222. continue; /* Too old, skip it. */
  223. }
  224. /*
  225. * Note that pattern exclusions are checked before
  226. * pathname rewrites are handled. This gives more
  227. * control over exclusions, since rewrites always lose
  228. * information. (For example, consider a rewrite
  229. * s/foo[0-9]/foo/. If we check exclusions after the
  230. * rewrite, there would be no way to exclude foo1/bar
  231. * while allowing foo2/bar.)
  232. */
  233. if (excluded(bsdtar, archive_entry_pathname(entry)))
  234. continue; /* Excluded by a pattern test. */
  235. if (mode == 't') {
  236. /* Perversely, gtar uses -O to mean "send to stderr"
  237. * when used with -t. */
  238. out = bsdtar->option_stdout ? stderr : stdout;
  239. /*
  240. * TODO: Provide some reasonable way to
  241. * preview rewrites. gtar always displays
  242. * the unedited path in -t output, which means
  243. * you cannot easily preview rewrites.
  244. */
  245. if (bsdtar->verbose < 2)
  246. safe_fprintf(out, "%s",
  247. archive_entry_pathname(entry));
  248. else
  249. list_item_verbose(bsdtar, out, entry);
  250. fflush(out);
  251. r = archive_read_data_skip(a);
  252. if (r == ARCHIVE_WARN) {
  253. print_separator(out, "\n",
  254. bsdtar->option_null_output, 1);
  255. bsdtar_warnc(bsdtar, 0, "%s",
  256. archive_error_string(a));
  257. }
  258. if (r == ARCHIVE_RETRY) {
  259. print_separator(out, "\n",
  260. bsdtar->option_null_output, 1);
  261. bsdtar_warnc(bsdtar, 0, "%s",
  262. archive_error_string(a));
  263. }
  264. if (r == ARCHIVE_FATAL) {
  265. print_separator(out, "\n",
  266. bsdtar->option_null_output, 1);
  267. bsdtar_warnc(bsdtar, 0, "%s",
  268. archive_error_string(a));
  269. bsdtar->return_value = 1;
  270. break;
  271. }
  272. print_separator(out, "\n",
  273. bsdtar->option_null_output, 1);
  274. } else {
  275. /* Note: some rewrite failures prevent extraction. */
  276. if (edit_pathname(bsdtar, entry))
  277. continue; /* Excluded by a rewrite failure. */
  278. /* Don't extract if the file already matches it. */
  279. if (bsdtar->option_resume_extract) {
  280. r = check_skip_file(
  281. archive_entry_pathname(entry), st);
  282. if (r == -1) {
  283. bsdtar_warnc(bsdtar, errno, "stat(%s)",
  284. archive_entry_pathname(entry));
  285. goto err1;
  286. }
  287. /* Skip file. */
  288. if (r == 0)
  289. continue;
  290. }
  291. if (bsdtar->option_interactive &&
  292. !yes("extract '%s'", archive_entry_pathname(entry)))
  293. continue;
  294. if (bsdtar->verbose > 1) {
  295. /* GNU tar uses -tv format with -xvv */
  296. safe_fprintf(stderr, "x ");
  297. list_item_verbose(bsdtar, stderr, entry);
  298. fflush(stderr);
  299. } else if (bsdtar->verbose > 0) {
  300. /* Format follows SUSv2, including the
  301. * deferred '\n'. */
  302. safe_fprintf(stderr, "x %s",
  303. archive_entry_pathname(entry));
  304. fflush(stderr);
  305. }
  306. /*
  307. * Tell the SIGINFO-handler code what we're doing.
  308. * a->file_count is incremented by
  309. * archive_read_next_header(), which has already
  310. * been called for this file. However,
  311. * siginfo_setinfo() takes the number of files we
  312. * have already processed (in the past), so we
  313. * need to subtract 1 from the reported file count.
  314. */
  315. siginfo_setinfo(bsdtar, "extracting",
  316. archive_entry_pathname(entry), 0,
  317. archive_file_count(a) - 1,
  318. archive_position_uncompressed(a));
  319. siginfo_printinfo(bsdtar, 0, 0);
  320. if (bsdtar->option_stdout)
  321. r = archive_read_data_into_fd(a, 1);
  322. else
  323. r = archive_read_extract(a, entry,
  324. bsdtar->extract_flags);
  325. if (r != ARCHIVE_OK) {
  326. if (!bsdtar->verbose)
  327. safe_fprintf(stderr, "%s",
  328. archive_entry_pathname(entry));
  329. safe_fprintf(stderr, ": %s",
  330. archive_error_string(a));
  331. if (!bsdtar->verbose)
  332. print_separator(stderr, "\n",
  333. bsdtar->option_null_output, 1);
  334. bsdtar->return_value = 1;
  335. }
  336. if (bsdtar->verbose)
  337. print_separator(stderr, "\n",
  338. bsdtar->option_null_output, 1);
  339. if (r == ARCHIVE_FATAL)
  340. break;
  341. }
  342. }
  343. /* We're not processing any more files. */
  344. if (mode == 'x') {
  345. /* siginfo was not initialized in 't' mode. */
  346. siginfo_setinfo(bsdtar, NULL, NULL, 0, archive_file_count(a),
  347. archive_position_uncompressed(a));
  348. }
  349. r = archive_read_close(a);
  350. if (r != ARCHIVE_OK)
  351. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  352. if (r <= ARCHIVE_WARN)
  353. bsdtar->return_value = 1;
  354. if (bsdtar->verbose > 2) {
  355. fprintf(stdout, "Archive Format: %s, Compression: %s",
  356. archive_format_name(a), archive_compression_name(a));
  357. print_separator(stdout, "\n", bsdtar->option_null_output, 1);
  358. }
  359. /* Always print a final message for --progress-bytes. */
  360. if ((mode == 'x') && (bsdtar->option_progress_bytes != 0))
  361. raise(SIGUSR1);
  362. /* Print a final update (if desired). */
  363. if (mode == 'x') {
  364. /* siginfo was not initialized in 't' mode. */
  365. siginfo_printinfo(bsdtar, 0, 1);
  366. }
  367. archive_read_finish(a);
  368. /* Success! */
  369. return;
  370. err1:
  371. archive_read_finish(a);
  372. err0:
  373. /* Failure! */
  374. bsdtar->return_value = 1;
  375. return;
  376. }