Move duplicated code from tar_mode_[cru] into archive_write.

Fix a bug I introduced 7 minutes ago: clean up properly from archive_write
if we exit the argv-handling loop due to -C not having an argument.
This commit is contained in:
Colin Percival 2008-07-05 08:10:55 +00:00
parent d47b2ddb21
commit a6d7fc3469
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=180284

View File

@ -155,9 +155,6 @@ tar_mode_c(struct bsdtar *bsdtar)
if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL)
bsdtar_errc(bsdtar, 1, 0, "no files or directories specified");
/* We want to catch SIGINFO and SIGUSR1. */
siginfo_init(bsdtar);
a = archive_write_new();
/* Support any format that the library supports. */
@ -219,16 +216,6 @@ tar_mode_c(struct bsdtar *bsdtar)
bsdtar_errc(bsdtar, 1, 0, archive_error_string(a));
write_archive(a, bsdtar);
if (bsdtar->option_totals) {
fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
(BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
}
archive_write_finish(a);
/* Restore old SIGINFO + SIGUSR1 handlers. */
siginfo_done(bsdtar);
}
/*
@ -247,9 +234,6 @@ tar_mode_r(struct bsdtar *bsdtar)
/* Sanity-test some arguments and the file. */
test_for_append(bsdtar);
/* We want to catch SIGINFO and SIGUSR1. */
siginfo_init(bsdtar);
format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
bsdtar->fd = open(bsdtar->filename, O_RDWR | O_CREAT, 0666);
@ -320,12 +304,6 @@ tar_mode_r(struct bsdtar *bsdtar)
write_archive(a, bsdtar); /* XXX check return val XXX */
if (bsdtar->option_totals) {
fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
(BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
}
archive_write_finish(a);
close(bsdtar->fd);
bsdtar->fd = -1;
}
@ -348,9 +326,6 @@ tar_mode_u(struct bsdtar *bsdtar)
/* Sanity-test some arguments and the file. */
test_for_append(bsdtar);
/* We want to catch SIGINFO and SIGUSR1. */
siginfo_init(bsdtar);
bsdtar->fd = open(bsdtar->filename, O_RDWR);
if (bsdtar->fd < 0)
bsdtar_errc(bsdtar, 1, errno,
@ -409,12 +384,6 @@ tar_mode_u(struct bsdtar *bsdtar)
write_archive(a, bsdtar);
if (bsdtar->option_totals) {
fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
(BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
}
archive_write_finish(a);
close(bsdtar->fd);
bsdtar->fd = -1;
@ -437,6 +406,9 @@ write_archive(struct archive *a, struct bsdtar *bsdtar)
const char *arg;
struct archive_entry *entry, *sparse_entry;
/* We want to catch SIGINFO and SIGUSR1. */
siginfo_init(bsdtar);
/* Allocate a buffer for file data. */
if ((bsdtar->buff = malloc(FILEDATABUFLEN)) == NULL)
bsdtar_errc(bsdtar, 1, 0, "cannot allocate memory");
@ -460,7 +432,7 @@ write_archive(struct archive *a, struct bsdtar *bsdtar)
bsdtar_warnc(bsdtar, 1, 0,
"Missing argument for -C");
bsdtar->return_value = 1;
return;
goto cleanup;
}
}
set_chdir(bsdtar, arg);
@ -492,8 +464,19 @@ write_archive(struct archive *a, struct bsdtar *bsdtar)
bsdtar->return_value = 1;
}
cleanup:
/* Free file data buffer. */
free(bsdtar->buff);
if (bsdtar->option_totals) {
fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
(BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
}
archive_write_finish(a);
/* Restore old SIGINFO + SIGUSR1 handlers. */
siginfo_done(bsdtar);
}
/*