write.c 34 KB

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