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)
|
|
|
|
{
|
2009-04-04 17:32:39 +00:00
|
|
|
const char *rootdir;
|
2009-02-03 02:18:00 +00:00
|
|
|
char *plist;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
2009-04-04 17:32:39 +00:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
plist = xbps_xasprintf("%s/%s/%s", rootdir,
|
|
|
|
XBPS_META_PATH, XBPS_REGPKGDB);
|
2009-02-03 02:18:00 +00:00
|
|
|
if (plist == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
2009-04-06 14:39:14 +00:00
|
|
|
rv = xbps_remove_pkg_dict_from_file(pkgname, plist);
|
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-04-04 17:32:39 +00:00
|
|
|
const char *rootdir;
|
|
|
|
char *metadir, *path;
|
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-04-04 17:32:39 +00:00
|
|
|
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
flags = xbps_get_flags();
|
2009-02-03 21:37:20 +00:00
|
|
|
|
2009-04-04 17:32:39 +00:00
|
|
|
metadir = xbps_xasprintf("%s/%s/metadata/%s", rootdir,
|
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (metadir == NULL)
|
|
|
|
return errno;
|
2009-02-03 21:37:20 +00:00
|
|
|
|
|
|
|
dirp = opendir(metadir);
|
2009-04-05 10:47:02 +00:00
|
|
|
if (dirp == NULL) {
|
|
|
|
free(metadir);
|
2009-02-03 21:37:20 +00:00
|
|
|
return errno;
|
2009-04-05 10:47:02 +00:00
|
|
|
}
|
2009-02-03 21:37:20 +00:00
|
|
|
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if ((strcmp(dp->d_name, ".") == 0) ||
|
|
|
|
(strcmp(dp->d_name, "..") == 0))
|
|
|
|
continue;
|
|
|
|
|
2009-04-04 17:32:39 +00:00
|
|
|
path = xbps_xasprintf("%s/%s", metadir, dp->d_name);
|
|
|
|
if (path == NULL) {
|
2009-02-03 21:37:20 +00:00
|
|
|
(void)closedir(dirp);
|
2009-04-04 17:32:39 +00:00
|
|
|
free(metadir);
|
2009-02-03 21:37:20 +00:00
|
|
|
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
|
|
|
}
|
2009-04-04 17:32:39 +00:00
|
|
|
free(path);
|
2009-02-03 21:37:20 +00:00
|
|
|
}
|
|
|
|
(void)closedir(dirp);
|
|
|
|
rv = rmdir(metadir);
|
2009-04-04 17:32:39 +00:00
|
|
|
free(metadir);
|
2009-02-03 21:37:20 +00:00
|
|
|
|
|
|
|
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-04-04 17:32:39 +00:00
|
|
|
const char *file, *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();
|
|
|
|
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-04-04 17:32:39 +00:00
|
|
|
path = xbps_xasprintf("%s/%s", 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-04-02 01:08:20 +00:00
|
|
|
xbps_remove_binary_pkg(const char *pkgname, bool update)
|
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-04-04 17:32:39 +00:00
|
|
|
char *path, *buf;
|
2009-04-08 01:20:13 +00:00
|
|
|
int rv = 0;
|
2009-02-03 15:07:08 +00:00
|
|
|
bool prepostf = false;
|
2009-02-03 02:18:00 +00:00
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
2009-04-05 12:48:24 +00:00
|
|
|
/*
|
|
|
|
* Check if pkg is installed before anything else.
|
|
|
|
*/
|
|
|
|
if (xbps_check_is_installed_pkgname(pkgname) == false)
|
|
|
|
return ENOENT;
|
|
|
|
|
2009-04-05 10:41:40 +00:00
|
|
|
if (strcmp(rootdir, "") == 0)
|
|
|
|
rootdir = "/";
|
|
|
|
|
2009-04-05 10:46:15 +00:00
|
|
|
if (chdir(rootdir) == -1)
|
|
|
|
return errno;
|
2009-02-03 02:18:00 +00:00
|
|
|
|
2009-04-08 01:20:13 +00:00
|
|
|
buf = xbps_xasprintf(".%s/metadata/%s/REMOVE",
|
2009-04-04 17:32:39 +00:00
|
|
|
XBPS_META_PATH, pkgname);
|
2009-02-03 15:07:08 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
return errno;
|
|
|
|
|
2009-02-27 15:03:25 +00:00
|
|
|
/*
|
|
|
|
* Find out if the REMOVE file exists.
|
|
|
|
*/
|
2009-04-08 01:20:13 +00:00
|
|
|
if (access(buf, R_OK) == 0) {
|
2009-02-27 15:03:25 +00:00
|
|
|
/*
|
|
|
|
* Run the pre remove action.
|
|
|
|
*/
|
2009-02-03 15:07:08 +00:00
|
|
|
prepostf = true;
|
2009-03-12 08:23:40 +00:00
|
|
|
(void)printf("\n");
|
|
|
|
(void)fflush(stdout);
|
2009-04-08 01:20:13 +00:00
|
|
|
rv = xbps_file_chdir_exec(rootdir, buf, "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.
|
|
|
|
*/
|
2009-04-04 17:32:39 +00:00
|
|
|
path = xbps_xasprintf("%s/%s/metadata/%s/files.plist",
|
2009-03-16 23:32:26 +00:00
|
|
|
rootdir, XBPS_META_PATH, pkgname);
|
2009-04-04 17:32:39 +00:00
|
|
|
if (path == NULL) {
|
|
|
|
free(buf);
|
|
|
|
return errno;
|
|
|
|
}
|
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);
|
2009-04-04 17:32:39 +00:00
|
|
|
free(path);
|
2009-02-26 04:41:49 +00:00
|
|
|
return errno;
|
2009-02-03 15:07:08 +00:00
|
|
|
}
|
2009-04-04 17:32:39 +00:00
|
|
|
free(path);
|
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-04-08 01:20:13 +00:00
|
|
|
if ((rv = xbps_file_chdir_exec(rootdir, buf, "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
|
|
|
/*
|
2009-04-06 15:00:52 +00:00
|
|
|
* Update the requiredby array of all required dependencies.
|
2009-02-27 15:03:25 +00:00
|
|
|
*/
|
2009-04-06 15:00:52 +00:00
|
|
|
rv = xbps_requiredby_pkg_remove(pkgname);
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
|
2009-04-02 01:08:20 +00:00
|
|
|
if (update == false) {
|
|
|
|
/*
|
2009-04-06 15:00:52 +00:00
|
|
|
* Unregister pkg from database only if it's a removal
|
|
|
|
* and not an update.
|
2009-04-02 01:08:20 +00:00
|
|
|
*/
|
|
|
|
rv = xbps_unregister_pkg(pkgname);
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
}
|
2009-02-27 15:03:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
}
|