void-packages/lib/install.c
Juan RP fa3b59b246 Another checkpoint for installing binary packages.
At least now dependencies are tracked, not sure it will be
enough for real life :-)

--HG--
extra : convert_revision : 962de22d515efa5599c148f918c14d32b5d9496d
2008-12-24 07:20:19 +01:00

155 lines
4.3 KiB
C

/*-
* Copyright (c) 2008 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 <xbps_api.h>
static int unpack_archive_cb(struct archive *);
int
xbps_install_binary_pkg(const char *pkgname, const char *dest)
{
prop_dictionary_t repo_dict, pkg_rdict, dict;
prop_object_t obj;
const char *repo = "/storage/xbps/binpkgs/pkg-index.plist";
char dbfile[PATH_MAX], binfile[PATH_MAX];
int rv = 0;
assert(pkgname != NULL);
if (dest) {
if ((rv = chdir(dest)) != 0)
return XBPS_PKG_ECHDIRDEST;
}
/* Get pkg metadata from a repository */
repo_dict = prop_dictionary_internalize_from_file(repo);
if (repo_dict == NULL)
return -1;
pkg_rdict = xbps_find_pkg_in_dict(repo_dict, pkgname);
if (pkg_rdict == NULL) {
prop_object_release(repo_dict);
return XBPS_PKG_ENOTINREPO;
}
if (!xbps_append_full_path(dbfile, NULL, XBPS_REGPKGDB)) {
prop_object_release(repo_dict);
return EINVAL;
}
/* Check if package is already installed. */
dict = prop_dictionary_internalize_from_file(dbfile);
if (dict && xbps_find_pkg_in_dict(dict, pkgname)) {
prop_object_release(repo_dict);
prop_object_release(dict);
return XBPS_PKG_EEXIST;
}
/* Append filename to the full path for binary pkg */
obj = prop_dictionary_get(pkg_rdict, "filename");
if (!xbps_append_full_path(binfile, "/storage/xbps/binpkgs",
prop_string_cstring_nocopy(obj))) {
prop_object_release(repo_dict);
return EINVAL;
}
obj = prop_dictionary_get(pkg_rdict, "version");
printf("=> Found package: %s-%s.",
pkgname, prop_string_cstring_nocopy(obj));
printf("\n");
(void)fflush(stdout);
printf("==> Checking dependencies... ");
(void)fflush(stdout);
/* Looks like it's not, check dependencies and install */
switch (xbps_check_reqdeps_in_pkg(dbfile, pkg_rdict)) {
case -1:
/* There was an error checking pkg deps */
prop_object_release(repo_dict);
printf("error, exiting!\n");
fflush(stdout);
return XBPS_PKG_EINDEPS;
case 0:
/* Package has no deps, just install it */
printf("none, unpacking... ");
(void)fflush(stdout);
rv = xbps_unpack_binary_pkg(binfile, unpack_archive_cb);
break;
case 1:
/* Package needs deps */
break;
}
prop_object_release(repo_dict);
return rv;
}
static int
unpack_archive_cb(struct archive *ar)
{
struct archive_entry *entry;
int rv = 0;
while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) {
if ((rv = archive_read_extract(ar, entry, 0)) != 0) {
printf("couldn't write %s (%s), ignoring!\n",
archive_entry_pathname(entry), strerror(errno));
}
}
printf("done.\n");
archive_read_finish(ar);
return rv;
}
int
xbps_unpack_binary_pkg(const char *filename, int (*cb)(struct archive *))
{
struct archive *ar;
int rv;
assert(filename != NULL);
ar = archive_read_new();
if (ar == NULL)
return ENOMEM;
/* Enable support for all format and compression methods */
archive_read_support_compression_all(ar);
archive_read_support_format_all(ar);
if ((rv = archive_read_open_filename(ar, filename, 2048)) != 0) {
archive_read_finish(ar);
return rv;
}
return (*cb)(ar);
}