2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
2011-01-22 17:10:19 +05:30
|
|
|
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
2009-08-17 22:37:20 +05:30
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
2011-01-19 05:01:22 +05:30
|
|
|
* @file lib/package_purge.c
|
2010-01-21 07:40:19 +05:30
|
|
|
* @brief Package purging routines
|
|
|
|
* @defgroup purge Package purging functions
|
|
|
|
*
|
|
|
|
* These functions will purge an specified package or all packages.
|
2010-01-23 07:07:19 +05:30
|
|
|
* Only packages in XBPS_PKG_STATE_CONFIG_FILES state will be processed
|
2010-01-21 07:40:19 +05:30
|
|
|
* (unless overriden). Package purging steps:
|
2010-11-06 11:14:00 +05:30
|
|
|
*
|
2011-01-30 16:02:46 +05:30
|
|
|
* - Unmodified configuration files will be removed.
|
|
|
|
* - The purge action in the REMOVE script will be executed (if found).
|
|
|
|
* - Metadata files will be removed and package will be unregistered
|
|
|
|
* with xbps_unregister_pkg().
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2011-02-21 22:12:47 +05:30
|
|
|
remove_pkg_metadata(const char *pkgname, const char *rootdir)
|
2010-01-21 07:40:19 +05:30
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
|
|
|
char *metadir, *path;
|
2011-01-22 17:10:19 +05:30
|
|
|
int rv = 0;
|
2010-01-21 07:40:19 +05:30
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
2011-10-19 20:23:38 +05:30
|
|
|
assert(rootdir != NULL);
|
2010-01-21 07:40:19 +05:30
|
|
|
|
2011-02-21 22:12:47 +05:30
|
|
|
metadir = xbps_xasprintf("%s/%s/metadata/%s", rootdir,
|
2010-01-21 07:40:19 +05:30
|
|
|
XBPS_META_PATH, pkgname);
|
|
|
|
if (metadir == NULL)
|
2010-11-19 18:10:13 +05:30
|
|
|
return ENOMEM;
|
2010-01-21 07:40:19 +05:30
|
|
|
|
|
|
|
dirp = opendir(metadir);
|
|
|
|
if (dirp == NULL) {
|
|
|
|
free(metadir);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if ((strcmp(dp->d_name, ".") == 0) ||
|
|
|
|
(strcmp(dp->d_name, "..") == 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path = xbps_xasprintf("%s/%s", metadir, dp->d_name);
|
|
|
|
if (path == NULL) {
|
|
|
|
(void)closedir(dirp);
|
|
|
|
free(metadir);
|
2010-11-19 18:10:13 +05:30
|
|
|
return ENOMEM;
|
2010-01-21 07:40:19 +05:30
|
|
|
}
|
|
|
|
|
2011-01-22 17:10:19 +05:30
|
|
|
if (unlink(path) == -1)
|
|
|
|
xbps_warn_printf("can't remove metadata file: "
|
|
|
|
"`%s': %s\n", dp->d_name, strerror(errno));
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
(void)closedir(dirp);
|
|
|
|
rv = rmdir(metadir);
|
|
|
|
free(metadir);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-06-01 13:07:32 +05:30
|
|
|
xbps_purge_packages(void)
|
2009-08-21 15:01:26 +05:30
|
|
|
{
|
2011-07-27 20:43:54 +05:30
|
|
|
struct xbps_handle *xhp;
|
2009-08-21 15:01:26 +05:30
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
|
|
|
const char *pkgname;
|
|
|
|
int rv = 0;
|
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
xhp = xbps_handle_get();
|
|
|
|
iter = xbps_array_iter_from_dict(xhp->regpkgdb_dictionary, "packages");
|
|
|
|
if (iter == NULL)
|
2009-11-26 07:52:50 +05:30
|
|
|
return errno;
|
2009-08-21 15:01:26 +05:30
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
|
2010-11-08 16:50:44 +05:30
|
|
|
if ((rv = xbps_purge_pkg(pkgname, true)) != 0)
|
2009-08-21 15:01:26 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
int
|
2009-08-21 15:01:26 +05:30
|
|
|
xbps_purge_pkg(const char *pkgname, bool check_state)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2011-07-27 20:43:54 +05:30
|
|
|
struct xbps_handle *xhp;
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_t dict, pkgd;
|
2011-01-30 16:02:46 +05:30
|
|
|
const char *version;
|
|
|
|
char *buf;
|
2011-02-21 22:12:47 +05:30
|
|
|
int rv = 0;
|
2011-01-30 16:02:46 +05:30
|
|
|
pkg_state_t state;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
2011-02-21 22:12:47 +05:30
|
|
|
xhp = xbps_handle_get();
|
2010-11-06 11:14:00 +05:30
|
|
|
/*
|
|
|
|
* Firstly let's get the pkg dictionary from regpkgdb.
|
|
|
|
*/
|
2011-06-04 17:07:53 +05:30
|
|
|
pkgd = xbps_find_pkg_in_dict_by_name(xhp->regpkgdb_dictionary,
|
|
|
|
"packages", pkgname);
|
2011-10-30 15:42:03 +05:30
|
|
|
if (pkgd == NULL) {
|
|
|
|
xbps_dbg_printf("[purge] %s: missing pkg dictionary (%s)\n",
|
|
|
|
pkgname, strerror(errno));
|
2010-11-06 11:14:00 +05:30
|
|
|
return errno;
|
2011-10-30 15:42:03 +05:30
|
|
|
}
|
2010-11-06 11:14:00 +05:30
|
|
|
|
2009-08-21 15:01:26 +05:30
|
|
|
if (check_state) {
|
|
|
|
/*
|
|
|
|
* Skip packages that aren't in "config-files" state.
|
|
|
|
*/
|
2011-06-01 13:07:32 +05:30
|
|
|
if ((rv = xbps_pkg_state_dictionary(pkgd, &state)) != 0)
|
2011-06-04 17:07:53 +05:30
|
|
|
return rv;
|
2011-10-30 15:42:03 +05:30
|
|
|
if (state != XBPS_PKG_STATE_CONFIG_FILES) {
|
|
|
|
xbps_dbg_printf("[purge] %s not in config-files "
|
|
|
|
"state.\n", pkgname);
|
2011-06-04 17:07:53 +05:30
|
|
|
return rv;
|
2011-10-30 15:42:03 +05:30
|
|
|
}
|
2009-08-21 15:01:26 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
2011-01-30 16:02:46 +05:30
|
|
|
* Remove unmodified configuration files.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2011-06-01 13:07:32 +05:30
|
|
|
dict = xbps_dictionary_from_metadata_plist(pkgname, XBPS_PKGFILES);
|
2011-10-30 15:42:03 +05:30
|
|
|
if (dict == NULL) {
|
|
|
|
xbps_dbg_printf("[purge] %s: failed to read files.plist (%s)\n",
|
|
|
|
pkgname, strerror(errno));
|
|
|
|
if (errno != ENOENT)
|
|
|
|
return errno;
|
|
|
|
} else {
|
|
|
|
if (prop_dictionary_get(dict, "conf_files")) {
|
|
|
|
rv = xbps_remove_pkg_files(dict, "conf_files");
|
2010-11-19 18:10:13 +05:30
|
|
|
prop_object_release(dict);
|
2011-10-30 15:42:03 +05:30
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
2011-01-30 16:02:46 +05:30
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Execute the purge action in REMOVE script (if found).
|
|
|
|
*/
|
2011-10-17 16:07:15 +05:30
|
|
|
if (chdir(prop_string_cstring_nocopy(xhp->rootdir)) == -1) {
|
2011-01-30 17:19:55 +05:30
|
|
|
rv = errno;
|
2011-10-30 15:42:03 +05:30
|
|
|
xbps_error_printf("[purge] %s: cannot change to rootdir "
|
|
|
|
"(%s)\n", pkgname, strerror(rv));
|
2011-06-04 17:07:53 +05:30
|
|
|
return rv;
|
2011-01-30 17:19:55 +05:30
|
|
|
}
|
2011-01-30 16:02:46 +05:30
|
|
|
buf = xbps_xasprintf(".%s/metadata/%s/REMOVE", XBPS_META_PATH, pkgname);
|
|
|
|
if (buf == NULL) {
|
|
|
|
rv = ENOMEM;
|
2011-06-04 17:07:53 +05:30
|
|
|
return rv;
|
2011-01-30 16:02:46 +05:30
|
|
|
}
|
2011-01-30 17:19:55 +05:30
|
|
|
if (access(buf, X_OK) == 0) {
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgd, "version", &version);
|
|
|
|
|
|
|
|
if (xbps_file_exec(buf, "purge",
|
|
|
|
pkgname, version, "no", NULL) != 0) {
|
|
|
|
free(buf);
|
|
|
|
if (errno && errno != ENOENT) {
|
|
|
|
rv = errno;
|
|
|
|
xbps_error_printf("%s: purge action error in "
|
|
|
|
"REMOVE script: %s\n", pkgname,
|
|
|
|
strerror(errno));
|
2011-06-04 17:07:53 +05:30
|
|
|
return rv;
|
2011-01-30 17:19:55 +05:30
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
2011-01-30 17:19:55 +05:30
|
|
|
free(buf);
|
2009-12-07 11:00:06 +05:30
|
|
|
/*
|
|
|
|
* Remove metadata dir and unregister package.
|
|
|
|
*/
|
2011-10-17 16:07:15 +05:30
|
|
|
if ((rv = remove_pkg_metadata(pkgname,
|
|
|
|
prop_string_cstring_nocopy(xhp->rootdir))) != 0) {
|
2011-10-30 15:42:03 +05:30
|
|
|
xbps_dbg_printf("[purge] %s: failed to remove metadata "
|
|
|
|
"files (%s)\n", pkgname, strerror(rv));
|
|
|
|
if (rv != ENOENT)
|
|
|
|
return rv;
|
2011-01-30 16:02:46 +05:30
|
|
|
}
|
|
|
|
if ((rv = xbps_unregister_pkg(pkgname)) != 0) {
|
|
|
|
xbps_error_printf("%s: couldn't unregister package: %s\n",
|
|
|
|
pkgname, strerror(rv));
|
2011-06-04 17:07:53 +05:30
|
|
|
return rv;
|
2011-01-30 16:02:46 +05:30
|
|
|
}
|
2011-02-21 22:12:47 +05:30
|
|
|
if (xhp->flags & XBPS_FLAG_VERBOSE)
|
2011-01-29 06:25:52 +05:30
|
|
|
xbps_printf("Package %s purged successfully.\n", pkgname);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|