feat: new implementation to achieve desired behavior

This commit is contained in:
jg 2025-07-12 18:48:54 +02:00
parent 04bf4511f6
commit 017cf982bf
3 changed files with 33 additions and 10 deletions

View file

@ -22,6 +22,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
@ -31,7 +32,6 @@
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdbool.h>
/* libalpm */
#include "remove.h"
@ -162,7 +162,7 @@ static void remove_prepare_keep_needed(alpm_handle_t *handle, alpm_list_t *lp)
* @param handle the context handle
* @param lp list of packages to be removed
*/
static void remove_notify_needed_optdepends(alpm_handle_t *handle, alpm_list_t *lp)
static void remove_notify_needed_optdepends(alpm_handle_t *handle, alpm_list_t *lp, int remove_optdepends)
{
alpm_list_t *i;
@ -170,23 +170,31 @@ static void remove_notify_needed_optdepends(alpm_handle_t *handle, alpm_list_t *
alpm_pkg_t *pkg = i->data;
alpm_list_t *optdeps = alpm_pkg_get_optdepends(pkg);
if(optdeps && !alpm_pkg_find(lp, pkg->name)) {
if (!optdeps || alpm_pkg_find(lp, pkg->name)) {
continue;
}
alpm_list_t *j;
for(j = optdeps; j; j = alpm_list_next(j)) {
alpm_depend_t *optdep = j->data;
char *optstring = alpm_dep_compute_string(optdep);
if(alpm_find_satisfier(lp, optstring)) {
if (remove_optdepends) {
// -Rss, we delete optdepends
alpm_event_optdep_removal_t event = {
.type = ALPM_EVENT_OPTDEP_REMOVAL,
.pkg = pkg,
.optdep = optdep
};
EVENT(handle, &event);
} else {
// -Rs, we keep optdepends
alpm_list_remove(lp, optdep, _alpm_pkg_dep_cmp,NULL);
}
}
free(optstring);
}
}
}
}
/**
@ -258,7 +266,7 @@ int _alpm_remove_prepare(alpm_handle_t *handle, alpm_list_t **data)
/* Note packages being removed that are optdepends for installed packages */
if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
remove_notify_needed_optdepends(handle, trans->remove);
remove_notify_needed_optdepends(handle, trans->remove, (trans->flags & ALPM_TRANS_FLAG_RECURSEALL));
}
if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {

View file

@ -835,6 +835,19 @@ int _alpm_ldconfig(alpm_handle_t *handle)
return 0;
}
/** Helper function for comparing names of a dependency and a package
* @param p1 void pointer of the package
* @param p2 void pointer of the dependency
* @return result of the comparison of the names
* */
int _alpm_pkg_dep_cmp(const void *p1, const void *p2)
{
alpm_pkg_t *pkg = (alpm_pkg_t *) p1;
alpm_depend_t *dep = (alpm_depend_t *) p2;
return _alpm_str_cmp(pkg->name, dep->name);
}
/** Helper function for comparing strings using the alpm "compare func"
* signature.
* @param s1 first string to be compared

View file

@ -160,6 +160,8 @@ void *_alpm_realloc(void **data, size_t *current, const size_t required);
void *_alpm_greedy_grow(void **data, size_t *current, const size_t required);
alpm_errno_t _alpm_read_file(const char *filepath, unsigned char **data, size_t *data_len);
int _alpm_pkg_dep_cmp(const void *p1, const void *p2);
#ifndef HAVE_STRSEP
char *strsep(char **, const char *);
#endif