extract: fix handling of error of regular file extraction

fix the error checking, allow --force-overwrite to work and
do not delete existing file in case of error
cute-signatures
Timo Teräs 2021-07-26 21:08:09 +03:00
parent ca1d975e5e
commit 2d4e88aeb1
2 changed files with 16 additions and 3 deletions

View File

@ -215,6 +215,7 @@ static int apk_extract_file(struct extract_ctx *ctx, off_t sz, struct apk_istrea
r = apk_archive_entry_extract(
ctx->root_fd, &fi, 0, 0, is, 0, 0, &dctx,
ctx->extract_flags, out);
if (r < 0) return r;
}
apk_digest_ctx_final(&dctx, &d);
apk_digest_ctx_free(&dctx);
@ -344,7 +345,7 @@ static int extract_main(void *pctx, struct apk_ctx *ac, struct apk_string_array
int r = 0;
ctx->ac = ac;
ctx->extract_flags |= APK_EXTRACTF_NO_OVERWRITE;
if (!(ac->force & APK_FORCE_OVERWRITE)) ctx->extract_flags |= APK_EXTRACTF_NO_OVERWRITE;
if (!ctx->destination) ctx->destination = ".";
ctx->root_fd = openat(AT_FDCWD, ctx->destination, O_RDONLY);
if (ctx->root_fd < 0) {

View File

@ -355,10 +355,22 @@ int apk_archive_entry_extract(int atfd, const struct apk_file_info *ae,
case S_IFREG:
if (ae->link_target == NULL) {
int flags = O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_EXCL;
struct apk_ostream *os = apk_ostream_to_fd(openat(atfd, fn, flags, ae->mode & 07777));
int fd = openat(atfd, fn, flags, ae->mode & 07777);
if (fd < 0) {
ret = -errno;
break;
}
struct apk_ostream *os = apk_ostream_to_fd(fd);
if (IS_ERR(os)) {
ret = PTR_ERR(os);
break;
}
apk_stream_copy(is, os, ae->size, cb, cb_ctx, dctx);
r = apk_ostream_close(os);
if (r < 0) ret = r;
if (r < 0) {
unlinkat(atfd, fn, 0);
ret = r;
}
} else {
r = linkat(atfd, link_target ?: ae->link_target, atfd, fn, 0);
if (r < 0) ret = -errno;