|
| 1 | +/*- |
| 2 | + * Copyright (c) 2008-2015 Juan Romero Pardines. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <ctype.h> |
| 27 | +#include <getopt.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <string.h> |
| 31 | +#include <getopt.h> |
| 32 | +#include <errno.h> |
| 33 | +#include <regex.h> |
| 34 | + |
| 35 | +#include <xbps.h> |
| 36 | + |
| 37 | + |
| 38 | +struct locate { |
| 39 | + const char* expr; |
| 40 | + bool case_ignore; |
| 41 | + regex_t regex; |
| 42 | +}; |
| 43 | + |
| 44 | +static void __attribute__((noreturn)) |
| 45 | +usage(bool fail) |
| 46 | +{ |
| 47 | + fprintf(stdout, "Usage: xbps-locate [OPTIONS] file-pattern...\n" |
| 48 | + "\n" |
| 49 | + "OPTIONS\n" |
| 50 | + " -C, --config <dir> Path to confdir (xbps.d)\n" |
| 51 | + " -c, --cachedir <dir> Path to cachedir\n" |
| 52 | + " -d, --debug Debug mode shown to stderr\n" |
| 53 | + " -h, --help Show usage\n" |
| 54 | + " -e, --regex Use extended regular expression pattern\n" |
| 55 | + " -i, --ignore-conf-repos Ignore repositories defined in xbps.d\n" |
| 56 | + " -I, --ignore-case Match case insensitive\n" |
| 57 | + " -M, --memory-sync Remote repository data is fetched and stored\n" |
| 58 | + " in memory, ignoring on-disk repodata archives\n" |
| 59 | + " -R, --repository <url> Add repository to the top of the list\n" |
| 60 | + " This option can be specified multiple times\n" |
| 61 | + " -r, --rootdir <dir> Full path to rootdir\n" |
| 62 | + " -V, --version Show XBPS version\n" |
| 63 | + " -v, --verbose Verbose messages\n"); |
| 64 | + |
| 65 | + exit(fail ? EXIT_FAILURE : EXIT_SUCCESS); |
| 66 | +} |
| 67 | + |
| 68 | +#define LOWERWHEN(ignorecase, chr) (!(ignorecase) ? (chr) : tolower(chr)) |
| 69 | + |
| 70 | +static bool strcontains(const char* haystack, const char* needle, bool ignorecase) { |
| 71 | + const char *a, *b; |
| 72 | + |
| 73 | + b = needle; |
| 74 | + for ( ; *haystack != 0; haystack++) { |
| 75 | + if (*haystack != *b) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + a = haystack; |
| 79 | + do { |
| 80 | + if (*b == 0) |
| 81 | + return true; |
| 82 | + } while (LOWERWHEN(ignorecase, *a++) == LOWERWHEN(ignorecase, *b++)); |
| 83 | + b = needle; |
| 84 | + } |
| 85 | + return false; |
| 86 | +} |
| 87 | + |
| 88 | +static int repo_search_files(struct xbps_repo* repo, void* locate_ptr, bool* done) { |
| 89 | + struct locate *locate = locate_ptr; |
| 90 | + const char *pkgname, *file; |
| 91 | + xbps_object_iterator_t iter; |
| 92 | + xbps_object_t pkgkey; |
| 93 | + xbps_array_t pkgfiles; |
| 94 | + |
| 95 | + (void) done; |
| 96 | + |
| 97 | + if (repo->files == NULL) { |
| 98 | + xbps_warn_printf("repository %s has no files-entry, skipping.\n", repo->uri); |
| 99 | + return 1; |
| 100 | + } |
| 101 | + |
| 102 | + iter = xbps_dictionary_iterator(repo->files); |
| 103 | + |
| 104 | + while ((pkgkey = xbps_object_iterator_next(iter)) != NULL) { |
| 105 | + pkgname = xbps_dictionary_keysym_cstring_nocopy(pkgkey); |
| 106 | + pkgfiles = xbps_dictionary_get_keysym(repo->files, pkgkey); |
| 107 | + |
| 108 | + for (unsigned int i = 0; i < xbps_array_count(pkgfiles); i++) { |
| 109 | + if (!xbps_array_get_cstring_nocopy(pkgfiles, i, &file)) |
| 110 | + continue; |
| 111 | + if (locate->expr != NULL) { |
| 112 | + if (strcontains(file, locate->expr, locate->case_ignore)) |
| 113 | + printf("%s: %s\n", pkgname, file); |
| 114 | + } else { |
| 115 | + if (!regexec(&locate->regex, file, 0, NULL, 0)) |
| 116 | + printf("%s: %s\n", pkgname, file); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + xbps_object_iterator_release(iter); |
| 122 | + |
| 123 | + return 1; |
| 124 | +} |
| 125 | + |
| 126 | +int |
| 127 | +main(int argc, char **argv) |
| 128 | +{ |
| 129 | + const char *shortopts = "C:c:dehiIMr:R:r:vV"; |
| 130 | + const struct option longopts[] = { |
| 131 | + { "config", required_argument, NULL, 'C' }, |
| 132 | + { "cachedir", required_argument, NULL, 'c' }, |
| 133 | + { "debug", no_argument, NULL, 'd' }, |
| 134 | + { "regex", no_argument, NULL, 'e' }, |
| 135 | + { "help", no_argument, NULL, 'h' }, |
| 136 | + { "ignore-conf-repos", no_argument, NULL, 'i' }, |
| 137 | + { "ignore-case", no_argument, NULL, 'I' }, |
| 138 | + { "memory-sync", no_argument, NULL, 'M' }, |
| 139 | + { "regex", no_argument, NULL, 'r' }, |
| 140 | + { "repository", required_argument, NULL, 'R' }, |
| 141 | + { "rootdir", required_argument, NULL, 'r' }, |
| 142 | + { "verbose", no_argument, NULL, 'v' }, |
| 143 | + { "version", no_argument, NULL, 'V' }, |
| 144 | + { NULL, 0, NULL, 0 }, |
| 145 | + }; |
| 146 | + |
| 147 | + struct xbps_handle xh; |
| 148 | + struct locate locate; |
| 149 | + const char *rootdir, *cachedir, *confdir; |
| 150 | + int c, flags, rv, regflags; |
| 151 | + bool regex; |
| 152 | + |
| 153 | + rootdir = cachedir = confdir = NULL; |
| 154 | + flags = rv = c = 0; |
| 155 | + regex = false; |
| 156 | + regflags = REG_EXTENDED | REG_NOSUB; |
| 157 | + |
| 158 | + locate.expr = NULL; |
| 159 | + locate.case_ignore = false; |
| 160 | + |
| 161 | + memset(&xh, 0, sizeof(xh)); |
| 162 | + |
| 163 | + while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { |
| 164 | + switch (c) { |
| 165 | + case 'C': |
| 166 | + confdir = optarg; |
| 167 | + break; |
| 168 | + case 'c': |
| 169 | + cachedir = optarg; |
| 170 | + break; |
| 171 | + case 'd': |
| 172 | + flags |= XBPS_FLAG_DEBUG; |
| 173 | + break; |
| 174 | + case 'h': |
| 175 | + usage(false); |
| 176 | + /* NOTREACHED */ |
| 177 | + case 'i': |
| 178 | + flags |= XBPS_FLAG_IGNORE_CONF_REPOS; |
| 179 | + break; |
| 180 | + case 'I': |
| 181 | + locate.case_ignore = true; |
| 182 | + regflags |= REG_ICASE; |
| 183 | + break; |
| 184 | + case 'M': |
| 185 | + flags |= XBPS_FLAG_REPOS_MEMSYNC; |
| 186 | + break; |
| 187 | + case 'R': |
| 188 | + xbps_repo_store(&xh, optarg); |
| 189 | + break; |
| 190 | + case 'r': |
| 191 | + rootdir = optarg; |
| 192 | + break; |
| 193 | + case 'v': |
| 194 | + flags |= XBPS_FLAG_VERBOSE; |
| 195 | + break; |
| 196 | + case 'V': |
| 197 | + printf("%s\n", XBPS_RELVER); |
| 198 | + exit(EXIT_SUCCESS); |
| 199 | + case 'e': |
| 200 | + regex = true; |
| 201 | + break; |
| 202 | + case '?': |
| 203 | + default: |
| 204 | + usage(true); |
| 205 | + /* NOTREACHED */ |
| 206 | + } |
| 207 | + } |
| 208 | + argc -= optind; |
| 209 | + argv += optind; |
| 210 | + |
| 211 | + if (!argc) { |
| 212 | + usage(true); |
| 213 | + /* NOTREACHED */ |
| 214 | + } |
| 215 | + |
| 216 | + if (regex) { |
| 217 | + if (regcomp(&locate.regex, *(argv++), regflags) != 0) { |
| 218 | + xbps_error_printf("invalid regular expression\n"); |
| 219 | + exit(1); |
| 220 | + } |
| 221 | + } else { |
| 222 | + locate.expr = *(argv++); |
| 223 | + } |
| 224 | + argc--; |
| 225 | + |
| 226 | + if (argc) { |
| 227 | + /* trailing parameters */ |
| 228 | + usage(true); |
| 229 | + /* NOTREACHED */ |
| 230 | + } |
| 231 | + /* |
| 232 | + * Initialize libxbps. |
| 233 | + */ |
| 234 | + if (rootdir) |
| 235 | + xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir)); |
| 236 | + if (cachedir) |
| 237 | + xbps_strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir)); |
| 238 | + if (confdir) |
| 239 | + xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir)); |
| 240 | + |
| 241 | + xh.flags = flags; |
| 242 | + |
| 243 | + if ((rv = xbps_init(&xh)) != 0) { |
| 244 | + xbps_error_printf("Failed to initialize libxbps: %s\n", |
| 245 | + strerror(rv)); |
| 246 | + exit(EXIT_FAILURE); |
| 247 | + } |
| 248 | + |
| 249 | + xbps_rpool_foreach(&xh, repo_search_files, &locate); |
| 250 | + |
| 251 | + if (regex) |
| 252 | + regfree(&locate.regex); |
| 253 | + |
| 254 | + xbps_end(&xh); |
| 255 | + exit(rv); |
| 256 | +} |
0 commit comments