2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
|
|
|
* 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 <dirent.h>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
|
|
|
* @file lib/remove.c
|
|
|
|
* @brief Package removal routines
|
|
|
|
* @defgroup pkg_remove Package removal functions
|
|
|
|
*
|
|
|
|
* These functions will remove a package or only a subset of its
|
|
|
|
* files. Package removal steps:
|
2010-01-23 07:07:19 +05:30
|
|
|
* -# Its <b>pre-remove</b> target specified in the REMOVE script
|
|
|
|
* will be executed.
|
|
|
|
* -# Its files, dirs and links will be removed. Modified files (not
|
2010-01-25 12:39:48 +05:30
|
|
|
* matching its sha256 hash) are preserved, unless XBPS_FLAG_FORCE
|
|
|
|
* is set via xbps_set_flags().
|
2010-01-23 07:07:19 +05:30
|
|
|
* -# Its <b>post-remove</b> target specified in the REMOVE script
|
|
|
|
* will be executed.
|
|
|
|
* -# Its requiredby objects will be removed from the installed packages
|
|
|
|
* database.
|
|
|
|
* -# Its state will be changed to XBPS_PKG_STATE_CONFIG_FILES.
|
2010-01-21 07:40:19 +05:30
|
|
|
*
|
2010-01-23 04:29:55 +05:30
|
|
|
* @note
|
2010-01-23 07:07:19 +05:30
|
|
|
* -# If a package is going to be updated and it's an essential package,
|
|
|
|
* only steps <b>1</b> and <b>4</b> will be executed.
|
|
|
|
* -# If a package is going to be updated and it's <b>NOT</b> an essential
|
|
|
|
* package, only steps <b>1</b>, <b>2</b> and <b>4</b> will be executed.
|
|
|
|
* -# If a package is going to be removed, all steps will be executed.
|
2010-01-21 07:40:19 +05:30
|
|
|
*
|
2010-01-23 04:29:55 +05:30
|
|
|
* The following image shows the structure of an internalized package's
|
|
|
|
* files.plist dictionary:
|
2010-01-21 07:40:19 +05:30
|
|
|
*
|
2010-01-23 04:29:55 +05:30
|
|
|
* @image html images/xbps_pkg_files_dictionary.png
|
2010-01-21 07:40:19 +05:30
|
|
|
*
|
2010-01-23 04:29:55 +05:30
|
|
|
* Legend:
|
|
|
|
* - <b>Salmon bg box</b>: XBPS_PKGFILES plist file.
|
|
|
|
* - <b>White bg box</b>: mandatory objects.
|
|
|
|
* - <b>Grey bg box</b>: optional objects.
|
2010-01-21 07:40:19 +05:30
|
|
|
*
|
2010-01-23 04:29:55 +05:30
|
|
|
* Text inside of white boxes are the key associated with the object, its
|
|
|
|
* data type is specified on its edge, i.e string, array, integer, dictionary.
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-07 11:00:06 +05:30
|
|
|
xbps_remove_pkg_files(prop_dictionary_t dict, const char *key)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
|
|
|
prop_array_t array;
|
|
|
|
prop_object_iterator_t iter;
|
|
|
|
prop_object_t obj;
|
2009-10-27 16:45:47 +05:30
|
|
|
const char *file, *sha256;
|
2009-08-17 22:37:20 +05:30
|
|
|
char *path = NULL;
|
|
|
|
int flags = 0, rv = 0;
|
|
|
|
|
2009-12-07 11:00:06 +05:30
|
|
|
assert(dict != NULL);
|
|
|
|
assert(key != NULL);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2009-12-07 11:00:06 +05:30
|
|
|
flags = xbps_get_flags();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2009-12-07 11:00:06 +05:30
|
|
|
array = prop_dictionary_get(dict, key);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (array == NULL || prop_array_count(array) == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-12-07 11:00:06 +05:30
|
|
|
iter = xbps_get_array_iter_from_dict(dict, key);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (iter == NULL)
|
2009-12-07 11:00:06 +05:30
|
|
|
return errno;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file)) {
|
2009-12-07 11:00:06 +05:30
|
|
|
rv = errno;
|
|
|
|
break;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2009-10-27 16:45:47 +05:30
|
|
|
path = xbps_xasprintf("%s/%s", xbps_get_rootdir(), file);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (path == NULL) {
|
2009-12-07 11:00:06 +05:30
|
|
|
rv = errno;
|
|
|
|
break;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2009-12-07 11:00:06 +05:30
|
|
|
if ((strcmp(key, "files") == 0) ||
|
|
|
|
(strcmp(key, "conf_files") == 0)) {
|
|
|
|
/*
|
|
|
|
* Check SHA256 hash in regular files and
|
|
|
|
* configuration files.
|
|
|
|
*/
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"sha256", &sha256)) {
|
|
|
|
free(path);
|
|
|
|
rv = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rv = xbps_check_file_hash(path, sha256);
|
|
|
|
if (rv == ENOENT) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: '%s' doesn't exist!\n", file);
|
2009-12-07 11:00:06 +05:30
|
|
|
free(path);
|
|
|
|
rv = 0;
|
|
|
|
continue;
|
|
|
|
} else if (rv == ERANGE) {
|
|
|
|
rv = 0;
|
2010-01-25 12:39:48 +05:30
|
|
|
if (flags & XBPS_FLAG_VERBOSE) {
|
|
|
|
if (flags & XBPS_FLAG_FORCE) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: '%s' SHA256 "
|
|
|
|
"mismatch, forcing "
|
|
|
|
"removal...\n", file);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: '%s' SHA256 "
|
|
|
|
"mismatch, preserving...\n",
|
|
|
|
file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((flags & XBPS_FLAG_FORCE) == 0) {
|
|
|
|
free(path);
|
|
|
|
continue;
|
|
|
|
}
|
2009-12-07 11:00:06 +05:30
|
|
|
} else if (rv != 0 && rv != ERANGE) {
|
|
|
|
free(path);
|
|
|
|
break;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2009-12-07 11:00:06 +05:30
|
|
|
} else if (strcmp(key, "dirs") == 0) {
|
|
|
|
if ((rv = rmdir(path)) == -1) {
|
|
|
|
rv = 0;
|
|
|
|
if (errno == ENOTEMPTY) {
|
|
|
|
free(path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (flags & XBPS_FLAG_VERBOSE) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: can't remove "
|
2009-12-07 11:00:06 +05:30
|
|
|
"directory %s (%s)\n", file,
|
|
|
|
strerror(errno));
|
|
|
|
free(path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strcmp(key, "dirs")) {
|
|
|
|
if ((rv = remove(path)) == -1) {
|
|
|
|
if (flags & XBPS_FLAG_VERBOSE)
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: can't remove %s "
|
2009-12-07 11:00:06 +05:30
|
|
|
"(%s)\n", file, strerror(errno));
|
|
|
|
|
|
|
|
rv = 0;
|
2009-08-17 22:37:20 +05:30
|
|
|
free(path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & XBPS_FLAG_VERBOSE)
|
2009-12-07 11:00:06 +05:30
|
|
|
printf("Removed: %s\n", file);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
2009-12-07 11:00:06 +05:30
|
|
|
return rv;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
int
|
2010-01-25 10:57:54 +05:30
|
|
|
xbps_remove_pkg(const char *pkgname, const char *version,
|
|
|
|
bool update, bool essential)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2010-01-25 10:57:54 +05:30
|
|
|
prop_dictionary_t dict;
|
2009-08-17 22:37:20 +05:30
|
|
|
const char *rootdir = xbps_get_rootdir();
|
|
|
|
char *path, *buf;
|
|
|
|
int rv = 0;
|
2010-01-25 10:57:54 +05:30
|
|
|
bool prepostf = false;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
assert(version != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if pkg is installed before anything else.
|
|
|
|
*/
|
2010-01-25 10:57:54 +05:30
|
|
|
if (!xbps_check_is_installed_pkgname(pkgname))
|
2009-08-17 22:37:20 +05:30
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
if (strcmp(rootdir, "") == 0)
|
|
|
|
rootdir = "/";
|
|
|
|
|
|
|
|
if (chdir(rootdir) == -1)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
buf = xbps_xasprintf(".%s/metadata/%s/REMOVE",
|
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (buf == NULL)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out if the REMOVE file exists.
|
|
|
|
*/
|
2009-08-25 09:35:59 +05:30
|
|
|
if (access(buf, X_OK) == 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Run the pre remove action.
|
|
|
|
*/
|
|
|
|
prepostf = true;
|
|
|
|
rv = xbps_file_chdir_exec(rootdir, buf, "pre", pkgname,
|
2009-10-02 17:08:33 +05:30
|
|
|
version, update ? "yes" : "no", NULL);
|
2009-08-17 22:37:20 +05:30
|
|
|
if (rv != 0) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"%s: prerm action target error (%s)\n", pkgname,
|
2009-08-17 22:37:20 +05:30
|
|
|
strerror(errno));
|
|
|
|
free(buf);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/*
|
|
|
|
* If updating an essential package, we just need to execute
|
|
|
|
* the current pre-remove action target, unregister its requiredby
|
|
|
|
* entries and continue. Its files will be overwritten later in
|
|
|
|
* the unpack phase.
|
|
|
|
*/
|
2010-01-25 10:57:54 +05:30
|
|
|
if (essential && update) {
|
|
|
|
free(buf);
|
2010-01-21 07:40:19 +05:30
|
|
|
return xbps_requiredby_pkg_remove(pkgname);
|
2010-01-25 10:57:54 +05:30
|
|
|
}
|
2010-01-21 07:40:19 +05:30
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
2009-12-07 11:00:06 +05:30
|
|
|
* Remove links, files and dirs.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
|
|
|
path = xbps_xasprintf("%s/%s/metadata/%s/%s",
|
|
|
|
rootdir, XBPS_META_PATH, pkgname, XBPS_PKGFILES);
|
|
|
|
if (path == NULL) {
|
|
|
|
free(buf);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
dict = prop_dictionary_internalize_from_file(path);
|
|
|
|
if (dict == NULL) {
|
|
|
|
free(buf);
|
|
|
|
free(path);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
|
2009-12-07 11:00:06 +05:30
|
|
|
/* Remove links */
|
|
|
|
if ((rv = xbps_remove_pkg_files(dict, "links")) != 0) {
|
|
|
|
free(buf);
|
|
|
|
prop_object_release(dict);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/* Remove regular files */
|
|
|
|
if ((rv = xbps_remove_pkg_files(dict, "files")) != 0) {
|
|
|
|
free(buf);
|
|
|
|
prop_object_release(dict);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/* Remove dirs */
|
|
|
|
if ((rv = xbps_remove_pkg_files(dict, "dirs")) != 0) {
|
2009-08-17 22:37:20 +05:30
|
|
|
free(buf);
|
|
|
|
prop_object_release(dict);
|
|
|
|
return rv;
|
|
|
|
}
|
2010-01-13 22:40:03 +05:30
|
|
|
prop_object_release(dict);
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Run the post remove action if REMOVE file is there
|
|
|
|
* and we aren't updating a package.
|
|
|
|
*/
|
|
|
|
if (update == false && prepostf) {
|
|
|
|
if ((rv = xbps_file_chdir_exec(rootdir, buf, "post",
|
|
|
|
pkgname, version, NULL)) != 0) {
|
2010-01-15 19:49:16 +05:30
|
|
|
fprintf(stderr,
|
|
|
|
"%s: postrm action target error (%s)\n",
|
2009-08-17 22:37:20 +05:30
|
|
|
pkgname, strerror(errno));
|
|
|
|
free(buf);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the requiredby array of all required dependencies.
|
|
|
|
*/
|
|
|
|
if ((rv = xbps_requiredby_pkg_remove(pkgname)) != 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set package state to "config-files".
|
|
|
|
*/
|
|
|
|
rv = xbps_set_pkg_state_installed(pkgname,
|
|
|
|
XBPS_PKG_STATE_CONFIG_FILES);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|