summaryrefslogtreecommitdiff
path: root/src/url.c
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 /src/url.c
parent417755cb2e16dee1cb674cbe2b2942c156e0b5da (diff)
downloadapk-tools-2a6896b2b4809849441756046ee7d8ad34abab34.tar.gz
apk-tools-2a6896b2b4809849441756046ee7d8ad34abab34.tar.bz2
apk-tools-2a6896b2b4809849441756046ee7d8ad34abab34.tar.xz
apk-tools-2a6896b2b4809849441756046ee7d8ad34abab34.zip
rework error handling for read streams
Diffstat (limited to 'src/url.c')
-rw-r--r--src/url.c37
1 files changed, 31 insertions, 6 deletions
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)