-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathownedby.c
More file actions
201 lines (175 loc) · 5.89 KB
/
ownedby.c
File metadata and controls
201 lines (175 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*-
* 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.
*/
#include "defs.h"
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <limits.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xbps.h>
struct ffdata {
bool rematch;
const char * pat, *repouri;
regex_t regex;
xbps_array_t allkeys;
xbps_dictionary_t filesd;
};
static void
match_files_by_pattern(xbps_dictionary_t pkg_filesd,
xbps_dictionary_keysym_t key,
struct ffdata* ffd,
const char* pkgver) {
xbps_array_t array;
const char * keyname = NULL, *typestr = NULL;
keyname = xbps_dictionary_keysym_cstring_nocopy(key);
if (strcmp(keyname, "files") == 0)
typestr = "regular file";
else if (strcmp(keyname, "links") == 0)
typestr = "link";
else if (strcmp(keyname, "conf_files") == 0)
typestr = "configuration file";
else
return;
array = xbps_dictionary_get_keysym(pkg_filesd, key);
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
xbps_object_t obj;
const char * filestr = NULL, *tgt = NULL;
obj = xbps_array_get(array, i);
xbps_dictionary_get_cstring_nocopy(obj, "file", &filestr);
if (filestr == NULL)
continue;
xbps_dictionary_get_cstring_nocopy(obj, "target", &tgt);
if (ffd->rematch) {
if (regexec(&ffd->regex, filestr, 0, 0, 0) == 0) {
printf("%s: %s%s%s (%s)\n",
pkgver, filestr,
tgt ? " -> " : "",
tgt ? tgt : "",
typestr);
}
} else {
if ((fnmatch(ffd->pat, filestr, FNM_PERIOD)) == 0) {
printf("%s: %s%s%s (%s)\n",
pkgver, filestr,
tgt ? " -> " : "",
tgt ? tgt : "",
typestr);
}
}
}
}
static int
ownedby_pkgdb_cb(struct xbps_handle* xhp,
xbps_object_t obj,
const char* obj_key UNUSED,
void* arg,
bool* done UNUSED) {
xbps_dictionary_t pkgmetad;
xbps_array_t files_keys;
struct ffdata* ffd = arg;
const char* pkgver = NULL;
(void) obj_key;
(void) done;
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
pkgmetad = xbps_pkgdb_get_pkg_files(xhp, pkgver);
if (pkgmetad == NULL)
return 0;
files_keys = xbps_dictionary_all_keys(pkgmetad);
for (unsigned int i = 0; i < xbps_array_count(files_keys); i++) {
match_files_by_pattern(pkgmetad,
xbps_array_get(files_keys, i), ffd, pkgver);
}
xbps_object_release(pkgmetad);
xbps_object_release(files_keys);
return 0;
}
static int
repo_match_cb(struct xbps_handle* xhp,
xbps_object_t obj,
const char* key UNUSED,
void* arg,
bool* done UNUSED) {
char bfile[PATH_MAX];
xbps_dictionary_t filesd;
xbps_array_t files_keys;
struct ffdata* ffd = arg;
const char* pkgver = NULL;
int r;
xbps_dictionary_set_cstring_nocopy(obj, "repository", ffd->repouri);
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
r = xbps_pkg_path_or_url(xhp, bfile, sizeof(bfile), obj);
if (r < 0) {
xbps_error_printf("could not get package path: %s\n", strerror(-r));
return -r;
}
filesd = xbps_archive_fetch_plist(bfile, "/files.plist");
if (!filesd) {
xbps_error_printf("%s: couldn't fetch files.plist from %s: %s\n",
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);
}
xbps_object_release(files_keys);
xbps_object_release(filesd);
return 0;
}
static int
repo_ownedby_cb(struct xbps_repo* repo, void* arg, bool* done UNUSED) {
xbps_array_t allkeys;
struct ffdata* ffd = arg;
int rv;
ffd->repouri = repo->uri;
allkeys = xbps_dictionary_all_keys(repo->idx);
rv = xbps_array_foreach_cb_multi(repo->xhp, allkeys, repo->idx, repo_match_cb, ffd);
xbps_object_release(allkeys);
return rv;
}
int ownedby(struct xbps_handle* xhp, const char* pat, bool repo, bool regex) {
struct ffdata ffd;
int rv;
ffd.rematch = false;
ffd.pat = pat;
if (regex) {
ffd.rematch = true;
if (regcomp(&ffd.regex, ffd.pat, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0)
return EINVAL;
}
if (repo)
rv = xbps_rpool_foreach(xhp, repo_ownedby_cb, &ffd);
else
rv = xbps_pkgdb_foreach_cb(xhp, ownedby_pkgdb_cb, &ffd);
if (regex)
regfree(&ffd.regex);
return rv;
}