2009-02-03 02:18:00 +00: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 21:37:20 +00:00
|
|
|
#include <dirent.h>
|
2009-02-03 02:18:00 +00:00
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
2009-02-26 04:41:49 +00:00
|
|
|
static int remove_pkg_files(prop_object_t, void *, bool *);
|
|
|
|
|
2009-02-03 02:18:00 +00:00
|
|
|
int
|
|
|
|
xbps_unregister_pkg(const char *pkgname)
|
|
|
|
{
|
|
|
|
char *plist;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
|
|
|
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
|
|
|
|
if (plist == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (!xbps_remove_pkg_dict_from_file(pkgname, plist))
|
|
|
|
rv = errno;
|
2009-02-03 15:07:08 +00:00
|
|
|
|
|
|
|
free(plist);
|
2009-02-03 02:18:00 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-02-03 21:37:20 +00:00
|
|
|
static int
|
2009-03-16 23:32:26 +00:00
|
|
|
xbps_remove_binary_pkg_meta(const char *pkgname)
|
2009-02-03 21:37:20 +00:00
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
2009-03-16 23:32:26 +00:00
|
|
|
const char *rootdir = xbps_get_rootdir();
|
2009-02-03 21:37:20 +00:00
|
|
|
char metadir[PATH_MAX - 1], path[PATH_MAX - 1];
|
2009-03-16 23:32:26 +00:00
|
|
|
int flags = 0, rv = 0;
|
2009-02-03 21:37:20 +00:00
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
if (rootdir == NULL)
|
|
|
|
rootdir = "";
|
|
|
|
flags = xbps_get_flags();
|
2009-02-03 21:37:20 +00:00
|
|
|
|
|
|
|
(void)snprintf(metadir, sizeof(metadir), "%s%s/metadata/%s",
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir, XBPS_META_PATH, pkgname);
|
2009-02-03 21:37:20 +00: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;
|
|
|
|
|
|
|
|
if (snprintf(path, sizeof(path), "%s%s/metadata/%s/%s",
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir, XBPS_META_PATH, pkgname, dp->d_name) < 0) {
|
2009-02-03 21:37:20 +00:00
|
|
|
(void)closedir(dirp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = unlink(path)) == -1) {
|
2009-03-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-16 23:08:03 +00:00
|
|
|
printf("WARNING: can't remove %s (%s)\n",
|
|
|
|
pkgname, strerror(errno));
|
2009-02-03 21:37:20 +00:00
|
|
|
}
|
|
|
|
(void)memset(&path, 0, sizeof(path));
|
|
|
|
}
|
|
|
|
(void)closedir(dirp);
|
|
|
|
rv = rmdir(metadir);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-02-26 04:41:49 +00:00
|
|
|
static int
|
|
|
|
remove_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
|
|
|
|
{
|
2009-02-27 16:41:31 +00:00
|
|
|
prop_bool_t bobj;
|
2009-03-16 23:32:26 +00:00
|
|
|
const char *file = NULL, *rootdir, *sha256, *type;
|
2009-02-26 04:41:49 +00:00
|
|
|
char *path = NULL;
|
2009-03-16 23:32:26 +00:00
|
|
|
int flags = 0, rv = 0;
|
2009-02-26 04:41:49 +00:00
|
|
|
|
2009-03-16 23:32:26 +00:00
|
|
|
(void)arg;
|
2009-02-26 04:41:49 +00:00
|
|
|
(void)loop_done;
|
|
|
|
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
if (rootdir == NULL)
|
|
|
|
rootdir = "";
|
|
|
|
flags = xbps_get_flags();
|
|
|
|
|
2009-03-02 01:47:12 +00:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file))
|
2009-02-28 16:32:38 +00:00
|
|
|
return EINVAL;
|
|
|
|
|
2009-03-16 23:32:26 +00:00
|
|
|
path = xbps_append_full_path(false, rootdir, file);
|
2009-02-28 16:32:38 +00:00
|
|
|
if (path == NULL)
|
|
|
|
return EINVAL;
|
2009-02-26 04:41:49 +00:00
|
|
|
|
2009-03-02 01:47:12 +00:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj, "type", &type)) {
|
|
|
|
free(path);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-02-28 16:32:38 +00:00
|
|
|
if (strcmp(type, "file") == 0) {
|
2009-02-26 04:41:49 +00:00
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "sha256", &sha256);
|
2009-02-27 16:41:31 +00: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-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 04:41:49 +00:00
|
|
|
printf("WARNING: SHA256 doesn't match for "
|
|
|
|
"file %s, ignoring...\n", file);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-02-28 16:32:38 +00:00
|
|
|
if ((rv = remove(path)) == -1) {
|
2009-03-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 04:41:49 +00:00
|
|
|
printf("WARNING: can't remove file %s (%s)\n",
|
|
|
|
file, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 04:41:49 +00:00
|
|
|
printf("Removed file: %s\n", file);
|
|
|
|
|
|
|
|
goto out;
|
2009-02-28 16:32:38 +00:00
|
|
|
} else if (strcmp(type, "dir") == 0) {
|
2009-02-27 16:41:31 +00:00
|
|
|
if ((bobj = prop_dictionary_get(obj, "keep")) != NULL) {
|
|
|
|
/* Skip permanent directory. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-26 04:41:49 +00:00
|
|
|
if ((rv = rmdir(path)) == -1) {
|
|
|
|
if (errno == ENOTEMPTY)
|
|
|
|
goto out;
|
|
|
|
|
2009-03-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE) {
|
2009-02-26 04:41:49 +00:00
|
|
|
printf("WARNING: can't remove "
|
|
|
|
"directory %s (%s)\n", file,
|
|
|
|
strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-26 04:41:49 +00:00
|
|
|
printf("Removed directory: %s\n", file);
|
|
|
|
}
|
2009-02-28 16:32:38 +00:00
|
|
|
} else if (strcmp(type, "link") == 0) {
|
|
|
|
if ((rv = remove(path)) == -1) {
|
2009-03-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-28 16:32:38 +00:00
|
|
|
printf("WARNING: can't remove link %s (%s)\n",
|
|
|
|
file, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-16 23:32:26 +00:00
|
|
|
if (flags & XBPS_VERBOSE)
|
2009-02-28 16:32:38 +00:00
|
|
|
printf("Removed link: %s\n", file);
|
2009-02-26 04:41:49 +00:00
|
|
|
}
|
2009-02-28 16:32:38 +00:00
|
|
|
|
2009-02-26 04:41:49 +00:00
|
|
|
out:
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-03 02:18:00 +00:00
|
|
|
int
|
2009-03-16 23:32:26 +00:00
|
|
|
xbps_remove_binary_pkg(const char *pkgname)
|
2009-02-03 02:18:00 +00:00
|
|
|
{
|
2009-02-27 15:03:25 +00:00
|
|
|
prop_dictionary_t dict;
|
2009-03-16 23:32:26 +00:00
|
|
|
const char *rootdir = xbps_get_rootdir();
|
2009-02-26 04:41:49 +00:00
|
|
|
char path[PATH_MAX - 1], *buf;
|
2009-02-03 15:07:08 +00:00
|
|
|
int fd, rv = 0;
|
2009-02-03 02:18:00 +00:00
|
|
|
size_t len = 0;
|
2009-02-03 15:07:08 +00:00
|
|
|
bool prepostf = false;
|
2009-02-03 02:18:00 +00:00
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
2009-03-16 23:32:26 +00:00
|
|
|
if (rootdir) {
|
|
|
|
if (chdir(rootdir) == -1)
|
2009-03-12 08:23:40 +00:00
|
|
|
return errno;
|
|
|
|
} else {
|
|
|
|
if (chdir("/") == -1)
|
|
|
|
return errno;
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir = "";
|
2009-03-12 08:23:40 +00:00
|
|
|
}
|
2009-02-03 02:18:00 +00:00
|
|
|
|
2009-02-03 15:51:42 +00:00
|
|
|
/* Check if pkg is installed */
|
2009-02-05 15:53:49 +00:00
|
|
|
if (xbps_check_is_installed_pkgname(pkgname) == false)
|
2009-02-03 15:51:42 +00:00
|
|
|
return ENOENT;
|
|
|
|
|
2009-02-03 15:07:08 +00:00
|
|
|
/*
|
2009-02-26 17:01:18 +00:00
|
|
|
* This length is '%s%s/metadata/%s/REMOVE' + NULL.
|
2009-02-03 15:07:08 +00:00
|
|
|
*/
|
2009-03-16 23:32:26 +00:00
|
|
|
len = strlen(XBPS_META_PATH) + strlen(rootdir) + strlen(pkgname) + 19;
|
2009-02-26 17:01:18 +00:00
|
|
|
buf = malloc(len);
|
2009-02-03 15:07:08 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
return errno;
|
|
|
|
|
2009-02-26 17:01:18 +00:00
|
|
|
if (snprintf(buf, len, "%s%s/metadata/%s/REMOVE",
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir, XBPS_META_PATH, pkgname) < 0) {
|
2009-02-03 15:07:08 +00:00
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-27 15:03:25 +00:00
|
|
|
/*
|
|
|
|
* Find out if the REMOVE file exists.
|
|
|
|
*/
|
2009-02-03 15:07:08 +00:00
|
|
|
if ((fd = open(buf, O_RDONLY)) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2009-02-26 04:41:49 +00:00
|
|
|
free(buf);
|
|
|
|
return errno;
|
2009-02-03 15:07:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-02-27 15:03:25 +00:00
|
|
|
/*
|
|
|
|
* Run the pre remove action.
|
|
|
|
*/
|
2009-02-03 15:07:08 +00:00
|
|
|
(void)close(fd);
|
|
|
|
prepostf = true;
|
2009-03-12 08:23:40 +00:00
|
|
|
(void)printf("\n");
|
|
|
|
(void)fflush(stdout);
|
2009-03-16 23:32:26 +00:00
|
|
|
rv = xbps_file_exec(buf, rootdir, "pre", pkgname, NULL);
|
2009-02-27 15:03:25 +00:00
|
|
|
if (rv != 0) {
|
2009-02-03 15:07:08 +00:00
|
|
|
printf("%s: prerm action target error (%s)\n", pkgname,
|
|
|
|
strerror(errno));
|
2009-02-26 04:41:49 +00:00
|
|
|
free(buf);
|
|
|
|
return rv;
|
2009-02-03 15:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-26 04:41:49 +00:00
|
|
|
/*
|
|
|
|
* Iterate over the pkg file list dictionary and remove all
|
|
|
|
* files/dirs associated.
|
|
|
|
*/
|
|
|
|
(void)snprintf(path, sizeof(path), "%s%s/metadata/%s/files.plist",
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir, XBPS_META_PATH, pkgname);
|
2009-02-03 02:18:00 +00:00
|
|
|
|
2009-02-27 15:03:25 +00:00
|
|
|
dict = prop_dictionary_internalize_from_file(path);
|
|
|
|
if (dict == NULL) {
|
2009-02-26 04:41:49 +00:00
|
|
|
free(buf);
|
|
|
|
return errno;
|
2009-02-03 15:07:08 +00:00
|
|
|
}
|
2009-02-03 02:18:00 +00:00
|
|
|
|
2009-02-27 15:03:25 +00:00
|
|
|
rv = xbps_callback_array_iter_in_dict(dict, "filelist",
|
2009-03-16 23:32:26 +00:00
|
|
|
remove_pkg_files, NULL);
|
2009-02-26 04:41:49 +00:00
|
|
|
if (rv != 0) {
|
|
|
|
free(buf);
|
2009-02-27 15:03:25 +00:00
|
|
|
prop_object_release(dict);
|
2009-02-26 04:41:49 +00:00
|
|
|
return rv;
|
2009-02-03 02:18:00 +00:00
|
|
|
}
|
2009-02-27 15:03:25 +00:00
|
|
|
prop_object_release(dict);
|
2009-02-03 02:18:00 +00:00
|
|
|
|
2009-02-27 15:03:25 +00:00
|
|
|
/*
|
|
|
|
* Run the post remove action if REMOVE file is there.
|
|
|
|
*/
|
|
|
|
if (prepostf) {
|
2009-03-16 23:32:26 +00:00
|
|
|
if ((rv = xbps_file_exec(buf, rootdir, "post",
|
2009-02-26 04:41:49 +00:00
|
|
|
pkgname, NULL)) != 0) {
|
|
|
|
printf("%s: postrm action target error (%s)\n",
|
|
|
|
pkgname, strerror(errno));
|
|
|
|
free(buf);
|
|
|
|
return rv;
|
2009-02-03 15:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
|
2009-02-27 15:03:25 +00:00
|
|
|
/*
|
|
|
|
* Update the required_by array of all required dependencies.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove pkg metadata directory.
|
|
|
|
*/
|
2009-03-16 23:32:26 +00:00
|
|
|
return xbps_remove_binary_pkg_meta(pkgname);
|
2009-02-03 02:18:00 +00:00
|
|
|
}
|