diff options
-rw-r--r-- | src/add.c | 6 | ||||
-rw-r--r-- | src/apk.c | 18 | ||||
-rw-r--r-- | src/apk_defines.h | 1 | ||||
-rw-r--r-- | src/apk_io.h | 4 | ||||
-rw-r--r-- | src/apk_package.h | 4 | ||||
-rw-r--r-- | src/archive.c | 21 | ||||
-rw-r--r-- | src/database.c | 5 | ||||
-rw-r--r-- | src/gunzip.c | 24 | ||||
-rw-r--r-- | src/index.c | 2 | ||||
-rw-r--r-- | src/io.c | 40 | ||||
-rw-r--r-- | src/package.c | 29 |
11 files changed, 92 insertions, 62 deletions
@@ -120,10 +120,10 @@ static int add_main(void *ctx, int argc, char **argv) struct apk_sign_ctx sctx; apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL); - pkg = apk_pkg_read(&db, argv[i], &sctx); + r = apk_pkg_read(&db, argv[i], &sctx, &pkg); apk_sign_ctx_free(&sctx); - if (pkg == NULL) { - apk_error("Unable to read '%s'", argv[i]); + if (r != 0) { + apk_error("%s: %s", argv[i], apk_error_str(r)); goto err; } @@ -50,6 +50,24 @@ static struct apk_option generic_options[] = { required_argument, "TIME" }, }; +const char *apk_error_str(int error) +{ + if (error < 0) + error = -error; + switch (error) { + case ENOKEY: + return "UNTRUSTED signature"; + case EKEYREJECTED: + return "BAD signature"; + case EIO: + return "IO ERROR"; + case EBADMSG: + return "BAD archive"; + default: + return strerror(error); + } +} + void apk_log(const char *prefix, const char *format, ...) { va_list va; diff --git a/src/apk_defines.h b/src/apk_defines.h index d1f26d2854..8cfb62b811 100644 --- a/src/apk_defines.h +++ b/src/apk_defines.h @@ -65,6 +65,7 @@ extern unsigned int apk_flags; #define apk_message(args...) do { if (apk_verbosity > 0) { apk_log(NULL, args); } } while (0) void apk_log(const char *prefix, const char *format, ...); +const char *apk_error_str(int error); static inline size_t apk_calc_installed_size(size_t size) { diff --git a/src/apk_io.h b/src/apk_io.h index fe6baacf34..25206657b5 100644 --- a/src/apk_io.h +++ b/src/apk_io.h @@ -31,7 +31,7 @@ struct apk_file_info { }; struct apk_istream { - size_t (*read)(void *stream, void *ptr, size_t size); + ssize_t (*read)(void *stream, void *ptr, size_t size); void (*close)(void *stream); }; @@ -44,7 +44,7 @@ struct apk_bstream { }; struct apk_ostream { - size_t (*write)(void *stream, const void *buf, size_t size); + ssize_t (*write)(void *stream, const void *buf, size_t size); void (*close)(void *stream); }; diff --git a/src/apk_package.h b/src/apk_package.h index 9f74deb269..be7395fcca 100644 --- a/src/apk_package.h +++ b/src/apk_package.h @@ -121,8 +121,8 @@ int apk_deps_write(struct apk_dependency_array *deps, struct apk_ostream *os); int apk_script_type(const char *name); struct apk_package *apk_pkg_new(void); -struct apk_package *apk_pkg_read(struct apk_database *db, const char *name, - struct apk_sign_ctx *ctx); +int apk_pkg_read(struct apk_database *db, const char *name, + struct apk_sign_ctx *ctx, struct apk_package **pkg); void apk_pkg_free(struct apk_package *pkg); int apk_pkg_parse_name(apk_blob_t apkname, apk_blob_t *name, apk_blob_t *version); diff --git a/src/archive.c b/src/archive.c index 2cfb3e60bc..3a430b22d7 100644 --- a/src/archive.c +++ b/src/archive.c @@ -81,27 +81,28 @@ struct apk_tar_entry_istream { struct apk_checksum *csum; }; -static size_t tar_entry_read(void *stream, void *ptr, size_t size) +static ssize_t tar_entry_read(void *stream, void *ptr, size_t size) { struct apk_tar_entry_istream *teis = container_of(stream, struct apk_tar_entry_istream, is); + ssize_t r; if (size > teis->bytes_left) size = teis->bytes_left; - size = teis->tar_is->read(teis->tar_is, ptr, size); - if (size < 0) - return -1; + r = teis->tar_is->read(teis->tar_is, ptr, size); + if (r < 0) + return r; - teis->bytes_left -= size; + teis->bytes_left -= r; if (teis->csum == NULL) - return size; + return r; - EVP_DigestUpdate(&teis->mdctx, ptr, size); + EVP_DigestUpdate(&teis->mdctx, ptr, r); if (teis->bytes_left == 0) { teis->csum->type = EVP_MD_CTX_size(&teis->mdctx); EVP_DigestFinal_ex(&teis->mdctx, teis->csum->data, NULL); } - return size; + return r; } static void tar_entry_close(void *stream) @@ -221,13 +222,13 @@ int apk_tar_parse(struct apk_istream *is, apk_archive_entry_parser parser, if (r == 512) { while ((r = is->read(is, &buf, 512)) == 512) { if (buf.name[0] != 0) - return -1; + return -EBADMSG; } } /* Check that there was no partial record */ if (r > 0) - r = -1; + r = -EBADMSG; return r; diff --git a/src/database.c b/src/database.c index 6138c5c91a..42de1b6921 100644 --- a/src/database.c +++ b/src/database.c @@ -1582,8 +1582,9 @@ static int apk_db_unpack_pkg(struct apk_database *db, tar->close(tar); if (r != 0) { - apk_error("%s-%s: package integrity check failed", - newpkg->name->name, newpkg->version); + apk_error("%s-%s: %s", + newpkg->name->name, newpkg->version, + apk_error_str(r)); goto err; } r = apk_db_run_pending_script(&ctx); diff --git a/src/gunzip.c b/src/gunzip.c index f14b3963df..f00d1484bb 100644 --- a/src/gunzip.c +++ b/src/gunzip.c @@ -8,6 +8,7 @@ * by the Free Software Foundation. See http://www.gnu.org/ for details. */ +#include <errno.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h> @@ -28,7 +29,7 @@ struct apk_gzip_istream { void *cbprev; }; -static size_t gzi_read(void *stream, void *ptr, size_t size) +static ssize_t gzi_read(void *stream, void *ptr, size_t size) { struct apk_gzip_istream *gis = container_of(stream, struct apk_gzip_istream, is); @@ -61,7 +62,7 @@ static size_t gzi_read(void *stream, void *ptr, size_t size) gis->zs.avail_in = blob.len; gis->zs.next_in = (void *) gis->cbprev; if (gis->zs.avail_in < 0) { - gis->err = -1; + gis->err = -EIO; goto ret; } else if (gis->zs.avail_in == 0) { gis->err = 1; @@ -69,7 +70,7 @@ static size_t gzi_read(void *stream, void *ptr, size_t size) r = gis->cb(gis->cbctx, APK_MPART_END, APK_BLOB_NULL); if (r > 0) - r = -1; + r = -ECANCELED; if (r != 0) gis->err = r; } @@ -85,9 +86,9 @@ static size_t gzi_read(void *stream, void *ptr, size_t size) r = gis->cb(gis->cbctx, APK_MPART_BOUNDARY, APK_BLOB_PTR_LEN(gis->cbprev, (void *)gis->zs.next_in - gis->cbprev)); + if (r > 0) + r = -ECANCELED; if (r != 0) { - if (r > 0) - r = -1; gis->err = r; goto ret; } @@ -95,12 +96,12 @@ static size_t gzi_read(void *stream, void *ptr, size_t size) } inflateEnd(&gis->zs); if (inflateInit2(&gis->zs, 15+32) != Z_OK) - return -1; + return -ENOMEM; break; case Z_OK: break; default: - gis->err = -1; + gis->err = -EIO; break; } } @@ -159,12 +160,11 @@ struct apk_gzip_ostream { z_stream zs; }; -static size_t gzo_write(void *stream, const void *ptr, size_t size) +static ssize_t gzo_write(void *stream, const void *ptr, size_t size) { struct apk_gzip_ostream *gos = (struct apk_gzip_ostream *) stream; unsigned char buffer[1024]; - size_t have; - int r; + ssize_t have, r; gos->zs.avail_in = size; gos->zs.next_in = (void *) ptr; @@ -173,12 +173,12 @@ static size_t gzo_write(void *stream, const void *ptr, size_t size) gos->zs.next_out = buffer; r = deflate(&gos->zs, Z_NO_FLUSH); if (r == Z_STREAM_ERROR) - return -1; + return -EIO; have = sizeof(buffer) - gos->zs.avail_out; if (have != 0) { r = gos->output->write(gos->output, buffer, have); if (r != have) - return -1; + return -EIO; } } diff --git a/src/index.c b/src/index.c index 608dea861d..cd0c0b5657 100644 --- a/src/index.c +++ b/src/index.c @@ -155,7 +155,7 @@ static int index_main(void *ctx, int argc, char **argv) if (!found) { struct apk_sign_ctx sctx; apk_sign_ctx_init(&sctx, ictx->method, NULL); - if (apk_pkg_read(&db, argv[i], &sctx) != NULL) + if (apk_pkg_read(&db, argv[i], &sctx, NULL) == 0) newpkgs++; apk_sign_ctx_free(&sctx); } @@ -9,6 +9,7 @@ * by the Free Software Foundation. See http://www.gnu.org/ for details. */ +#include <errno.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h> @@ -23,22 +24,22 @@ struct apk_fd_istream { int fd; }; -static size_t fdi_read(void *stream, void *ptr, size_t size) +static ssize_t fdi_read(void *stream, void *ptr, size_t size) { struct apk_fd_istream *fis = container_of(stream, struct apk_fd_istream, is); - size_t i = 0, r; + ssize_t i = 0, r; if (ptr == NULL) { if (lseek(fis->fd, size, SEEK_CUR) < 0) - return -1; + return -errno; return size; } while (i < size) { r = read(fis->fd, ptr + i, size - i); if (r < 0) - return r; + return -errno; if (r == 0) return i; i += r; @@ -121,7 +122,7 @@ size_t apk_istream_splice(void *stream, int fd, size_t size, buf = malloc(bufsz); if (buf == NULL) - return -1; + return -ENOMEM; while (done < size) { if (done != 0 && cb != NULL) @@ -134,7 +135,8 @@ size_t apk_istream_splice(void *stream, int fd, size_t size, if (r < 0) goto err; if (write(fd, buf, r) != r) { - r = -1; + if (r < 0) + r = -errno; goto err; } done += r; @@ -447,7 +449,7 @@ int apk_file_get_info(const char *filename, int checksum, struct apk_file_info * struct apk_bstream *bs; if (stat(filename, &st) != 0) - return -1; + return -errno; *fi = (struct apk_file_info) { .size = st.st_size, @@ -492,14 +494,14 @@ struct apk_fd_ostream { char buffer[1024]; }; -static size_t safe_write(int fd, const void *ptr, size_t size) +static ssize_t safe_write(int fd, const void *ptr, size_t size) { - size_t i = 0, r; + ssize_t i = 0, r; while (i < size) { r = write(fd, ptr + i, size - i); if (r < 0) - return r; + return -errno; if (r == 0) return i; i += r; @@ -508,26 +510,30 @@ static size_t safe_write(int fd, const void *ptr, size_t size) return i; } -static int fdo_flush(struct apk_fd_ostream *fos) +static ssize_t fdo_flush(struct apk_fd_ostream *fos) { + ssize_t r; + if (fos->bytes == 0) return 0; - if (safe_write(fos->fd, fos->buffer, fos->bytes) != fos->bytes) - return -1; + if ((r = safe_write(fos->fd, fos->buffer, fos->bytes)) != fos->bytes) + return r; fos->bytes = 0; return 0; } -static size_t fdo_write(void *stream, const void *ptr, size_t size) +static ssize_t fdo_write(void *stream, const void *ptr, size_t size) { struct apk_fd_ostream *fos = container_of(stream, struct apk_fd_ostream, os); + ssize_t r; if (size + fos->bytes >= sizeof(fos->buffer)) { - if (fdo_flush(fos)) - return -1; + r = fdo_flush(fos); + if (r != 0) + return r; if (size >= sizeof(fos->buffer) / 2) return safe_write(fos->fd, ptr, size); } @@ -586,7 +592,7 @@ struct apk_counter_ostream { off_t *counter; }; -static size_t co_write(void *stream, const void *ptr, size_t size) +static ssize_t co_write(void *stream, const void *ptr, size_t size) { struct apk_counter_ostream *cos = container_of(stream, struct apk_counter_ostream, os); diff --git a/src/package.c b/src/package.c index 5bf4478d4e..e480c6f77f 100644 --- a/src/package.c +++ b/src/package.c @@ -693,8 +693,8 @@ static int read_info_entry(void *ctx, const struct apk_file_info *ae, return 0; } -struct apk_package *apk_pkg_read(struct apk_database *db, const char *file, - struct apk_sign_ctx *sctx) +int apk_pkg_read(struct apk_database *db, const char *file, + struct apk_sign_ctx *sctx, struct apk_package **pkg) { struct read_info_ctx ctx; struct apk_file_info fi; @@ -704,16 +704,17 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file, int r; if (realpath(file, realfile) < 0) - return NULL; - if (apk_file_get_info(realfile, APK_CHECKSUM_NONE, &fi) < 0) - return NULL; + return -errno; + r = apk_file_get_info(realfile, APK_CHECKSUM_NONE, &fi); + if (r != 0) + return r; memset(&ctx, 0, sizeof(ctx)); ctx.sctx = sctx; ctx.pkg = apk_pkg_new(); + r = -ENOMEM; if (ctx.pkg == NULL) - return NULL; - + goto err; bs = apk_bstream_from_file(realfile); if (bs == NULL) goto err; @@ -727,11 +728,10 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file, tar->close(tar); if (r < 0 && r != -ECANCELED) goto err; - if (ctx.pkg->name == NULL) - goto err; - if (sctx->action == APK_SIGN_VERIFY && !sctx->data_verified && - !(apk_flags & APK_FORCE)) + if (ctx.pkg->name == NULL) { + r = -EBADMSG; goto err; + } if (sctx->action != APK_SIGN_VERIFY) ctx.pkg->csum = sctx->identity; @@ -744,10 +744,13 @@ struct apk_package *apk_pkg_read(struct apk_database *db, const char *file, } ctx.pkg->filename = strdup(realfile); - return apk_db_pkg_add(db, ctx.pkg); + ctx.pkg = apk_db_pkg_add(db, ctx.pkg); + if (pkg != NULL) + *pkg = ctx.pkg; + r = 0; err: apk_pkg_free(ctx.pkg); - return NULL; + return r; } void apk_pkg_free(struct apk_package *pkg) |