2009-11-25 07:39:36 +05:30
|
|
|
/*-
|
2014-11-17 20:15:46 +05:30
|
|
|
* Copyright (c) 2009-2014 Juan Romero Pardines.
|
2009-11-25 07:39:36 +05:30
|
|
|
* Copyright (c) 2008, 2009 Joerg Sonnenberger <joerg (at) NetBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*-
|
|
|
|
* From: $NetBSD: pkg_io.c,v 1.9 2009/08/16 21:10:15 joerg Exp $
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-11-25 07:39:36 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
2011-06-01 13:07:32 +05:30
|
|
|
* @file lib/plist_fetch.c
|
|
|
|
* @brief Package URL metadata files handling
|
|
|
|
* @defgroup plist_fetch Package URL metadata files handling
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
|
|
|
|
2009-11-25 07:39:36 +05:30
|
|
|
struct fetch_archive {
|
|
|
|
struct url *url;
|
|
|
|
struct fetchIO *fetch;
|
|
|
|
char buffer[32768];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
fetch_archive_open(struct archive *a UNUSED, void *client_data)
|
2009-11-25 07:39:36 +05:30
|
|
|
{
|
|
|
|
struct fetch_archive *f = client_data;
|
|
|
|
|
|
|
|
f->fetch = fetchGet(f->url, NULL);
|
2014-12-22 22:48:20 +05:30
|
|
|
|
2009-11-25 07:39:36 +05:30
|
|
|
if (f->fetch == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-10 13:43:28 +05:30
|
|
|
static ssize_t
|
2018-07-18 07:54:26 +05:30
|
|
|
fetch_archive_read(struct archive *a UNUSED, void *client_data, const void **buf)
|
2009-11-25 07:39:36 +05:30
|
|
|
{
|
|
|
|
struct fetch_archive *f = client_data;
|
|
|
|
|
|
|
|
*buf = f->buffer;
|
2014-12-23 15:22:54 +05:30
|
|
|
return fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
|
2009-11-25 07:39:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
fetch_archive_close(struct archive *a UNUSED, void *client_data)
|
2009-11-25 07:39:36 +05:30
|
|
|
{
|
|
|
|
struct fetch_archive *f = client_data;
|
|
|
|
|
2012-06-14 11:52:11 +05:30
|
|
|
if (f->fetch != NULL)
|
2009-11-25 07:39:36 +05:30
|
|
|
fetchIO_close(f->fetch);
|
|
|
|
free(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct archive *
|
|
|
|
open_archive_by_url(struct url *url)
|
|
|
|
{
|
|
|
|
struct fetch_archive *f;
|
|
|
|
struct archive *a;
|
|
|
|
|
|
|
|
f = malloc(sizeof(struct fetch_archive));
|
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
f->url = url;
|
2014-12-22 22:48:20 +05:30
|
|
|
|
2009-11-25 07:39:36 +05:30
|
|
|
if ((a = archive_read_new()) == NULL) {
|
|
|
|
free(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-22 08:46:36 +05:30
|
|
|
archive_read_support_compression_gzip(a);
|
|
|
|
archive_read_support_compression_bzip2(a);
|
|
|
|
archive_read_support_compression_xz(a);
|
2009-11-25 07:39:36 +05:30
|
|
|
archive_read_support_format_tar(a);
|
|
|
|
|
|
|
|
if (archive_read_open(a, f, fetch_archive_open, fetch_archive_read,
|
|
|
|
fetch_archive_close)) {
|
|
|
|
archive_read_finish(a);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct archive *
|
|
|
|
open_archive(const char *url)
|
|
|
|
{
|
|
|
|
struct url *u;
|
|
|
|
struct archive *a;
|
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
if (!xbps_repository_is_remote(url)) {
|
2009-11-25 07:39:36 +05:30
|
|
|
if ((a = archive_read_new()) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2012-02-22 08:46:36 +05:30
|
|
|
archive_read_support_compression_gzip(a);
|
|
|
|
archive_read_support_compression_bzip2(a);
|
|
|
|
archive_read_support_compression_xz(a);
|
2009-11-25 07:39:36 +05:30
|
|
|
archive_read_support_format_tar(a);
|
|
|
|
|
2013-08-29 18:00:14 +05:30
|
|
|
if (archive_read_open_filename(a, url, 32768)) {
|
|
|
|
archive_read_finish(a);
|
2009-11-25 07:39:36 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((u = fetchParseURL(url)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
a = open_archive_by_url(u);
|
|
|
|
fetchFreeURL(u);
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:15:46 +05:30
|
|
|
char *
|
2014-11-21 14:21:57 +05:30
|
|
|
xbps_archive_fetch_file(const char *url, const char *fname)
|
2009-11-25 08:22:58 +05:30
|
|
|
{
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
2014-11-17 20:15:46 +05:30
|
|
|
char *buf = NULL;
|
2009-11-25 08:22:58 +05:30
|
|
|
|
2014-11-17 20:15:46 +05:30
|
|
|
assert(url);
|
|
|
|
assert(fname);
|
2011-01-25 08:44:33 +05:30
|
|
|
|
2014-11-17 20:15:46 +05:30
|
|
|
if ((a = open_archive(url)) == NULL)
|
2009-11-25 08:22:58 +05:30
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while ((archive_read_next_header(a, &entry)) == ARCHIVE_OK) {
|
2014-11-17 20:15:46 +05:30
|
|
|
const char *bfile;
|
|
|
|
|
|
|
|
bfile = archive_entry_pathname(entry);
|
2014-11-26 20:37:16 +05:30
|
|
|
if (bfile[0] == '.')
|
|
|
|
bfile++; /* skip first dot */
|
|
|
|
|
2014-11-17 20:15:46 +05:30
|
|
|
if (strcmp(bfile, fname) == 0) {
|
|
|
|
buf = xbps_archive_get_file(a, entry);
|
2009-11-25 08:22:58 +05:30
|
|
|
break;
|
|
|
|
}
|
2014-11-17 20:15:46 +05:30
|
|
|
archive_read_data_skip(a);
|
2009-11-25 08:22:58 +05:30
|
|
|
}
|
2013-08-29 18:00:14 +05:30
|
|
|
archive_read_finish(a);
|
2009-11-25 08:22:58 +05:30
|
|
|
|
2014-11-17 20:15:46 +05:30
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2014-11-26 20:37:16 +05:30
|
|
|
bool
|
|
|
|
xbps_repo_fetch_remote(struct xbps_repo *repo, const char *url)
|
|
|
|
{
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
uint8_t i = 0;
|
|
|
|
|
|
|
|
assert(url);
|
|
|
|
assert(repo);
|
|
|
|
|
|
|
|
if ((a = open_archive(url)) == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while ((archive_read_next_header(a, &entry)) == ARCHIVE_OK) {
|
|
|
|
const char *bfile;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
bfile = archive_entry_pathname(entry);
|
|
|
|
if (bfile[0] == '.')
|
|
|
|
bfile++; /* skip first dot */
|
|
|
|
|
|
|
|
if (strcmp(bfile, "index-meta.plist") == 0) {
|
|
|
|
buf = xbps_archive_get_file(a, entry);
|
|
|
|
repo->idxmeta = xbps_dictionary_internalize(buf);
|
|
|
|
free(buf);
|
|
|
|
i++;
|
|
|
|
} else if (strcmp(bfile, "index.plist") == 0) {
|
|
|
|
buf = xbps_archive_get_file(a, entry);
|
|
|
|
repo->idx = xbps_dictionary_internalize(buf);
|
|
|
|
free(buf);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
archive_read_data_skip(a);
|
|
|
|
}
|
|
|
|
if (i == 2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
archive_read_finish(a);
|
|
|
|
|
|
|
|
if (xbps_object_type(repo->idxmeta) == XBPS_TYPE_DICTIONARY)
|
|
|
|
repo->is_signed = true;
|
|
|
|
|
|
|
|
if (xbps_object_type(repo->idx) == XBPS_TYPE_DICTIONARY)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:13:08 +05:30
|
|
|
int
|
2014-11-21 14:21:57 +05:30
|
|
|
xbps_archive_fetch_file_into_fd(const char *url, const char *fname, int fd)
|
2014-11-17 23:13:08 +05:30
|
|
|
{
|
|
|
|
struct archive *a;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(url);
|
|
|
|
assert(fname);
|
|
|
|
assert(fd != -1);
|
|
|
|
|
|
|
|
if ((a = open_archive(url)) == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
while ((archive_read_next_header(a, &entry)) == ARCHIVE_OK) {
|
|
|
|
const char *bfile;
|
|
|
|
|
|
|
|
bfile = archive_entry_pathname(entry);
|
2014-11-26 20:37:16 +05:30
|
|
|
if (bfile[0] == '.')
|
|
|
|
bfile++; /* skip first dot */
|
|
|
|
|
2014-11-17 23:13:08 +05:30
|
|
|
if (strcmp(bfile, fname) == 0) {
|
|
|
|
rv = archive_read_data_into_fd(a, fd);
|
|
|
|
if (rv != 0)
|
|
|
|
rv = archive_errno(a);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
archive_read_data_skip(a);
|
|
|
|
}
|
|
|
|
archive_read_finish(a);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:15:46 +05:30
|
|
|
xbps_dictionary_t
|
2014-11-21 14:21:57 +05:30
|
|
|
xbps_archive_fetch_plist(const char *url, const char *plistf)
|
2014-11-17 20:15:46 +05:30
|
|
|
{
|
|
|
|
xbps_dictionary_t d;
|
|
|
|
char *buf;
|
|
|
|
|
2014-11-21 14:21:57 +05:30
|
|
|
if ((buf = xbps_archive_fetch_file(url, plistf)) == NULL)
|
2014-11-17 20:15:46 +05:30
|
|
|
return NULL;
|
|
|
|
|
|
|
|
d = xbps_dictionary_internalize(buf);
|
|
|
|
free(buf);
|
|
|
|
return d;
|
2009-11-25 08:22:58 +05:30
|
|
|
}
|