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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
#include <alpm.h>
#include <alpm_list.h>
#include <regex.h>
#include "pacman.h"
#include "util.h"
#include "conf.h"
#include "package.h"
static void print_line_machinereadable(alpm_db_t *db, alpm_pkg_t *pkg, char *filename)
{
fputs(alpm_db_get_name(db), stdout);
fputc(0, stdout);
fputs(alpm_pkg_get_name(pkg), stdout);
fputc(0, stdout);
fputs(alpm_pkg_get_version(pkg), stdout);
fputc(0, stdout);
fputs(filename, stdout);
fputs("\n", stdout);
}
static void dump_pkg_machinereadable(alpm_db_t *db, alpm_pkg_t *pkg)
{
alpm_filelist_t *pkgfiles = alpm_pkg_get_files(pkg);
for(size_t filenum = 0; filenum < pkgfiles->count; filenum++) {
const alpm_file_t *file = pkgfiles->files + filenum;
print_line_machinereadable(db, pkg, file->name);
}
}
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\n"), filename,
colstr->repo, alpm_db_get_name(repo), colstr->title,
alpm_pkg_get_name(pkg), colstr->version,
alpm_pkg_get_version(pkg));
} else {
printf("%s/%s\n", alpm_db_get_name(repo), alpm_pkg_get_name(pkg));
}
found = 1;
}
}
}
if(!found) {
ret++;
}
}
return 0;
}
static int files_search(alpm_list_t *syncs, alpm_list_t *targets, int regex) {
int ret = 0;
alpm_list_t *t;
const colstr_t *colstr = &config->colstr;
for(t = targets; t; t = alpm_list_next(t)) {
char *targ = t->data;
alpm_list_t *s;
int found = 0;
regex_t reg;
if(regex) {
if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE) != 0) {
TODO
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);
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(m == 0) {
match = alpm_list_add(match, files->files[f].name);
found = 1;
}
}
f++;
}
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\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);
}
}
alpm_list_free(match);
}
}
}
if(regex) {
regfree(®);
}
notfound:
if(!found) {
ret++;
}
}
return 0;
}
static void dump_file_list(alpm_pkg_t *pkg) {
const char *pkgname;
alpm_filelist_t *pkgfiles;
size_t i;
pkgname = alpm_pkg_get_name(pkg);
pkgfiles = alpm_pkg_get_files(pkg);
for(i = 0; i < pkgfiles->count; i++) {
const alpm_file_t *file = pkgfiles->files + i;
if(!config->quiet) {
printf("%s%s%s ", config->colstr.title, pkgname, config->colstr.nocolor);
}
printf("%s\n", file->name);
}
fflush(stdout);
}
static int files_list(alpm_list_t *syncs, alpm_list_t *targets) {
alpm_list_t *i, *j;
int ret = 0;
if(targets != NULL) {
for(i = targets; i; i = alpm_list_next(i)) {
int found = 0;
char *targ = i->data;
char *repo = NULL;
char *c = strchr(targ, '/');
if(c) {
if(! *(c + 1)) {
pm_printf(ALPM_LOG_ERROR,
_("invalid package: '%s'\n"), targ);
ret += 1;
continue;
}
repo = strndup(targ, c - targ);
targ = c + 1;
}
for(j = syncs; j; j = alpm_list_next(j)) {
alpm_pkg_t *pkg;
alpm_db_t *db = j->data;
if(repo) {
if(strcmp(alpm_db_get_name(db), repo) != 0) {
continue;
}
}
if((pkg = alpm_db_get_pkg(db, targ)) != NULL) {
found = 1;
if(config->op_f_machinereadable) {
dump_pkg_machinereadable(db, pkg);
} else {
dump_file_list(pkg);
}
break;
}
}
if(!found) {
targ = i->data;
pm_printf(ALPM_LOG_ERROR,
_("package '%s' was not found\n"), targ);
ret += 1;
}
free(repo);
}
} else {
for(i = syncs; i; i = alpm_list_next(i)) {
alpm_db_t *db = i->data;
for(j = alpm_db_get_pkgcache(db); j; j = alpm_list_next(j)) {
alpm_pkg_t *pkg = j->data;
if(config->op_f_machinereadable) {
dump_pkg_machinereadable(db, pkg);
} else {
dump_file_list(pkg);
}
}
}
}
return ret;
}
int pacman_files(alpm_list_t *targets)
{
alpm_list_t *files_dbs = NULL;
if(check_syncdbs(1, 0)) {
return 1;
}
files_dbs = alpm_get_syncdbs(config->handle);
if(config->op_s_sync) {
colon_printf(_("Synchronizing package databases...\n"));
alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
"synchronizing package lists\n");
if(!sync_syncdbs(config->op_s_sync, files_dbs)) {
return 1;
}
}
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;
}
if(config->op_q_owns) {
return files_fileowner(files_dbs, targets);
}
if(config->op_s_search) {
return files_search(files_dbs, targets, config->op_f_regex);
}
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"));
return 1;
}
return 0;
}
|