summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2015-03-10 13:04:14 +0200
committerTimo Teräs <timo.teras@iki.fi>2015-03-10 13:15:31 +0200
commit2a6896b2b4809849441756046ee7d8ad34abab34 (patch)
treee42c99fc0a6bb2fa35fcce241776d9208a8c0471
parent417755cb2e16dee1cb674cbe2b2942c156e0b5da (diff)
downloadapk-tools-2a6896b2b4809849441756046ee7d8ad34abab34.tar.bz2
apk-tools-2a6896b2b4809849441756046ee7d8ad34abab34.tar.xz
rework error handling for read streams
-rw-r--r--src/apk.c4
-rw-r--r--src/database.c35
-rw-r--r--src/fetch.c4
-rw-r--r--src/gunzip.c7
-rw-r--r--src/io.c23
-rw-r--r--src/package.c4
-rw-r--r--src/url.c37
-rw-r--r--src/verify.c2
8 files changed, 74 insertions, 42 deletions
diff --git a/src/apk.c b/src/apk.c
index 17b8b69..8632587 100644
--- a/src/apk.c
+++ b/src/apk.c
@@ -524,7 +524,7 @@ int main(int argc, char **argv)
}
if (test_installed_db != NULL) {
struct apk_bstream *bs = apk_bstream_from_file(AT_FDCWD, test_installed_db);
- if (bs != NULL) {
+ if (!IS_ERR_OR_NULL(bs)) {
apk_db_index_read(&db, bs, -1);
bs->close(bs, NULL);
}
@@ -548,7 +548,7 @@ int main(int argc, char **argv)
}
bs = apk_bstream_from_file(AT_FDCWD, name.ptr);
- if (bs == NULL) {
+ if (IS_ERR_OR_NULL(bs)) {
apk_error("Failed to open repository: " BLOB_FMT, BLOB_PRINTF(name));
goto err;
}
diff --git a/src/database.c b/src/database.c
index 25cf6cd..bbf5a9d 100644
--- a/src/database.c
+++ b/src/database.c
@@ -698,6 +698,8 @@ int apk_db_read_overlay(struct apk_database *db, struct apk_bstream *bs)
struct apk_installed_package *ipkg;
apk_blob_t token = APK_BLOB_STR("\n"), line, bdir, bfile;
+ if (IS_ERR_OR_NULL(bs)) return -1;
+
pkg = apk_pkg_new();
if (pkg == NULL)
return -1;
@@ -1118,15 +1120,14 @@ static int apk_db_read_state(struct apk_database *db, int flags)
if (!(flags & APK_OPENF_NO_INSTALLED)) {
bs = apk_bstream_from_file(db->root_fd, apk_installed_file);
- if (bs != NULL) {
+ if (!IS_ERR_OR_NULL(bs)) {
r = apk_db_index_read(db, bs, -1);
bs->close(bs, NULL);
- if (r != 0)
- return -1;
+ if (r != 0) return -1;
}
bs = apk_bstream_from_file(db->root_fd, apk_triggers_file);
- if (bs != NULL) {
+ if (!IS_ERR_OR_NULL(bs)) {
apk_db_triggers_read(db, bs);
bs->close(bs, NULL);
}
@@ -1134,7 +1135,7 @@ static int apk_db_read_state(struct apk_database *db, int flags)
if (!(flags & APK_OPENF_NO_SCRIPTS)) {
is = apk_istream_from_file(db->root_fd, apk_scripts_file);
- if (is != NULL) {
+ if (!IS_ERR_OR_NULL(is)) {
apk_tar_parse(is, apk_read_script_archive_entry, db,
FALSE, &db->id_cache);
is->close(is);
@@ -1580,7 +1581,7 @@ int apk_db_open(struct apk_database *db, struct apk_db_options *dbopts)
if (!(dbopts->open_flags & APK_OPENF_NO_INSTALLED_REPO)) {
if (apk_db_cache_active(db)) {
bs = apk_bstream_from_file(db->cache_fd, "installed");
- if (bs != NULL) {
+ if (!IS_ERR_OR_NULL(bs)) {
apk_db_index_read(db, bs, -2);
bs->close(bs, NULL);
}
@@ -2043,8 +2044,10 @@ static int load_apkindex(void *sctx, const struct apk_file_info *fi,
} else if (strcmp(fi->name, "APKINDEX") == 0) {
ctx->found = 1;
bs = apk_bstream_from_istream(is);
- apk_db_index_read(ctx->db, bs, ctx->repo);
- bs->close(bs, NULL);
+ if (!IS_ERR_OR_NULL(bs)) {
+ apk_db_index_read(ctx->db, bs, ctx->repo);
+ bs->close(bs, NULL);
+ }
}
return 0;
@@ -2055,6 +2058,8 @@ static int load_index(struct apk_database *db, struct apk_bstream *bs,
{
int r = 0;
+ if (IS_ERR_OR_NULL(bs)) return bs ? PTR_ERR(bs) : -EINVAL;
+
if (targz) {
struct apk_istream *is;
struct apkindex_ctx ctx;
@@ -2072,8 +2077,10 @@ static int load_index(struct apk_database *db, struct apk_bstream *bs,
r = -ENOMSG;
} else {
bs = apk_bstream_from_istream(apk_bstream_gunzip(bs));
- apk_db_index_read(db, bs, repo);
- bs->close(bs, NULL);
+ if (!IS_ERR_OR_NULL(bs)) {
+ apk_db_index_read(db, bs, repo);
+ bs->close(bs, NULL);
+ }
}
return r;
}
@@ -2145,10 +2152,10 @@ int apk_db_add_repository(apk_database_t _db, apk_blob_t _repository)
}
if (r == 0) {
bs = apk_bstream_from_fd_url(db->cache_fd, buf);
- if (bs != NULL)
+ if (!IS_ERR_OR_NULL(bs))
r = load_index(db, bs, targz, repo_num);
else
- r = -ENOENT;
+ r = PTR_ERR(bs);
}
if (r != 0) {
@@ -2564,8 +2571,8 @@ static int apk_db_unpack_pkg(struct apk_database *db,
need_copy = FALSE;
bs = apk_bstream_from_fd_url(filefd, file);
- if (bs == NULL) {
- r = -errno;
+ if (IS_ERR_OR_NULL(bs)) {
+ r = PTR_ERR(bs);
goto err_msg;
}
if (need_copy) {
diff --git a/src/fetch.c b/src/fetch.c
index 7caf9d4..dc4571b 100644
--- a/src/fetch.c
+++ b/src/fetch.c
@@ -169,8 +169,8 @@ static int fetch_package(apk_hash_item item, void *pctx)
}
is = apk_istream_from_fd_url(urlfd, url);
- if (is == NULL) {
- r = -EIO;
+ if (IS_ERR_OR_NULL(is)) {
+ r = PTR_ERR(is) ?: -EIO;
goto err;
}
diff --git a/src/gunzip.c b/src/gunzip.c
index 45e3b3c..366094f 100644
--- a/src/gunzip.c
+++ b/src/gunzip.c
@@ -170,7 +170,7 @@ struct apk_istream *apk_bstream_gunzip_mpart(struct apk_bstream *bs,
return &gis->is;
err:
bs->close(bs, NULL);
- return NULL;
+ return ERR_PTR(-ENOMEM);
}
struct apk_gzip_ostream {
@@ -236,8 +236,7 @@ struct apk_ostream *apk_ostream_gzip(struct apk_ostream *output)
if (IS_ERR_OR_NULL(output)) return ERR_CAST(output);
gos = malloc(sizeof(struct apk_gzip_ostream));
- if (gos == NULL)
- goto err;
+ if (gos == NULL) goto err;
*gos = (struct apk_gzip_ostream) {
.os.write = gzo_write,
@@ -254,6 +253,6 @@ struct apk_ostream *apk_ostream_gzip(struct apk_ostream *output)
return &gos->os;
err:
output->close(output);
- return NULL;
+ return ERR_PTR(-ENOMEM);
}
diff --git a/src/io.c b/src/io.c
index 75cb03e..f183c2a 100644
--- a/src/io.c
+++ b/src/io.c
@@ -83,12 +83,12 @@ struct apk_istream *apk_istream_from_fd_pid(int fd, pid_t pid, int (*translate_s
{
struct apk_fd_istream *fis;
- if (fd < 0) return NULL;
+ if (fd < 0) return ERR_PTR(-EBADF);
fis = malloc(sizeof(struct apk_fd_istream));
if (fis == NULL) {
close(fd);
- return NULL;
+ return ERR_PTR(-ENOMEM);
}
*fis = (struct apk_fd_istream) {
@@ -107,7 +107,7 @@ struct apk_istream *apk_istream_from_file(int atfd, const char *file)
int fd;
fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
- if (fd < 0) return NULL;
+ if (fd < 0) return ERR_PTR(-errno);
return apk_istream_from_fd(fd);
}
@@ -273,7 +273,7 @@ struct apk_bstream *apk_bstream_from_istream(struct apk_istream *istream)
if (IS_ERR_OR_NULL(istream)) return ERR_CAST(istream);
isbs = malloc(sizeof(struct apk_istream_bstream));
- if (isbs == NULL) return NULL;
+ if (isbs == NULL) return ERR_PTR(-ENOMEM);
isbs->bs = (struct apk_bstream) {
.read = is_bs_read,
@@ -330,15 +330,15 @@ static struct apk_bstream *apk_mmap_bstream_from_fd(int fd)
struct stat st;
void *ptr;
- if (fstat(fd, &st) < 0) return NULL;
+ if (fstat(fd, &st) < 0) return ERR_PTR(-errno);
ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (ptr == MAP_FAILED) return NULL;
+ if (ptr == MAP_FAILED) return ERR_PTR(-errno);
mbs = malloc(sizeof(struct apk_mmap_bstream));
if (mbs == NULL) {
munmap(ptr, st.st_size);
- return NULL;
+ return ERR_PTR(-ENOMEM);
}
mbs->bs = (struct apk_bstream) {
@@ -358,12 +358,11 @@ struct apk_bstream *apk_bstream_from_fd_pid(int fd, pid_t pid, int (*translate_s
{
struct apk_bstream *bs;
- if (fd < 0) return NULL;
+ if (fd < 0) return ERR_PTR(-EBADF);
if (pid == 0) {
bs = apk_mmap_bstream_from_fd(fd);
- if (bs != NULL)
- return bs;
+ if (IS_ERR_OR_NULL(bs)) return ERR_CAST(bs);
}
return apk_bstream_from_istream(apk_istream_from_fd_pid(fd, pid, translate_status));
@@ -374,7 +373,7 @@ struct apk_bstream *apk_bstream_from_file(int atfd, const char *file)
int fd;
fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
- if (fd < 0) return NULL;
+ if (fd < 0) return ERR_PTR(-errno);
return apk_bstream_from_fd(fd);
}
@@ -569,7 +568,7 @@ int apk_file_get_info(int atfd, const char *filename, unsigned int flags,
}
bs = apk_bstream_from_file(atfd, filename);
- if (bs != NULL) {
+ if (!IS_ERR_OR_NULL(bs)) {
EVP_MD_CTX mdctx;
apk_blob_t blob;
diff --git a/src/package.c b/src/package.c
index 71d1023..7111968 100644
--- a/src/package.c
+++ b/src/package.c
@@ -903,8 +903,10 @@ int apk_pkg_read(struct apk_database *db, const char *file,
if (ctx.pkg == NULL)
goto err;
bs = apk_bstream_from_file(AT_FDCWD, file);
- if (bs == NULL)
+ if (IS_ERR_OR_NULL(bs)) {
+ r = PTR_ERR(bs) ?: -EIO;
goto err;
+ }
ctx.db = db;
ctx.pkg->size = fi.size;
diff --git a/src/url.c b/src/url.c
index 8ea89f4..d62a7e7 100644
--- a/src/url.c
+++ b/src/url.c
@@ -38,6 +38,34 @@ struct apk_fetch_istream {
fetchIO *fetchIO;
};
+static int fetch_maperror(int ec)
+{
+ static const char map[] = {
+ [FETCH_ABORT] = -ECONNABORTED,
+ [FETCH_AUTH] = -EACCES,
+ [FETCH_DOWN] = -ECONNREFUSED,
+ [FETCH_EXISTS] = -EEXIST,
+ [FETCH_FULL] = -ENOSPC,
+ /* [FETCH_INFO] = , */
+ [FETCH_MEMORY] = -ENOMEM,
+ [FETCH_MOVED] = -ENOENT,
+ [FETCH_NETWORK] = -ENETUNREACH,
+ /* [FETCH_OK] = , */
+ [FETCH_PROTO] = -EPROTO,
+ [FETCH_RESOLV] = -ENXIO,
+ [FETCH_SERVER] = -EREMOTEIO,
+ [FETCH_TEMP] = -EAGAIN,
+ [FETCH_TIMEOUT] = -ETIMEDOUT,
+ [FETCH_UNAVAIL] = -ENOENT,
+ [FETCH_UNKNOWN] = -EIO,
+ [FETCH_URL] = -EINVAL,
+ [FETCH_UNCHANGED] = -EALREADY,
+ };
+
+ if (ec < 0 || ec >= ARRAY_SIZE(map) || !map[ec]) return -EIO;
+ return map[ec];
+}
+
static ssize_t fetch_read(void *stream, void *ptr, size_t size)
{
struct apk_fetch_istream *fis = container_of(stream, struct apk_fetch_istream, is);
@@ -70,14 +98,11 @@ static struct apk_istream *apk_istream_fetch(const char *url, time_t since)
fetchIO *io;
u = fetchParseURL(url);
- if (!u) return NULL;
+ if (!u) return ERR_PTR(-ENOMEM);
u->last_modified = since;
io = fetchGet(u, "i");
fetchFreeURL(u);
- if (!io) {
- if (fetchLastErrCode == FETCH_UNCHANGED) return ERR_PTR(-EALREADY);
- return NULL;
- }
+ if (!io) return ERR_PTR(fetch_maperror(fetchLastErrCode));
fis = malloc(sizeof(*fis));
if (!fis) goto err;
@@ -91,7 +116,7 @@ static struct apk_istream *apk_istream_fetch(const char *url, time_t since)
return &fis->is;
err:
if (io) fetchIO_close(io);
- return NULL;
+ return ERR_PTR(-ENOMEM);
}
struct apk_istream *apk_istream_from_fd_url_if_modified(int atfd, const char *url, time_t since)
diff --git a/src/verify.c b/src/verify.c
index ee3ecd6..986e3c9 100644
--- a/src/verify.c
+++ b/src/verify.c
@@ -28,7 +28,7 @@ static int verify_main(void *ctx, struct apk_database *db, struct apk_string_arr
apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL, db->keys_fd);
is = apk_bstream_gunzip_mpart(apk_bstream_from_file(AT_FDCWD, *parg),
apk_sign_ctx_mpart_cb, &sctx);
- if (is == NULL) {
+ if (IS_ERR_OR_NULL(is)) {
if (apk_verbosity >= 1)
apk_error("%s: %s", *parg, strerror(errno));
else