libxbps: share code for removing pkg files in remove.c and purge.c.
A new function has been created, xbps_remove_pkg_files() that accepts a dictionary internalized from files.plist and a key to remove links, dirs, files and conf_files. As result of this, now when purging a package those directories that were used in configuration files will also be removed if they are empty. Bump XBPS_RELVER to 20091207. --HG-- extra : convert_revision : xtraeme%40gmail.com-20091207053006-egw65u6y1jcuraje
This commit is contained in:
parent
f5744b0d3d
commit
972af411d1
@ -40,7 +40,7 @@
|
|||||||
#include <archive_entry.h>
|
#include <archive_entry.h>
|
||||||
|
|
||||||
/* Current release version */
|
/* Current release version */
|
||||||
#define XBPS_RELVER "20091202"
|
#define XBPS_RELVER "20091207"
|
||||||
|
|
||||||
/* Default root PATH for xbps to store metadata info. */
|
/* Default root PATH for xbps to store metadata info. */
|
||||||
#define XBPS_META_PATH "/var/db/xbps"
|
#define XBPS_META_PATH "/var/db/xbps"
|
||||||
@ -186,6 +186,7 @@ void SYMEXPORT xbps_regpkgs_dictionary_release(void);
|
|||||||
|
|
||||||
/* From lib/remove.c */
|
/* From lib/remove.c */
|
||||||
int SYMEXPORT xbps_remove_pkg(const char *, const char *, bool);
|
int SYMEXPORT xbps_remove_pkg(const char *, const char *, bool);
|
||||||
|
int SYMEXPORT xbps_remove_pkg_files(prop_dictionary_t, const char *);
|
||||||
|
|
||||||
/* From lib/remove_obsoletes.c */
|
/* From lib/remove_obsoletes.c */
|
||||||
int xbps_remove_obsoletes(prop_dictionary_t, prop_dictionary_t);
|
int xbps_remove_obsoletes(prop_dictionary_t, prop_dictionary_t);
|
||||||
|
80
lib/purge.c
80
lib/purge.c
@ -82,10 +82,6 @@ int SYMEXPORT
|
|||||||
xbps_purge_pkg(const char *pkgname, bool check_state)
|
xbps_purge_pkg(const char *pkgname, bool check_state)
|
||||||
{
|
{
|
||||||
prop_dictionary_t dict;
|
prop_dictionary_t dict;
|
||||||
prop_array_t array;
|
|
||||||
prop_object_t obj;
|
|
||||||
prop_object_iterator_t iter;
|
|
||||||
const char *file, *sha256;
|
|
||||||
char *path;
|
char *path;
|
||||||
int rv = 0, flags;
|
int rv = 0, flags;
|
||||||
pkg_state_t state = 0;
|
pkg_state_t state = 0;
|
||||||
@ -105,8 +101,7 @@ xbps_purge_pkg(const char *pkgname, bool check_state)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Iterate over the pkg file list dictionary and remove all
|
* Remove unmodified configuration files.
|
||||||
* unmodified configuration files.
|
|
||||||
*/
|
*/
|
||||||
path = xbps_xasprintf("%s/%s/metadata/%s/%s", xbps_get_rootdir(),
|
path = xbps_xasprintf("%s/%s/metadata/%s/%s", xbps_get_rootdir(),
|
||||||
XBPS_META_PATH, pkgname, XBPS_PKGFILES);
|
XBPS_META_PATH, pkgname, XBPS_PKGFILES);
|
||||||
@ -119,75 +114,20 @@ xbps_purge_pkg(const char *pkgname, bool check_state)
|
|||||||
return errno;
|
return errno;
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
|
if ((rv = xbps_remove_pkg_files(dict, "conf_files")) != 0) {
|
||||||
array = prop_dictionary_get(dict, "conf_files");
|
|
||||||
if (array == NULL) {
|
|
||||||
goto out;
|
|
||||||
} else if (prop_object_type(array) != PROP_TYPE_ARRAY) {
|
|
||||||
prop_object_release(dict);
|
prop_object_release(dict);
|
||||||
return EINVAL;
|
return rv;
|
||||||
} else if (prop_array_count(array) == 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
/* Also try to remove empty dirs used in conf_files */
|
||||||
iter = xbps_get_array_iter_from_dict(dict, "conf_files");
|
if ((rv = xbps_remove_pkg_files(dict, "dirs")) != 0) {
|
||||||
if (iter == NULL)
|
prop_object_release(dict);
|
||||||
return EINVAL;
|
return rv;
|
||||||
|
|
||||||
while ((obj = prop_object_iterator_next(iter))) {
|
|
||||||
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file)) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
prop_object_release(dict);
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
path = xbps_xasprintf("%s/%s", xbps_get_rootdir(), file);
|
|
||||||
if (path == NULL) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
prop_object_release(dict);
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
||||||
"sha256", &sha256)) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
prop_object_release(dict);
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
rv = xbps_check_file_hash(path, sha256);
|
|
||||||
if (rv == ENOENT) {
|
|
||||||
printf("Configuration file %s doesn't exist!\n", file);
|
|
||||||
free(path);
|
|
||||||
continue;
|
|
||||||
} else if (rv == ERANGE) {
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("Configuration file %s has been "
|
|
||||||
"modified, preserving...\n", file);
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
continue;
|
|
||||||
} else if (rv != 0 && rv != ERANGE) {
|
|
||||||
free(path);
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
prop_object_release(dict);
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
if ((rv = remove(path)) == -1) {
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("WARNING: can't remove %s (%s)\n",
|
|
||||||
file, strerror(errno));
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("Removed configuration file %s\n", file);
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
out:
|
|
||||||
prop_object_release(dict);
|
prop_object_release(dict);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Remove metadata dir and unregister package.
|
||||||
|
*/
|
||||||
if ((rv = remove_pkg_metadata(pkgname)) == 0) {
|
if ((rv = remove_pkg_metadata(pkgname)) == 0) {
|
||||||
if ((rv = xbps_unregister_pkg(pkgname)) == 0)
|
if ((rv = xbps_unregister_pkg(pkgname)) == 0)
|
||||||
printf("Package %s has been purged successfully.\n",
|
printf("Package %s has been purged successfully.\n",
|
||||||
|
198
lib/remove.c
198
lib/remove.c
@ -32,10 +32,8 @@
|
|||||||
|
|
||||||
#include <xbps_api.h>
|
#include <xbps_api.h>
|
||||||
|
|
||||||
static int remove_pkg_files(prop_dictionary_t);
|
int SYMEXPORT
|
||||||
|
xbps_remove_pkg_files(prop_dictionary_t dict, const char *key)
|
||||||
static int
|
|
||||||
remove_pkg_files(prop_dictionary_t dict)
|
|
||||||
{
|
{
|
||||||
prop_array_t array;
|
prop_array_t array;
|
||||||
prop_object_iterator_t iter;
|
prop_object_iterator_t iter;
|
||||||
@ -44,142 +42,93 @@ remove_pkg_files(prop_dictionary_t dict)
|
|||||||
char *path = NULL;
|
char *path = NULL;
|
||||||
int flags = 0, rv = 0;
|
int flags = 0, rv = 0;
|
||||||
|
|
||||||
|
assert(dict != NULL);
|
||||||
|
assert(key != NULL);
|
||||||
|
|
||||||
flags = xbps_get_flags();
|
flags = xbps_get_flags();
|
||||||
|
|
||||||
/* Links */
|
array = prop_dictionary_get(dict, key);
|
||||||
array = prop_dictionary_get(dict, "links");
|
|
||||||
if (array == NULL || prop_array_count(array) == 0)
|
|
||||||
goto files;
|
|
||||||
|
|
||||||
iter = xbps_get_array_iter_from_dict(dict, "links");
|
|
||||||
if (iter == NULL)
|
|
||||||
return EINVAL;
|
|
||||||
|
|
||||||
while ((obj = prop_object_iterator_next(iter))) {
|
|
||||||
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file)) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
path = xbps_xasprintf("%s/%s", xbps_get_rootdir(), file);
|
|
||||||
if (path == NULL) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
if ((rv = remove(path)) == -1) {
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("WARNING: can't remove link %s (%s)\n",
|
|
||||||
file, strerror(errno));
|
|
||||||
free(path);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("Removed link: %s\n", file);
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
}
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
path = NULL;
|
|
||||||
|
|
||||||
files:
|
|
||||||
/* Regular files */
|
|
||||||
array = prop_dictionary_get(dict, "files");
|
|
||||||
if (array == NULL || prop_array_count(array) == 0)
|
|
||||||
goto dirs;
|
|
||||||
|
|
||||||
iter = xbps_get_array_iter_from_dict(dict, "files");
|
|
||||||
if (iter == NULL)
|
|
||||||
return EINVAL;
|
|
||||||
|
|
||||||
while ((obj = prop_object_iterator_next(iter))) {
|
|
||||||
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file)) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
path = xbps_xasprintf("%s/%s", xbps_get_rootdir(), file);
|
|
||||||
if (path == NULL) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
return EINVAL;
|
|
||||||
}
|
|
||||||
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
||||||
"sha256", &sha256)) {
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
free(path);
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
rv = xbps_check_file_hash(path, sha256);
|
|
||||||
if (rv == ENOENT) {
|
|
||||||
printf("WARNING: '%s' doesn't exist!\n", file);
|
|
||||||
free(path);
|
|
||||||
continue;
|
|
||||||
} else if (rv == ERANGE) {
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("WARNING: SHA256 doesn't match "
|
|
||||||
"for file %s, ignoring...\n", file);
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
continue;
|
|
||||||
} else if (rv != 0 && rv != ERANGE) {
|
|
||||||
free(path);
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
if ((rv = remove(path)) == -1) {
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("WARNING: can't remove file %s (%s)\n",
|
|
||||||
file, strerror(errno));
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
|
||||||
printf("Removed file: %s\n", file);
|
|
||||||
|
|
||||||
free(path);
|
|
||||||
}
|
|
||||||
prop_object_iterator_release(iter);
|
|
||||||
path = NULL;
|
|
||||||
|
|
||||||
dirs:
|
|
||||||
/* Directories */
|
|
||||||
array = prop_dictionary_get(dict, "dirs");
|
|
||||||
if (array == NULL || prop_array_count(array) == 0)
|
if (array == NULL || prop_array_count(array) == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
iter = xbps_get_array_iter_from_dict(dict, "dirs");
|
iter = xbps_get_array_iter_from_dict(dict, key);
|
||||||
if (iter == NULL)
|
if (iter == NULL)
|
||||||
return EINVAL;
|
return errno;
|
||||||
|
|
||||||
while ((obj = prop_object_iterator_next(iter))) {
|
while ((obj = prop_object_iterator_next(iter))) {
|
||||||
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file)) {
|
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file)) {
|
||||||
prop_object_iterator_release(iter);
|
rv = errno;
|
||||||
return EINVAL;
|
break;
|
||||||
}
|
}
|
||||||
path = xbps_xasprintf("%s/%s", xbps_get_rootdir(), file);
|
path = xbps_xasprintf("%s/%s", xbps_get_rootdir(), file);
|
||||||
if (path == NULL) {
|
if (path == NULL) {
|
||||||
prop_object_iterator_release(iter);
|
rv = errno;
|
||||||
return EINVAL;
|
break;
|
||||||
}
|
}
|
||||||
if ((rv = rmdir(path)) == -1) {
|
if ((strcmp(key, "files") == 0) ||
|
||||||
if (errno == ENOTEMPTY) {
|
(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) {
|
||||||
|
printf("WARNING: '%s' doesn't exist!\n", file);
|
||||||
|
free(path);
|
||||||
|
rv = 0;
|
||||||
|
continue;
|
||||||
|
} else if (rv == ERANGE) {
|
||||||
|
if (flags & XBPS_FLAG_VERBOSE)
|
||||||
|
printf("WARNING: '%s' SHA256 mismatch, "
|
||||||
|
"preserving...\n", file);
|
||||||
|
rv = 0;
|
||||||
free(path);
|
free(path);
|
||||||
continue;
|
continue;
|
||||||
|
} else if (rv != 0 && rv != ERANGE) {
|
||||||
|
free(path);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (flags & XBPS_FLAG_VERBOSE) {
|
} else if (strcmp(key, "dirs") == 0) {
|
||||||
printf("WARNING: can't remove "
|
if ((rv = rmdir(path)) == -1) {
|
||||||
"directory %s (%s)\n", file,
|
rv = 0;
|
||||||
strerror(errno));
|
if (errno == ENOTEMPTY) {
|
||||||
|
free(path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (flags & XBPS_FLAG_VERBOSE) {
|
||||||
|
printf("WARNING: can't remove "
|
||||||
|
"directory %s (%s)\n", file,
|
||||||
|
strerror(errno));
|
||||||
|
free(path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (strcmp(key, "dirs")) {
|
||||||
|
if ((rv = remove(path)) == -1) {
|
||||||
|
if (flags & XBPS_FLAG_VERBOSE)
|
||||||
|
printf("WARNING: can't remove %s "
|
||||||
|
"(%s)\n", file, strerror(errno));
|
||||||
|
|
||||||
|
rv = 0;
|
||||||
free(path);
|
free(path);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (flags & XBPS_FLAG_VERBOSE)
|
if (flags & XBPS_FLAG_VERBOSE)
|
||||||
printf("Removed directory: %s\n", file);
|
printf("Removed: %s\n", file);
|
||||||
|
|
||||||
free(path);
|
free(path);
|
||||||
}
|
}
|
||||||
prop_object_iterator_release(iter);
|
prop_object_iterator_release(iter);
|
||||||
|
|
||||||
return 0;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SYMEXPORT
|
int SYMEXPORT
|
||||||
@ -230,8 +179,7 @@ xbps_remove_pkg(const char *pkgname, const char *version, bool update)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Iterate over the pkg file list dictionary and remove all
|
* Remove links, files and dirs.
|
||||||
* files, configuration files, links and dirs.
|
|
||||||
*/
|
*/
|
||||||
path = xbps_xasprintf("%s/%s/metadata/%s/%s",
|
path = xbps_xasprintf("%s/%s/metadata/%s/%s",
|
||||||
rootdir, XBPS_META_PATH, pkgname, XBPS_PKGFILES);
|
rootdir, XBPS_META_PATH, pkgname, XBPS_PKGFILES);
|
||||||
@ -248,14 +196,24 @@ xbps_remove_pkg(const char *pkgname, const char *version, bool update)
|
|||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
|
|
||||||
rv = remove_pkg_files(dict);
|
/* Remove links */
|
||||||
if (rv != 0) {
|
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) {
|
||||||
free(buf);
|
free(buf);
|
||||||
prop_object_release(dict);
|
prop_object_release(dict);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
prop_object_release(dict);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Run the post remove action if REMOVE file is there
|
* Run the post remove action if REMOVE file is there
|
||||||
* and we aren't updating a package.
|
* and we aren't updating a package.
|
||||||
|
Loading…
Reference in New Issue
Block a user