2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* Copyright (c) 2010-2015 Juan Romero Pardines.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2012-11-02 19:34:25 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <dirent.h>
|
2013-03-05 13:22:40 +05:30
|
|
|
#include <assert.h>
|
2014-05-26 21:00:44 +05:30
|
|
|
#include <regex.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
2012-11-16 21:25:35 +05:30
|
|
|
struct ffdata {
|
2015-01-08 15:02:08 +05:30
|
|
|
bool rematch;
|
2014-05-26 21:00:44 +05:30
|
|
|
const char *pat, *repouri;
|
2015-01-08 15:02:08 +05:30
|
|
|
regex_t regex;
|
2013-07-27 13:17:16 +05:30
|
|
|
xbps_array_t allkeys;
|
|
|
|
xbps_dictionary_t filesd;
|
2012-11-16 21:25:35 +05:30
|
|
|
};
|
|
|
|
|
2012-12-15 23:49:42 +05:30
|
|
|
static void
|
2013-06-20 13:56:12 +05:30
|
|
|
match_files_by_pattern(xbps_dictionary_t pkg_filesd,
|
|
|
|
xbps_dictionary_keysym_t key,
|
2012-12-15 23:49:42 +05:30
|
|
|
struct ffdata *ffd,
|
|
|
|
const char *pkgver)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t array;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *keyname = NULL, *typestr = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
keyname = xbps_dictionary_keysym_cstring_nocopy(key);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
if (strcmp(keyname, "files") == 0)
|
|
|
|
typestr = "regular file";
|
|
|
|
else if (strcmp(keyname, "links") == 0)
|
|
|
|
typestr = "link";
|
2012-11-16 21:25:35 +05:30
|
|
|
else if (strcmp(keyname, "conf_files") == 0)
|
2012-11-02 19:34:25 +05:30
|
|
|
typestr = "configuration file";
|
2012-11-16 21:25:35 +05:30
|
|
|
else
|
2012-12-15 23:49:42 +05:30
|
|
|
return;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
array = xbps_dictionary_get_keysym(pkg_filesd, key);
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
|
2015-01-29 22:02:14 +05:30
|
|
|
xbps_object_t obj;
|
|
|
|
const char *filestr = NULL, *tgt = NULL;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
obj = xbps_array_get(array, i);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "file", &filestr);
|
2013-03-05 13:22:40 +05:30
|
|
|
if (filestr == NULL)
|
|
|
|
continue;
|
2015-01-29 22:02:14 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "target", &tgt);
|
2015-01-08 15:02:08 +05:30
|
|
|
if (ffd->rematch) {
|
|
|
|
if (regexec(&ffd->regex, filestr, 0, 0, 0) == 0) {
|
2016-01-20 18:11:13 +05:30
|
|
|
printf("%s: %s%s%s (%s)\n",
|
|
|
|
pkgver, filestr,
|
|
|
|
tgt ? " -> " : "",
|
|
|
|
tgt ? tgt : "",
|
|
|
|
typestr);
|
2012-12-15 23:49:42 +05:30
|
|
|
}
|
2014-05-26 21:00:44 +05:30
|
|
|
} else {
|
2015-01-29 22:02:14 +05:30
|
|
|
if ((fnmatch(ffd->pat, filestr, FNM_PERIOD)) == 0) {
|
2016-01-20 18:11:13 +05:30
|
|
|
printf("%s: %s%s%s (%s)\n",
|
|
|
|
pkgver, filestr,
|
|
|
|
tgt ? " -> " : "",
|
|
|
|
tgt ? tgt : "",
|
|
|
|
typestr);
|
2015-01-29 22:02:14 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 21:25:35 +05:30
|
|
|
static int
|
2013-08-29 14:15:30 +05:30
|
|
|
ownedby_pkgdb_cb(struct xbps_handle *xhp,
|
|
|
|
xbps_object_t obj,
|
2018-07-18 07:54:26 +05:30
|
|
|
const char *obj_key UNUSED,
|
2013-08-29 14:15:30 +05:30
|
|
|
void *arg,
|
2018-07-18 07:54:26 +05:30
|
|
|
bool *done UNUSED)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t pkgmetad;
|
|
|
|
xbps_array_t files_keys;
|
2012-11-16 21:25:35 +05:30
|
|
|
struct ffdata *ffd = arg;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *pkgver = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
(void)obj_key;
|
2012-11-16 21:25:35 +05:30
|
|
|
(void)done;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2014-09-11 03:42:12 +05:30
|
|
|
pkgmetad = xbps_pkgdb_get_pkg_files(xhp, pkgver);
|
2014-09-11 21:40:04 +05:30
|
|
|
if (pkgmetad == NULL)
|
|
|
|
return 0;
|
2012-11-16 21:25:35 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
files_keys = xbps_dictionary_all_keys(pkgmetad);
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(files_keys); i++) {
|
2012-12-15 23:49:42 +05:30
|
|
|
match_files_by_pattern(pkgmetad,
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get(files_keys, i), ffd, pkgver);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-07-21 14:19:30 +05:30
|
|
|
xbps_object_release(pkgmetad);
|
|
|
|
xbps_object_release(files_keys);
|
|
|
|
|
2012-12-15 23:49:42 +05:30
|
|
|
return 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
static int
|
2014-11-13 21:39:43 +05:30
|
|
|
repo_match_cb(struct xbps_handle *xhp,
|
2013-08-29 14:15:30 +05:30
|
|
|
xbps_object_t obj,
|
2018-07-18 07:54:26 +05:30
|
|
|
const char *key UNUSED,
|
2013-08-29 14:15:30 +05:30
|
|
|
void *arg,
|
2018-07-18 07:54:26 +05:30
|
|
|
bool *done UNUSED)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2014-11-13 21:39:43 +05:30
|
|
|
xbps_dictionary_t filesd;
|
|
|
|
xbps_array_t files_keys;
|
2013-07-27 13:17:16 +05:30
|
|
|
struct ffdata *ffd = arg;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *pkgver = NULL;
|
2014-11-13 21:39:43 +05:30
|
|
|
char *bfile;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2014-11-13 21:39:43 +05:30
|
|
|
xbps_dictionary_set_cstring_nocopy(obj, "repository", ffd->repouri);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
|
|
|
|
bfile = xbps_repository_pkg_path(xhp, obj);
|
|
|
|
assert(bfile);
|
2014-11-21 14:21:57 +05:30
|
|
|
filesd = xbps_archive_fetch_plist(bfile, "/files.plist");
|
2014-11-13 21:39:43 +05:30
|
|
|
if (filesd == NULL) {
|
2022-12-24 18:16:33 +05:30
|
|
|
xbps_dbg_printf("%s: couldn't fetch files.plist from %s: %s\n",
|
2014-11-13 21:39:43 +05:30
|
|
|
pkgver, bfile, strerror(errno));
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
files_keys = xbps_dictionary_all_keys(filesd);
|
|
|
|
for (unsigned int i = 0; i < xbps_array_count(files_keys); i++) {
|
|
|
|
match_files_by_pattern(filesd,
|
|
|
|
xbps_array_get(files_keys, i), ffd, pkgver);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2014-11-13 21:39:43 +05:30
|
|
|
xbps_object_release(files_keys);
|
|
|
|
xbps_object_release(filesd);
|
|
|
|
free(bfile);
|
2013-07-27 13:17:16 +05:30
|
|
|
|
|
|
|
return 0;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
repo_ownedby_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-07-27 13:17:16 +05:30
|
|
|
xbps_array_t allkeys;
|
2012-11-02 19:34:25 +05:30
|
|
|
struct ffdata *ffd = arg;
|
2013-07-27 13:17:16 +05:30
|
|
|
int rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
ffd->repouri = repo->uri;
|
2014-11-13 21:39:43 +05:30
|
|
|
allkeys = xbps_dictionary_all_keys(repo->idx);
|
|
|
|
rv = xbps_array_foreach_cb_multi(repo->xhp, allkeys, repo->idx, repo_match_cb, ffd);
|
2013-07-21 14:19:30 +05:30
|
|
|
xbps_object_release(allkeys);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
return rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-05-26 21:00:44 +05:30
|
|
|
ownedby(struct xbps_handle *xhp, const char *pat, bool repo, bool regex)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
|
|
|
struct ffdata ffd;
|
2013-09-15 13:36:49 +05:30
|
|
|
int rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2015-01-08 15:02:08 +05:30
|
|
|
ffd.rematch = false;
|
2014-05-26 21:00:44 +05:30
|
|
|
ffd.pat = pat;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2015-01-08 15:02:08 +05:30
|
|
|
if (regex) {
|
|
|
|
ffd.rematch = true;
|
2015-01-18 22:20:26 +05:30
|
|
|
if (regcomp(&ffd.regex, ffd.pat, REG_EXTENDED|REG_NOSUB|REG_ICASE) != 0)
|
2015-01-08 15:02:08 +05:30
|
|
|
return EINVAL;
|
|
|
|
}
|
2014-05-26 21:00:44 +05:30
|
|
|
if (repo)
|
|
|
|
rv = xbps_rpool_foreach(xhp, repo_ownedby_cb, &ffd);
|
|
|
|
else
|
|
|
|
rv = xbps_pkgdb_foreach_cb(xhp, ownedby_pkgdb_cb, &ffd);
|
2013-07-27 13:17:16 +05:30
|
|
|
|
2015-01-08 15:02:08 +05:30
|
|
|
if (regex)
|
|
|
|
regfree(&ffd.regex);
|
|
|
|
|
2013-07-27 13:17:16 +05:30
|
|
|
return rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|