From 39f5bfd26ab10a754a566fcd238ca2cac5916f91 Mon Sep 17 00:00:00 2001 From: "thibault.ferrante" Date: Thu, 7 Jan 2021 17:21:36 +0100 Subject: database: Propagate errors when loading an APKINDEX In case of failure when loading an APKINDEX, no errors are propagated to the user which may uncorrectly interpret the current problem. --- src/apk.c | 5 +++-- src/apk_defines.h | 1 + src/apk_io.h | 6 +++--- src/database.c | 13 +++++++------ src/io.c | 18 +++++++++++++----- src/io_gunzip.c | 6 ++++-- src/io_url.c | 4 +++- src/print.c | 2 ++ 8 files changed, 36 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/apk.c b/src/apk.c index 6b3e700..ee51c79 100644 --- a/src/apk.c +++ b/src/apk.c @@ -547,8 +547,9 @@ int main(int argc, char **argv) name = spec; } - if (apk_db_index_read(&db, apk_istream_from_file(AT_FDCWD, name.ptr), repo) != 0) { - apk_error("Failed to open repository: " BLOB_FMT, BLOB_PRINTF(name)); + r = apk_db_index_read(&db, apk_istream_from_file(AT_FDCWD, name.ptr), repo); + if (r != 0) { + apk_error("Failed to open repository " BLOB_FMT " : %s", BLOB_PRINTF(name), apk_error_str(r)); goto err; } diff --git a/src/apk_defines.h b/src/apk_defines.h index 505b212..8e46afa 100644 --- a/src/apk_defines.h +++ b/src/apk_defines.h @@ -34,6 +34,7 @@ #define EAPKBADURL 1024 #define EAPKSTALEINDEX 1025 +#define EAPKFORMAT 1026 static inline void *ERR_PTR(long error) { return (void*) error; } static inline void *ERR_CAST(const void *ptr) { return (void*) ptr; } diff --git a/src/apk_io.h b/src/apk_io.h index 18b8ecc..309a508 100644 --- a/src/apk_io.h +++ b/src/apk_io.h @@ -60,7 +60,7 @@ struct apk_ostream; struct apk_istream_ops { void (*get_meta)(struct apk_istream *is, struct apk_file_meta *meta); ssize_t (*read)(struct apk_istream *is, void *ptr, size_t size); - void (*close)(struct apk_istream *is); + int (*close)(struct apk_istream *is); }; #define APK_ISTREAM_SINGLE_READ 0x0001 @@ -106,9 +106,9 @@ static inline void apk_istream_get_meta(struct apk_istream *is, struct apk_file_ { is->ops->get_meta(is, meta); } -static inline void apk_istream_close(struct apk_istream *is) +static inline int apk_istream_close(struct apk_istream *is) { - is->ops->close(is); + return is->ops->close(is); } #define APK_MPART_DATA 1 /* data processed so far */ diff --git a/src/database.c b/src/database.c index bbf072e..b21737a 100644 --- a/src/database.c +++ b/src/database.c @@ -895,17 +895,18 @@ int apk_db_index_read(struct apk_database *db, struct apk_istream *is, int repo) } if (APK_BLOB_IS_NULL(l)) goto bad_entry; } - apk_istream_close(is); - return 0; + + return apk_istream_close(is); old_apk_tools: /* Installed db should not have unsupported fields */ apk_error("This apk-tools is too old to handle installed packages"); + is->err = -EAPKFORMAT; goto err; bad_entry: apk_error("FDB format error (line %d, entry '%c')", lineno, field); + is->err = -EAPKFORMAT; err: - apk_istream_close(is); - return -1; + return apk_istream_close(is); } static void apk_blob_push_db_acl(apk_blob_t *b, char field, struct apk_db_acl *acl) @@ -2192,10 +2193,10 @@ static int load_apkindex(void *sctx, const struct apk_file_info *fi, repo->description = apk_blob_from_istream(is, fi->size); } else if (strcmp(fi->name, "APKINDEX") == 0) { ctx->found = 1; - apk_db_index_read(ctx->db, is, ctx->repo); + r = apk_db_index_read(ctx->db, is, ctx->repo); } - return 0; + return r; } static int load_index(struct apk_database *db, struct apk_istream *is, diff --git a/src/io.c b/src/io.c index 9643218..dc8919c 100644 --- a/src/io.c +++ b/src/io.c @@ -207,14 +207,16 @@ static ssize_t segment_read(struct apk_istream *is, void *ptr, size_t size) return r; } -static void segment_close(struct apk_istream *is) +static int segment_close(struct apk_istream *is) { + int r = is->err; struct apk_segment_istream *sis = container_of(is, struct apk_segment_istream, is); if (sis->bytes_left) { apk_istream_read(sis->pis, NULL, sis->bytes_left); sis->bytes_left = 0; } + return r < 0 ? r : 0; } static const struct apk_istream_ops segment_istream_ops = { @@ -283,8 +285,9 @@ static ssize_t tee_read(struct apk_istream *is, void *ptr, size_t size) return __tee_write(tee, ptr, r); } -static void tee_close(struct apk_istream *is) +static int tee_close(struct apk_istream *is) { + int r; struct apk_tee_istream *tee = container_of(is, struct apk_tee_istream, is); struct apk_file_meta meta; @@ -293,9 +296,10 @@ static void tee_close(struct apk_istream *is) apk_file_meta_to_fd(tee->fd, &meta); } - apk_istream_close(tee->inner_is); + r = apk_istream_close(tee->inner_is); close(tee->fd); free(tee); + return r; } static const struct apk_istream_ops tee_istream_ops = { @@ -368,13 +372,15 @@ static ssize_t mmap_read(struct apk_istream *is, void *ptr, size_t size) return 0; } -static void mmap_close(struct apk_istream *is) +static int mmap_close(struct apk_istream *is) { + int r = is->err; struct apk_mmap_istream *mis = container_of(is, struct apk_mmap_istream, is); munmap(mis->is.buf, mis->is.buf_size); close(mis->fd); free(mis); + return r < 0 ? r : 0; } static const struct apk_istream_ops mmap_istream_ops = { @@ -434,12 +440,14 @@ static ssize_t fdi_read(struct apk_istream *is, void *ptr, size_t size) return r; } -static void fdi_close(struct apk_istream *is) +static int fdi_close(struct apk_istream *is) { + int r = is->err; struct apk_fd_istream *fis = container_of(is, struct apk_fd_istream, is); close(fis->fd); free(fis); + return r < 0 ? r : 0; } static const struct apk_istream_ops fd_istream_ops = { diff --git a/src/io_gunzip.c b/src/io_gunzip.c index 6faf74f..70f5b6f 100644 --- a/src/io_gunzip.c +++ b/src/io_gunzip.c @@ -118,13 +118,15 @@ ret: return size - gis->zs.avail_out; } -static void gzi_close(struct apk_istream *is) +static int gzi_close(struct apk_istream *is) { + int r; struct apk_gzip_istream *gis = container_of(is, struct apk_gzip_istream, is); inflateEnd(&gis->zs); - apk_istream_close(gis->zis); + r = apk_istream_close(gis->zis); free(gis); + return r; } static const struct apk_istream_ops gunzip_istream_ops = { diff --git a/src/io_url.c b/src/io_url.c index 93728aa..3f6a6b6 100644 --- a/src/io_url.c +++ b/src/io_url.c @@ -85,12 +85,14 @@ static ssize_t fetch_read(struct apk_istream *is, void *ptr, size_t size) return r; } -static void fetch_close(struct apk_istream *is) +static int fetch_close(struct apk_istream *is) { + int r = is->err; struct apk_fetch_istream *fis = container_of(is, struct apk_fetch_istream, is); fetchIO_close(fis->fetchIO); free(fis); + return r < 0 ? r : 0; } static const struct apk_istream_ops fetch_istream_ops = { diff --git a/src/print.c b/src/print.c index 38dad1c..78516ee 100644 --- a/src/print.c +++ b/src/print.c @@ -179,6 +179,8 @@ const char *apk_error_str(int error) return "invalid URL (check your repositories file)"; case EAPKSTALEINDEX: return "package mentioned in index not found (try 'apk update')"; + case EAPKFORMAT: + return "package file format error"; default: return strerror(error); } -- cgit v1.2.3-60-g2f50