[pacman-dev] [PATCH 1/2] pacman: refactor file match printing to its own function
Signed-off-by: morganamilo <morganamilo@gmail.com> --- src/pacman/files.c | 56 +++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/pacman/files.c b/src/pacman/files.c index fa4170bd..3ebd9b9b 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -99,11 +99,39 @@ static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { return 0; } +static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) +{ + alpm_db_t *db_local = alpm_get_localdb(config->handle); + const colstr_t *colstr = &config->colstr; + + if(config->op_f_machinereadable) { + alpm_list_t *ml; + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + print_line_machinereadable(repo, pkg, filename); + } + } else if(config->quiet) { + printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); + } else { + alpm_list_t *ml; + printf("%s%s/%s%s %s%s%s", colstr->repo, alpm_db_get_name(repo), + colstr->title, alpm_pkg_get_name(pkg), + colstr->version, alpm_pkg_get_version(pkg), colstr->nocolor); + + print_groups(pkg); + print_installed(db_local, pkg); + printf("\n"); + + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + printf(" %s\n", filename); + } + } +} + static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int ret = 0; - alpm_db_t *db_local = alpm_get_localdb(config->handle); alpm_list_t *t; - const colstr_t *colstr = &config->colstr; for(t = targets; t; t = alpm_list_next(t)) { char *targ = t->data; @@ -148,29 +176,7 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { } if(match != NULL) { - if(config->op_f_machinereadable) { - alpm_list_t *ml; - for(ml = match; ml; ml = alpm_list_next(ml)) { - char *filename = ml->data; - print_line_machinereadable(repo, pkg, filename); - } - } else if(config->quiet) { - printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); - } else { - alpm_list_t *ml; - printf("%s%s/%s%s %s%s%s", colstr->repo, alpm_db_get_name(repo), - colstr->title, alpm_pkg_get_name(pkg), - colstr->version, alpm_pkg_get_version(pkg), colstr->nocolor); - - print_groups(pkg); - print_installed(db_local, pkg); - printf("\n"); - - for(ml = match; ml; ml = alpm_list_next(ml)) { - c = ml->data; - printf(" %s\n", c); - } - } + print_match(match, repo, pkg); alpm_list_free(match); } } -- 2.20.1
Reworks the UI of -F according to FS#47949 In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s. Signed-off-by: morganamilo <morganamilo@gmail.com> --- This patch is WIP. Functional changes made, documentation still needs to be changed. Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2 src/pacman/conf.h | 1 - src/pacman/files.c | 119 +++++++++++++++----------------------------- src/pacman/pacman.c | 19 ++----- 3 files changed, 42 insertions(+), 97 deletions(-) diff --git a/src/pacman/conf.h b/src/pacman/conf.h index ababf2e0..04bba2dd 100644 --- a/src/pacman/conf.h +++ b/src/pacman/conf.h @@ -91,7 +91,6 @@ typedef struct __config_t { unsigned short op_s_search; unsigned short op_s_upgrade; - unsigned short op_f_regex; unsigned short op_f_machinereadable; unsigned short group; diff --git a/src/pacman/files.c b/src/pacman/files.c index 3ebd9b9b..5ec47c54 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -49,56 +49,6 @@ static void dump_pkg_machinereadable(alpm_db_t *db, alpm_pkg_t *pkg) } } -static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { - int ret = 0; - alpm_list_t *t; - - for(t = targets; t; t = alpm_list_next(t)) { - char *filename = t->data; - int found = 0; - alpm_list_t *s; - size_t len = strlen(filename); - - while(len > 1 && filename[0] == '/') { - filename++; - len--; - } - - for(s = syncs; s; s = alpm_list_next(s)) { - alpm_list_t *p; - alpm_db_t *repo = s->data; - alpm_list_t *packages = alpm_db_get_pkgcache(repo); - - for(p = packages; p; p = alpm_list_next(p)) { - alpm_pkg_t *pkg = p->data; - alpm_filelist_t *files = alpm_pkg_get_files(pkg); - - if(alpm_filelist_contains(files, filename)) { - if(config->op_f_machinereadable) { - print_line_machinereadable(repo, pkg, filename); - } else if(!config->quiet) { - const colstr_t *colstr = &config->colstr; - printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename, - colstr->repo, alpm_db_get_name(repo), colstr->title, - alpm_pkg_get_name(pkg), colstr->version, - alpm_pkg_get_version(pkg), colstr->nocolor); - } else { - printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); - } - - found = 1; - } - } - } - - if(!found) { - ret++; - } - } - - return 0; -} - static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) { alpm_db_t *db_local = alpm_get_localdb(config->handle); @@ -138,6 +88,15 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { alpm_list_t *s; int found = 0; regex_t reg; + size_t len = strlen(targ); + char *exact_file = strchr(targ, '/'); + + if(exact_file != NULL) { + while(len > 1 && targ[0] == '/') { + targ++; + len--; + } + } if(regex) { if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) { @@ -153,26 +112,40 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int m; for(p = packages; p; p = alpm_list_next(p)) { - size_t f = 0; - char* c; alpm_pkg_t *pkg = p->data; alpm_filelist_t *files = alpm_pkg_get_files(pkg); alpm_list_t *match = NULL; - while(f < files->count) { - c = strrchr(files->files[f].name, '/'); - if(c && *(c + 1)) { - if(regex) { - m = regexec(®, (c + 1), 0, 0, 0); - } else { - m = strcmp(c + 1, targ); + if(exact_file != NULL) { + if (regex) { + for(size_t f = 0; f < files->count; f++) { + char *c = files->files[f].name; + if(regexec(®, c, 0, 0, 0) == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } } - if(m == 0) { - match = alpm_list_add(match, files->files[f].name); + } else { + if(alpm_filelist_contains(files, targ)) { + match = alpm_list_add(match, targ); found = 1; } } - f++; + } else { + for(size_t f = 0; f < files->count; f++) { + char *c = strrchr(files->files[f].name, '/'); + if(c && *(c + 1)) { + if(regex) { + m = regexec(®, (c + 1), 0, 0, 0); + } else { + m = strcmp(c + 1, targ); + } + if(m == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } + } + } } if(match != NULL) { @@ -307,30 +280,16 @@ int pacman_files(alpm_list_t *targets) } } - if(targets == NULL && (config->op_q_owns | config->op_s_search)) { - pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); - return 1; - } - - /* determine the owner of a file */ - if(config->op_q_owns) { - return files_fileowner(files_dbs, targets); - } - - /* search for a file */ - if(config->op_s_search) { - return files_search(files_dbs, targets, config->op_f_regex); - } - /* get a listing of files in sync DBs */ if(config->op_q_list) { return files_list(files_dbs, targets); } - if(targets != NULL) { - pm_printf(ALPM_LOG_ERROR, _("no options specified (use -h for help)\n")); + if(targets == NULL) { + pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); return 1; } - return 0; + /* search for a file */ + return files_search(files_dbs, targets, config->op_q_search); } diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index a2a420b6..1ec57ff3 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -753,26 +753,18 @@ static int parsearg_files(int opt) return 0; } switch(opt) { - case OP_OWNS: - case 'o': - config->op_q_owns = 1; - break; case OP_LIST: case 'l': config->op_q_list = 1; break; case OP_SEARCH: case 's': - config->op_s_search = 1; + config->op_q_search = 1; break; case OP_REFRESH: case 'y': (config->op_s_sync)++; break; - case OP_REGEX: - case 'x': - config->op_f_regex = 1; - break; case OP_MACHINEREADABLE: config->op_f_machinereadable = 1; break; @@ -788,13 +780,8 @@ static int parsearg_files(int opt) static void checkargs_files(void) { - if(config->op_q_owns) { - invalid_opt(config->op_q_list, "--owns", "--list"); - invalid_opt(config->op_q_search, "--owns", "--search"); - invalid_opt(config->op_f_regex, "--owns", "--regex"); - } else if(config->op_q_list) { - invalid_opt(config->op_q_search, "--list", "--search"); - invalid_opt(config->op_f_regex, "--list", "--regex"); + if(config->op_q_search) { + invalid_opt(config->op_q_list, "--search", "--list"); } } -- 2.20.1
On 3/2/19 4:42 am, morganamilo wrote:
Reworks the UI of -F according to FS#47949
In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s.
Signed-off-by: morganamilo <morganamilo@gmail.com> ---
This patch is WIP. Functional changes made, documentation still needs to be changed.
Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2
I'm OK with the changes (without having done a review of your code...). However, I think we need to work on the output. Old: $ pacman -Fo opt/ opt/ is owned by core/filesystem 2018.12-2 opt/ is owned by extra/bullet 2.88-1 opt/ is owned by extra/postgresql-old-upgrade 10.6-1 opt/ is owned by community/9base 6-6 opt/ is owned by community/aspnet-runtime 2.2.1+102-1 ... New: $ ./src/pacman/pacman -F opt/ core/filesystem 2018.12-2 (base) [installed] opt/ extra/bullet 2.88-1 opt/ extra/postgresql-old-upgrade 10.6-1 opt/ ... So the new output follows the old -Fs, which was good when the filepath was different for each match. But it not great for some situations now... That output remains good for the new -Fs (rainbow issues being ignored!), but a rethink is needed for the new -F operations. Allan
On Thu, 7 Feb 2019 at 01:31, Allan McRae <allan@archlinux.org> wrote:
On 3/2/19 4:42 am, morganamilo wrote:
Reworks the UI of -F according to FS#47949
In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s.
Signed-off-by: morganamilo <morganamilo@gmail.com> ---
This patch is WIP. Functional changes made, documentation still needs to be changed.
Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2
I'm OK with the changes (without having done a review of your code...). However, I think we need to work on the output.
Old: $ pacman -Fo opt/ opt/ is owned by core/filesystem 2018.12-2 opt/ is owned by extra/bullet 2.88-1 opt/ is owned by extra/postgresql-old-upgrade 10.6-1 opt/ is owned by community/9base 6-6 opt/ is owned by community/aspnet-runtime 2.2.1+102-1 ...
New: $ ./src/pacman/pacman -F opt/ core/filesystem 2018.12-2 (base) [installed] opt/ extra/bullet 2.88-1 opt/ extra/postgresql-old-upgrade 10.6-1 opt/ ...
So the new output follows the old -Fs, which was good when the filepath was different for each match. But it not great for some situations now... That output remains good for the new -Fs (rainbow issues being ignored!), but a rethink is needed for the new -F operations.
Allan
What if we keep-Fo but have it just change the output format? -F and -Fo would both work on files and paths.
On 2/6/19 9:22 PM, Morgan Adamiec wrote:
On Thu, 7 Feb 2019 at 01:31, Allan McRae <allan@archlinux.org> wrote:
On 3/2/19 4:42 am, morganamilo wrote:
Reworks the UI of -F according to FS#47949
In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s.
Signed-off-by: morganamilo <morganamilo@gmail.com> ---
This patch is WIP. Functional changes made, documentation still needs to be changed.
Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2
I'm OK with the changes (without having done a review of your code...). However, I think we need to work on the output.
Old: $ pacman -Fo opt/ opt/ is owned by core/filesystem 2018.12-2 opt/ is owned by extra/bullet 2.88-1 opt/ is owned by extra/postgresql-old-upgrade 10.6-1 opt/ is owned by community/9base 6-6 opt/ is owned by community/aspnet-runtime 2.2.1+102-1 ...
New: $ ./src/pacman/pacman -F opt/ core/filesystem 2018.12-2 (base) [installed] opt/ extra/bullet 2.88-1 opt/ extra/postgresql-old-upgrade 10.6-1 opt/ ...
So the new output follows the old -Fs, which was good when the filepath was different for each match. But it not great for some situations now... That output remains good for the new -Fs (rainbow issues being ignored!), but a rethink is needed for the new -F operations.
Allan
What if we keep-Fo but have it just change the output format? -F and -Fo would both work on files and paths.
What would it mean, though? Perhaps -o, --oneline -- Eli Schwartz Bug Wrangler and Trusted User
On Thu, 7 Feb 2019 at 03:13, Eli Schwartz <eschwartz@archlinux.org> wrote:
On 2/6/19 9:22 PM, Morgan Adamiec wrote:
On Thu, 7 Feb 2019 at 01:31, Allan McRae <allan@archlinux.org> wrote:
On 3/2/19 4:42 am, morganamilo wrote:
Reworks the UI of -F according to FS#47949
In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s.
Signed-off-by: morganamilo <morganamilo@gmail.com> ---
This patch is WIP. Functional changes made, documentation still needs to be changed.
Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2
I'm OK with the changes (without having done a review of your code...). However, I think we need to work on the output.
Old: $ pacman -Fo opt/ opt/ is owned by core/filesystem 2018.12-2 opt/ is owned by extra/bullet 2.88-1 opt/ is owned by extra/postgresql-old-upgrade 10.6-1 opt/ is owned by community/9base 6-6 opt/ is owned by community/aspnet-runtime 2.2.1+102-1 ...
New: $ ./src/pacman/pacman -F opt/ core/filesystem 2018.12-2 (base) [installed] opt/ extra/bullet 2.88-1 opt/ extra/postgresql-old-upgrade 10.6-1 opt/ ...
So the new output follows the old -Fs, which was good when the filepath was different for each match. But it not great for some situations now... That output remains good for the new -Fs (rainbow issues being ignored!), but a rethink is needed for the new -F operations.
Allan
What if we keep-Fo but have it just change the output format? -F and -Fo would both work on files and paths.
What would it mean, though? Perhaps
-o, --oneline
-- Eli Schwartz Bug Wrangler and Trusted User
Could do but I see no reason why it can't continue to mean "owns" Assuming the message is still in the form: opt/ is owned by core/filesystem 2018.12-2
On 2/7/19 11:19 AM, Morgan Adamiec wrote:
On Thu, 7 Feb 2019 at 03:13, Eli Schwartz <eschwartz@archlinux.org> wrote:
On 2/6/19 9:22 PM, Morgan Adamiec wrote:
What if we keep-Fo but have it just change the output format? -F and -Fo would both work on files and paths.
First you say this.
What would it mean, though? Perhaps
-o, --oneline
-- Eli Schwartz Bug Wrangler and Trusted User
Could do but I see no reason why it can't continue to mean "owns"
Assuming the message is still in the form: opt/ is owned by core/filesystem 2018.12-2
Then you say this. ... Do you not see how "continue to mean 'owns'" and "it just changes the output format" are logically inconsistent? If the default is to show you what is "own'ed", and a flag called --owns reformats the output instead, then the flag is simply a lie. -- Eli Schwartz Bug Wrangler and Trusted User
On Thu, 7 Feb 2019 at 17:01, Eli Schwartz <eschwartz@archlinux.org> wrote:
On 2/7/19 11:19 AM, Morgan Adamiec wrote:
On Thu, 7 Feb 2019 at 03:13, Eli Schwartz <eschwartz@archlinux.org> wrote:
On 2/6/19 9:22 PM, Morgan Adamiec wrote:
What if we keep-Fo but have it just change the output format? -F and -Fo would both work on files and paths.
First you say this.
What would it mean, though? Perhaps
-o, --oneline
-- Eli Schwartz Bug Wrangler and Trusted User
Could do but I see no reason why it can't continue to mean "owns"
Assuming the message is still in the form: opt/ is owned by core/filesystem 2018.12-2
Then you say this.
...
Do you not see how "continue to mean 'owns'" and "it just changes the output format" are logically inconsistent?
If the default is to show you what is "own'ed", and a flag called --owns reformats the output instead, then the flag is simply a lie.
-- Eli Schwartz Bug Wrangler and Trusted User
Yeah I get how --owns would kinda of not make sense given the default -F output still technically shows who owns what. I guess --oneline would make one sense then.
Reworks the UI of -F according to FS#47949 In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s. --oneline/-o can be used to change the output format to match the old -Fo Signed-off-by: morganamilo <morganamilo@gmail.com> --- v1: This patch is WIP. Functional changes made, documentation still needs to be changed. Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2 v2: added --oneline/-o diff --git a/src/pacman/conf.h b/src/pacman/conf.h index f45ed436..523b54a5 100644 --- a/src/pacman/conf.h +++ b/src/pacman/conf.h @@ -90,8 +90,8 @@ typedef struct __config_t { unsigned short op_s_search; unsigned short op_s_upgrade; - unsigned short op_f_regex; unsigned short op_f_machinereadable; + unsigned short op_f_oneline; unsigned short group; unsigned short noask; @@ -200,6 +200,7 @@ enum { OP_SEARCH, OP_REGEX, OP_MACHINEREADABLE, + OP_ONELINE, OP_UNREQUIRED, OP_UPGRADES, OP_SYSUPGRADE, diff --git a/src/pacman/files.c b/src/pacman/files.c index 3ebd9b9b..905e16ed 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -49,54 +49,13 @@ static void dump_pkg_machinereadable(alpm_db_t *db, alpm_pkg_t *pkg) } } -static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { - int ret = 0; - alpm_list_t *t; - - for(t = targets; t; t = alpm_list_next(t)) { - char *filename = t->data; - int found = 0; - alpm_list_t *s; - size_t len = strlen(filename); - - while(len > 1 && filename[0] == '/') { - filename++; - len--; - } - - for(s = syncs; s; s = alpm_list_next(s)) { - alpm_list_t *p; - alpm_db_t *repo = s->data; - alpm_list_t *packages = alpm_db_get_pkgcache(repo); - - for(p = packages; p; p = alpm_list_next(p)) { - alpm_pkg_t *pkg = p->data; - alpm_filelist_t *files = alpm_pkg_get_files(pkg); - - if(alpm_filelist_contains(files, filename)) { - if(config->op_f_machinereadable) { - print_line_machinereadable(repo, pkg, filename); - } else if(!config->quiet) { - const colstr_t *colstr = &config->colstr; - printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename, - colstr->repo, alpm_db_get_name(repo), colstr->title, - alpm_pkg_get_name(pkg), colstr->version, - alpm_pkg_get_version(pkg), colstr->nocolor); - } else { - printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); - } - - found = 1; - } - } - } - - if(!found) { - ret++; - } - } - - return 0; +static void print_owned_by(alpm_db_t *db, alpm_pkg_t *pkg, char *filename) +{ + const colstr_t *colstr = &config->colstr; + printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename, + colstr->repo, alpm_db_get_name(db), colstr->title, + alpm_pkg_get_name(pkg), colstr->version, + alpm_pkg_get_version(pkg), colstr->nocolor); } static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) @@ -110,6 +69,12 @@ static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) char *filename = ml->data; print_line_machinereadable(repo, pkg, filename); } + } else if(config->op_f_oneline) { + alpm_list_t *ml; + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + print_owned_by(repo, pkg, filename); + } } else if(config->quiet) { printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); } else { @@ -138,6 +103,15 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { alpm_list_t *s; int found = 0; regex_t reg; + size_t len = strlen(targ); + char *exact_file = strchr(targ, '/'); + + if(exact_file != NULL) { + while(len > 1 && targ[0] == '/') { + targ++; + len--; + } + } if(regex) { if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) { @@ -153,26 +127,40 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int m; for(p = packages; p; p = alpm_list_next(p)) { - size_t f = 0; - char* c; alpm_pkg_t *pkg = p->data; alpm_filelist_t *files = alpm_pkg_get_files(pkg); alpm_list_t *match = NULL; - while(f < files->count) { - c = strrchr(files->files[f].name, '/'); - if(c && *(c + 1)) { - if(regex) { - m = regexec(®, (c + 1), 0, 0, 0); - } else { - m = strcmp(c + 1, targ); + if(exact_file != NULL) { + if (regex) { + for(size_t f = 0; f < files->count; f++) { + char *c = files->files[f].name; + if(regexec(®, c, 0, 0, 0) == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } } - if(m == 0) { - match = alpm_list_add(match, files->files[f].name); + } else { + if(alpm_filelist_contains(files, targ)) { + match = alpm_list_add(match, targ); found = 1; } } - f++; + } else { + for(size_t f = 0; f < files->count; f++) { + char *c = strrchr(files->files[f].name, '/'); + if(c && *(c + 1)) { + if(regex) { + m = regexec(®, (c + 1), 0, 0, 0); + } else { + m = strcmp(c + 1, targ); + } + if(m == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } + } + } } if(match != NULL) { @@ -307,30 +295,16 @@ int pacman_files(alpm_list_t *targets) } } - if(targets == NULL && (config->op_q_owns | config->op_s_search)) { - pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); - return 1; - } - - /* determine the owner of a file */ - if(config->op_q_owns) { - return files_fileowner(files_dbs, targets); - } - - /* search for a file */ - if(config->op_s_search) { - return files_search(files_dbs, targets, config->op_f_regex); - } - /* get a listing of files in sync DBs */ if(config->op_q_list) { return files_list(files_dbs, targets); } - if(targets != NULL) { - pm_printf(ALPM_LOG_ERROR, _("no options specified (use -h for help)\n")); + if(targets == NULL) { + pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); return 1; } - return 0; + /* search for a file */ + return files_search(files_dbs, targets, config->op_q_search); } diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 3bb622e6..45b3ab9b 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -753,29 +753,25 @@ static int parsearg_files(int opt) return 0; } switch(opt) { - case OP_OWNS: - case 'o': - config->op_q_owns = 1; - break; case OP_LIST: case 'l': config->op_q_list = 1; break; case OP_SEARCH: case 's': - config->op_s_search = 1; + config->op_q_search = 1; break; case OP_REFRESH: case 'y': (config->op_s_sync)++; break; - case OP_REGEX: - case 'x': - config->op_f_regex = 1; - break; case OP_MACHINEREADABLE: config->op_f_machinereadable = 1; break; + case OP_ONELINE: + case 'o': + config->op_f_oneline = 1; + break; case OP_QUIET: case 'q': config->quiet = 1; @@ -788,13 +784,8 @@ static int parsearg_files(int opt) static void checkargs_files(void) { - if(config->op_q_owns) { - invalid_opt(config->op_q_list, "--owns", "--list"); - invalid_opt(config->op_q_search, "--owns", "--search"); - invalid_opt(config->op_f_regex, "--owns", "--regex"); - } else if(config->op_q_list) { - invalid_opt(config->op_q_search, "--list", "--search"); - invalid_opt(config->op_f_regex, "--list", "--regex"); + if(config->op_q_search) { + invalid_opt(config->op_q_list, "--search", "--list"); } } @@ -918,6 +909,7 @@ static int parseargs(int argc, char *argv[]) {"native", no_argument, 0, OP_NATIVE}, {"nosave", no_argument, 0, OP_NOSAVE}, {"owns", no_argument, 0, OP_OWNS}, + {"oneline", no_argument, 0, OP_ONELINE}, {"file", no_argument, 0, OP_FILE}, {"print", no_argument, 0, OP_PRINT}, {"quiet", no_argument, 0, OP_QUIET}, -- 2.21.0
On Sat, 20 Apr 2019 at 19:38, morganamilo <morganamilo@gmail.com> wrote:
Reworks the UI of -F according to FS#47949
In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s. --oneline/-o can be used to change the output format to match the old -Fo
Signed-off-by: morganamilo <morganamilo@gmail.com> ---
v1: This patch is WIP. Functional changes made, documentation still needs to be changed.
Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2
v2: added --oneline/-o
diff --git a/src/pacman/conf.h b/src/pacman/conf.h index f45ed436..523b54a5 100644 --- a/src/pacman/conf.h +++ b/src/pacman/conf.h @@ -90,8 +90,8 @@ typedef struct __config_t { unsigned short op_s_search; unsigned short op_s_upgrade;
- unsigned short op_f_regex; unsigned short op_f_machinereadable; + unsigned short op_f_oneline;
unsigned short group; unsigned short noask; @@ -200,6 +200,7 @@ enum { OP_SEARCH, OP_REGEX, OP_MACHINEREADABLE, + OP_ONELINE, OP_UNREQUIRED, OP_UPGRADES, OP_SYSUPGRADE, diff --git a/src/pacman/files.c b/src/pacman/files.c index 3ebd9b9b..905e16ed 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -49,54 +49,13 @@ static void dump_pkg_machinereadable(alpm_db_t *db, alpm_pkg_t *pkg) } }
-static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { - int ret = 0; - alpm_list_t *t; - - for(t = targets; t; t = alpm_list_next(t)) { - char *filename = t->data; - int found = 0; - alpm_list_t *s; - size_t len = strlen(filename); - - while(len > 1 && filename[0] == '/') { - filename++; - len--; - } - - for(s = syncs; s; s = alpm_list_next(s)) { - alpm_list_t *p; - alpm_db_t *repo = s->data; - alpm_list_t *packages = alpm_db_get_pkgcache(repo); - - for(p = packages; p; p = alpm_list_next(p)) { - alpm_pkg_t *pkg = p->data; - alpm_filelist_t *files = alpm_pkg_get_files(pkg); - - if(alpm_filelist_contains(files, filename)) { - if(config->op_f_machinereadable) { - print_line_machinereadable(repo, pkg, filename); - } else if(!config->quiet) { - const colstr_t *colstr = &config->colstr; - printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename, - colstr->repo, alpm_db_get_name(repo), colstr->title, - alpm_pkg_get_name(pkg), colstr->version, - alpm_pkg_get_version(pkg), colstr->nocolor); - } else { - printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); - } - - found = 1; - } - } - } - - if(!found) { - ret++; - } - } - - return 0; +static void print_owned_by(alpm_db_t *db, alpm_pkg_t *pkg, char *filename) +{ + const colstr_t *colstr = &config->colstr; + printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename, + colstr->repo, alpm_db_get_name(db), colstr->title, + alpm_pkg_get_name(pkg), colstr->version, + alpm_pkg_get_version(pkg), colstr->nocolor); }
static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) @@ -110,6 +69,12 @@ static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) char *filename = ml->data; print_line_machinereadable(repo, pkg, filename); } + } else if(config->op_f_oneline) { + alpm_list_t *ml; + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + print_owned_by(repo, pkg, filename); + } } else if(config->quiet) { printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); } else { @@ -138,6 +103,15 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { alpm_list_t *s; int found = 0; regex_t reg; + size_t len = strlen(targ); + char *exact_file = strchr(targ, '/'); + + if(exact_file != NULL) { + while(len > 1 && targ[0] == '/') { + targ++; + len--; + } + }
if(regex) { if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) { @@ -153,26 +127,40 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int m;
for(p = packages; p; p = alpm_list_next(p)) { - size_t f = 0; - char* c; alpm_pkg_t *pkg = p->data; alpm_filelist_t *files = alpm_pkg_get_files(pkg); alpm_list_t *match = NULL;
- while(f < files->count) { - c = strrchr(files->files[f].name, '/'); - if(c && *(c + 1)) { - if(regex) { - m = regexec(®, (c + 1), 0, 0, 0); - } else { - m = strcmp(c + 1, targ); + if(exact_file != NULL) { + if (regex) { + for(size_t f = 0; f < files->count; f++) { + char *c = files->files[f].name; + if(regexec(®, c, 0, 0, 0) == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } } - if(m == 0) { - match = alpm_list_add(match, files->files[f].name); + } else { + if(alpm_filelist_contains(files, targ)) { + match = alpm_list_add(match, targ); found = 1; } } - f++; + } else { + for(size_t f = 0; f < files->count; f++) { + char *c = strrchr(files->files[f].name, '/'); + if(c && *(c + 1)) { + if(regex) { + m = regexec(®, (c + 1), 0, 0, 0); + } else { + m = strcmp(c + 1, targ); + } + if(m == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } + } + } }
if(match != NULL) { @@ -307,30 +295,16 @@ int pacman_files(alpm_list_t *targets) } }
- if(targets == NULL && (config->op_q_owns | config->op_s_search)) { - pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); - return 1; - } - - /* determine the owner of a file */ - if(config->op_q_owns) { - return files_fileowner(files_dbs, targets); - } - - /* search for a file */ - if(config->op_s_search) { - return files_search(files_dbs, targets, config->op_f_regex); - } - /* get a listing of files in sync DBs */ if(config->op_q_list) { return files_list(files_dbs, targets); }
- if(targets != NULL) { - pm_printf(ALPM_LOG_ERROR, _("no options specified (use -h for help)\n")); + if(targets == NULL) { + pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); return 1; }
- return 0; + /* search for a file */ + return files_search(files_dbs, targets, config->op_q_search); } diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 3bb622e6..45b3ab9b 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -753,29 +753,25 @@ static int parsearg_files(int opt) return 0; } switch(opt) { - case OP_OWNS: - case 'o': - config->op_q_owns = 1; - break; case OP_LIST: case 'l': config->op_q_list = 1; break; case OP_SEARCH: case 's': - config->op_s_search = 1; + config->op_q_search = 1; break; case OP_REFRESH: case 'y': (config->op_s_sync)++; break; - case OP_REGEX: - case 'x': - config->op_f_regex = 1; - break; case OP_MACHINEREADABLE: config->op_f_machinereadable = 1; break; + case OP_ONELINE: + case 'o': + config->op_f_oneline = 1; + break; case OP_QUIET: case 'q': config->quiet = 1; @@ -788,13 +784,8 @@ static int parsearg_files(int opt)
static void checkargs_files(void) { - if(config->op_q_owns) { - invalid_opt(config->op_q_list, "--owns", "--list"); - invalid_opt(config->op_q_search, "--owns", "--search"); - invalid_opt(config->op_f_regex, "--owns", "--regex"); - } else if(config->op_q_list) { - invalid_opt(config->op_q_search, "--list", "--search"); - invalid_opt(config->op_f_regex, "--list", "--regex"); + if(config->op_q_search) { + invalid_opt(config->op_q_list, "--search", "--list"); } }
@@ -918,6 +909,7 @@ static int parseargs(int argc, char *argv[]) {"native", no_argument, 0, OP_NATIVE}, {"nosave", no_argument, 0, OP_NOSAVE}, {"owns", no_argument, 0, OP_OWNS}, + {"oneline", no_argument, 0, OP_ONELINE}, {"file", no_argument, 0, OP_FILE}, {"print", no_argument, 0, OP_PRINT}, {"quiet", no_argument, 0, OP_QUIET}, -- 2.21.0
I'm finally gonna get around to finishing this. Hopefully every one is fine with how --oneline/-o works. Although I have been thinking that maybe -o should be the default and some other flag could be used for a verbose mode. Also thought's on having -F automatically search path like -Qo?
On 21/4/19 5:40 am, Morgan Adamiec wrote:
On Sat, 20 Apr 2019 at 19:38, morganamilo <morganamilo@gmail.com> wrote:
Reworks the UI of -F according to FS#47949
In short -F replaces both -Fs and -Fo. --regex/-x has been replaced with --search/-s. --oneline/-o can be used to change the output format to match the old -Fo
Signed-off-by: morganamilo <morganamilo@gmail.com> ---
v1: This patch is WIP. Functional changes made, documentation still needs to be changed.
Additionally I think https://bugs.archlinux.org/task/47949#comment143477 Is a good idea and I will probably be included in v2
v2: added --oneline/-o
<snip>
I'm finally gonna get around to finishing this. Hopefully every one is fine with how --oneline/-o works. Although I have been thinking that maybe -o should be the default and some other flag could be used for a verbose mode. Also thought's on having -F automatically search path like -Qo?
Sorry for the delayed review. This is a very busy month for me and work. I'm don't think we need a unified output for -F here. We are essentially switching between the old -Fs and -Fo search when there is a "/" in the search. This can be see by running: $ ./src/pacman/pacman -F opt extra/llvm 8.0.0-2 usr/bin/opt ... $ ./src/pacman/pacman -F opt/ core/filesystem 2018.12-2 (base) [installed] opt/ ... So keeping the old output based on which search is being used seems a good solution. This keeps the output in line with -Qs and -Qo, while simplifying the options and would solve the main issue with this change. I am also against changing -x to -s because we are only removing -s in this release. That would give a big behaviour change when -s without notification (assuming someone did not read the changelog). Not changing -x would mean removing -s and -o will give errors when people try using them. Allan
--- src/pacman/files.c | 71 ++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/src/pacman/files.c b/src/pacman/files.c index fa4170bd..26f96a67 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -49,6 +49,15 @@ static void dump_pkg_machinereadable(alpm_db_t *db, alpm_pkg_t *pkg) } } +static void print_owned_by(alpm_db_t *db, alpm_pkg_t *pkg, char *filename) +{ + const colstr_t *colstr = &config->colstr; + printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename, + colstr->repo, alpm_db_get_name(db), colstr->title, + alpm_pkg_get_name(pkg), colstr->version, + alpm_pkg_get_version(pkg), colstr->nocolor); +} + static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { int ret = 0; alpm_list_t *t; @@ -77,11 +86,7 @@ static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { if(config->op_f_machinereadable) { print_line_machinereadable(repo, pkg, filename); } else if(!config->quiet) { - const colstr_t *colstr = &config->colstr; - printf(_("%s is owned by %s%s/%s%s %s%s%s\n"), filename, - colstr->repo, alpm_db_get_name(repo), colstr->title, - alpm_pkg_get_name(pkg), colstr->version, - alpm_pkg_get_version(pkg), colstr->nocolor); + print_owned_by(repo, pkg, filename); } else { printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); } @@ -99,11 +104,39 @@ static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { return 0; } +static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) +{ + alpm_db_t *db_local = alpm_get_localdb(config->handle); + const colstr_t *colstr = &config->colstr; + + if(config->op_f_machinereadable) { + alpm_list_t *ml; + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + print_line_machinereadable(repo, pkg, filename); + } + } else if(config->quiet) { + printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); + } else { + alpm_list_t *ml; + printf("%s%s/%s%s %s%s%s", colstr->repo, alpm_db_get_name(repo), + colstr->title, alpm_pkg_get_name(pkg), + colstr->version, alpm_pkg_get_version(pkg), colstr->nocolor); + + print_groups(pkg); + print_installed(db_local, pkg); + printf("\n"); + + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + printf(" %s\n", filename); + } + } +} + static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int ret = 0; - alpm_db_t *db_local = alpm_get_localdb(config->handle); alpm_list_t *t; - const colstr_t *colstr = &config->colstr; for(t = targets; t; t = alpm_list_next(t)) { char *targ = t->data; @@ -148,29 +181,7 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { } if(match != NULL) { - if(config->op_f_machinereadable) { - alpm_list_t *ml; - for(ml = match; ml; ml = alpm_list_next(ml)) { - char *filename = ml->data; - print_line_machinereadable(repo, pkg, filename); - } - } else if(config->quiet) { - printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); - } else { - alpm_list_t *ml; - printf("%s%s/%s%s %s%s%s", colstr->repo, alpm_db_get_name(repo), - colstr->title, alpm_pkg_get_name(pkg), - colstr->version, alpm_pkg_get_version(pkg), colstr->nocolor); - - print_groups(pkg); - print_installed(db_local, pkg); - printf("\n"); - - for(ml = match; ml; ml = alpm_list_next(ml)) { - c = ml->data; - printf(" %s\n", c); - } - } + print_match(match, repo, pkg); alpm_list_free(match); } } -- 2.21.0
Reworks the UI of -F according to FS#47949 In short -F replaces both -Fs and -Fo. Searching for an exact path (target contains "/"), causes the output to switch to the old -Fo output. Otherwise the old -Fs output is used. Also strip the leading "/" from targets like how -Qo does. TODO: docs --- src/pacman/files.c | 125 +++++++++++++++++--------------------------- src/pacman/pacman.c | 17 +----- 2 files changed, 49 insertions(+), 93 deletions(-) diff --git a/src/pacman/files.c b/src/pacman/files.c index 26f96a67..74d06815 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -58,53 +58,7 @@ static void print_owned_by(alpm_db_t *db, alpm_pkg_t *pkg, char *filename) alpm_pkg_get_version(pkg), colstr->nocolor); } -static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { - int ret = 0; - alpm_list_t *t; - - for(t = targets; t; t = alpm_list_next(t)) { - char *filename = t->data; - int found = 0; - alpm_list_t *s; - size_t len = strlen(filename); - - while(len > 1 && filename[0] == '/') { - filename++; - len--; - } - - for(s = syncs; s; s = alpm_list_next(s)) { - alpm_list_t *p; - alpm_db_t *repo = s->data; - alpm_list_t *packages = alpm_db_get_pkgcache(repo); - - for(p = packages; p; p = alpm_list_next(p)) { - alpm_pkg_t *pkg = p->data; - alpm_filelist_t *files = alpm_pkg_get_files(pkg); - - if(alpm_filelist_contains(files, filename)) { - if(config->op_f_machinereadable) { - print_line_machinereadable(repo, pkg, filename); - } else if(!config->quiet) { - print_owned_by(repo, pkg, filename); - } else { - printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); - } - - found = 1; - } - } - } - - if(!found) { - ret++; - } - } - - return 0; -} - -static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) +static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg, char *exact_file) { alpm_db_t *db_local = alpm_get_localdb(config->handle); const colstr_t *colstr = &config->colstr; @@ -117,6 +71,12 @@ static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) } } else if(config->quiet) { printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); + } else if(exact_file != NULL) { + alpm_list_t *ml; + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + print_owned_by(repo, pkg, filename); + } } else { alpm_list_t *ml; printf("%s%s/%s%s %s%s%s", colstr->repo, alpm_db_get_name(repo), @@ -143,6 +103,15 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { alpm_list_t *s; int found = 0; regex_t reg; + size_t len = strlen(targ); + char *exact_file = strchr(targ, '/'); + + if(exact_file != NULL) { + while(len > 1 && targ[0] == '/') { + targ++; + len--; + } + } if(regex) { if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) { @@ -158,30 +127,44 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int m; for(p = packages; p; p = alpm_list_next(p)) { - size_t f = 0; - char* c; alpm_pkg_t *pkg = p->data; alpm_filelist_t *files = alpm_pkg_get_files(pkg); alpm_list_t *match = NULL; - while(f < files->count) { - c = strrchr(files->files[f].name, '/'); - if(c && *(c + 1)) { - if(regex) { - m = regexec(®, (c + 1), 0, 0, 0); - } else { - m = strcmp(c + 1, targ); + if(exact_file != NULL) { + if (regex) { + for(size_t f = 0; f < files->count; f++) { + char *c = files->files[f].name; + if(regexec(®, c, 0, 0, 0) == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } } - if(m == 0) { - match = alpm_list_add(match, files->files[f].name); + } else { + if(alpm_filelist_contains(files, targ)) { + match = alpm_list_add(match, targ); found = 1; } } - f++; + } else { + for(size_t f = 0; f < files->count; f++) { + char *c = strrchr(files->files[f].name, '/'); + if(c && *(c + 1)) { + if(regex) { + m = regexec(®, (c + 1), 0, 0, 0); + } else { + m = strcmp(c + 1, targ); + } + if(m == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } + } + } } if(match != NULL) { - print_match(match, repo, pkg); + print_match(match, repo, pkg, exact_file); alpm_list_free(match); } } @@ -312,30 +295,16 @@ int pacman_files(alpm_list_t *targets) } } - if(targets == NULL && (config->op_q_owns | config->op_s_search)) { - pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); - return 1; - } - - /* determine the owner of a file */ - if(config->op_q_owns) { - return files_fileowner(files_dbs, targets); - } - - /* search for a file */ - if(config->op_s_search) { - return files_search(files_dbs, targets, config->op_f_regex); - } - /* get a listing of files in sync DBs */ if(config->op_q_list) { return files_list(files_dbs, targets); } - if(targets != NULL) { - pm_printf(ALPM_LOG_ERROR, _("no options specified (use -h for help)\n")); + if(targets == NULL) { + pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); return 1; } - return 0; + /* search for a file */ + return files_search(files_dbs, targets, config->op_f_regex); } diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 3bb622e6..4bb6ac2f 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -753,18 +753,10 @@ static int parsearg_files(int opt) return 0; } switch(opt) { - case OP_OWNS: - case 'o': - config->op_q_owns = 1; - break; case OP_LIST: case 'l': config->op_q_list = 1; break; - case OP_SEARCH: - case 's': - config->op_s_search = 1; - break; case OP_REFRESH: case 'y': (config->op_s_sync)++; @@ -788,13 +780,8 @@ static int parsearg_files(int opt) static void checkargs_files(void) { - if(config->op_q_owns) { - invalid_opt(config->op_q_list, "--owns", "--list"); - invalid_opt(config->op_q_search, "--owns", "--search"); - invalid_opt(config->op_f_regex, "--owns", "--regex"); - } else if(config->op_q_list) { - invalid_opt(config->op_q_search, "--list", "--search"); - invalid_opt(config->op_f_regex, "--list", "--regex"); + if(config->op_q_search) { + invalid_opt(config->op_q_list, "--regex", "--list"); } } -- 2.21.0
On 29/5/19 7:30 am, morganamilo wrote:
Reworks the UI of -F according to FS#47949
In short -F replaces both -Fs and -Fo.
Searching for an exact path (target contains "/"), causes the output to switch to the old -Fo output. Otherwise the old -Fs output is used.
Also strip the leading "/" from targets like how -Qo does.
TODO: docs --- src/pacman/files.c | 125 +++++++++++++++++--------------------------- src/pacman/pacman.c | 17 +----- 2 files changed, 49 insertions(+), 93 deletions(-)
This patch looks good to me. Happy to apply with updated docs. A
Reworks the UI of -F according to FS#47949 In short -F replaces both -Fs and -Fo. Searching for an exact path (target contains "/"), causes the output to switch to the old -Fo output. Otherwise the old -Fs output is used. Also strip the leading "/" from targets like how -Qo does. --- Docs changed and wip title removed. doc/pacman.8.asciidoc | 14 ++--- src/pacman/files.c | 125 ++++++++++++++++-------------------------- src/pacman/pacman.c | 19 +------ 3 files changed, 53 insertions(+), 105 deletions(-) diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index fe95178d..68b62a90 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -488,14 +488,8 @@ File Options (apply to '-F')[[FO]] *-l, \--list*:: List the files owned by the queried package. -*-s, \--search*:: - Search package file names for matching strings. - *-x, --regex*:: - Treat arguments to '--search' as regular expressions. - -*-o, \--owns*:: - Search for packages that own a particular file. + Interpret each query as a regular expression. *-q, \--quiet*:: Show less information for certain file operations. This is useful when @@ -503,9 +497,9 @@ File Options (apply to '-F')[[FO]] '--machinereadable' instead. *--machinereadable*:: - Use a machine readable output format for '--list', '--search' and - '--owns'. The format is 'repository\0pkgname\0pkgver\0path\n' with '\0' - being the NULL character and '\n' a linefeed. + Print each match in a machine readable output format. The format is + 'repository\0pkgname\0pkgver\0path\n' with '\0' being the NULL character + and '\n' a linefeed. Handling Config Files[[HCF]] ---------------------------- diff --git a/src/pacman/files.c b/src/pacman/files.c index 26f96a67..74d06815 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -58,53 +58,7 @@ static void print_owned_by(alpm_db_t *db, alpm_pkg_t *pkg, char *filename) alpm_pkg_get_version(pkg), colstr->nocolor); } -static int files_fileowner(alpm_list_t *syncs, alpm_list_t *targets) { - int ret = 0; - alpm_list_t *t; - - for(t = targets; t; t = alpm_list_next(t)) { - char *filename = t->data; - int found = 0; - alpm_list_t *s; - size_t len = strlen(filename); - - while(len > 1 && filename[0] == '/') { - filename++; - len--; - } - - for(s = syncs; s; s = alpm_list_next(s)) { - alpm_list_t *p; - alpm_db_t *repo = s->data; - alpm_list_t *packages = alpm_db_get_pkgcache(repo); - - for(p = packages; p; p = alpm_list_next(p)) { - alpm_pkg_t *pkg = p->data; - alpm_filelist_t *files = alpm_pkg_get_files(pkg); - - if(alpm_filelist_contains(files, filename)) { - if(config->op_f_machinereadable) { - print_line_machinereadable(repo, pkg, filename); - } else if(!config->quiet) { - print_owned_by(repo, pkg, filename); - } else { - printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); - } - - found = 1; - } - } - } - - if(!found) { - ret++; - } - } - - return 0; -} - -static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) +static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg, char *exact_file) { alpm_db_t *db_local = alpm_get_localdb(config->handle); const colstr_t *colstr = &config->colstr; @@ -117,6 +71,12 @@ static void print_match(alpm_list_t *match, alpm_db_t *repo, alpm_pkg_t *pkg) } } else if(config->quiet) { printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg)); + } else if(exact_file != NULL) { + alpm_list_t *ml; + for(ml = match; ml; ml = alpm_list_next(ml)) { + char *filename = ml->data; + print_owned_by(repo, pkg, filename); + } } else { alpm_list_t *ml; printf("%s%s/%s%s %s%s%s", colstr->repo, alpm_db_get_name(repo), @@ -143,6 +103,15 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { alpm_list_t *s; int found = 0; regex_t reg; + size_t len = strlen(targ); + char *exact_file = strchr(targ, '/'); + + if(exact_file != NULL) { + while(len > 1 && targ[0] == '/') { + targ++; + len--; + } + } if(regex) { if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) { @@ -158,30 +127,44 @@ static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) { int m; for(p = packages; p; p = alpm_list_next(p)) { - size_t f = 0; - char* c; alpm_pkg_t *pkg = p->data; alpm_filelist_t *files = alpm_pkg_get_files(pkg); alpm_list_t *match = NULL; - while(f < files->count) { - c = strrchr(files->files[f].name, '/'); - if(c && *(c + 1)) { - if(regex) { - m = regexec(®, (c + 1), 0, 0, 0); - } else { - m = strcmp(c + 1, targ); + if(exact_file != NULL) { + if (regex) { + for(size_t f = 0; f < files->count; f++) { + char *c = files->files[f].name; + if(regexec(®, c, 0, 0, 0) == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } } - if(m == 0) { - match = alpm_list_add(match, files->files[f].name); + } else { + if(alpm_filelist_contains(files, targ)) { + match = alpm_list_add(match, targ); found = 1; } } - f++; + } else { + for(size_t f = 0; f < files->count; f++) { + char *c = strrchr(files->files[f].name, '/'); + if(c && *(c + 1)) { + if(regex) { + m = regexec(®, (c + 1), 0, 0, 0); + } else { + m = strcmp(c + 1, targ); + } + if(m == 0) { + match = alpm_list_add(match, files->files[f].name); + found = 1; + } + } + } } if(match != NULL) { - print_match(match, repo, pkg); + print_match(match, repo, pkg, exact_file); alpm_list_free(match); } } @@ -312,30 +295,16 @@ int pacman_files(alpm_list_t *targets) } } - if(targets == NULL && (config->op_q_owns | config->op_s_search)) { - pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); - return 1; - } - - /* determine the owner of a file */ - if(config->op_q_owns) { - return files_fileowner(files_dbs, targets); - } - - /* search for a file */ - if(config->op_s_search) { - return files_search(files_dbs, targets, config->op_f_regex); - } - /* get a listing of files in sync DBs */ if(config->op_q_list) { return files_list(files_dbs, targets); } - if(targets != NULL) { - pm_printf(ALPM_LOG_ERROR, _("no options specified (use -h for help)\n")); + if(targets == NULL) { + pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n")); return 1; } - return 0; + /* search for a file */ + return files_search(files_dbs, targets, config->op_f_regex); } diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 3bb622e6..93fb98a4 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -177,9 +177,7 @@ static void usage(int op, const char * const myname) printf("%s:\n", str_opt); } else if(op == PM_OP_FILES) { addlist(_(" -l, --list list the files owned by the queried package\n")); - addlist(_(" -o, --owns <file> query the package that owns <file>\n")); addlist(_(" -q, --quiet show less information for query and search\n")); - addlist(_(" -s, --search <file> search package file names for matching strings\n")); addlist(_(" -x, --regex enable searching using regular expressions\n")); addlist(_(" -y, --refresh download fresh package databases from the server\n" " (-yy to force a refresh even if up to date)\n")); @@ -753,18 +751,10 @@ static int parsearg_files(int opt) return 0; } switch(opt) { - case OP_OWNS: - case 'o': - config->op_q_owns = 1; - break; case OP_LIST: case 'l': config->op_q_list = 1; break; - case OP_SEARCH: - case 's': - config->op_s_search = 1; - break; case OP_REFRESH: case 'y': (config->op_s_sync)++; @@ -788,13 +778,8 @@ static int parsearg_files(int opt) static void checkargs_files(void) { - if(config->op_q_owns) { - invalid_opt(config->op_q_list, "--owns", "--list"); - invalid_opt(config->op_q_search, "--owns", "--search"); - invalid_opt(config->op_f_regex, "--owns", "--regex"); - } else if(config->op_q_list) { - invalid_opt(config->op_q_search, "--list", "--search"); - invalid_opt(config->op_f_regex, "--list", "--regex"); + if(config->op_q_search) { + invalid_opt(config->op_q_list, "--regex", "--list"); } } -- 2.21.0
participants (4)
-
Allan McRae
-
Eli Schwartz
-
Morgan Adamiec
-
morganamilo