2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008-2009 Juan Romero Pardines.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
2009-12-02 11:01:03 +05:30
|
|
|
static int unpack_archive_fini(struct archive *, prop_dictionary_t);
|
2009-09-30 21:59:21 +05:30
|
|
|
static void set_extract_flags(int *);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2009-10-27 06:16:00 +05:30
|
|
|
int SYMEXPORT
|
2009-12-02 11:01:03 +05:30
|
|
|
xbps_unpack_binary_pkg(prop_dictionary_t pkg)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2009-11-28 07:51:29 +05:30
|
|
|
const char *pkgname, *repoloc;
|
2009-11-28 07:08:41 +05:30
|
|
|
struct archive *ar = NULL;
|
2009-11-26 07:52:50 +05:30
|
|
|
char *binfile = NULL;
|
2009-08-17 22:37:20 +05:30
|
|
|
int pkg_fd, rv = 0;
|
|
|
|
|
|
|
|
assert(pkg != NULL);
|
|
|
|
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &pkgname))
|
|
|
|
return errno;
|
2009-11-28 07:51:29 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(pkg, "repository", &repoloc))
|
|
|
|
return errno;
|
|
|
|
binfile = xbps_get_binpkg_local_path(pkg, repoloc);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (binfile == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if ((pkg_fd = open(binfile, O_RDONLY)) == -1) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ar = archive_read_new();
|
|
|
|
if (ar == NULL) {
|
2009-11-26 07:52:50 +05:30
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable support for tar format and all compression methods.
|
|
|
|
*/
|
|
|
|
archive_read_support_compression_all(ar);
|
|
|
|
archive_read_support_format_tar(ar);
|
|
|
|
|
|
|
|
if ((rv = archive_read_open_fd(ar, pkg_fd,
|
|
|
|
ARCHIVE_READ_BLOCKSIZE)) != 0)
|
2009-11-26 07:52:50 +05:30
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2009-12-02 11:01:03 +05:30
|
|
|
if ((rv = unpack_archive_fini(ar, pkg)) == 0) {
|
2009-11-28 07:19:36 +05:30
|
|
|
/*
|
|
|
|
* If installation of package was successful, make sure
|
|
|
|
* its files are written in storage (if possible).
|
|
|
|
*/
|
2009-08-19 02:20:29 +05:30
|
|
|
if (fsync(pkg_fd) == -1) {
|
|
|
|
rv = errno;
|
2009-11-26 07:52:50 +05:30
|
|
|
goto out;
|
2009-08-19 02:20:29 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Set package state to unpacked.
|
|
|
|
*/
|
|
|
|
rv = xbps_set_pkg_state_installed(pkgname,
|
|
|
|
XBPS_PKG_STATE_UNPACKED);
|
|
|
|
}
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2009-08-19 02:20:29 +05:30
|
|
|
out:
|
2009-11-26 07:52:50 +05:30
|
|
|
if (ar)
|
|
|
|
archive_read_finish(ar);
|
|
|
|
if (pkg_fd != -1)
|
|
|
|
(void)close(pkg_fd);
|
|
|
|
if (binfile)
|
|
|
|
free(binfile);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-09-30 21:59:21 +05:30
|
|
|
set_extract_flags(int *flags)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2009-10-21 21:46:17 +05:30
|
|
|
*flags = 0;
|
2009-08-17 22:37:20 +05:30
|
|
|
if (getuid() == 0)
|
2009-09-30 21:59:21 +05:30
|
|
|
*flags = FEXTRACT_FLAGS;
|
2009-08-17 22:37:20 +05:30
|
|
|
else
|
2009-09-30 21:59:21 +05:30
|
|
|
*flags = EXTRACT_FLAGS;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: remove printfs and return appropiate errors to be interpreted by
|
|
|
|
* the consumer.
|
|
|
|
*/
|
|
|
|
static int
|
2009-12-02 11:01:03 +05:30
|
|
|
unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2009-11-19 10:40:42 +05:30
|
|
|
prop_dictionary_t filesd = NULL, old_filesd = NULL;
|
2009-08-17 22:37:20 +05:30
|
|
|
struct archive_entry *entry;
|
2009-12-07 11:54:22 +05:30
|
|
|
size_t entry_idx = 0;
|
2009-10-21 21:46:17 +05:30
|
|
|
const char *pkgname, *version, *rootdir, *entry_str;
|
2009-08-17 22:37:20 +05:30
|
|
|
char *buf, *buf2;
|
2009-09-30 21:59:21 +05:30
|
|
|
int rv = 0, flags, lflags;
|
2009-12-09 20:44:35 +05:30
|
|
|
bool essential, preserve, actgt, skip_entry, update;
|
2009-12-07 11:54:22 +05:30
|
|
|
bool props_plist_found, files_plist_found;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
assert(ar != NULL);
|
|
|
|
assert(pkg != NULL);
|
2009-12-02 11:01:03 +05:30
|
|
|
|
2009-12-09 20:44:35 +05:30
|
|
|
essential = preserve = actgt = skip_entry = update = false;
|
2009-12-07 11:54:22 +05:30
|
|
|
props_plist_found = files_plist_found = false;
|
2009-08-17 22:37:20 +05:30
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
flags = xbps_get_flags();
|
|
|
|
|
|
|
|
if (strcmp(rootdir, "") == 0)
|
|
|
|
rootdir = "/";
|
|
|
|
|
|
|
|
if (chdir(rootdir) == -1)
|
|
|
|
return errno;
|
|
|
|
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &pkgname))
|
|
|
|
return errno;
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(pkg, "version", &version))
|
|
|
|
return errno;
|
2009-12-02 11:01:03 +05:30
|
|
|
/*
|
|
|
|
* The following two objects are OPTIONAL.
|
|
|
|
*/
|
|
|
|
prop_dictionary_get_bool(pkg, "essential", &essential);
|
|
|
|
prop_dictionary_get_bool(pkg, "preserve", &preserve);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2009-12-09 20:44:35 +05:30
|
|
|
if (xbps_check_is_installed_pkgname(pkgname))
|
|
|
|
update = true;
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) {
|
2009-12-07 11:54:22 +05:30
|
|
|
if (entry_idx >= 5) {
|
|
|
|
/*
|
|
|
|
* If we have processed 6 entries and the two
|
|
|
|
* required metadata files weren't found, bail out.
|
|
|
|
* This is not an XBPS binary package.
|
|
|
|
*/
|
|
|
|
if (!props_plist_found && !files_plist_found)
|
|
|
|
return ENOPKG;
|
|
|
|
}
|
|
|
|
|
2009-10-21 21:46:17 +05:30
|
|
|
entry_str = archive_entry_pathname(entry);
|
2009-09-30 21:59:21 +05:30
|
|
|
set_extract_flags(&lflags);
|
2009-10-21 21:46:17 +05:30
|
|
|
if (((strcmp("./INSTALL", entry_str)) == 0) ||
|
|
|
|
((strcmp("./REMOVE", entry_str)) == 0) ||
|
2009-10-21 21:57:18 +05:30
|
|
|
((strcmp("./files.plist", entry_str)) == 0) ||
|
2009-10-21 21:46:17 +05:30
|
|
|
((strcmp("./props.plist", entry_str)) == 0) || essential) {
|
|
|
|
/*
|
|
|
|
* Always overwrite files in essential packages,
|
|
|
|
* and metadata files.
|
|
|
|
*/
|
|
|
|
lflags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE;
|
|
|
|
lflags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the pre INSTALL action if the file is there.
|
|
|
|
*/
|
2009-10-21 21:46:17 +05:30
|
|
|
if (strcmp("./INSTALL", entry_str) == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
buf = xbps_xasprintf(".%s/metadata/%s/INSTALL",
|
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (buf == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
actgt = true;
|
|
|
|
archive_entry_set_pathname(entry, buf);
|
2009-12-11 15:28:46 +05:30
|
|
|
archive_entry_set_mode(entry, 0750);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2009-09-30 21:59:21 +05:30
|
|
|
if (archive_read_extract(ar, entry, lflags) != 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
if ((rv = archive_errno(ar)) != EEXIST) {
|
|
|
|
free(buf);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2009-12-09 20:44:35 +05:30
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
if ((rv = xbps_file_chdir_exec(rootdir, buf, "pre",
|
2009-12-09 20:44:35 +05:30
|
|
|
pkgname, version, update ? "yes" : "no",
|
|
|
|
NULL)) != 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
free(buf);
|
|
|
|
printf("%s: preinst action target error %s\n",
|
|
|
|
pkgname, strerror(errno));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/* pass to the next entry if successful */
|
|
|
|
free(buf);
|
2009-12-07 11:54:22 +05:30
|
|
|
entry_idx++;
|
2009-08-17 22:37:20 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unpack metadata files in final directory.
|
|
|
|
*/
|
2009-10-21 21:46:17 +05:30
|
|
|
} else if (strcmp("./REMOVE", entry_str) == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
buf2 = xbps_xasprintf(".%s/metadata/%s/REMOVE",
|
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (buf2 == NULL)
|
|
|
|
return errno;
|
|
|
|
archive_entry_set_pathname(entry, buf2);
|
2009-12-11 15:28:46 +05:30
|
|
|
archive_entry_set_mode(entry, 0750);
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
free(buf2);
|
|
|
|
buf2 = NULL;
|
2009-10-21 21:46:17 +05:30
|
|
|
} else if (strcmp("./files.plist", entry_str) == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Now we have a dictionary from the entry
|
|
|
|
* in memory. Will be written to disk later, when
|
|
|
|
* all files are extracted.
|
|
|
|
*/
|
|
|
|
filesd = xbps_read_dict_from_archive_entry(ar, entry);
|
|
|
|
if (filesd == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
/* Pass to next entry */
|
2009-12-07 11:54:22 +05:30
|
|
|
files_plist_found = true;
|
|
|
|
entry_idx++;
|
2009-08-17 22:37:20 +05:30
|
|
|
continue;
|
|
|
|
|
2009-10-21 21:46:17 +05:30
|
|
|
} else if (strcmp("./props.plist", entry_str) == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
buf2 = xbps_xasprintf(".%s/metadata/%s/props.plist",
|
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (buf2 == NULL)
|
|
|
|
return errno;
|
|
|
|
archive_entry_set_pathname(entry, buf2);
|
|
|
|
free(buf2);
|
2009-12-07 11:54:22 +05:30
|
|
|
props_plist_found = true;
|
2009-10-21 21:46:17 +05:30
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Handle configuration files.
|
|
|
|
*/
|
2009-11-28 10:13:21 +05:30
|
|
|
if ((rv = xbps_config_file_from_archive_entry(filesd,
|
|
|
|
entry, pkgname, &lflags, &skip_entry)) != 0) {
|
2009-10-21 21:46:17 +05:30
|
|
|
prop_object_release(filesd);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (skip_entry) {
|
|
|
|
archive_read_data_skip(ar);
|
|
|
|
skip_entry = false;
|
2009-12-07 11:54:22 +05:30
|
|
|
entry_idx++;
|
2009-10-21 21:46:17 +05:30
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2009-10-21 21:46:17 +05:30
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Extract entry from archive.
|
|
|
|
*/
|
2009-09-30 21:59:21 +05:30
|
|
|
if (archive_read_extract(ar, entry, lflags) != 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = archive_errno(ar);
|
|
|
|
if (rv != EEXIST) {
|
|
|
|
printf("ERROR: %s...exiting!\n",
|
|
|
|
archive_error_string(ar));
|
|
|
|
return rv;;
|
|
|
|
} else if (rv == EEXIST) {
|
|
|
|
if (flags & XBPS_FLAG_VERBOSE) {
|
|
|
|
printf("WARNING: ignoring existent "
|
|
|
|
"path: %s\n",
|
|
|
|
archive_entry_pathname(entry));
|
|
|
|
}
|
|
|
|
rv = 0;
|
2009-12-07 11:54:22 +05:30
|
|
|
entry_idx++;
|
2009-08-17 22:37:20 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-12-07 11:54:22 +05:30
|
|
|
entry_idx++;
|
2009-08-17 22:37:20 +05:30
|
|
|
if (flags & XBPS_FLAG_VERBOSE)
|
|
|
|
printf(" %s\n", archive_entry_pathname(entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = archive_errno(ar)) == 0) {
|
2009-11-09 07:00:04 +05:30
|
|
|
buf2 = xbps_xasprintf(".%s/metadata/%s/files.plist",
|
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (buf2 == NULL) {
|
|
|
|
prop_object_release(filesd);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check if files.plist exists and pkg is marked as
|
2009-12-02 11:01:03 +05:30
|
|
|
* essential and NOT preserve, in that case we need to check
|
|
|
|
* for obsolete files and remove them if necessary.
|
2009-11-09 07:00:04 +05:30
|
|
|
*/
|
2009-12-02 11:01:03 +05:30
|
|
|
if (!preserve && essential && (access(buf2, R_OK) == 0)) {
|
2009-11-09 07:00:04 +05:30
|
|
|
old_filesd =
|
|
|
|
prop_dictionary_internalize_from_file(buf2);
|
|
|
|
if (old_filesd == NULL) {
|
|
|
|
prop_object_release(filesd);
|
|
|
|
free(buf2);
|
|
|
|
return errno;
|
|
|
|
}
|
2009-11-28 10:13:21 +05:30
|
|
|
rv = xbps_remove_obsoletes(old_filesd, filesd);
|
2009-11-09 07:00:04 +05:30
|
|
|
if (rv != 0) {
|
|
|
|
prop_object_release(old_filesd);
|
|
|
|
prop_object_release(filesd);
|
|
|
|
free(buf2);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
prop_object_release(old_filesd);
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Now that all files were successfully unpacked, we
|
|
|
|
* can safely externalize files.plist because the path
|
|
|
|
* is reachable.
|
|
|
|
*/
|
|
|
|
if (!prop_dictionary_externalize_to_file(filesd, buf2)) {
|
|
|
|
prop_object_release(filesd);
|
|
|
|
free(buf2);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
free(buf2);
|
|
|
|
}
|
|
|
|
prop_object_release(filesd);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|