2009-02-07 13:27:24 +01:00
|
|
|
/*-
|
|
|
|
* 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 <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2009-02-16 22:18:40 +01:00
|
|
|
#include <sys/stat.h>
|
2009-02-07 13:27:24 +01:00
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
static int unpack_archive_init(prop_dictionary_t, const char *);
|
|
|
|
static int unpack_archive_fini(struct archive *, prop_dictionary_t);
|
2009-02-07 13:27:24 +01:00
|
|
|
|
|
|
|
int
|
2009-03-17 00:32:26 +01:00
|
|
|
xbps_unpack_binary_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
|
2009-02-07 13:27:24 +01:00
|
|
|
{
|
2009-02-16 00:39:41 +01:00
|
|
|
prop_string_t filename, repoloc, arch;
|
|
|
|
char *binfile, *path;
|
2009-02-07 13:27:24 +01:00
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(pkg != NULL);
|
|
|
|
|
|
|
|
/* Append filename to the full path for binary pkg */
|
|
|
|
filename = prop_dictionary_get(pkg, "filename");
|
2009-02-16 00:39:41 +01:00
|
|
|
arch = prop_dictionary_get(pkg, "architecture");
|
2009-02-07 13:27:24 +01:00
|
|
|
if (repo)
|
|
|
|
repoloc = prop_dictionary_get(repo, "location-local");
|
|
|
|
else
|
2009-02-09 16:39:55 +01:00
|
|
|
repoloc = prop_dictionary_get(pkg, "repository");
|
2009-02-07 13:27:24 +01:00
|
|
|
|
2009-02-16 00:39:41 +01:00
|
|
|
path = xbps_append_full_path(false,
|
2009-02-07 13:27:24 +01:00
|
|
|
prop_string_cstring_nocopy(repoloc),
|
2009-02-16 00:39:41 +01:00
|
|
|
prop_string_cstring_nocopy(arch));
|
|
|
|
if (path == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
binfile = xbps_append_full_path(false, path,
|
2009-02-07 13:27:24 +01:00
|
|
|
prop_string_cstring_nocopy(filename));
|
2009-02-16 00:39:41 +01:00
|
|
|
if (binfile == NULL) {
|
|
|
|
free(path);
|
2009-02-07 13:27:24 +01:00
|
|
|
return EINVAL;
|
2009-02-16 00:39:41 +01:00
|
|
|
}
|
|
|
|
free(path);
|
2009-02-07 13:27:24 +01:00
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
rv = unpack_archive_init(pkg, binfile);
|
2009-02-07 13:27:24 +01:00
|
|
|
free(binfile);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2009-03-17 00:32:26 +01:00
|
|
|
unpack_archive_init(prop_dictionary_t pkg, const char *binfile)
|
2009-02-07 13:27:24 +01:00
|
|
|
{
|
|
|
|
struct archive *ar;
|
|
|
|
int pkg_fd, rv;
|
|
|
|
|
|
|
|
assert(pkg != NULL);
|
|
|
|
assert(binfile != NULL);
|
|
|
|
|
|
|
|
if ((pkg_fd = open(binfile, O_RDONLY)) == -1)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
ar = archive_read_new();
|
|
|
|
if (ar == NULL) {
|
|
|
|
(void)close(pkg_fd);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable support for all format and compression methods */
|
|
|
|
archive_read_support_compression_all(ar);
|
|
|
|
archive_read_support_format_all(ar);
|
|
|
|
|
|
|
|
/* 2048 is arbitrary... dunno what value is better. */
|
|
|
|
if ((rv = archive_read_open_fd(ar, pkg_fd, 2048)) != 0) {
|
|
|
|
archive_read_finish(ar);
|
|
|
|
(void)close(pkg_fd);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
rv = unpack_archive_fini(ar, pkg);
|
2009-02-07 13:27:24 +01:00
|
|
|
/*
|
|
|
|
* If installation of package was successful, make sure the package
|
|
|
|
* is really on storage (if possible).
|
|
|
|
*/
|
|
|
|
if (rv == 0)
|
2009-02-27 16:03:25 +01:00
|
|
|
if (fdatasync(pkg_fd) == -1)
|
2009-02-07 13:27:24 +01:00
|
|
|
rv = errno;
|
|
|
|
|
|
|
|
archive_read_finish(ar);
|
|
|
|
(void)close(pkg_fd);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Flags for extracting files in binary packages.
|
|
|
|
*/
|
2009-02-07 18:05:40 +01:00
|
|
|
#define EXTRACT_FLAGS ARCHIVE_EXTRACT_SECURE_NODOTDOT | \
|
2009-02-07 13:27:24 +01:00
|
|
|
ARCHIVE_EXTRACT_SECURE_SYMLINKS | \
|
2009-02-27 17:41:31 +01:00
|
|
|
ARCHIVE_EXTRACT_NO_OVERWRITE | \
|
2009-03-08 16:59:48 +01:00
|
|
|
ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER
|
2009-02-07 18:05:40 +01:00
|
|
|
#define FEXTRACT_FLAGS ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | \
|
|
|
|
ARCHIVE_EXTRACT_TIME | EXTRACT_FLAGS
|
2009-02-07 13:27:24 +01:00
|
|
|
|
2009-02-07 13:33:20 +01:00
|
|
|
/*
|
2009-02-07 13:27:24 +01:00
|
|
|
* TODO: remove printfs and return appropiate errors to be interpreted by
|
|
|
|
* the consumer.
|
|
|
|
*/
|
|
|
|
static int
|
2009-03-17 00:32:26 +01:00
|
|
|
unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg)
|
2009-02-07 13:27:24 +01:00
|
|
|
{
|
|
|
|
struct archive_entry *entry;
|
|
|
|
size_t len;
|
2009-02-26 18:01:18 +01:00
|
|
|
const char *prepost = "./INSTALL";
|
2009-03-17 00:32:26 +01:00
|
|
|
const char *pkgname, *version, *rootdir;
|
2009-02-27 16:03:25 +01:00
|
|
|
char *buf;
|
2009-03-17 00:32:26 +01:00
|
|
|
int rv = 0, flags = 0, lflags = 0;
|
2009-02-07 13:27:24 +01:00
|
|
|
bool actgt = false;
|
|
|
|
|
|
|
|
assert(ar != NULL);
|
|
|
|
assert(pkg != NULL);
|
2009-03-17 00:32:26 +01:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
flags = xbps_get_flags();
|
|
|
|
|
|
|
|
if (rootdir) {
|
|
|
|
if (chdir(rootdir) == -1)
|
|
|
|
return errno;
|
|
|
|
} else {
|
|
|
|
if (chdir("/") == -1)
|
|
|
|
return errno;
|
|
|
|
rootdir = "";
|
|
|
|
}
|
2009-02-07 13:27:24 +01:00
|
|
|
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &pkgname);
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkg, "version", &version);
|
|
|
|
|
2009-02-07 18:05:40 +01:00
|
|
|
if (getuid() == 0)
|
2009-02-17 00:08:03 +01:00
|
|
|
lflags = FEXTRACT_FLAGS;
|
2009-02-07 18:05:40 +01:00
|
|
|
else
|
2009-02-17 00:08:03 +01:00
|
|
|
lflags = EXTRACT_FLAGS;
|
2009-02-07 18:05:40 +01:00
|
|
|
|
2009-02-07 13:27:24 +01:00
|
|
|
/*
|
2009-02-26 18:01:18 +01:00
|
|
|
* This length is '.%s/metadata/%s/INSTALL' + NULL.
|
2009-02-07 13:27:24 +01:00
|
|
|
*/
|
2009-02-26 18:01:18 +01:00
|
|
|
len = strlen(XBPS_META_PATH) + strlen(pkgname) + 20;
|
|
|
|
buf = malloc(len);
|
2009-02-07 13:27:24 +01:00
|
|
|
if (buf == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2009-02-26 18:01:18 +01:00
|
|
|
if (snprintf(buf, len, ".%s/metadata/%s/INSTALL",
|
2009-02-07 13:27:24 +01:00
|
|
|
XBPS_META_PATH, pkgname) < 0) {
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) {
|
|
|
|
/*
|
|
|
|
* Run the pre installation action target if there's a script
|
|
|
|
* before writing data to disk.
|
|
|
|
*/
|
|
|
|
if (strcmp(prepost, archive_entry_pathname(entry)) == 0) {
|
|
|
|
actgt = true;
|
2009-02-16 23:00:50 +01:00
|
|
|
printf("\n");
|
2009-02-17 19:41:21 +01:00
|
|
|
(void)fflush(stdout);
|
2009-02-07 13:27:24 +01:00
|
|
|
|
|
|
|
archive_entry_set_pathname(entry, buf);
|
|
|
|
|
2009-02-27 16:03:25 +01:00
|
|
|
if (archive_read_extract(ar, entry, lflags) != 0) {
|
|
|
|
if ((rv = archive_errno(ar)) != EEXIST)
|
|
|
|
break;
|
|
|
|
}
|
2009-02-07 13:27:24 +01:00
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
if ((rv = xbps_file_exec(buf, rootdir, "pre",
|
2009-02-07 13:27:24 +01:00
|
|
|
pkgname, version, NULL)) != 0) {
|
|
|
|
printf("%s: preinst action target error %s\n",
|
|
|
|
pkgname, strerror(errno));
|
|
|
|
(void)fflush(stdout);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pass to the next entry if successful */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Extract all data from the archive now.
|
|
|
|
*/
|
2009-02-27 16:03:25 +01:00
|
|
|
if (archive_read_extract(ar, entry, lflags) != 0) {
|
|
|
|
rv = archive_errno(ar);
|
|
|
|
if (rv != EEXIST) {
|
|
|
|
printf("ERROR: couldn't unpack %s (%s), "
|
|
|
|
"exiting!\n", archive_entry_pathname(entry),
|
|
|
|
archive_error_string(ar));
|
2009-02-17 00:08:03 +01:00
|
|
|
(void)fflush(stdout);
|
2009-02-27 16:03:25 +01:00
|
|
|
break;
|
|
|
|
} else if (rv == EEXIST) {
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE) {
|
2009-02-27 16:03:25 +01:00
|
|
|
printf("WARNING: ignoring existent "
|
|
|
|
"path: %s\n",
|
|
|
|
archive_entry_pathname(entry));
|
|
|
|
(void)fflush(stdout);
|
|
|
|
}
|
|
|
|
rv = 0;
|
|
|
|
continue;
|
2009-02-17 00:08:03 +01:00
|
|
|
}
|
2009-02-07 13:27:24 +01:00
|
|
|
}
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE) {
|
2009-02-17 00:08:03 +01:00
|
|
|
printf(" %s\n", archive_entry_pathname(entry));
|
|
|
|
(void)fflush(stdout);
|
|
|
|
}
|
2009-02-07 13:27:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == 0 && actgt) {
|
|
|
|
/*
|
|
|
|
* Run the post installaction action target, if package
|
|
|
|
* contains the script.
|
|
|
|
*/
|
2009-03-17 00:32:26 +01:00
|
|
|
if ((rv = xbps_file_exec(buf, rootdir, "post",
|
2009-02-07 13:27:24 +01:00
|
|
|
pkgname, version, NULL)) != 0) {
|
|
|
|
printf("%s: postinst action target error %s\n",
|
|
|
|
pkgname, strerror(errno));
|
|
|
|
(void)fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|