2020-02-14 11:53:29 +00:00
|
|
|
/* io_url.c - Alpine Package Keeper (APK)
|
2008-11-28 13:03:27 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2008-11-28 13:03:27 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2020-04-22 13:33:41 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
2008-11-28 13:03:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2009-07-31 13:08:09 +00:00
|
|
|
#include <errno.h>
|
2008-11-28 13:03:27 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2009-04-16 17:05:22 +00:00
|
|
|
#include <sys/wait.h>
|
2008-11-28 13:03:27 +00:00
|
|
|
|
2014-10-08 07:10:45 +00:00
|
|
|
#include <fetch.h>
|
|
|
|
|
2008-11-28 13:03:27 +00:00
|
|
|
#include "apk_io.h"
|
|
|
|
|
2009-04-16 17:05:22 +00:00
|
|
|
const char *apk_url_local_file(const char *url)
|
2008-11-28 13:03:27 +00:00
|
|
|
{
|
|
|
|
if (strncmp(url, "file:", 5) == 0)
|
|
|
|
return &url[5];
|
|
|
|
|
|
|
|
if (strncmp(url, "http:", 5) != 0 &&
|
|
|
|
strncmp(url, "https:", 6) != 0 &&
|
|
|
|
strncmp(url, "ftp:", 4) != 0)
|
|
|
|
return url;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-08 07:10:45 +00:00
|
|
|
struct apk_fetch_istream {
|
|
|
|
struct apk_istream is;
|
|
|
|
fetchIO *fetchIO;
|
2015-11-09 10:47:23 +00:00
|
|
|
struct url_stat urlstat;
|
2014-10-08 07:10:45 +00:00
|
|
|
};
|
|
|
|
|
2015-03-10 11:04:14 +00:00
|
|
|
static int fetch_maperror(int ec)
|
|
|
|
{
|
2015-04-13 06:40:36 +00:00
|
|
|
static const signed short map[] = {
|
2015-03-10 11:04:14 +00:00
|
|
|
[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,
|
2015-04-24 06:31:28 +00:00
|
|
|
[FETCH_URL] = -EAPKBADURL,
|
2015-03-10 11:04:14 +00:00
|
|
|
[FETCH_UNCHANGED] = -EALREADY,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ec < 0 || ec >= ARRAY_SIZE(map) || !map[ec]) return -EIO;
|
|
|
|
return map[ec];
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static void fetch_get_meta(struct apk_istream *is, struct apk_file_meta *meta)
|
2015-11-09 10:47:23 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fetch_istream *fis = container_of(is, struct apk_fetch_istream, is);
|
2015-11-09 10:47:23 +00:00
|
|
|
|
|
|
|
*meta = (struct apk_file_meta) {
|
|
|
|
.atime = fis->urlstat.atime,
|
|
|
|
.mtime = fis->urlstat.mtime,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static ssize_t fetch_read(struct apk_istream *is, void *ptr, size_t size)
|
2010-12-09 08:47:09 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fetch_istream *fis = container_of(is, struct apk_fetch_istream, is);
|
2020-01-10 09:02:48 +00:00
|
|
|
ssize_t r;
|
2014-10-08 07:10:45 +00:00
|
|
|
|
2020-01-10 09:02:48 +00:00
|
|
|
r = fetchIO_read(fis->fetchIO, ptr, size);
|
|
|
|
if (r < 0) return -EIO;
|
|
|
|
return r;
|
2010-12-09 08:47:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 08:00:29 +00:00
|
|
|
static void fetch_close(struct apk_istream *is)
|
2008-11-28 13:03:27 +00:00
|
|
|
{
|
2019-12-18 08:00:29 +00:00
|
|
|
struct apk_fetch_istream *fis = container_of(is, struct apk_fetch_istream, is);
|
2008-11-28 13:03:27 +00:00
|
|
|
|
2014-10-08 07:10:45 +00:00
|
|
|
fetchIO_close(fis->fetchIO);
|
|
|
|
free(fis);
|
|
|
|
}
|
2008-11-28 13:03:27 +00:00
|
|
|
|
2017-06-21 13:07:58 +00:00
|
|
|
static const struct apk_istream_ops fetch_istream_ops = {
|
|
|
|
.get_meta = fetch_get_meta,
|
|
|
|
.read = fetch_read,
|
|
|
|
.close = fetch_close,
|
|
|
|
};
|
|
|
|
|
2014-10-08 08:13:21 +00:00
|
|
|
static struct apk_istream *apk_istream_fetch(const char *url, time_t since)
|
2014-10-08 07:10:45 +00:00
|
|
|
{
|
2018-05-08 18:39:16 +00:00
|
|
|
struct apk_fetch_istream *fis = NULL;
|
2014-10-08 08:13:21 +00:00
|
|
|
struct url *u;
|
2015-11-09 10:47:23 +00:00
|
|
|
fetchIO *io = NULL;
|
2018-05-08 18:39:16 +00:00
|
|
|
int rc = -EIO;
|
2008-11-28 13:03:27 +00:00
|
|
|
|
2014-10-08 08:13:21 +00:00
|
|
|
u = fetchParseURL(url);
|
2018-05-08 18:39:16 +00:00
|
|
|
if (!u) {
|
|
|
|
rc = -EAPKBADURL;
|
|
|
|
goto err;
|
|
|
|
}
|
2020-01-10 09:02:48 +00:00
|
|
|
fis = malloc(sizeof *fis + apk_io_bufsize);
|
2018-05-08 18:39:16 +00:00
|
|
|
if (!fis) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
2015-11-09 10:47:23 +00:00
|
|
|
|
|
|
|
u->last_modified = since;
|
2018-01-03 13:20:12 +00:00
|
|
|
io = fetchXGet(u, &fis->urlstat, (apk_force & APK_FORCE_REFRESH) ? "Ci" : "i");
|
2015-11-09 10:47:23 +00:00
|
|
|
if (!io) {
|
|
|
|
rc = fetch_maperror(fetchLastErrCode);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-10-08 08:13:21 +00:00
|
|
|
|
2014-10-08 07:10:45 +00:00
|
|
|
*fis = (struct apk_fetch_istream) {
|
2017-06-21 13:07:58 +00:00
|
|
|
.is.ops = &fetch_istream_ops,
|
2020-01-10 09:02:48 +00:00
|
|
|
.is.buf = (uint8_t*)(fis+1),
|
|
|
|
.is.buf_size = apk_io_bufsize,
|
2014-10-08 07:10:45 +00:00
|
|
|
.fetchIO = io,
|
2015-11-09 10:47:23 +00:00
|
|
|
.urlstat = fis->urlstat,
|
2014-10-08 07:10:45 +00:00
|
|
|
};
|
2015-11-09 10:47:23 +00:00
|
|
|
fetchFreeURL(u);
|
2010-12-09 08:47:09 +00:00
|
|
|
|
2014-10-08 07:10:45 +00:00
|
|
|
return &fis->is;
|
2014-10-08 08:13:21 +00:00
|
|
|
err:
|
2015-11-09 10:47:23 +00:00
|
|
|
if (u) fetchFreeURL(u);
|
2014-10-08 08:13:21 +00:00
|
|
|
if (io) fetchIO_close(io);
|
2015-11-09 10:47:23 +00:00
|
|
|
if (fis) free(fis);
|
|
|
|
return ERR_PTR(rc);
|
2008-11-28 13:03:27 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 08:13:21 +00:00
|
|
|
struct apk_istream *apk_istream_from_fd_url_if_modified(int atfd, const char *url, time_t since)
|
2008-11-28 13:03:27 +00:00
|
|
|
{
|
2013-06-18 07:52:23 +00:00
|
|
|
if (apk_url_local_file(url) != NULL)
|
2013-06-17 10:51:49 +00:00
|
|
|
return apk_istream_from_file(atfd, apk_url_local_file(url));
|
2014-10-08 08:13:21 +00:00
|
|
|
return apk_istream_fetch(url, since);
|
2008-11-28 13:03:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-15 10:55:26 +00:00
|
|
|
struct apk_istream *apk_istream_from_url_gz(const char *file)
|
|
|
|
{
|
2020-01-11 07:16:38 +00:00
|
|
|
return apk_istream_gunzip(apk_istream_from_url(file));
|
2008-11-28 13:03:27 +00:00
|
|
|
}
|