2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2015-01-11 15:31:09 +05:30
|
|
|
* Copyright (c) 2008-2015 Juan Romero Pardines.
|
2009-08-17 22:37:20 +05:30
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-02-24 04:54:11 +05:30
|
|
|
#include <sys/stat.h>
|
2009-08-17 22:37:20 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2011-02-24 04:54:11 +05:30
|
|
|
#include <unistd.h>
|
2011-12-24 16:37:30 +05:30
|
|
|
#include <libgen.h>
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2011-07-28 19:55:01 +05:30
|
|
|
static int
|
2012-10-26 14:55:17 +05:30
|
|
|
set_extract_flags(uid_t euid)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2011-07-28 19:55:01 +05:30
|
|
|
int flags;
|
2010-01-28 20:51:08 +05:30
|
|
|
|
2012-10-26 14:55:17 +05:30
|
|
|
if (euid == 0)
|
2011-07-28 19:55:01 +05:30
|
|
|
flags = FEXTRACT_FLAGS;
|
2009-08-17 22:37:20 +05:30
|
|
|
else
|
2011-07-28 19:55:01 +05:30
|
|
|
flags = EXTRACT_FLAGS;
|
2010-01-28 20:51:08 +05:30
|
|
|
|
2011-07-28 19:55:01 +05:30
|
|
|
return flags;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
2014-08-01 18:39:51 +05:30
|
|
|
static bool
|
|
|
|
match_preserved_file(struct xbps_handle *xhp, const char *entry)
|
|
|
|
{
|
|
|
|
char *file;
|
|
|
|
|
|
|
|
if (xhp->preserved_files == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (entry[0] == '.' && entry[1] != '\0') {
|
|
|
|
file = strchr(entry, '.') + 1;
|
|
|
|
assert(file);
|
|
|
|
} else {
|
|
|
|
file = __UNCONST(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return xbps_match_string_in_array(xhp->preserved_files, file);
|
|
|
|
}
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
static int
|
2012-06-14 11:52:11 +05:30
|
|
|
unpack_archive(struct xbps_handle *xhp,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkg_repod,
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver,
|
|
|
|
const char *fname,
|
2012-06-14 11:52:11 +05:30
|
|
|
struct archive *ar)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2019-06-17 15:05:24 +05:30
|
|
|
xbps_dictionary_t binpkg_propsd, binpkg_filesd, pkg_filesd, obsd;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t array, obsoletes;
|
2014-09-11 03:42:12 +05:30
|
|
|
xbps_data_t data;
|
2020-02-21 13:38:22 +05:30
|
|
|
xbps_trans_type_t ttype;
|
2013-11-06 15:15:33 +05:30
|
|
|
const struct stat *entry_statp;
|
2012-11-18 16:37:43 +05:30
|
|
|
void *instbuf = NULL, *rembuf = NULL;
|
2012-03-07 16:30:08 +05:30
|
|
|
struct stat st;
|
2014-10-05 01:29:47 +05:30
|
|
|
struct xbps_unpack_cb_data xucd;
|
2009-08-17 22:37:20 +05:30
|
|
|
struct archive_entry *entry;
|
2013-09-15 13:36:49 +05:30
|
|
|
size_t instbufsiz = 0, rembufsiz = 0;
|
2012-11-16 21:25:35 +05:30
|
|
|
ssize_t entry_size;
|
2020-02-21 13:38:22 +05:30
|
|
|
const char *entry_pname, *binpkg_pkgver, *pkgname;
|
|
|
|
char *buf = NULL;
|
2015-01-18 14:52:05 +05:30
|
|
|
int ar_rv, rv, error, entry_type, flags;
|
2019-03-19 17:42:08 +05:30
|
|
|
bool preserve, update, file_exists, keep_conf_file;
|
2014-01-16 02:22:35 +05:30
|
|
|
bool skip_extract, force, xucd_stats;
|
2012-10-26 14:55:17 +05:30
|
|
|
uid_t euid;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2017-07-09 18:16:01 +05:30
|
|
|
binpkg_propsd = binpkg_filesd = pkg_filesd = NULL;
|
2014-10-05 15:27:20 +05:30
|
|
|
force = preserve = update = file_exists = false;
|
2015-02-27 00:21:03 +05:30
|
|
|
xucd_stats = false;
|
2015-01-18 14:52:05 +05:30
|
|
|
ar_rv = rv = error = entry_type = flags = 0;
|
2012-05-26 02:14:58 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_bool(pkg_repod, "preserve", &preserve);
|
2020-02-21 13:38:22 +05:30
|
|
|
ttype = xbps_transaction_pkg_type(pkg_repod);
|
2011-11-24 15:53:08 +05:30
|
|
|
|
2014-10-05 10:42:30 +05:30
|
|
|
memset(&xucd, 0, sizeof(xucd));
|
|
|
|
|
2012-10-26 14:55:17 +05:30
|
|
|
euid = geteuid();
|
|
|
|
|
2020-02-21 13:38:22 +05:30
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgname", &pkgname)) {
|
|
|
|
return EINVAL;
|
2020-02-09 00:01:29 +05:30
|
|
|
}
|
2013-03-05 11:12:25 +05:30
|
|
|
|
2020-02-21 13:38:22 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_FORCE_UNPACK) {
|
2012-11-20 01:20:58 +05:30
|
|
|
force = true;
|
2020-02-21 13:38:22 +05:30
|
|
|
}
|
2012-11-20 01:20:58 +05:30
|
|
|
|
2020-02-21 13:38:22 +05:30
|
|
|
if (ttype == XBPS_TRANS_UPDATE) {
|
2009-12-09 20:44:35 +05:30
|
|
|
update = true;
|
2020-02-21 13:38:22 +05:30
|
|
|
}
|
2019-06-17 15:05:24 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove obsolete files.
|
|
|
|
*/
|
|
|
|
if (!preserve &&
|
|
|
|
xbps_dictionary_get_dict(xhp->transd, "obsolete_files", &obsd) &&
|
|
|
|
(obsoletes = xbps_dictionary_get(obsd, pkgname))) {
|
|
|
|
for (unsigned int i = 0; i < xbps_array_count(obsoletes); i++) {
|
|
|
|
const char *file = NULL;
|
|
|
|
xbps_array_get_cstring_nocopy(obsoletes, i, &file);
|
|
|
|
if (remove(file) == -1) {
|
|
|
|
xbps_set_cb_state(xhp,
|
|
|
|
XBPS_STATE_REMOVE_FILE_OBSOLETE_FAIL,
|
|
|
|
errno, pkgver,
|
|
|
|
"%s: failed to remove obsolete entry `%s': %s",
|
|
|
|
pkgver, file, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
xbps_set_cb_state(xhp,
|
|
|
|
XBPS_STATE_REMOVE_FILE_OBSOLETE,
|
|
|
|
0, pkgver, "%s: removed obsolete entry: %s", pkgver, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-23 04:26:39 +05:30
|
|
|
/*
|
|
|
|
* Process the archive files.
|
|
|
|
*/
|
2013-03-15 17:48:30 +05:30
|
|
|
flags = set_extract_flags(euid);
|
2014-01-16 02:22:35 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* First get all metadata files on archive in this order:
|
2014-07-02 15:00:41 +05:30
|
|
|
* - INSTALL <optional>
|
|
|
|
* - REMOVE <optional>
|
2014-10-21 12:55:15 +05:30
|
|
|
* - props.plist <required> but currently ignored
|
2014-07-02 15:00:41 +05:30
|
|
|
* - files.plist <required>
|
2014-01-16 02:22:35 +05:30
|
|
|
*
|
|
|
|
* The XBPS package must contain props and files plists, otherwise
|
|
|
|
* it's not a valid package.
|
|
|
|
*/
|
|
|
|
for (uint8_t i = 0; i < 4; i++) {
|
2012-06-05 20:16:50 +05:30
|
|
|
ar_rv = archive_read_next_header(ar, &entry);
|
2014-01-16 01:33:53 +05:30
|
|
|
if (ar_rv == ARCHIVE_EOF || ar_rv == ARCHIVE_FATAL)
|
|
|
|
break;
|
|
|
|
|
2011-01-20 07:25:40 +05:30
|
|
|
entry_pname = archive_entry_pathname(entry);
|
2012-11-16 21:25:35 +05:30
|
|
|
entry_size = archive_entry_size(entry);
|
2014-01-16 02:22:35 +05:30
|
|
|
|
2011-01-20 07:25:40 +05:30
|
|
|
if (strcmp("./INSTALL", entry_pname) == 0) {
|
|
|
|
/*
|
2014-01-16 02:22:35 +05:30
|
|
|
* Store file in a buffer to execute it later.
|
2011-01-20 07:25:40 +05:30
|
|
|
*/
|
2012-11-16 21:25:35 +05:30
|
|
|
instbufsiz = entry_size;
|
|
|
|
instbuf = malloc(entry_size);
|
|
|
|
assert(instbuf);
|
2014-01-16 02:22:35 +05:30
|
|
|
if (archive_read_data(ar, instbuf, entry_size) != entry_size) {
|
2012-11-16 21:25:35 +05:30
|
|
|
rv = EINVAL;
|
2011-01-21 21:46:58 +05:30
|
|
|
goto out;
|
2012-11-16 21:25:35 +05:30
|
|
|
}
|
2011-01-20 07:25:40 +05:30
|
|
|
} else if (strcmp("./REMOVE", entry_pname) == 0) {
|
2014-01-16 02:22:35 +05:30
|
|
|
/*
|
|
|
|
* Store file in a buffer to execute it later.
|
|
|
|
*/
|
2012-11-16 21:25:35 +05:30
|
|
|
rembufsiz = entry_size;
|
|
|
|
rembuf = malloc(entry_size);
|
|
|
|
assert(rembuf);
|
2014-01-16 02:22:35 +05:30
|
|
|
if (archive_read_data(ar, rembuf, entry_size) != entry_size) {
|
2012-11-16 21:25:35 +05:30
|
|
|
rv = EINVAL;
|
2010-11-19 18:10:13 +05:30
|
|
|
goto out;
|
2012-11-16 21:25:35 +05:30
|
|
|
}
|
2017-07-09 18:16:01 +05:30
|
|
|
} else if (strcmp("./props.plist", entry_pname) == 0) {
|
2020-01-06 19:03:49 +05:30
|
|
|
binpkg_propsd = xbps_archive_get_dictionary(ar, entry);
|
2017-07-09 18:16:01 +05:30
|
|
|
if (binpkg_propsd == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-01-16 02:22:35 +05:30
|
|
|
} else if (strcmp("./files.plist", entry_pname) == 0) {
|
2020-01-06 19:03:49 +05:30
|
|
|
binpkg_filesd = xbps_archive_get_dictionary(ar, entry);
|
2014-09-11 03:42:12 +05:30
|
|
|
if (binpkg_filesd == NULL) {
|
2014-01-16 02:22:35 +05:30
|
|
|
rv = EINVAL;
|
2014-01-16 01:33:53 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2014-10-21 12:55:15 +05:30
|
|
|
} else {
|
|
|
|
archive_read_data_skip(ar);
|
2013-11-15 15:06:31 +05:30
|
|
|
}
|
2014-10-21 12:55:15 +05:30
|
|
|
if (binpkg_filesd)
|
2014-01-16 02:22:35 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If there was any error extracting files from archive, error out.
|
|
|
|
*/
|
|
|
|
if (ar_rv == ARCHIVE_FATAL) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver,
|
|
|
|
"%s: [unpack] 1: failed to extract files: %s",
|
|
|
|
pkgver, archive_error_string(ar));
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Bail out if required metadata files are not in archive.
|
|
|
|
*/
|
2017-07-09 18:16:01 +05:30
|
|
|
if (binpkg_propsd == NULL || binpkg_filesd == NULL) {
|
2014-01-16 02:22:35 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, ENODEV, pkgver,
|
|
|
|
"%s: [unpack] invalid binary package `%s'.", pkgver, fname);
|
|
|
|
rv = ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-07-09 18:16:01 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the pkgver in the binpkg matches the one we're looking for.
|
|
|
|
*/
|
|
|
|
xbps_dictionary_get_cstring_nocopy(binpkg_propsd, "pkgver", &binpkg_pkgver);
|
|
|
|
if (strcmp(pkgver, binpkg_pkgver) != 0) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, EINVAL, pkgver,
|
|
|
|
"%s: [unpack] pkgver mismatch repodata: `%s' binpkg: `%s'.", fname, pkgver, binpkg_pkgver);
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-01-16 02:22:35 +05:30
|
|
|
/*
|
2014-09-11 03:42:12 +05:30
|
|
|
* Internalize current pkg metadata files plist.
|
2014-01-16 02:22:35 +05:30
|
|
|
*/
|
2014-09-11 03:42:12 +05:30
|
|
|
pkg_filesd = xbps_pkgdb_get_pkg_files(xhp, pkgname);
|
|
|
|
|
|
|
|
/* Add pkg install/remove scripts data objects into our dictionary */
|
|
|
|
if (instbuf != NULL) {
|
|
|
|
data = xbps_data_create_data(instbuf, instbufsiz);
|
|
|
|
assert(data);
|
|
|
|
xbps_dictionary_set(pkg_repod, "install-script", data);
|
|
|
|
xbps_object_release(data);
|
|
|
|
}
|
|
|
|
if (rembuf != NULL) {
|
|
|
|
data = xbps_data_create_data(rembuf, rembufsiz);
|
|
|
|
assert(data);
|
|
|
|
xbps_dictionary_set(pkg_repod, "remove-script", data);
|
|
|
|
xbps_object_release(data);
|
|
|
|
}
|
2014-01-16 02:22:35 +05:30
|
|
|
/*
|
|
|
|
* Execute INSTALL "pre" ACTION before unpacking files.
|
|
|
|
*/
|
|
|
|
if (instbuf != NULL) {
|
|
|
|
rv = xbps_pkg_exec_buffer(xhp, instbuf, instbufsiz, pkgver, "pre", update);
|
|
|
|
if (rv != 0) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver,
|
|
|
|
"%s: [unpack] INSTALL script failed to execute pre ACTION: %s",
|
|
|
|
pkgver, strerror(rv));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Unpack all files on archive now.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
ar_rv = archive_read_next_header(ar, &entry);
|
|
|
|
if (ar_rv == ARCHIVE_EOF || ar_rv == ARCHIVE_FATAL)
|
|
|
|
break;
|
|
|
|
else if (ar_rv == ARCHIVE_RETRY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
entry_pname = archive_entry_pathname(entry);
|
|
|
|
entry_size = archive_entry_size(entry);
|
|
|
|
entry_type = archive_entry_filetype(entry);
|
|
|
|
entry_statp = archive_entry_stat(entry);
|
2014-01-16 01:33:53 +05:30
|
|
|
/*
|
2014-01-16 02:22:35 +05:30
|
|
|
* Ignore directories from archive.
|
2014-01-16 01:33:53 +05:30
|
|
|
*/
|
2014-01-16 02:22:35 +05:30
|
|
|
if (entry_type == AE_IFDIR) {
|
|
|
|
archive_read_data_skip(ar);
|
|
|
|
continue;
|
2014-01-16 01:33:53 +05:30
|
|
|
}
|
2013-05-17 11:54:47 +05:30
|
|
|
/*
|
|
|
|
* Prepare unpack callback ops.
|
|
|
|
*/
|
|
|
|
if (xhp->unpack_cb != NULL) {
|
|
|
|
xucd.xhp = xhp;
|
|
|
|
xucd.pkgver = pkgver;
|
|
|
|
xucd.entry = entry_pname;
|
|
|
|
xucd.entry_size = entry_size;
|
|
|
|
xucd.entry_is_conf = false;
|
2013-11-15 15:06:31 +05:30
|
|
|
/*
|
|
|
|
* Compute total entries in progress data, if set.
|
|
|
|
* total_entries = files + conf_files + links.
|
|
|
|
*/
|
2014-09-11 03:42:12 +05:30
|
|
|
if (binpkg_filesd && !xucd_stats) {
|
|
|
|
array = xbps_dictionary_get(binpkg_filesd, "files");
|
2013-11-15 15:06:31 +05:30
|
|
|
xucd.entry_total_count +=
|
|
|
|
(ssize_t)xbps_array_count(array);
|
2014-09-11 03:42:12 +05:30
|
|
|
array = xbps_dictionary_get(binpkg_filesd, "conf_files");
|
2013-11-15 15:06:31 +05:30
|
|
|
xucd.entry_total_count +=
|
|
|
|
(ssize_t)xbps_array_count(array);
|
2014-09-11 03:42:12 +05:30
|
|
|
array = xbps_dictionary_get(binpkg_filesd, "links");
|
2013-11-15 15:06:31 +05:30
|
|
|
xucd.entry_total_count +=
|
|
|
|
(ssize_t)xbps_array_count(array);
|
|
|
|
xucd_stats = true;
|
|
|
|
}
|
2011-11-24 15:53:08 +05:30
|
|
|
}
|
2020-01-18 19:19:59 +05:30
|
|
|
/*
|
|
|
|
* Skip files that match noextract patterns from configuration file.
|
|
|
|
*/
|
|
|
|
if (xhp->noextract && xbps_patterns_match(xhp->noextract, entry_pname+1)) {
|
2020-01-26 15:05:40 +05:30
|
|
|
xbps_dbg_printf(xhp, "[unpack] %s skipped (matched by a pattern)\n", entry_pname+1);
|
2020-01-18 19:19:59 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FILE_PRESERVED, 0,
|
|
|
|
pkgver, "%s: file `%s' won't be extracted, "
|
|
|
|
"it matches a noextract pattern.", pkgver, entry_pname);
|
2020-01-26 15:05:40 +05:30
|
|
|
archive_read_data_skip(ar);
|
2020-01-18 19:19:59 +05:30
|
|
|
continue;
|
|
|
|
}
|
2011-12-19 15:50:27 +05:30
|
|
|
/*
|
|
|
|
* Always check that extracted file exists and hash
|
|
|
|
* doesn't match, in that case overwrite the file.
|
|
|
|
* Otherwise skip extracting it.
|
|
|
|
*/
|
2019-03-19 17:42:08 +05:30
|
|
|
skip_extract = file_exists = keep_conf_file = false;
|
2013-03-11 18:36:47 +05:30
|
|
|
if (lstat(entry_pname, &st) == 0)
|
2012-10-26 14:55:17 +05:30
|
|
|
file_exists = true;
|
2014-08-01 18:39:51 +05:30
|
|
|
/*
|
|
|
|
* Check if the file to be extracted must be preserved, if true,
|
|
|
|
* pass to the next file.
|
|
|
|
*/
|
|
|
|
if (file_exists && match_preserved_file(xhp, entry_pname)) {
|
|
|
|
archive_read_data_skip(ar);
|
2015-02-24 14:04:39 +05:30
|
|
|
xbps_dbg_printf(xhp, "[unpack] `%s' exists on disk "
|
2016-02-06 13:43:38 +05:30
|
|
|
"and must be preserved, skipping.\n", entry_pname);
|
2015-02-24 14:04:39 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FILE_PRESERVED, 0,
|
|
|
|
pkgver, "%s: file `%s' won't be extracted, "
|
2020-01-18 19:19:59 +05:30
|
|
|
"it's preserved.", pkgver, entry_pname);
|
2014-08-01 18:39:51 +05:30
|
|
|
continue;
|
|
|
|
}
|
2019-03-19 17:42:08 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if current entry is a configuration file,
|
|
|
|
* that should be kept.
|
|
|
|
*/
|
|
|
|
if (!force && (entry_type == AE_IFREG)) {
|
|
|
|
buf = strchr(entry_pname, '.') + 1;
|
|
|
|
assert(buf != NULL);
|
|
|
|
keep_conf_file = xbps_entry_is_a_conf_file(binpkg_filesd, buf);
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:26:36 +05:30
|
|
|
/*
|
2013-11-09 13:29:02 +05:30
|
|
|
* If file to be extracted does not match the file type of
|
2019-03-19 17:42:08 +05:30
|
|
|
* file currently stored on disk and is not a conf file
|
|
|
|
* that should be kept, remove file on disk.
|
2013-11-05 14:26:36 +05:30
|
|
|
*/
|
2019-03-19 17:42:08 +05:30
|
|
|
if (file_exists && !keep_conf_file &&
|
2013-11-09 13:29:02 +05:30
|
|
|
((entry_statp->st_mode & S_IFMT) != (st.st_mode & S_IFMT)))
|
2015-07-26 11:52:27 +05:30
|
|
|
(void)remove(entry_pname);
|
2012-10-26 13:57:24 +05:30
|
|
|
|
2013-03-15 17:48:30 +05:30
|
|
|
if (!force && (entry_type == AE_IFREG)) {
|
2019-03-19 17:42:08 +05:30
|
|
|
if (file_exists && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
|
2012-10-26 13:57:24 +05:30
|
|
|
/*
|
2019-03-19 17:42:08 +05:30
|
|
|
* Handle configuration files.
|
|
|
|
* Skip packages that don't have "conf_files"
|
|
|
|
* array on its XBPS_PKGPROPS
|
2012-10-26 13:57:24 +05:30
|
|
|
* dictionary.
|
|
|
|
*/
|
2019-03-19 17:42:08 +05:30
|
|
|
if (keep_conf_file) {
|
2012-10-26 13:57:24 +05:30
|
|
|
if (xhp->unpack_cb != NULL)
|
|
|
|
xucd.entry_is_conf = true;
|
|
|
|
|
|
|
|
rv = xbps_entry_install_conf_file(xhp,
|
2014-09-11 03:42:12 +05:30
|
|
|
binpkg_filesd, pkg_filesd, entry,
|
2019-03-19 17:42:08 +05:30
|
|
|
entry_pname, pkgver, S_ISLNK(st.st_mode));
|
2012-10-26 13:57:24 +05:30
|
|
|
if (rv == -1) {
|
|
|
|
/* error */
|
|
|
|
goto out;
|
|
|
|
} else if (rv == 0) {
|
|
|
|
/*
|
|
|
|
* Keep curfile as is.
|
|
|
|
*/
|
|
|
|
skip_extract = true;
|
|
|
|
}
|
2014-01-16 02:22:35 +05:30
|
|
|
rv = 0;
|
2012-10-26 13:57:24 +05:30
|
|
|
} else {
|
|
|
|
rv = xbps_file_hash_check_dictionary(
|
2014-09-11 03:42:12 +05:30
|
|
|
xhp, binpkg_filesd, "files", buf);
|
2012-10-26 13:57:24 +05:30
|
|
|
if (rv == -1) {
|
|
|
|
/* error */
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
2013-03-05 08:38:42 +05:30
|
|
|
"%s: failed to check"
|
2012-10-26 13:57:24 +05:30
|
|
|
" hash for `%s': %s\n",
|
2013-03-05 08:38:42 +05:30
|
|
|
pkgver, entry_pname,
|
2012-03-07 16:30:08 +05:30
|
|
|
strerror(errno));
|
|
|
|
goto out;
|
2012-10-26 13:57:24 +05:30
|
|
|
} else if (rv == 0) {
|
|
|
|
/*
|
|
|
|
* hash match, skip extraction.
|
|
|
|
*/
|
|
|
|
xbps_dbg_printf(xhp,
|
2013-03-05 08:38:42 +05:30
|
|
|
"%s: file %s "
|
2012-10-26 13:57:24 +05:30
|
|
|
"matches existing SHA256, "
|
2013-03-05 08:38:42 +05:30
|
|
|
"skipping...\n",
|
|
|
|
pkgver, entry_pname);
|
2014-07-30 22:20:43 +05:30
|
|
|
skip_extract = true;
|
2012-03-07 16:30:08 +05:30
|
|
|
}
|
2014-01-16 02:22:35 +05:30
|
|
|
rv = 0;
|
2011-08-01 16:05:47 +05:30
|
|
|
}
|
2011-12-19 15:50:27 +05:30
|
|
|
}
|
2012-10-26 13:57:24 +05:30
|
|
|
}
|
2012-10-26 14:55:17 +05:30
|
|
|
/*
|
|
|
|
* Check if current uid/gid differs from file in binpkg,
|
|
|
|
* and change permissions if true.
|
|
|
|
*/
|
2012-11-20 01:20:58 +05:30
|
|
|
if ((!force && file_exists && skip_extract && (euid == 0)) &&
|
2013-03-15 17:48:30 +05:30
|
|
|
(((archive_entry_uid(entry) != st.st_uid)) ||
|
|
|
|
((archive_entry_gid(entry) != st.st_gid)))) {
|
2013-03-17 21:43:07 +05:30
|
|
|
if (lchown(entry_pname,
|
2013-03-15 17:48:30 +05:30
|
|
|
archive_entry_uid(entry),
|
|
|
|
archive_entry_gid(entry)) != 0) {
|
2012-10-26 14:55:17 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
2013-03-05 08:38:42 +05:30
|
|
|
"%s: failed "
|
2018-07-18 05:56:08 +05:30
|
|
|
"to set uid/gid to %"PRIu64":%"PRIu64" (%s)\n",
|
2013-03-15 17:48:30 +05:30
|
|
|
pkgver, archive_entry_uid(entry),
|
|
|
|
archive_entry_gid(entry),
|
2012-10-26 14:55:17 +05:30
|
|
|
strerror(errno));
|
|
|
|
} else {
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: entry %s changed "
|
2018-07-18 05:56:08 +05:30
|
|
|
"uid/gid to %"PRIu64":%"PRIu64".\n", pkgver, entry_pname,
|
2013-03-15 17:48:30 +05:30
|
|
|
archive_entry_uid(entry),
|
|
|
|
archive_entry_gid(entry));
|
2012-10-26 14:55:17 +05:30
|
|
|
}
|
2011-12-19 15:50:27 +05:30
|
|
|
}
|
2013-10-30 16:32:16 +05:30
|
|
|
/*
|
|
|
|
* Check if current file mode differs from file mode
|
|
|
|
* in binpkg and apply perms if true.
|
|
|
|
*/
|
|
|
|
if (!force && file_exists && skip_extract &&
|
|
|
|
(archive_entry_mode(entry) != st.st_mode)) {
|
|
|
|
if (chmod(entry_pname,
|
|
|
|
archive_entry_mode(entry)) != 0) {
|
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"%s: failed "
|
|
|
|
"to set perms %s to %s: %s\n",
|
|
|
|
pkgver, archive_entry_strmode(entry),
|
|
|
|
entry_pname,
|
|
|
|
strerror(errno));
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
xbps_dbg_printf(xhp, "%s: entry %s changed file "
|
|
|
|
"mode to %s.\n", pkgver, entry_pname,
|
|
|
|
archive_entry_strmode(entry));
|
|
|
|
}
|
2015-02-25 15:40:18 +05:30
|
|
|
/*
|
|
|
|
* Check if current file mtime differs from archive entry
|
|
|
|
* in binpkg and apply mtime if true.
|
|
|
|
*/
|
|
|
|
if (!force && file_exists && skip_extract &&
|
|
|
|
(archive_entry_mtime_nsec(entry) != st.st_mtime)) {
|
|
|
|
struct timespec ts[2];
|
|
|
|
|
|
|
|
ts[0].tv_sec = archive_entry_atime(entry);
|
|
|
|
ts[0].tv_nsec = archive_entry_atime_nsec(entry);
|
|
|
|
ts[1].tv_sec = archive_entry_mtime(entry);
|
|
|
|
ts[1].tv_nsec = archive_entry_mtime_nsec(entry);
|
|
|
|
|
|
|
|
if (utimensat(AT_FDCWD, entry_pname, ts,
|
|
|
|
AT_SYMLINK_NOFOLLOW) == -1) {
|
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"%s: failed "
|
2018-07-18 05:56:08 +05:30
|
|
|
"to set mtime %lu to %s: %s\n",
|
2015-02-25 15:40:18 +05:30
|
|
|
pkgver, archive_entry_mtime_nsec(entry),
|
|
|
|
entry_pname,
|
|
|
|
strerror(errno));
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
xbps_dbg_printf(xhp, "%s: updated file timestamps to %s\n",
|
|
|
|
pkgver, entry_pname);
|
|
|
|
}
|
2012-11-20 01:20:58 +05:30
|
|
|
if (!force && skip_extract) {
|
2012-10-26 13:57:24 +05:30
|
|
|
archive_read_data_skip(ar);
|
|
|
|
continue;
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
2012-10-01 20:11:50 +05:30
|
|
|
/*
|
|
|
|
* Reset entry_pname again because if entry's pathname
|
|
|
|
* has been changed it will become a dangling pointer.
|
|
|
|
*/
|
|
|
|
entry_pname = archive_entry_pathname(entry);
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Extract entry from archive.
|
|
|
|
*/
|
2011-01-21 21:46:58 +05:30
|
|
|
if (archive_read_extract(ar, entry, flags) != 0) {
|
2015-01-18 14:52:05 +05:30
|
|
|
error = archive_errno(ar);
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
2015-01-18 14:52:05 +05:30
|
|
|
error, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"%s: [unpack] failed to extract file `%s': %s",
|
2015-01-18 14:52:05 +05:30
|
|
|
pkgver, entry_pname, strerror(error));
|
|
|
|
break;
|
2012-10-26 13:57:24 +05:30
|
|
|
} else {
|
|
|
|
if (xhp->unpack_cb != NULL) {
|
2015-05-28 14:09:39 +05:30
|
|
|
xucd.entry = entry_pname;
|
2012-10-26 13:57:24 +05:30
|
|
|
xucd.entry_extract_count++;
|
2012-11-30 11:41:51 +05:30
|
|
|
(*xhp->unpack_cb)(&xucd, xhp->unpack_cb_data);
|
2012-10-26 13:57:24 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
}
|
2011-11-10 01:31:25 +05:30
|
|
|
/*
|
|
|
|
* If there was any error extracting files from archive, error out.
|
|
|
|
*/
|
2015-01-18 14:52:05 +05:30
|
|
|
if (error || ar_rv == ARCHIVE_FATAL) {
|
|
|
|
rv = error;
|
|
|
|
if (!rv)
|
|
|
|
rv = ar_rv;
|
2014-01-16 02:22:35 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver,
|
2012-06-05 20:16:50 +05:30
|
|
|
"%s: [unpack] failed to extract files: %s",
|
2015-01-18 14:52:05 +05:30
|
|
|
pkgver, strerror(rv));
|
2011-11-10 01:31:25 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2014-09-14 21:34:10 +05:30
|
|
|
/*
|
|
|
|
* Externalize binpkg files.plist to disk, if not empty.
|
|
|
|
*/
|
|
|
|
if (xbps_dictionary_count(binpkg_filesd)) {
|
2017-02-27 22:35:27 +05:30
|
|
|
mode_t prev_umask;
|
|
|
|
prev_umask = umask(022);
|
2014-09-14 21:34:10 +05:30
|
|
|
buf = xbps_xasprintf("%s/.%s-files.plist", xhp->metadir, pkgname);
|
2014-10-24 13:45:41 +05:30
|
|
|
if (!xbps_dictionary_externalize_to_file(binpkg_filesd, buf)) {
|
2014-09-14 21:34:10 +05:30
|
|
|
rv = errno;
|
2017-02-27 22:35:27 +05:30
|
|
|
umask(prev_umask);
|
2014-09-14 21:34:10 +05:30
|
|
|
free(buf);
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
|
|
|
rv, pkgver, "%s: [unpack] failed to externalize pkg "
|
|
|
|
"pkg metadata files: %s", pkgver, strerror(rv));
|
|
|
|
goto out;
|
|
|
|
}
|
2017-02-27 22:35:27 +05:30
|
|
|
umask(prev_umask);
|
2014-09-14 21:34:10 +05:30
|
|
|
free(buf);
|
|
|
|
}
|
2015-01-27 22:23:37 +05:30
|
|
|
out:
|
2014-09-14 21:34:10 +05:30
|
|
|
/*
|
|
|
|
* If unpacked pkg has no files, remove its files metadata plist.
|
|
|
|
*/
|
|
|
|
if (!xbps_dictionary_count(binpkg_filesd)) {
|
|
|
|
buf = xbps_xasprintf("%s/.%s-files.plist", xhp->metadir, pkgname);
|
|
|
|
unlink(buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
2017-07-09 18:16:01 +05:30
|
|
|
if (xbps_object_type(binpkg_propsd) == XBPS_TYPE_DICTIONARY)
|
|
|
|
xbps_object_release(binpkg_propsd);
|
2014-09-11 03:42:12 +05:30
|
|
|
if (xbps_object_type(binpkg_filesd) == XBPS_TYPE_DICTIONARY)
|
|
|
|
xbps_object_release(binpkg_filesd);
|
2013-04-04 13:25:35 +05:30
|
|
|
if (instbuf != NULL)
|
|
|
|
free(instbuf);
|
|
|
|
if (rembuf != NULL)
|
|
|
|
free(rembuf);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2011-01-21 21:46:58 +05:30
|
|
|
|
2012-01-17 20:47:03 +05:30
|
|
|
int HIDDEN
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod)
|
2010-01-21 07:40:19 +05:30
|
|
|
{
|
2011-12-22 12:53:11 +05:30
|
|
|
struct archive *ar = NULL;
|
2013-08-29 18:00:14 +05:30
|
|
|
struct stat st;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *pkgver;
|
2013-08-29 18:00:14 +05:30
|
|
|
char *bpkg = NULL;
|
|
|
|
int pkg_fd = -1, rv = 0;
|
2015-01-11 15:31:09 +05:30
|
|
|
mode_t myumask;
|
2010-01-21 07:40:19 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
assert(xbps_object_type(pkg_repod) == XBPS_TYPE_DICTIONARY);
|
2010-01-21 07:40:19 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver);
|
2013-04-04 13:45:32 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK, 0, pkgver, NULL);
|
2011-11-24 15:53:08 +05:30
|
|
|
|
2012-11-30 11:41:51 +05:30
|
|
|
bpkg = xbps_repository_pkg_path(xhp, pkg_repod);
|
2011-01-21 21:46:58 +05:30
|
|
|
if (bpkg == NULL) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
2013-03-05 08:38:42 +05:30
|
|
|
errno, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"%s: [unpack] cannot determine binary package "
|
2012-11-30 11:41:51 +05:30
|
|
|
"file for `%s': %s", pkgver, bpkg, strerror(errno));
|
2011-01-21 21:46:58 +05:30
|
|
|
return errno;
|
2010-01-21 07:40:19 +05:30
|
|
|
}
|
|
|
|
|
2011-07-28 19:55:01 +05:30
|
|
|
if ((ar = archive_read_new()) == NULL) {
|
2011-12-22 12:53:11 +05:30
|
|
|
free(bpkg);
|
|
|
|
return ENOMEM;
|
2010-01-21 07:40:19 +05:30
|
|
|
}
|
|
|
|
/*
|
2019-06-17 10:44:24 +05:30
|
|
|
* Enable support for tar format and some compression methods.
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
2019-06-17 10:44:24 +05:30
|
|
|
archive_read_support_filter_gzip(ar);
|
|
|
|
archive_read_support_filter_bzip2(ar);
|
|
|
|
archive_read_support_filter_xz(ar);
|
|
|
|
archive_read_support_filter_lz4(ar);
|
|
|
|
archive_read_support_filter_zstd(ar);
|
2010-01-21 07:40:19 +05:30
|
|
|
archive_read_support_format_tar(ar);
|
|
|
|
|
2015-01-11 15:31:09 +05:30
|
|
|
myumask = umask(022);
|
|
|
|
|
2012-12-15 14:21:44 +05:30
|
|
|
pkg_fd = open(bpkg, O_RDONLY|O_CLOEXEC);
|
|
|
|
if (pkg_fd == -1) {
|
2013-08-29 18:00:14 +05:30
|
|
|
rv = errno;
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
2013-03-05 08:38:42 +05:30
|
|
|
rv, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"%s: [unpack] failed to open binary package `%s': %s",
|
2012-11-30 11:41:51 +05:30
|
|
|
pkgver, bpkg, strerror(rv));
|
2013-08-29 18:00:14 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (fstat(pkg_fd, &st) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
|
|
|
rv, pkgver,
|
|
|
|
"%s: [unpack] failed to fstat binary package `%s': %s",
|
|
|
|
pkgver, bpkg, strerror(rv));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (archive_read_open_fd(ar, pkg_fd, st.st_blksize) == ARCHIVE_FATAL) {
|
|
|
|
rv = archive_errno(ar);
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
|
|
|
rv, pkgver,
|
|
|
|
"%s: [unpack] failed to read binary package `%s': %s",
|
|
|
|
pkgver, bpkg, strerror(rv));
|
|
|
|
goto out;
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
2014-09-11 03:42:12 +05:30
|
|
|
/*
|
|
|
|
* Externalize pkg files dictionary to metadir.
|
|
|
|
*/
|
|
|
|
if (access(xhp->metadir, R_OK|X_OK) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
if (rv != ENOENT)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (xbps_mkpath(xhp->metadir, 0755) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2011-01-21 21:46:58 +05:30
|
|
|
/*
|
|
|
|
* Extract archive files.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
if ((rv = unpack_archive(xhp, pkg_repod, pkgver, bpkg, ar)) != 0) {
|
2014-01-16 02:22:35 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver,
|
2011-12-24 16:37:30 +05:30
|
|
|
"%s: [unpack] failed to unpack files from archive: %s",
|
2011-11-24 15:53:08 +05:30
|
|
|
pkgver, strerror(rv));
|
2010-01-23 04:26:39 +05:30
|
|
|
goto out;
|
2011-11-24 15:53:08 +05:30
|
|
|
}
|
2010-01-23 04:26:39 +05:30
|
|
|
/*
|
|
|
|
* Set package state to unpacked.
|
|
|
|
*/
|
2014-09-11 03:42:12 +05:30
|
|
|
if ((rv = xbps_set_pkg_state_dictionary(pkg_repod,
|
2011-07-28 19:55:01 +05:30
|
|
|
XBPS_PKG_STATE_UNPACKED)) != 0) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
2013-03-05 08:38:42 +05:30
|
|
|
rv, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"%s: [unpack] failed to set state to unpacked: %s",
|
|
|
|
pkgver, strerror(rv));
|
2011-01-21 21:46:58 +05:30
|
|
|
}
|
2015-10-18 14:08:35 +05:30
|
|
|
/* register alternatives */
|
|
|
|
if ((rv = xbps_alternatives_register(xhp, pkg_repod)) != 0) {
|
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
|
|
|
|
rv, pkgver,
|
|
|
|
"%s: [unpack] failed to register alternatives: %s",
|
|
|
|
pkgver, strerror(rv));
|
|
|
|
}
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
out:
|
2013-08-29 18:00:14 +05:30
|
|
|
if (pkg_fd != -1)
|
|
|
|
close(pkg_fd);
|
2012-12-15 14:21:44 +05:30
|
|
|
if (ar)
|
2013-08-29 18:00:14 +05:30
|
|
|
archive_read_finish(ar);
|
2013-03-05 08:38:42 +05:30
|
|
|
if (bpkg)
|
|
|
|
free(bpkg);
|
2012-12-15 14:21:44 +05:30
|
|
|
|
2015-01-11 15:31:09 +05:30
|
|
|
/* restore */
|
|
|
|
umask(myumask);
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
return rv;
|
|
|
|
}
|