From acf95f6b3b6e2bef174da51e799bd07ce9f4cfd0 Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Thu, 18 Jun 2015 18:28:13 +1000 Subject: Implement searching for a file in the sync databases Locates all packages that contain the listed file e.g. pacman -Fs libpng.so Signed-off-by: Allan McRae --- src/pacman/files.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/pacman/files.c b/src/pacman/files.c index 695f13d8..fc06ae7d 100644 --- a/src/pacman/files.c +++ b/src/pacman/files.c @@ -83,7 +83,69 @@ notfound: return 0; } -static int files_search(alpm_list_t __attribute__((unused)) *syncs, alpm_list_t __attribute__((unused)) *targets) { +static int files_search(alpm_list_t *syncs, alpm_list_t *targets) { + int ret = 0; + alpm_list_t *t; + const colstr_t *colstr = &config->colstr; + + for(t = targets; t; t = alpm_list_next(t)) { + char *filename = NULL; + alpm_list_t *s; + int found = 0; + + if((filename = strdup(t->data)) == NULL) { + goto notfound; + } + + 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)) { + 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(strcmp(c + 1, filename) == 0) { + match = alpm_list_add(match, strdup(files->files[f].name)); + found = 1; + } + } + f++; + } + + if(match != NULL) { + 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\n", colstr->repo, alpm_db_get_name(repo), + colstr->title, alpm_pkg_get_name(pkg), + colstr->version, alpm_pkg_get_version(pkg), colstr->nocolor); + + for(ml = match; ml; ml = alpm_list_next(ml)) { + c = ml->data; + printf(" %s\n", c); + } + FREELIST(match); + } + } + } + } + free(filename); + +notfound: + if(!found) { + ret++; + } + } + return 0; } -- cgit v1.2.3-24-g4f1b