read.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*-
  2. * Copyright 2006-2008 Colin Percival
  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. static void read_archive(struct bsdtar *bsdtar, char mode);
  73. void
  74. tarsnap_mode_t(struct bsdtar *bsdtar)
  75. {
  76. read_archive(bsdtar, 't');
  77. unmatched_inclusions_warn(bsdtar, "Not found in archive");
  78. }
  79. void
  80. tarsnap_mode_x(struct bsdtar *bsdtar)
  81. {
  82. /* We want to catch SIGINFO and SIGUSR1. */
  83. siginfo_init(bsdtar);
  84. read_archive(bsdtar, 'x');
  85. unmatched_inclusions_warn(bsdtar, "Not found in archive");
  86. /* Restore old SIGINFO + SIGUSR1 handlers. */
  87. siginfo_done(bsdtar);
  88. }
  89. static void
  90. progress_func(void * cookie)
  91. {
  92. struct bsdtar * bsdtar = cookie;
  93. siginfo_printinfo(bsdtar, 0, 0);
  94. }
  95. /*
  96. * Should we skip over this file if given --resume-extract?
  97. * 0: skip it. 1: don't skip. -1: error.
  98. */
  99. static int
  100. check_skip_file(const char * filename, const struct stat * archive_st)
  101. {
  102. struct stat file_st;
  103. /* Get info about the file on disk. */
  104. if (stat(filename, &file_st) == -1) {
  105. if (errno == ENOENT)
  106. goto noskip;
  107. goto err0;
  108. }
  109. /*
  110. * Compare file size and mtime (seconds). Some filesystems don't have
  111. * sub-second timestamp precision, so comparing the full timespecs
  112. * would produce a lot of false negatives.
  113. */
  114. if (file_st.st_size != archive_st->st_size)
  115. goto noskip;
  116. #ifdef POSIXFAIL_STAT_ST_MTIM
  117. /* POSIX Issue 7. */
  118. if (file_st.st_mtim.tv_sec != archive_st->st_mtim.tv_sec)
  119. goto noskip;
  120. #else
  121. /* POSIX Issue 6 and below: use time_t st_mtime instead of st_mtim. */
  122. if (file_st.st_mtime != archive_st->st_mtime)
  123. goto noskip;
  124. #endif
  125. /* Skip file. */
  126. return (0);
  127. noskip:
  128. /* Don't skip. */
  129. return (1);
  130. err0:
  131. /* Failure! */
  132. return (-1);
  133. }
  134. /*
  135. * Handle 'x' and 't' modes.
  136. */
  137. static void
  138. read_archive(struct bsdtar *bsdtar, char mode)
  139. {
  140. FILE *out;
  141. struct archive *a;
  142. struct archive_entry *entry;
  143. const struct stat *st;
  144. int r;
  145. while (*bsdtar->argv) {
  146. include(bsdtar, *bsdtar->argv);
  147. bsdtar->argv++;
  148. }
  149. if (bsdtar->names_from_file != NULL)
  150. include_from_file(bsdtar, bsdtar->names_from_file);
  151. if ((a = archive_read_new()) == NULL) {
  152. bsdtar_warnc(bsdtar, ENOMEM, "Cannot allocate memory");
  153. goto err0;
  154. }
  155. archive_read_support_compression_none(a);
  156. archive_read_support_format_tar(a);
  157. if (archive_read_open_multitape(a, bsdtar->machinenum,
  158. bsdtar->tapenames[0]) == NULL) {
  159. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  160. goto err1;
  161. }
  162. do_chdir(bsdtar);
  163. if (mode == 'x') {
  164. /* Set an extract callback so that we can handle SIGINFO. */
  165. archive_read_extract_set_progress_callback(a, progress_func,
  166. bsdtar);
  167. }
  168. if (mode == 'x' && bsdtar->option_chroot) {
  169. #if HAVE_CHROOT
  170. if (chroot(".") != 0) {
  171. bsdtar_warnc(bsdtar, errno, "Can't chroot to \".\"");
  172. goto err1;
  173. }
  174. #else
  175. bsdtar_warnc(bsdtar, 0,
  176. "chroot isn't supported on this platform");
  177. goto err1;
  178. #endif
  179. }
  180. for (;;) {
  181. /* Support --fast-read option */
  182. if (bsdtar->option_fast_read &&
  183. unmatched_inclusions(bsdtar) == 0)
  184. break;
  185. r = archive_read_next_header(a, &entry);
  186. if (r == ARCHIVE_EOF)
  187. break;
  188. if (r < ARCHIVE_OK)
  189. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  190. if (r <= ARCHIVE_WARN)
  191. bsdtar->return_value = 1;
  192. if (r == ARCHIVE_RETRY) {
  193. /* Retryable error: try again */
  194. bsdtar_warnc(bsdtar, 0, "Retrying...");
  195. continue;
  196. }
  197. if (r == ARCHIVE_FATAL)
  198. break;
  199. if (bsdtar->option_numeric_owner) {
  200. archive_entry_set_uname(entry, NULL);
  201. archive_entry_set_gname(entry, NULL);
  202. }
  203. /*
  204. * Exclude entries that are too old.
  205. */
  206. st = archive_entry_stat(entry);
  207. if (bsdtar->newer_ctime_sec > 0) {
  208. if (st->st_ctime < bsdtar->newer_ctime_sec)
  209. continue; /* Too old, skip it. */
  210. if (st->st_ctime == bsdtar->newer_ctime_sec
  211. && ARCHIVE_STAT_CTIME_NANOS(st)
  212. <= bsdtar->newer_ctime_nsec)
  213. continue; /* Too old, skip it. */
  214. }
  215. if (bsdtar->newer_mtime_sec > 0) {
  216. if (st->st_mtime < bsdtar->newer_mtime_sec)
  217. continue; /* Too old, skip it. */
  218. if (st->st_mtime == bsdtar->newer_mtime_sec
  219. && ARCHIVE_STAT_MTIME_NANOS(st)
  220. <= bsdtar->newer_mtime_nsec)
  221. continue; /* Too old, skip it. */
  222. }
  223. /*
  224. * Note that pattern exclusions are checked before
  225. * pathname rewrites are handled. This gives more
  226. * control over exclusions, since rewrites always lose
  227. * information. (For example, consider a rewrite
  228. * s/foo[0-9]/foo/. If we check exclusions after the
  229. * rewrite, there would be no way to exclude foo1/bar
  230. * while allowing foo2/bar.)
  231. */
  232. if (excluded(bsdtar, archive_entry_pathname(entry)))
  233. continue; /* Excluded by a pattern test. */
  234. if (mode == 't') {
  235. /* Perversely, gtar uses -O to mean "send to stderr"
  236. * when used with -t. */
  237. out = bsdtar->option_stdout ? stderr : stdout;
  238. /*
  239. * TODO: Provide some reasonable way to
  240. * preview rewrites. gtar always displays
  241. * the unedited path in -t output, which means
  242. * you cannot easily preview rewrites.
  243. */
  244. if (bsdtar->verbose < 2)
  245. safe_fprintf(out, "%s",
  246. archive_entry_pathname(entry));
  247. else
  248. list_item_verbose(bsdtar, out, entry);
  249. fflush(out);
  250. r = archive_read_data_skip(a);
  251. if (r == ARCHIVE_WARN) {
  252. fprintf(out, "\n");
  253. bsdtar_warnc(bsdtar, 0, "%s",
  254. archive_error_string(a));
  255. }
  256. if (r == ARCHIVE_RETRY) {
  257. fprintf(out, "\n");
  258. bsdtar_warnc(bsdtar, 0, "%s",
  259. archive_error_string(a));
  260. }
  261. if (r == ARCHIVE_FATAL) {
  262. fprintf(out, "\n");
  263. bsdtar_warnc(bsdtar, 0, "%s",
  264. archive_error_string(a));
  265. bsdtar->return_value = 1;
  266. break;
  267. }
  268. fprintf(out, "\n");
  269. } else {
  270. /* Note: some rewrite failures prevent extraction. */
  271. if (edit_pathname(bsdtar, entry))
  272. continue; /* Excluded by a rewrite failure. */
  273. /* Don't extract if the file already matches it. */
  274. if (bsdtar->option_resume_extract) {
  275. r = check_skip_file(
  276. archive_entry_pathname(entry), st);
  277. if (r == -1) {
  278. bsdtar_warnc(bsdtar, errno, "stat(%s)",
  279. archive_entry_pathname(entry));
  280. goto err1;
  281. }
  282. /* Skip file. */
  283. if (r == 0)
  284. continue;
  285. }
  286. if (bsdtar->option_interactive &&
  287. !yes("extract '%s'", archive_entry_pathname(entry)))
  288. continue;
  289. if (bsdtar->verbose > 1) {
  290. /* GNU tar uses -tv format with -xvv */
  291. safe_fprintf(stderr, "x ");
  292. list_item_verbose(bsdtar, stderr, entry);
  293. fflush(stderr);
  294. } else if (bsdtar->verbose > 0) {
  295. /* Format follows SUSv2, including the
  296. * deferred '\n'. */
  297. safe_fprintf(stderr, "x %s",
  298. archive_entry_pathname(entry));
  299. fflush(stderr);
  300. }
  301. /*
  302. * Tell the SIGINFO-handler code what we're doing.
  303. * a->file_count is incremented by
  304. * archive_read_next_header(), which has already
  305. * been called for this file. However,
  306. * siginfo_setinfo() takes the number of files we
  307. * have already processed (in the past), so we
  308. * need to subtract 1 from the reported file count.
  309. */
  310. siginfo_setinfo(bsdtar, "extracting",
  311. archive_entry_pathname(entry), 0,
  312. archive_file_count(a) - 1,
  313. archive_position_uncompressed(a));
  314. siginfo_printinfo(bsdtar, 0, 0);
  315. if (bsdtar->option_stdout)
  316. r = archive_read_data_into_fd(a, 1);
  317. else
  318. r = archive_read_extract(a, entry,
  319. bsdtar->extract_flags);
  320. if (r != ARCHIVE_OK) {
  321. if (!bsdtar->verbose)
  322. safe_fprintf(stderr, "%s",
  323. archive_entry_pathname(entry));
  324. safe_fprintf(stderr, ": %s",
  325. archive_error_string(a));
  326. if (!bsdtar->verbose)
  327. fprintf(stderr, "\n");
  328. bsdtar->return_value = 1;
  329. }
  330. if (bsdtar->verbose)
  331. fprintf(stderr, "\n");
  332. if (r == ARCHIVE_FATAL)
  333. break;
  334. }
  335. }
  336. /* We're not processing any more files. */
  337. if (mode == 'x') {
  338. /* siginfo was not initialized in 't' mode. */
  339. siginfo_setinfo(bsdtar, NULL, NULL, 0, archive_file_count(a),
  340. archive_position_uncompressed(a));
  341. }
  342. r = archive_read_close(a);
  343. if (r != ARCHIVE_OK)
  344. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  345. if (r <= ARCHIVE_WARN)
  346. bsdtar->return_value = 1;
  347. if (bsdtar->verbose > 2)
  348. fprintf(stdout, "Archive Format: %s, Compression: %s\n",
  349. archive_format_name(a), archive_compression_name(a));
  350. /* Always print a final message for --progress-bytes. */
  351. if ((mode == 'x') && (bsdtar->option_progress_bytes != 0))
  352. raise(SIGUSR1);
  353. /* Print a final update (if desired). */
  354. if (mode == 'x') {
  355. /* siginfo was not initialized in 't' mode. */
  356. siginfo_printinfo(bsdtar, 0, 1);
  357. }
  358. archive_read_finish(a);
  359. /* Success! */
  360. return;
  361. err1:
  362. archive_read_finish(a);
  363. err0:
  364. /* Failure! */
  365. bsdtar->return_value = 1;
  366. return;
  367. }