[PATCH 0/2] Permit query filter options with --owns
Recently I wanted to find foreign packages that own a path and found that pacman -Qmoq /my/path gives an error. I don't see any reason in principle why this should not be allowed, so I'd like to change it. Ronan Pigott (2): query: move declarations of filter related functions query: permit filter options with --owns src/pacman/pacman.c | 1 - src/pacman/query.c | 131 ++++++++++++++++++++++++-------------------- 2 files changed, 71 insertions(+), 61 deletions(-) -- 2.40.1
Signed-off-by: Ronan Pigott <ronan@rjp.ie> --- src/pacman/query.c | 114 ++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/src/pacman/query.c b/src/pacman/query.c index d75c4c801be5..84b885776e2d 100644 --- a/src/pacman/query.c +++ b/src/pacman/query.c @@ -135,6 +135,63 @@ cleanup: return (success ? resolved_path : NULL); } +static unsigned short pkg_get_locality(alpm_pkg_t *pkg) +{ + const char *pkgname = alpm_pkg_get_name(pkg); + alpm_list_t *j; + alpm_list_t *sync_dbs = alpm_get_syncdbs(config->handle); + + for(j = sync_dbs; j; j = alpm_list_next(j)) { + if(alpm_db_get_pkg(j->data, pkgname)) { + return PKG_LOCALITY_NATIVE; + } + } + return PKG_LOCALITY_FOREIGN; +} + +static int is_unrequired(alpm_pkg_t *pkg, unsigned short level) +{ + alpm_list_t *requiredby = alpm_pkg_compute_requiredby(pkg); + if(requiredby == NULL) { + if(level == 1) { + requiredby = alpm_pkg_compute_optionalfor(pkg); + } + if(requiredby == NULL) { + return 1; + } + } + FREELIST(requiredby); + return 0; +} + +static int filter(alpm_pkg_t *pkg) +{ + /* check if this package was explicitly installed */ + if(config->op_q_explicit && + alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_EXPLICIT) { + return 0; + } + /* check if this package was installed as a dependency */ + if(config->op_q_deps && + alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_DEPEND) { + return 0; + } + /* check if this pkg is or isn't in a sync DB */ + if(config->op_q_locality && config->op_q_locality != pkg_get_locality(pkg)) { + return 0; + } + /* check if this pkg is unrequired */ + if(config->op_q_unrequired && !is_unrequired(pkg, config->op_q_unrequired)) { + return 0; + } + /* check if this pkg is outdated */ + if(config->op_q_upgrade && (alpm_sync_get_new_version(pkg, + alpm_get_syncdbs(config->handle)) == NULL)) { + return 0; + } + return 1; +} + static int query_fileowner(alpm_list_t *targets) { int ret = 0; @@ -244,63 +301,6 @@ static int query_search(alpm_list_t *targets) } -static unsigned short pkg_get_locality(alpm_pkg_t *pkg) -{ - const char *pkgname = alpm_pkg_get_name(pkg); - alpm_list_t *j; - alpm_list_t *sync_dbs = alpm_get_syncdbs(config->handle); - - for(j = sync_dbs; j; j = alpm_list_next(j)) { - if(alpm_db_get_pkg(j->data, pkgname)) { - return PKG_LOCALITY_NATIVE; - } - } - return PKG_LOCALITY_FOREIGN; -} - -static int is_unrequired(alpm_pkg_t *pkg, unsigned short level) -{ - alpm_list_t *requiredby = alpm_pkg_compute_requiredby(pkg); - if(requiredby == NULL) { - if(level == 1) { - requiredby = alpm_pkg_compute_optionalfor(pkg); - } - if(requiredby == NULL) { - return 1; - } - } - FREELIST(requiredby); - return 0; -} - -static int filter(alpm_pkg_t *pkg) -{ - /* check if this package was explicitly installed */ - if(config->op_q_explicit && - alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_EXPLICIT) { - return 0; - } - /* check if this package was installed as a dependency */ - if(config->op_q_deps && - alpm_pkg_get_reason(pkg) != ALPM_PKG_REASON_DEPEND) { - return 0; - } - /* check if this pkg is or isn't in a sync DB */ - if(config->op_q_locality && config->op_q_locality != pkg_get_locality(pkg)) { - return 0; - } - /* check if this pkg is unrequired */ - if(config->op_q_unrequired && !is_unrequired(pkg, config->op_q_unrequired)) { - return 0; - } - /* check if this pkg is outdated */ - if(config->op_q_upgrade && (alpm_sync_get_new_version(pkg, - alpm_get_syncdbs(config->handle)) == NULL)) { - return 0; - } - return 1; -} - static int display(alpm_pkg_t *pkg) { int ret = 0; -- 2.40.1
On 05/29/23 at 02:54pm, Ronan Pigott wrote:
Signed-off-by: Ronan Pigott <ronan@rjp.ie> --- src/pacman/query.c | 114 ++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 57 deletions(-)
I think I'd prefer just adding a forward declaration at the top of the file instead of all this churn.
Signed-off-by: Ronan Pigott <ronan@rjp.ie> --- The error string is missing translations in this version, would appreciate some advice on what to do about that. src/pacman/pacman.c | 1 - src/pacman/query.c | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index e5c6e4200932..320c7654e0a7 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -608,7 +608,6 @@ static void checkargs_query(void) } else if(config->op_q_owns) { invalid_opt(config->group, "--owns", "--groups"); checkargs_query_display_opts("--owns"); - checkargs_query_filter_opts("--owns"); } else if(config->group) { checkargs_query_display_opts("--groups"); } diff --git a/src/pacman/query.c b/src/pacman/query.c index 84b885776e2d..dda3209a250c 100644 --- a/src/pacman/query.c +++ b/src/pacman/query.c @@ -251,8 +251,7 @@ static int query_fileowner(alpm_list_t *targets) if(strncmp(rpath, root, rootlen) != 0) { /* file is outside root, we know nothing can own it */ - pm_printf(ALPM_LOG_ERROR, _("No package owns %s\n"), filename); - goto targcleanup; + goto targnotfound; } rel_path = rpath + rootlen; @@ -267,13 +266,25 @@ static int query_fileowner(alpm_list_t *targets) } for(i = packages; i && (!found || is_dir); i = alpm_list_next(i)) { + if (!filter(i->data)) { + continue; + } if(alpm_filelist_contains(alpm_pkg_get_files(i->data), rel_path)) { print_query_fileowner(rpath, i->data); found = 1; } } + +targnotfound: if(!found) { - pm_printf(ALPM_LOG_ERROR, _("No package owns %s\n"), filename); + pm_printf(ALPM_LOG_ERROR, _("No %s%s%s%s%s%spackage owns %s\n"), + config->op_q_deps ? "non-explicit " : "", + config->op_q_explicit ? "explicit " : "", + config->op_q_upgrade ? "upgradable " : "", + config->op_q_unrequired ? "unrequired " : "", + config->op_q_locality & PKG_LOCALITY_NATIVE ? "native " : "", + config->op_q_locality & PKG_LOCALITY_FOREIGN ? "foreign " : "", + filename); } targcleanup: -- 2.40.1
On 05/29/23 at 02:54pm, Ronan Pigott wrote:
Signed-off-by: Ronan Pigott <ronan@rjp.ie> --- The error string is missing translations in this version, would appreciate some advice on what to do about that.
src/pacman/pacman.c | 1 - src/pacman/query.c | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index e5c6e4200932..320c7654e0a7 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -608,7 +608,6 @@ static void checkargs_query(void) } else if(config->op_q_owns) { invalid_opt(config->group, "--owns", "--groups"); checkargs_query_display_opts("--owns"); - checkargs_query_filter_opts("--owns"); } else if(config->group) { checkargs_query_display_opts("--groups"); } diff --git a/src/pacman/query.c b/src/pacman/query.c index 84b885776e2d..dda3209a250c 100644 --- a/src/pacman/query.c +++ b/src/pacman/query.c @@ -251,8 +251,7 @@ static int query_fileowner(alpm_list_t *targets)
if(strncmp(rpath, root, rootlen) != 0) { /* file is outside root, we know nothing can own it */ - pm_printf(ALPM_LOG_ERROR, _("No package owns %s\n"), filename); - goto targcleanup; + goto targnotfound; }
rel_path = rpath + rootlen; @@ -267,13 +266,25 @@ static int query_fileowner(alpm_list_t *targets) }
for(i = packages; i && (!found || is_dir); i = alpm_list_next(i)) { + if (!filter(i->data)) { + continue; + } if(alpm_filelist_contains(alpm_pkg_get_files(i->data), rel_path)) { print_query_fileowner(rpath, i->data); found = 1; } } + +targnotfound: if(!found) { - pm_printf(ALPM_LOG_ERROR, _("No package owns %s\n"), filename); + pm_printf(ALPM_LOG_ERROR, _("No %s%s%s%s%s%spackage owns %s\n"), + config->op_q_deps ? "non-explicit " : "", + config->op_q_explicit ? "explicit " : "", + config->op_q_upgrade ? "upgradable " : "", + config->op_q_unrequired ? "unrequired " : "", + config->op_q_locality & PKG_LOCALITY_NATIVE ? "native " : "", + config->op_q_locality & PKG_LOCALITY_FOREIGN ? "foreign " : "", + filename);
I really don't think this is necessary. Sure, you can argue that if a filter is used "No package owns X" is not technically true, but the user explicitly provided the filter and this adds something non-obvious we'd have to keep in sync if we add more filtering criteria.
}
targcleanup: -- 2.40.1
On 30/5/23 07:54, Ronan Pigott wrote:
Recently I wanted to find foreign packages that own a path and found that pacman -Qmoq /my/path gives an error. I don't see any reason in principle why this should not be allowed, so I'd like to change it.
Ronan Pigott (2): query: move declarations of filter related functions query: permit filter options with --owns
src/pacman/pacman.c | 1 - src/pacman/query.c | 131 ++++++++++++++++++++++++-------------------- 2 files changed, 71 insertions(+), 61 deletions(-)
Why? Only one package can ever own a path. I can not see a reason for this. Allan
Why? Only one package can ever own a path. I can not see a reason for this.
That isn't true? Try `pacman -Qo /etc` and I'm sure you'll get a big list. `pacman -Qmo /etc` now filters that to a smaller list.
On 30/5/23 10:33, ronan@rjp.ie wrote:
Why? Only one package can ever own a path. I can not see a reason for this.
That isn't true? Try `pacman -Qo /etc` and I'm sure you'll get a big list. `pacman -Qmo /etc` now filters that to a smaller list.
I forgot about directories... I am not highly motivated to look at the patches given this works: pacman -Qqo /etc | pacman -Qm
May 29, 2023 5:59 PM, "Allan McRae" <allan@archlinux.org> wrote:
I am not highly motivated to look at the patches given this works: pacman -Qqo /etc | pacman -Qm
I'm guessing you mean `pacman -Qqo /etc | pacman -Qm -`? Sure I guess but not as well. Consider: $ pacman -Qmo /etc /var /etc/ is owned by aurutils 10b-1 [...] error: No foreign package owns /var vs $ pacman -Qqo /etc /var | pacman -Qm - aurutils 10b-1 [...] Which of the multiple path arguments is owned by aurutils? It isn't as easy to recover this info in the pipeline. It's also just faster and shorter to type. Who doesn't like that?
participants (4)
-
Allan McRae
-
Andrew Gregory
-
Ronan Pigott
-
ronan@rjp.ie