write.c 34 KB

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