write.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /*-
  2. * Copyright 2006-2009 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/write.c,v 1.79 2008/11/27 05:49:52 kientzle Exp $");
  32. #ifdef HAVE_SYS_TYPES_H
  33. #include <sys/types.h>
  34. #endif
  35. #ifdef HAVE_SYS_ACL_H
  36. #include <sys/acl.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_STAT_H
  42. #include <sys/stat.h>
  43. #endif
  44. #ifdef HAVE_ATTR_XATTR_H
  45. #include <attr/xattr.h>
  46. #endif
  47. #ifdef HAVE_ERRNO_H
  48. #include <errno.h>
  49. #endif
  50. #ifdef HAVE_FCNTL_H
  51. #include <fcntl.h>
  52. #endif
  53. #ifdef HAVE_FNMATCH_H
  54. #include <fnmatch.h>
  55. #endif
  56. #ifdef HAVE_GRP_H
  57. #include <grp.h>
  58. #endif
  59. #ifdef HAVE_LIMITS_H
  60. #include <limits.h>
  61. #endif
  62. #ifdef HAVE_LINUX_FS_H
  63. #include <linux/fs.h> /* for Linux file flags */
  64. #endif
  65. /*
  66. * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
  67. * As the include guards don't agree, the order of include is important.
  68. */
  69. #ifdef HAVE_LINUX_EXT2_FS_H
  70. #include <linux/ext2_fs.h> /* for Linux file flags */
  71. #endif
  72. #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
  73. /* This header exists but is broken on Cygwin. */
  74. #include <ext2fs/ext2_fs.h>
  75. #endif
  76. #ifdef HAVE_PWD_H
  77. #include <pwd.h>
  78. #endif
  79. #include <signal.h>
  80. #include <stdio.h>
  81. #ifdef HAVE_STDLIB_H
  82. #include <stdlib.h>
  83. #endif
  84. #ifdef HAVE_STRING_H
  85. #include <string.h>
  86. #endif
  87. #include <time.h>
  88. #ifdef HAVE_UNISTD_H
  89. #include <unistd.h>
  90. #endif
  91. #include "bsdtar.h"
  92. #include "tree.h"
  93. #include "archive_multitape.h"
  94. #include "ccache.h"
  95. #include "getfstype.h"
  96. #include "sigquit.h"
  97. #include "tsnetwork.h"
  98. /* Size of buffer for holding file data prior to writing. */
  99. #define FILEDATABUFLEN 65536
  100. static int append_archive(struct bsdtar *, struct archive *,
  101. struct archive *ina, void * cookie);
  102. static int append_archive_filename(struct bsdtar *,
  103. struct archive *, const char *fname);
  104. static int append_archive_tarsnap(struct bsdtar *,
  105. struct archive *, const char *tapename);
  106. static void archive_names_from_file(struct bsdtar *bsdtar,
  107. struct archive *a);
  108. static int archive_names_from_file_helper(struct bsdtar *bsdtar,
  109. const char *line);
  110. static int copy_file_data(struct bsdtar *bsdtar,
  111. struct archive *a, struct archive *ina);
  112. static int getdevino(struct archive *, const char *, dev_t *,
  113. ino_t *);
  114. static int new_enough(struct bsdtar *, const char *path,
  115. const struct stat *);
  116. static int truncate_archive(struct bsdtar *);
  117. static void write_archive(struct archive *, struct bsdtar *);
  118. static void write_entry_backend(struct bsdtar *, struct archive *,
  119. struct archive_entry *,
  120. const struct stat *, const char *);
  121. static int write_file_data(struct bsdtar *, struct archive *,
  122. struct archive_entry *, int fd);
  123. static void write_hierarchy(struct bsdtar *, struct archive *,
  124. const char *);
  125. /*
  126. * Macros to simplify mode-switching.
  127. */
  128. #define MODE_SET(bsdtar, a, mode) \
  129. archive_write_multitape_setmode(a, bsdtar->write_cookie, mode)
  130. #define MODE_HEADER(bsdtar, a) MODE_SET(bsdtar, a, 0)
  131. #define MODE_DATA(bsdtar, a) MODE_SET(bsdtar, a, 1)
  132. #define MODE_DONE(bsdtar, a) \
  133. (archive_write_finish_entry(a) || \
  134. MODE_SET(bsdtar, a, 2))
  135. /* Get the device and inode numbers of a path. */
  136. static int
  137. getdevino(struct archive * a, const char * path, dev_t * d, ino_t * i)
  138. {
  139. struct stat sb;
  140. if (path == NULL) {
  141. /*
  142. * Return bogus values to avoid comparing against
  143. * uninitialized data later on.
  144. */
  145. *d = -1;
  146. *i = -1;
  147. return 0;
  148. }
  149. if (stat(path, &sb)) {
  150. archive_set_error(a, errno, "%s", path);
  151. return (-1);
  152. } else {
  153. *d = sb.st_dev;
  154. *i = sb.st_ino;
  155. return (0);
  156. }
  157. }
  158. /* Determine if we need to truncate the archive at the current point. */
  159. static int
  160. truncate_archive(struct bsdtar *bsdtar)
  161. {
  162. static int msgprinted = 0;
  163. if (sigquit_received == 0)
  164. return (0);
  165. /* Tell the user that we got the message. */
  166. if (msgprinted == 0) {
  167. bsdtar_warnc(bsdtar, 0, "quit signal received or bandwidth "
  168. "limit reached; archive is being truncated");
  169. msgprinted = 1;
  170. }
  171. /* Tell the multitape code to truncate the archive. */
  172. archive_write_multitape_truncate(bsdtar->write_cookie);
  173. /* Yes, we need to stop writing now. */
  174. return (1);
  175. }
  176. /* If the --disk-pause option was used, sleep for a while. */
  177. static void
  178. disk_pause(struct bsdtar *bsdtar)
  179. {
  180. struct timespec ts;
  181. /* Do we want to sleep? */
  182. if (bsdtar->disk_pause == 0)
  183. return;
  184. /* Sleep. */
  185. ts.tv_sec = bsdtar->disk_pause / 1000;
  186. ts.tv_nsec = bsdtar->disk_pause * 1000000;
  187. nanosleep(&ts, NULL);
  188. }
  189. static volatile sig_atomic_t sigusr2_received = 0;
  190. static void sigusr2_handler(int sig);
  191. static void
  192. sigusr2_handler(int sig)
  193. {
  194. (void)sig; /* UNUSED */
  195. /* SIGUSR2 has been received. */
  196. sigusr2_received = 1;
  197. }
  198. /* Create a checkpoint in the archive if necessary. */
  199. static int
  200. checkpoint_archive(struct bsdtar *bsdtar, int withinline)
  201. {
  202. int rc;
  203. if (sigusr2_received == 0)
  204. return (0);
  205. sigusr2_received = 0;
  206. /* If the user asked for verbosity, be verbose. */
  207. if (bsdtar->verbose) {
  208. if (withinline)
  209. fprintf(stderr, "\n");
  210. fprintf(stderr, "tarsnap: Creating checkpoint...");
  211. }
  212. /* Create the checkpoint. */
  213. rc = archive_write_multitape_checkpoint(bsdtar->write_cookie);
  214. /* If the user asked for verbosity, be verbose. */
  215. if (bsdtar->verbose) {
  216. if (rc == 0)
  217. fprintf(stderr, " done.");
  218. if (!withinline)
  219. fprintf(stderr, "\n");
  220. }
  221. /* Return status code from checkpoint creation. */
  222. return (rc);
  223. }
  224. void
  225. tarsnap_mode_c(struct bsdtar *bsdtar)
  226. {
  227. struct archive *a;
  228. size_t i;
  229. if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
  230. bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
  231. /* Warn if "--" appears at the beginning of a file or dir to archive. */
  232. for (i = 0; bsdtar->argv[i] != NULL; i++) {
  233. if (bsdtar->argv[i][0] == '-' && bsdtar->argv[i][1] == '-') {
  234. bsdtar_warnc(bsdtar, 0,
  235. "List of objects to archive includes '%s'."
  236. " This might not be what you intended.",
  237. bsdtar->argv[i]);
  238. break;
  239. }
  240. }
  241. if ((a = archive_write_new()) == NULL) {
  242. bsdtar_warnc(bsdtar, ENOMEM, "Cannot allocate memory");
  243. goto err0;
  244. }
  245. /* We only support the pax restricted format. */
  246. archive_write_set_format_pax_restricted(a);
  247. /* Set the block size to zero -- we don't want buffering. */
  248. archive_write_set_bytes_per_block(a, 0);
  249. /* Open the archive, keeping a cookie for talking to the tape layer. */
  250. bsdtar->write_cookie = archive_write_open_multitape(a,
  251. bsdtar->machinenum, bsdtar->cachedir, bsdtar->tapenames[0],
  252. bsdtar->argc_orig, bsdtar->argv_orig,
  253. bsdtar->option_print_stats, bsdtar->option_dryrun,
  254. bsdtar->creationtime, bsdtar->option_csv_filename);
  255. if (bsdtar->write_cookie == NULL) {
  256. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  257. goto err1;
  258. }
  259. /*
  260. * Remember the device and inode numbers of the cache directory, so
  261. * that we can skip it in write_hierarchy().
  262. */
  263. if (getdevino(a, bsdtar->cachedir,
  264. &bsdtar->cachedir_dev, &bsdtar->cachedir_ino)) {
  265. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  266. goto err1;
  267. }
  268. /* If the chunkification cache is enabled, read it. */
  269. if ((bsdtar->cachecrunch < 2) && (bsdtar->cachedir != NULL)) {
  270. bsdtar->chunk_cache = ccache_read(bsdtar->cachedir);
  271. if (bsdtar->chunk_cache == NULL) {
  272. bsdtar_warnc(bsdtar, errno, "Error reading cache");
  273. goto err1;
  274. }
  275. }
  276. /*
  277. * write_archive(a, bsdtar) calls archive_write_finish(a), so we set
  278. * a = NULL to avoid doing this again in err1.
  279. */
  280. write_archive(a, bsdtar);
  281. a = NULL;
  282. /*
  283. * If this isn't a dry run and we're running with the chunkification
  284. * cache enabled, write the cache back to disk.
  285. */
  286. if ((bsdtar->option_dryrun == 0) && (bsdtar->cachecrunch < 2)) {
  287. if (ccache_write(bsdtar->chunk_cache, bsdtar->cachedir)) {
  288. bsdtar_warnc(bsdtar, errno, "Error writing cache");
  289. goto err2;
  290. }
  291. }
  292. /* Free the chunkification cache. */
  293. if (bsdtar->cachecrunch < 2)
  294. ccache_free(bsdtar->chunk_cache);
  295. /* Success! */
  296. return;
  297. err2:
  298. ccache_free(bsdtar->chunk_cache);
  299. err1:
  300. archive_write_finish(a);
  301. err0:
  302. /* Failure! */
  303. bsdtar->return_value = 1;
  304. return;
  305. }
  306. /*
  307. * Write user-specified files/dirs to opened archive.
  308. */
  309. static void
  310. write_archive(struct archive *a, struct bsdtar *bsdtar)
  311. {
  312. struct sigaction sa;
  313. const char *arg;
  314. struct archive_entry *entry, *sparse_entry;
  315. /* We want to catch SIGINFO and SIGUSR1. */
  316. siginfo_init(bsdtar);
  317. /* We also want to catch SIGQUIT and ^Q. */
  318. if (sigquit_init())
  319. exit(1);
  320. /* And SIGUSR2, too. */
  321. sa.sa_handler = sigusr2_handler;
  322. sigemptyset(&sa.sa_mask);
  323. sa.sa_flags = 0;
  324. if (sigaction(SIGUSR2, &sa, NULL))
  325. bsdtar_errc(bsdtar, 1, 0, "cannot install signal handler");
  326. /* Allocate a buffer for file data. */
  327. if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
  328. bsdtar_errc(bsdtar, 1, 0, "cannot allocate memory");
  329. if ((bsdtar->resolver = archive_entry_linkresolver_new()) == NULL)
  330. bsdtar_errc(bsdtar, 1, 0, "cannot create link resolver");
  331. archive_entry_linkresolver_set_strategy(bsdtar->resolver,
  332. archive_format(a));
  333. if ((bsdtar->diskreader = archive_read_disk_new()) == NULL)
  334. bsdtar_errc(bsdtar, 1, 0, "Cannot create read_disk object");
  335. archive_read_disk_set_standard_lookup(bsdtar->diskreader);
  336. if (bsdtar->names_from_file != NULL)
  337. archive_names_from_file(bsdtar, a);
  338. while (*bsdtar->argv) {
  339. if (truncate_archive(bsdtar))
  340. break;
  341. if (checkpoint_archive(bsdtar, 0))
  342. exit(1);
  343. arg = *bsdtar->argv;
  344. if (arg[0] == '-' && arg[1] == 'C') {
  345. arg += 2;
  346. if (*arg == '\0') {
  347. bsdtar->argv++;
  348. arg = *bsdtar->argv;
  349. if (arg == NULL) {
  350. bsdtar_warnc(bsdtar, 0,
  351. "Missing argument for -C");
  352. bsdtar->return_value = 1;
  353. goto cleanup;
  354. }
  355. if (*arg == '\0') {
  356. bsdtar_warnc(bsdtar, 0,
  357. "Meaningless argument for -C: ''");
  358. bsdtar->return_value = 1;
  359. goto cleanup;
  360. }
  361. }
  362. set_chdir(bsdtar, arg);
  363. } else {
  364. if (arg[0] != '/' &&
  365. (arg[0] != '@' || arg[1] != '/') &&
  366. (arg[0] != '@' || arg[1] != '@'))
  367. do_chdir(bsdtar); /* Handle a deferred -C */
  368. if (arg[0] == '@' && arg[1] == '@') {
  369. if (append_archive_tarsnap(bsdtar, a,
  370. arg + 2) != 0)
  371. break;
  372. } else if (arg[0] == '@') {
  373. if (append_archive_filename(bsdtar, a,
  374. arg + 1) != 0)
  375. break;
  376. } else
  377. #if defined(_WIN32) && !defined(__CYGWIN__)
  378. write_hierarchy_win(bsdtar, a, arg,
  379. write_hierarchy);
  380. #else
  381. write_hierarchy(bsdtar, a, arg);
  382. #endif
  383. }
  384. bsdtar->argv++;
  385. }
  386. /*
  387. * This code belongs to bsdtar and is used when writing archives in
  388. * "new cpio" format. It has no effect in tarsnap, since tarsnap
  389. * doesn't write archives in this format; but I'm leaving it in to
  390. * make the diffs smaller.
  391. */
  392. entry = NULL;
  393. archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
  394. while (entry != NULL) {
  395. write_entry_backend(bsdtar, a, entry, NULL, NULL);
  396. archive_entry_free(entry);
  397. entry = NULL;
  398. archive_entry_linkify(bsdtar->resolver, &entry, &sparse_entry);
  399. }
  400. /*
  401. * Check whether the archive is empty; this is unaffected by
  402. * deduplication. Must be done before the archive is closed since
  403. * even empty archives end up with nonzero size due to tar EOF blocks.
  404. */
  405. if ((!bsdtar->option_quiet) &&
  406. (archive_position_compressed(a) == 0)) {
  407. bsdtar_warnc(bsdtar, 0, "Warning: Archive contains no files");
  408. }
  409. if (archive_write_close(a)) {
  410. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  411. bsdtar->return_value = 1;
  412. }
  413. cleanup:
  414. /* Free file data buffer. */
  415. free(bsdtar->buff);
  416. archive_entry_linkresolver_free(bsdtar->resolver);
  417. bsdtar->resolver = NULL;
  418. archive_read_finish(bsdtar->diskreader);
  419. bsdtar->diskreader = NULL;
  420. if (bsdtar->option_totals && (bsdtar->return_value == 0)) {
  421. fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
  422. (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
  423. }
  424. archive_write_finish(a);
  425. /* Restore old SIGINFO + SIGUSR1 handlers. */
  426. siginfo_done(bsdtar);
  427. }
  428. /*
  429. * Archive names specified in file.
  430. *
  431. * Unless --null was specified, a line containing exactly "-C" will
  432. * cause the next line to be a directory to pass to chdir(). If
  433. * --null is specified, then a line "-C" is just another filename.
  434. */
  435. void
  436. archive_names_from_file(struct bsdtar *bsdtar, struct archive *a)
  437. {
  438. bsdtar->archive = a;
  439. bsdtar->next_line_is_dir = 0;
  440. process_lines(bsdtar, bsdtar->names_from_file,
  441. archive_names_from_file_helper, bsdtar->option_null);
  442. if (bsdtar->next_line_is_dir)
  443. bsdtar_errc(bsdtar, 1, errno,
  444. "Unexpected end of filename list; "
  445. "directory expected after -C");
  446. }
  447. static int
  448. archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line)
  449. {
  450. if (bsdtar->next_line_is_dir) {
  451. if (*line != '\0')
  452. set_chdir(bsdtar, line);
  453. else {
  454. bsdtar_warnc(bsdtar, 0,
  455. "Meaningless argument for -C: ''");
  456. bsdtar->return_value = 1;
  457. }
  458. bsdtar->next_line_is_dir = 0;
  459. } else if (!bsdtar->option_null && strcmp(line, "-C") == 0)
  460. bsdtar->next_line_is_dir = 1;
  461. else {
  462. if (*line != '/')
  463. do_chdir(bsdtar); /* Handle a deferred -C */
  464. write_hierarchy(bsdtar, bsdtar->archive, line);
  465. }
  466. return (0);
  467. }
  468. /*
  469. * Copy from specified archive to current archive. Returns non-zero
  470. * for write errors (which force us to terminate the entire archiving
  471. * operation). If there are errors reading the input archive, we set
  472. * bsdtar->return_value but return zero, so the overall archiving
  473. * operation will complete and return non-zero.
  474. */
  475. static int
  476. append_archive_filename(struct bsdtar *bsdtar, struct archive *a,
  477. const char *filename)
  478. {
  479. struct archive *ina;
  480. int rc;
  481. if (strcmp(filename, "-") == 0)
  482. filename = NULL; /* Library uses NULL for stdio. */
  483. ina = archive_read_new();
  484. archive_read_support_format_all(ina);
  485. archive_read_support_compression_all(ina);
  486. if (archive_read_open_file(ina, filename, 10240)) {
  487. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
  488. bsdtar->return_value = 1;
  489. return (0);
  490. }
  491. rc = append_archive(bsdtar, a, ina, NULL);
  492. if (archive_errno(ina)) {
  493. bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
  494. filename, archive_error_string(ina));
  495. bsdtar->return_value = 1;
  496. }
  497. archive_read_finish(ina);
  498. return (rc);
  499. }
  500. static int
  501. append_archive_tarsnap(struct bsdtar *bsdtar, struct archive *a,
  502. const char *tapename)
  503. {
  504. struct archive *ina;
  505. void * cookie;
  506. int rc;
  507. ina = archive_read_new();
  508. archive_read_support_format_tar(ina);
  509. archive_read_support_compression_none(ina);
  510. cookie = archive_read_open_multitape(ina, bsdtar->machinenum,
  511. tapename);
  512. if (cookie == NULL) {
  513. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina));
  514. bsdtar->return_value = 1;
  515. return (0);
  516. }
  517. rc = append_archive(bsdtar, a, ina, cookie);
  518. /* Handle errors which haven't already been reported. */
  519. if (archive_errno(ina)) {
  520. bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s",
  521. tapename, archive_error_string(ina));
  522. bsdtar->return_value = 1;
  523. }
  524. archive_read_finish(ina);
  525. return (rc);
  526. }
  527. static int
  528. append_archive(struct bsdtar *bsdtar, struct archive *a, struct archive *ina,
  529. void * cookie)
  530. {
  531. struct archive_entry *in_entry;
  532. int e;
  533. while (0 == archive_read_next_header(ina, &in_entry)) {
  534. if (truncate_archive(bsdtar))
  535. break;
  536. if (checkpoint_archive(bsdtar, 0))
  537. exit(1);
  538. if (cookie == NULL)
  539. disk_pause(bsdtar);
  540. if (network_select(0))
  541. exit(1);
  542. if (!new_enough(bsdtar, archive_entry_pathname(in_entry),
  543. archive_entry_stat(in_entry)))
  544. continue;
  545. if (excluded(bsdtar, archive_entry_pathname(in_entry)))
  546. continue;
  547. if (bsdtar->option_interactive &&
  548. !yes("copy '%s'", archive_entry_pathname(in_entry)))
  549. continue;
  550. if (bsdtar->verbose)
  551. safe_fprintf(stderr, "a %s",
  552. archive_entry_pathname(in_entry));
  553. siginfo_setinfo(bsdtar, "copying",
  554. archive_entry_pathname(in_entry),
  555. archive_entry_size(in_entry));
  556. siginfo_printinfo(bsdtar, 0);
  557. if (MODE_HEADER(bsdtar, a))
  558. goto err_fatal;
  559. e = archive_write_header(a, in_entry);
  560. if (e != ARCHIVE_OK) {
  561. if (!bsdtar->verbose)
  562. bsdtar_warnc(bsdtar, 0, "%s: %s",
  563. archive_entry_pathname(in_entry),
  564. archive_error_string(a));
  565. else
  566. fprintf(stderr, ": %s", archive_error_string(a));
  567. }
  568. if (e == ARCHIVE_FATAL)
  569. exit(1);
  570. if (e < ARCHIVE_WARN)
  571. goto done;
  572. if (MODE_DATA(bsdtar, a))
  573. goto err_fatal;
  574. if (archive_entry_size(in_entry) == 0)
  575. archive_read_data_skip(ina);
  576. else if (cookie == NULL) {
  577. if (copy_file_data(bsdtar, a, ina))
  578. exit(1);
  579. } else {
  580. switch (archive_multitape_copy(ina, cookie, a,
  581. bsdtar->write_cookie)) {
  582. case -1:
  583. goto err_fatal;
  584. case -2:
  585. goto err_read;
  586. }
  587. }
  588. done:
  589. if (MODE_DONE(bsdtar, a))
  590. goto err_fatal;
  591. if (bsdtar->verbose)
  592. fprintf(stderr, "\n");
  593. continue;
  594. err_read:
  595. bsdtar->return_value = 1;
  596. if (MODE_DONE(bsdtar, a))
  597. goto err_fatal;
  598. if (bsdtar->verbose)
  599. fprintf(stderr, "\n");
  600. break;
  601. err_fatal:
  602. bsdtar_warnc(bsdtar, archive_errno(a), "%s",
  603. archive_error_string(a));
  604. exit(1);
  605. }
  606. /* Note: If we got here, we saw no write errors, so return success. */
  607. return (0);
  608. }
  609. /* Helper function to copy data between archives. */
  610. static int
  611. copy_file_data(struct bsdtar *bsdtar, struct archive *a, struct archive *ina)
  612. {
  613. ssize_t bytes_read;
  614. ssize_t bytes_written;
  615. off_t progress = 0;
  616. bytes_read = archive_read_data(ina, bsdtar->buff, FILEDATABUFLEN);
  617. while (bytes_read > 0) {
  618. disk_pause(bsdtar);
  619. if (network_select(0))
  620. return (-1);
  621. siginfo_printinfo(bsdtar, progress);
  622. bytes_written = archive_write_data(a, bsdtar->buff,
  623. bytes_read);
  624. if (bytes_written < bytes_read) {
  625. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  626. return (-1);
  627. }
  628. if (truncate_archive(bsdtar))
  629. break;
  630. if (checkpoint_archive(bsdtar, 1))
  631. return (-1);
  632. progress += bytes_written;
  633. bytes_read = archive_read_data(ina, bsdtar->buff,
  634. FILEDATABUFLEN);
  635. }
  636. return (0);
  637. }
  638. /*
  639. * Add the file or dir hierarchy named by 'path' to the archive
  640. */
  641. static void
  642. write_hierarchy(struct bsdtar *bsdtar, struct archive *a, const char *path)
  643. {
  644. struct archive_entry *entry = NULL, *spare_entry = NULL;
  645. struct tree *tree;
  646. char symlink_mode = bsdtar->symlink_mode;
  647. dev_t first_dev = 0;
  648. int dev_recorded = 0;
  649. int tree_ret;
  650. dev_t last_dev = 0;
  651. char * fstype;
  652. tree = tree_open(path);
  653. if (!tree) {
  654. bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path);
  655. bsdtar->return_value = 1;
  656. return;
  657. }
  658. while ((tree_ret = tree_next(tree))) {
  659. int r;
  660. const char *name = tree_current_path(tree);
  661. const struct stat *st = NULL; /* info to use for this entry */
  662. const struct stat *lst = NULL; /* lstat() information */
  663. int descend;
  664. if (truncate_archive(bsdtar))
  665. break;
  666. if (checkpoint_archive(bsdtar, 0))
  667. exit(1);
  668. disk_pause(bsdtar);
  669. if (network_select(0))
  670. exit(1);
  671. if (tree_ret == TREE_ERROR_FATAL)
  672. bsdtar_errc(bsdtar, 1, tree_errno(tree),
  673. "%s: Unable to continue traversing directory tree",
  674. name);
  675. if (tree_ret == TREE_ERROR_DIR) {
  676. bsdtar_warnc(bsdtar, errno,
  677. "%s: Couldn't visit directory", name);
  678. bsdtar->return_value = 1;
  679. }
  680. if (tree_ret != TREE_REGULAR)
  681. continue;
  682. /*
  683. * If this file/dir is excluded by a filename
  684. * pattern, skip it.
  685. */
  686. if (excluded(bsdtar, name))
  687. continue;
  688. /*
  689. * Get lstat() info from the tree library.
  690. */
  691. lst = tree_current_lstat(tree);
  692. if (lst == NULL) {
  693. /* Couldn't lstat(); must not exist. */
  694. bsdtar_warnc(bsdtar, errno, "%s: Cannot stat", name);
  695. /* Return error if files disappear during traverse. */
  696. bsdtar->return_value = 1;
  697. continue;
  698. }
  699. /*
  700. * Distinguish 'L'/'P'/'H' symlink following.
  701. */
  702. switch(symlink_mode) {
  703. case 'H':
  704. /* 'H': After the first item, rest like 'P'. */
  705. symlink_mode = 'P';
  706. /* 'H': First item (from command line) like 'L'. */
  707. /* FALLTHROUGH */
  708. case 'L':
  709. /* 'L': Do descend through a symlink to dir. */
  710. descend = tree_current_is_dir(tree);
  711. /* 'L': Follow symlinks to files. */
  712. archive_read_disk_set_symlink_logical(bsdtar->diskreader);
  713. /* 'L': Archive symlinks as targets, if we can. */
  714. st = tree_current_stat(tree);
  715. if (st != NULL)
  716. break;
  717. /* If stat fails, we have a broken symlink;
  718. * in that case, don't follow the link. */
  719. /* FALLTHROUGH */
  720. default:
  721. /* 'P': Don't descend through a symlink to dir. */
  722. descend = tree_current_is_physical_dir(tree);
  723. /* 'P': Don't follow symlinks to files. */
  724. archive_read_disk_set_symlink_physical(bsdtar->diskreader);
  725. /* 'P': Archive symlinks as symlinks. */
  726. st = lst;
  727. break;
  728. }
  729. if (bsdtar->option_no_subdirs)
  730. descend = 0;
  731. /*
  732. * If user has asked us not to cross mount points,
  733. * then don't descend into a dir on a different
  734. * device.
  735. */
  736. if (!dev_recorded) {
  737. last_dev = first_dev = lst->st_dev;
  738. dev_recorded = 1;
  739. }
  740. if (bsdtar->option_dont_traverse_mounts) {
  741. if (lst->st_dev != first_dev)
  742. descend = 0;
  743. }
  744. /*
  745. * If the user did not specify --insane-filesystems, do not
  746. * cross into a new filesystem which is known to be synthetic.
  747. * Note that we will archive synthetic filesystems if we are
  748. * explicitly told to do so.
  749. */
  750. if ((bsdtar->option_insane_filesystems == 0) &&
  751. (descend != 0) &&
  752. (lst->st_dev != last_dev)) {
  753. fstype = getfstype(tree_current_access_path(tree));
  754. if (fstype == NULL)
  755. bsdtar_errc(bsdtar, 1, errno,
  756. "%s: Error getting filesystem type",
  757. name);
  758. if (getfstype_issynthetic(fstype)) {
  759. if (!bsdtar->option_quiet)
  760. bsdtar_warnc(bsdtar, 0,
  761. "Not descending into filesystem of type %s: %s",
  762. fstype, name);
  763. descend = 0;
  764. } else {
  765. /* This device is ok to archive. */
  766. last_dev = lst->st_dev;
  767. }
  768. free(fstype);
  769. }
  770. /*
  771. * In -u mode, check that the file is newer than what's
  772. * already in the archive; in all modes, obey --newerXXX flags.
  773. */
  774. if (!new_enough(bsdtar, name, st)) {
  775. if (!descend)
  776. continue;
  777. if (bsdtar->option_interactive &&
  778. !yes("add '%s'", name))
  779. continue;
  780. tree_descend(tree);
  781. continue;
  782. }
  783. archive_entry_free(entry);
  784. entry = archive_entry_new();
  785. archive_entry_set_pathname(entry, name);
  786. archive_entry_copy_sourcepath(entry,
  787. tree_current_access_path(tree));
  788. /* Populate the archive_entry with metadata from the disk. */
  789. /* XXX TODO: Arrange to open a regular file before
  790. * calling this so we can pass in an fd and shorten
  791. * the race to query metadata. The linkify dance
  792. * makes this more complex than it might sound. */
  793. r = archive_read_disk_entry_from_file(bsdtar->diskreader,
  794. entry, -1, st);
  795. if (r != ARCHIVE_OK) {
  796. bsdtar_warnc(bsdtar, archive_errno(bsdtar->diskreader),
  797. "%s", archive_error_string(bsdtar->diskreader));
  798. archive_clear_error(bsdtar->diskreader);
  799. }
  800. if (r < ARCHIVE_WARN)
  801. continue;
  802. /* XXX TODO: Just use flag data from entry; avoid the
  803. * duplicate check here. */
  804. /*
  805. * If this file/dir is flagged "nodump" and we're
  806. * honoring such flags, skip this file/dir.
  807. */
  808. #if defined(HAVE_STRUCT_STAT_ST_FLAGS) && defined(UF_NODUMP)
  809. /* BSD systems store flags in struct stat */
  810. if (bsdtar->option_honor_nodump &&
  811. (lst->st_flags & UF_NODUMP))
  812. continue;
  813. #endif
  814. #if defined(EXT2_NODUMP_FL)
  815. /* Linux uses ioctl to read flags. */
  816. if (bsdtar->option_honor_nodump) {
  817. unsigned long fflags, dummy;
  818. archive_entry_fflags(entry, &fflags, &dummy);
  819. if (fflags & EXT2_NODUMP_FL)
  820. continue;
  821. }
  822. #endif
  823. /*
  824. * Don't back up the cache directory or any files inside it.
  825. */
  826. if ((lst->st_ino == bsdtar->cachedir_ino) &&
  827. (lst->st_dev == bsdtar->cachedir_dev)) {
  828. if (!bsdtar->option_quiet)
  829. bsdtar_warnc(bsdtar, 0,
  830. "Not adding cache directory to archive: %s",
  831. name);
  832. continue;
  833. }
  834. /*
  835. * If the user vetoes this file/directory, skip it.
  836. * We want this to be fairly late; if some other
  837. * check would veto this file, we shouldn't bother
  838. * the user with it.
  839. */
  840. if (bsdtar->option_interactive &&
  841. !yes("add '%s'", name))
  842. continue;
  843. /* Note: if user vetoes, we won't descend. */
  844. if (descend)
  845. tree_descend(tree);
  846. /*
  847. * Rewrite the pathname to be archived. If rewrite
  848. * fails, skip the entry.
  849. */
  850. if (edit_pathname(bsdtar, entry))
  851. continue;
  852. /*
  853. * If this is a socket, skip the entry: POSIX requires that
  854. * pax(1) emit a "diagnostic message" (i.e., warning) that
  855. * sockets cannot be archived, but this can make backups of
  856. * running systems very noisy.
  857. */
  858. if (S_ISSOCK(st->st_mode))
  859. continue;
  860. /* Display entry as we process it.
  861. * This format is required by SUSv2. */
  862. if (bsdtar->verbose)
  863. safe_fprintf(stderr, "a %s",
  864. archive_entry_pathname(entry));
  865. /*
  866. * If the user hasn't specifically asked to have the access
  867. * time stored, zero it. At the moment this usually only
  868. * matters for files which have flags set, since the "posix
  869. * restricted" format doesn't store access times for most
  870. * other files.
  871. */
  872. if (bsdtar->option_store_atime == 0)
  873. archive_entry_set_atime(entry, 0, 0);
  874. /* Non-regular files get archived with zero size. */
  875. if (!S_ISREG(st->st_mode))
  876. archive_entry_set_size(entry, 0);
  877. /* Record what we're doing, for SIGINFO / SIGUSR1. */
  878. siginfo_setinfo(bsdtar, "adding",
  879. archive_entry_pathname(entry), archive_entry_size(entry));
  880. archive_entry_linkify(bsdtar->resolver, &entry, &spare_entry);
  881. /* Handle SIGINFO / SIGUSR1 request if one was made. */
  882. siginfo_printinfo(bsdtar, 0);
  883. while (entry != NULL) {
  884. write_entry_backend(bsdtar, a, entry, st,
  885. tree_current_realpath(tree));
  886. archive_entry_free(entry);
  887. entry = spare_entry;
  888. spare_entry = NULL;
  889. }
  890. if (bsdtar->verbose)
  891. fprintf(stderr, "\n");
  892. }
  893. archive_entry_free(entry);
  894. if (tree_close(tree))
  895. bsdtar_errc(bsdtar, 1, 0, "Error traversing directory tree");
  896. }
  897. /*
  898. * Backend for write_entry.
  899. */
  900. static void
  901. write_entry_backend(struct bsdtar *bsdtar, struct archive *a,
  902. struct archive_entry *entry, const struct stat *st, const char *rpath)
  903. {
  904. off_t skiplen;
  905. CCACHE_ENTRY *cce = NULL;
  906. int filecached = 0;
  907. int fd = -1;
  908. int e;
  909. /*
  910. * If this archive entry needs data, we have a canonical path to the
  911. * relevant file, and the chunkification cache isn't disabled, ask
  912. * the chunkification cache to find the entry for the file (if one
  913. * already exists) and tell us if it can provide the entire file.
  914. */
  915. if ((st != NULL) && S_ISREG(st->st_mode) && (rpath != NULL) &&
  916. (archive_entry_size(entry) > 0) && (bsdtar->cachecrunch < 2) &&
  917. (bsdtar->chunk_cache != NULL)) {
  918. cce = ccache_entry_lookup(bsdtar->chunk_cache, rpath, st,
  919. bsdtar->write_cookie, &filecached);
  920. }
  921. /*
  922. * Open the file if we need to write archive entry data and the
  923. * chunkification cache can't provide all of it.
  924. */
  925. if ((archive_entry_size(entry) > 0) && (filecached == 0)) {
  926. const char *pathname = archive_entry_sourcepath(entry);
  927. fd = open(pathname, O_RDONLY);
  928. if (fd == -1) {
  929. if (!bsdtar->verbose)
  930. bsdtar_warnc(bsdtar, errno,
  931. "%s: could not open file", pathname);
  932. else
  933. fprintf(stderr, ": %s", strerror(errno));
  934. return;
  935. }
  936. }
  937. /* Write the archive header. */
  938. if (MODE_HEADER(bsdtar, a)) {
  939. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  940. exit(1);
  941. }
  942. e = archive_write_header(a, entry);
  943. if (e != ARCHIVE_OK) {
  944. if (!bsdtar->verbose)
  945. bsdtar_warnc(bsdtar, 0, "%s: %s",
  946. archive_entry_pathname(entry),
  947. archive_error_string(a));
  948. else
  949. fprintf(stderr, ": %s", archive_error_string(a));
  950. }
  951. if (e == ARCHIVE_FATAL)
  952. exit(1);
  953. /*
  954. * If we opened a file earlier, write it out now. Note that
  955. * the format handler might have reset the size field to zero
  956. * to inform us that the archive body won't get stored. In
  957. * that case, just skip the write.
  958. */
  959. /* If the cache can provide the entire archive entry, do it. */
  960. if (e >= ARCHIVE_WARN && filecached &&
  961. archive_entry_size(entry) > 0) {
  962. if (MODE_DATA(bsdtar, a)) {
  963. bsdtar_warnc(bsdtar, 0, "%s",
  964. archive_error_string(a));
  965. exit(1);
  966. }
  967. skiplen = ccache_entry_write(cce, bsdtar->write_cookie);
  968. if (skiplen < st->st_size) {
  969. bsdtar_warnc(bsdtar, 0,
  970. "Error writing cached archive entry");
  971. exit(1);
  972. }
  973. if (archive_write_skip(a, skiplen)) {
  974. bsdtar_warnc(bsdtar, 0, "%s",
  975. archive_error_string(a));
  976. exit(1);
  977. }
  978. }
  979. /*
  980. * We don't need to write anything now if the file was cached
  981. * and the cache wrote it out earlier.
  982. */
  983. if (e >= ARCHIVE_WARN && fd >= 0 && archive_entry_size(entry) > 0 &&
  984. filecached == 0) {
  985. /* Switch into data mode. */
  986. if (MODE_DATA(bsdtar, a)) {
  987. bsdtar_warnc(bsdtar, 0, "%s",
  988. archive_error_string(a));
  989. exit(1);
  990. }
  991. if (cce != NULL) {
  992. /* Ask the cache to write as much as possible. */
  993. skiplen = ccache_entry_writefile(cce,
  994. bsdtar->write_cookie, bsdtar->cachecrunch, fd);
  995. if (skiplen < 0) {
  996. bsdtar_warnc(bsdtar, 0,
  997. "Error writing archive");
  998. exit(1);
  999. }
  1000. /* Skip forward in the file. */
  1001. if (lseek(fd, skiplen, SEEK_SET) == -1) {
  1002. bsdtar_warnc(bsdtar, errno, "lseek(%s)",
  1003. archive_entry_pathname(entry));
  1004. exit(1);
  1005. }
  1006. /* Tell the archive layer that we've skipped. */
  1007. if (archive_write_skip(a, skiplen)) {
  1008. bsdtar_warnc(bsdtar, 0, "%s",
  1009. archive_error_string(a));
  1010. exit(1);
  1011. }
  1012. }
  1013. if (write_file_data(bsdtar, a, entry, fd))
  1014. exit(1);
  1015. }
  1016. /* This entry is done. */
  1017. if (!truncate_archive(bsdtar) && MODE_DONE(bsdtar, a)) {
  1018. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  1019. exit(1);
  1020. }
  1021. /* Tell the cache that we're done. */
  1022. if (cce != NULL) {
  1023. if (ccache_entry_end(bsdtar->chunk_cache, cce,
  1024. bsdtar->write_cookie, rpath, bsdtar->snaptime))
  1025. exit(1);
  1026. cce = NULL;
  1027. }
  1028. /*
  1029. * If we opened a file, close it now even if there was an error
  1030. * which made us decide not to write the archive body.
  1031. */
  1032. if (fd >= 0)
  1033. close(fd);
  1034. }
  1035. /* Helper function to copy file to archive. */
  1036. static int
  1037. write_file_data(struct bsdtar *bsdtar, struct archive *a,
  1038. struct archive_entry *entry, int fd)
  1039. {
  1040. ssize_t bytes_read;
  1041. ssize_t bytes_written;
  1042. off_t progress = 0;
  1043. bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
  1044. while (bytes_read > 0) {
  1045. disk_pause(bsdtar);
  1046. if (network_select(0))
  1047. return (-1);
  1048. siginfo_printinfo(bsdtar, progress);
  1049. bytes_written = archive_write_data(a, bsdtar->buff,
  1050. bytes_read);
  1051. if (bytes_written < 0) {
  1052. /* Write failed; this is bad */
  1053. bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a));
  1054. return (-1);
  1055. }
  1056. if (bytes_written < bytes_read) {
  1057. /* Write was truncated; warn but continue. */
  1058. if (!bsdtar->option_quiet)
  1059. bsdtar_warnc(bsdtar, 0,
  1060. "%s: Truncated write; file may have "
  1061. "grown while being archived.",
  1062. archive_entry_pathname(entry));
  1063. return (0);
  1064. }
  1065. if (truncate_archive(bsdtar))
  1066. break;
  1067. if (checkpoint_archive(bsdtar, 1))
  1068. exit(1);
  1069. progress += bytes_written;
  1070. bytes_read = read(fd, bsdtar->buff, FILEDATABUFLEN);
  1071. }
  1072. return 0;
  1073. }
  1074. /*
  1075. * Test if the specified file is new enough to include in the archive.
  1076. */
  1077. int
  1078. new_enough(struct bsdtar *bsdtar, const char *path, const struct stat *st)
  1079. {
  1080. (void)path; /* UNUSED */
  1081. /*
  1082. * If this file/dir is excluded by a time comparison, skip it.
  1083. */
  1084. if (bsdtar->newer_ctime_sec > 0) {
  1085. if (st->st_ctime < bsdtar->newer_ctime_sec)
  1086. return (0); /* Too old, skip it. */
  1087. if (st->st_ctime == bsdtar->newer_ctime_sec
  1088. && ARCHIVE_STAT_CTIME_NANOS(st)
  1089. <= bsdtar->newer_ctime_nsec)
  1090. return (0); /* Too old, skip it. */
  1091. }
  1092. if (bsdtar->newer_mtime_sec > 0) {
  1093. if (st->st_mtime < bsdtar->newer_mtime_sec)
  1094. return (0); /* Too old, skip it. */
  1095. if (st->st_mtime == bsdtar->newer_mtime_sec
  1096. && ARCHIVE_STAT_MTIME_NANOS(st)
  1097. <= bsdtar->newer_mtime_nsec)
  1098. return (0); /* Too old, skip it. */
  1099. }
  1100. /* If the file wasn't rejected, include it. */
  1101. return (1);
  1102. }