2009-02-03 03:18:00 +01:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 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>
|
|
|
|
#include <ctype.h>
|
2009-02-03 22:37:20 +01:00
|
|
|
#include <dirent.h>
|
2009-02-03 03:18:00 +01:00
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
2009-02-26 05:41:49 +01:00
|
|
|
static int remove_pkg_files(prop_object_t, void *, bool *);
|
|
|
|
|
2009-02-03 03:18:00 +01:00
|
|
|
int
|
|
|
|
xbps_unregister_pkg(const char *pkgname)
|
|
|
|
{
|
2009-04-04 19:32:39 +02:00
|
|
|
const char *rootdir;
|
2009-02-03 03:18:00 +01:00
|
|
|
char *plist;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
plist = xbps_xasprintf("%s/%s/%s", rootdir,
|
|
|
|
XBPS_META_PATH, XBPS_REGPKGDB);
|
2009-02-03 03:18:00 +01:00
|
|
|
if (plist == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (!xbps_remove_pkg_dict_from_file(pkgname, plist))
|
|
|
|
rv = errno;
|
2009-02-03 16:07:08 +01:00
|
|
|
|
|
|
|
free(plist);
|
2009-02-03 03:18:00 +01:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-02-03 22:37:20 +01:00
|
|
|
static int
|
2009-03-17 00:32:26 +01:00
|
|
|
xbps_remove_binary_pkg_meta(const char *pkgname)
|
2009-02-03 22:37:20 +01:00
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
2009-04-04 19:32:39 +02:00
|
|
|
const char *rootdir;
|
|
|
|
char *metadir, *path;
|
2009-03-17 00:32:26 +01:00
|
|
|
int flags = 0, rv = 0;
|
2009-02-03 22:37:20 +01:00
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
2009-04-04 19:32:39 +02:00
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
flags = xbps_get_flags();
|
2009-02-03 22:37:20 +01:00
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
metadir = xbps_xasprintf("%s/%s/metadata/%s", rootdir,
|
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (metadir == NULL)
|
|
|
|
return errno;
|
2009-02-03 22:37:20 +01:00
|
|
|
|
|
|
|
dirp = opendir(metadir);
|
|
|
|
if (dirp == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if ((strcmp(dp->d_name, ".") == 0) ||
|
|
|
|
(strcmp(dp->d_name, "..") == 0))
|
|
|
|
continue;
|
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
path = xbps_xasprintf("%s/%s", metadir, dp->d_name);
|
|
|
|
if (path == NULL) {
|
2009-02-03 22:37:20 +01:00
|
|
|
(void)closedir(dirp);
|
2009-04-04 19:32:39 +02:00
|
|
|
free(metadir);
|
2009-02-03 22:37:20 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = unlink(path)) == -1) {
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-17 00:08:03 +01:00
|
|
|
printf("WARNING: can't remove %s (%s)\n",
|
|
|
|
pkgname, strerror(errno));
|
2009-02-03 22:37:20 +01:00
|
|
|
}
|
2009-04-04 19:32:39 +02:00
|
|
|
free(path);
|
2009-02-03 22:37:20 +01:00
|
|
|
}
|
|
|
|
(void)closedir(dirp);
|
|
|
|
rv = rmdir(metadir);
|
2009-04-04 19:32:39 +02:00
|
|
|
free(metadir);
|
2009-02-03 22:37:20 +01:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-02-26 05:41:49 +01:00
|
|
|
static int
|
|
|
|
remove_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
|
|
|
|
{
|
2009-02-27 17:41:31 +01:00
|
|
|
prop_bool_t bobj;
|
2009-04-04 19:32:39 +02:00
|
|
|
const char *file, *rootdir, *sha256, *type;
|
2009-02-26 05:41:49 +01:00
|
|
|
char *path = NULL;
|
2009-03-17 00:32:26 +01:00
|
|
|
int flags = 0, rv = 0;
|
2009-02-26 05:41:49 +01:00
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
(void)arg;
|
2009-02-26 05:41:49 +01:00
|
|
|
(void)loop_done;
|
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
flags = xbps_get_flags();
|
|
|
|
|
2009-03-02 02:47:12 +01:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file))
|
2009-02-28 17:32:38 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
path = xbps_xasprintf("%s/%s", rootdir, file);
|
2009-02-28 17:32:38 +01:00
|
|
|
if (path == NULL)
|
|
|
|
return EINVAL;
|
2009-02-26 05:41:49 +01:00
|
|
|
|
2009-03-02 02:47:12 +01:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj, "type", &type)) {
|
|
|
|
free(path);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-02-28 17:32:38 +01:00
|
|
|
if (strcmp(type, "file") == 0) {
|
2009-02-26 05:41:49 +01:00
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "sha256", &sha256);
|
2009-02-27 17:41:31 +01:00
|
|
|
rv = xbps_check_file_hash(path, sha256);
|
|
|
|
if (rv != 0 && rv != ERANGE) {
|
|
|
|
free(path);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
bobj = prop_dictionary_get(obj, "conf_file");
|
|
|
|
if (bobj != NULL) {
|
|
|
|
/*
|
|
|
|
* If hash is the same than the one provided by
|
|
|
|
* package, that means the file hasn't been changed
|
|
|
|
* and therefore can be removed. Otherwise keep it.
|
|
|
|
*/
|
|
|
|
if (rv == ERANGE)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == ERANGE) {
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 05:41:49 +01:00
|
|
|
printf("WARNING: SHA256 doesn't match for "
|
|
|
|
"file %s, ignoring...\n", file);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-02-28 17:32:38 +01:00
|
|
|
if ((rv = remove(path)) == -1) {
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 05:41:49 +01:00
|
|
|
printf("WARNING: can't remove file %s (%s)\n",
|
|
|
|
file, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 05:41:49 +01:00
|
|
|
printf("Removed file: %s\n", file);
|
|
|
|
|
|
|
|
goto out;
|
2009-02-28 17:32:38 +01:00
|
|
|
} else if (strcmp(type, "dir") == 0) {
|
2009-02-27 17:41:31 +01:00
|
|
|
if ((bobj = prop_dictionary_get(obj, "keep")) != NULL) {
|
|
|
|
/* Skip permanent directory. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-26 05:41:49 +01:00
|
|
|
if ((rv = rmdir(path)) == -1) {
|
|
|
|
if (errno == ENOTEMPTY)
|
|
|
|
goto out;
|
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE) {
|
2009-02-26 05:41:49 +01:00
|
|
|
printf("WARNING: can't remove "
|
|
|
|
"directory %s (%s)\n", file,
|
|
|
|
strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 05:41:49 +01:00
|
|
|
printf("Removed directory: %s\n", file);
|
|
|
|
}
|
2009-02-28 17:32:38 +01:00
|
|
|
} else if (strcmp(type, "link") == 0) {
|
|
|
|
if ((rv = remove(path)) == -1) {
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-28 17:32:38 +01:00
|
|
|
printf("WARNING: can't remove link %s (%s)\n",
|
|
|
|
file, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-17 00:32:26 +01:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-28 17:32:38 +01:00
|
|
|
printf("Removed link: %s\n", file);
|
2009-02-26 05:41:49 +01:00
|
|
|
}
|
2009-02-28 17:32:38 +01:00
|
|
|
|
2009-02-26 05:41:49 +01:00
|
|
|
out:
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-03 03:18:00 +01:00
|
|
|
int
|
2009-04-02 03:08:20 +02:00
|
|
|
xbps_remove_binary_pkg(const char *pkgname, bool update)
|
2009-02-03 03:18:00 +01:00
|
|
|
{
|
2009-02-27 16:03:25 +01:00
|
|
|
prop_dictionary_t dict;
|
2009-03-17 00:32:26 +01:00
|
|
|
const char *rootdir = xbps_get_rootdir();
|
2009-04-04 19:32:39 +02:00
|
|
|
char *path, *buf;
|
2009-02-03 16:07:08 +01:00
|
|
|
int fd, rv = 0;
|
|
|
|
bool prepostf = false;
|
2009-02-03 03:18:00 +01:00
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
2009-03-17 00:32:26 +01:00
|
|
|
if (rootdir) {
|
|
|
|
if (chdir(rootdir) == -1)
|
2009-03-12 09:23:40 +01:00
|
|
|
return errno;
|
|
|
|
} else {
|
|
|
|
if (chdir("/") == -1)
|
|
|
|
return errno;
|
2009-03-17 00:32:26 +01:00
|
|
|
rootdir = "";
|
2009-03-12 09:23:40 +01:00
|
|
|
}
|
2009-02-03 03:18:00 +01:00
|
|
|
|
2009-02-03 16:51:42 +01:00
|
|
|
/* Check if pkg is installed */
|
2009-02-05 16:53:49 +01:00
|
|
|
if (xbps_check_is_installed_pkgname(pkgname) == false)
|
2009-02-03 16:51:42 +01:00
|
|
|
return ENOENT;
|
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
buf = xbps_xasprintf("%s/%s/metadata/%s/REMOVE", rootdir,
|
|
|
|
XBPS_META_PATH, pkgname);
|
2009-02-03 16:07:08 +01:00
|
|
|
if (buf == NULL)
|
|
|
|
return errno;
|
|
|
|
|
2009-02-27 16:03:25 +01:00
|
|
|
/*
|
|
|
|
* Find out if the REMOVE file exists.
|
|
|
|
*/
|
2009-02-03 16:07:08 +01:00
|
|
|
if ((fd = open(buf, O_RDONLY)) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2009-02-26 05:41:49 +01:00
|
|
|
free(buf);
|
|
|
|
return errno;
|
2009-02-03 16:07:08 +01:00
|
|
|
}
|
|
|
|
} else {
|
2009-02-27 16:03:25 +01:00
|
|
|
/*
|
|
|
|
* Run the pre remove action.
|
|
|
|
*/
|
2009-02-03 16:07:08 +01:00
|
|
|
(void)close(fd);
|
|
|
|
prepostf = true;
|
2009-03-12 09:23:40 +01:00
|
|
|
(void)printf("\n");
|
|
|
|
(void)fflush(stdout);
|
2009-03-17 00:32:26 +01:00
|
|
|
rv = xbps_file_exec(buf, rootdir, "pre", pkgname, NULL);
|
2009-02-27 16:03:25 +01:00
|
|
|
if (rv != 0) {
|
2009-02-03 16:07:08 +01:00
|
|
|
printf("%s: prerm action target error (%s)\n", pkgname,
|
|
|
|
strerror(errno));
|
2009-02-26 05:41:49 +01:00
|
|
|
free(buf);
|
|
|
|
return rv;
|
2009-02-03 16:07:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-26 05:41:49 +01:00
|
|
|
/*
|
|
|
|
* Iterate over the pkg file list dictionary and remove all
|
|
|
|
* files/dirs associated.
|
|
|
|
*/
|
2009-04-04 19:32:39 +02:00
|
|
|
path = xbps_xasprintf("%s/%s/metadata/%s/files.plist",
|
2009-03-17 00:32:26 +01:00
|
|
|
rootdir, XBPS_META_PATH, pkgname);
|
2009-04-04 19:32:39 +02:00
|
|
|
if (path == NULL) {
|
|
|
|
free(buf);
|
|
|
|
return errno;
|
|
|
|
}
|
2009-02-03 03:18:00 +01:00
|
|
|
|
2009-02-27 16:03:25 +01:00
|
|
|
dict = prop_dictionary_internalize_from_file(path);
|
|
|
|
if (dict == NULL) {
|
2009-02-26 05:41:49 +01:00
|
|
|
free(buf);
|
2009-04-04 19:32:39 +02:00
|
|
|
free(path);
|
2009-02-26 05:41:49 +01:00
|
|
|
return errno;
|
2009-02-03 16:07:08 +01:00
|
|
|
}
|
2009-04-04 19:32:39 +02:00
|
|
|
free(path);
|
2009-02-03 03:18:00 +01:00
|
|
|
|
2009-02-27 16:03:25 +01:00
|
|
|
rv = xbps_callback_array_iter_in_dict(dict, "filelist",
|
2009-03-17 00:32:26 +01:00
|
|
|
remove_pkg_files, NULL);
|
2009-02-26 05:41:49 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
free(buf);
|
2009-02-27 16:03:25 +01:00
|
|
|
prop_object_release(dict);
|
2009-02-26 05:41:49 +01:00
|
|
|
return rv;
|
2009-02-03 03:18:00 +01:00
|
|
|
}
|
2009-02-27 16:03:25 +01:00
|
|
|
prop_object_release(dict);
|
2009-02-03 03:18:00 +01:00
|
|
|
|
2009-02-27 16:03:25 +01:00
|
|
|
/*
|
|
|
|
* Run the post remove action if REMOVE file is there.
|
|
|
|
*/
|
|
|
|
if (prepostf) {
|
2009-03-17 00:32:26 +01:00
|
|
|
if ((rv = xbps_file_exec(buf, rootdir, "post",
|
2009-02-26 05:41:49 +01:00
|
|
|
pkgname, NULL)) != 0) {
|
|
|
|
printf("%s: postrm action target error (%s)\n",
|
|
|
|
pkgname, strerror(errno));
|
|
|
|
free(buf);
|
|
|
|
return rv;
|
2009-02-03 16:07:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
|
2009-02-27 16:03:25 +01:00
|
|
|
/*
|
2009-04-02 03:08:20 +02:00
|
|
|
* Update the required_by array of all required dependencies
|
|
|
|
* and unregister package if this is really a removal and
|
|
|
|
* not an update.
|
2009-02-27 16:03:25 +01:00
|
|
|
*/
|
2009-04-02 03:08:20 +02:00
|
|
|
if (update == false) {
|
|
|
|
rv = xbps_requiredby_pkg_remove(pkgname);
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
/*
|
|
|
|
* Unregister pkg from database.
|
|
|
|
*/
|
|
|
|
rv = xbps_unregister_pkg(pkgname);
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
}
|
2009-02-27 16:03:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove pkg metadata directory.
|
|
|
|
*/
|
2009-03-17 00:32:26 +01:00
|
|
|
return xbps_remove_binary_pkg_meta(pkgname);
|
2009-02-03 03:18:00 +01:00
|
|
|
}
|