-
Notifications
You must be signed in to change notification settings - Fork 0
/
portageutils.c
375 lines (334 loc) · 12.8 KB
/
portageutils.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* portage utilities
* Copyright © 2007-2011 Andrew Savchenko
*
* This file is part of symlookup.
*
* symlookup is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation
*
* symlookup is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License version 3 for more details.
*
* You should have received a copy of the GNU General Public License version 3
* along with symlookup. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_PORTAGE
#include <search.h>
#include <error.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include "portageutils.h"
#include "symlookup.h"
#include "safemem.h"
#define CONTENTS_NAME "/CONTENTS"
#define CONTENTS_LEN 10 // len + '\0'
/* 2D array of ebuilds corresponding to file_arr */
struct str_t *ebuild_arr;
extern struct str_t file_arr;
char *const str_ebuild_nf = "<ebuild not found>";
/* disables ebuild support */
static void ebuild_disable(void)
{
/* remove ebuild from the sort sequence */
unsigned int found = 0;
for (unsigned int i = 0; i<opt.sort.cnt; i++)
{
if (!found)
{
if (opt.sort.seq[i] == mtype.ebuild)
found=1;
continue;
}
opt.sort.seq[i-1] = opt.sort.seq[i];
}
if (opt.sort.cnt)
opt.sort.cnt--;
opt.ebuild = 2;
// ebuild is the last type now, so no need to reduce match
// type values of other fields
}
/* Selects only categories which are directories
* and do not begin with '.' */
static int filter_directory(const struct dirent *const dir)
{
if (dir->d_type == DT_DIR && dir->d_name[0] != '.')
return 1;
else
return 0;
}
/* Process mmaped file.
* Store "category/package" if file is found in the hash table.
* ptr -- mmap start;
* mbuf_end -- mmap end + 1;
* package_name -- "category/package" without trailing '\0';
* package_len -- length of "category/package" */
static inline void
process_list(char *ptr, const char *const mbuf_end,
const char *const package_name, const size_t package_len)
{
static char *begin;
static ENTRY request;
static ENTRY *result;
static struct str_t *ebuild; // current element of ebuild array
for (; ptr < mbuf_end; ptr++)
{
begin = ptr;
// search for end of line
for (; ptr < mbuf_end; ptr++)
if (*ptr == '\n')
break;
/* minimum length for valid string of interest is 49:
* "obj X 617ae644c40ec045954426e0702d936e 1260230791"
* 3:1:name:1:32:1:10\n */
// continue on bad length or header
if (ptr-begin < 49 || strncmp(begin,"obj",3))
continue;
// denote end of file name
*(ptr-44) = '\0';
/* check match */
request.key = begin+4;
result = hsearch(request, FIND);
if (!result)
continue;
/* add ebuild to ebuilds array by index corresponding to file array */
ebuild = &ebuild_arr[(unsigned long)(result->data)];
// grow ebuild array
ebuild->str = xrealloc(ebuild->str, sizeof(char*) * (ebuild->size + 1));
// allocate and fill new string
ebuild->str[ebuild->size] = xmalloc(package_len + 1);
memcpy(ebuild->str[ebuild->size], package_name, package_len);
ebuild->str[ebuild->size][package_len] = '\0';
// grow ebuild array
ebuild->size++;
}
}
/* Fills ebuild data in the match array,
* if several owners were found grows match array as needed.
* match_idx - index in match array to fill;
* ebuild_idx - index in ebuild array to use */
static void fill_ebuild(const unsigned int match_idx, const unsigned int ebuild_idx)
{
static char **match;
static const struct str_t *ebuild;
match = match_arr.match[match_idx];
ebuild = &ebuild_arr[ebuild_idx];
match[mtype.ebuild] = ebuild->size ? ebuild->str[0] : str_ebuild_nf;
// add extra matches (should be rare case, but still possible)
if (ebuild->size > 1)
{
match_arr.count += ebuild->size - 1;
match_arr.match = xrealloc(match_arr.match, sizeof(char**) * match_arr.count);
// fill new records
for (unsigned int i = match_arr.count - ebuild->size + 1; i < match_arr.count; i++)
{
match_arr.match[i] = xmalloc(sizeof(char*) * M_SAVEMEM);
match_arr.match[i][mtype.symbol] = match[mtype.symbol];
match_arr.match[i][mtype.file] = match[mtype.file];
match_arr.match[i][mtype.file] = ebuild->str[match_arr.count - i];
#ifdef HAVE_RPM
if (opt.rpm)
match_arr.match[i][mtype.rpm] = match[mtype.rpm];
#endif //HAVE_RPM
}
/* In case of match grouped output an appropriate match
* pointer MUST be found in symbol structure.
* This is inefficient, but such cases shoulde be very rare
* (and must be absent on clean system at all) and overhead
* of alternative implementation is too large. */
if (opt.sort.match)
{
unsigned int i; // i needs to be saved
for (i = 0; i < symbol.size; i++)
for (unsigned int j = 0; j < symbol.match_count[i]; j++)
if (symbol.match[i][j] == match)
break;
// allocate for new match data
symbol.match_count[i] += ebuild->size - 1;
symbol.match[i] = xrealloc(symbol.match[i], sizeof(char***) * symbol.match_count[i]);
// add new matches
for (unsigned int k = 1; k < ebuild->size; k++)
symbol.match[i][symbol.match_count[i] - k] = match_arr.match[match_arr.count - k];
}
}
}
/* builds hash table for files found and searches portage db for them */
void find_ebuilds(const struct str_t *const file)
{
// nothing to do on empty list
if (!file->size)
return;
/* initialize hash */
if (!hcreate(file->size * 4/3 + 1))
{
if (opt.verb)
error(0, errno, "error: cannot init file hash table! "
"Disabling ebuild support.");
ebuild_disable();
return;
}
/* initialize portage root DB dir entry */
struct dirent **category;
int categories;
/* open portage root DB dir */
categories = scandir(opt.portageDB, &category, filter_directory, 0);
if (categories == -1 || chdir(opt.portageDB) == -1)
{
if (opt.verb)
error(0, errno, "error: cannot open portage root directory %s. "
"Disabling ebuild support", opt.portageDB);
ebuild_disable();
// additional cleanups an this stage
if (categories >= 0)
{
for (int i=0; i<categories; i++)
free(category[i]);
free(category);
}
hdestroy();
return;
}
/* hash found files */
ENTRY entry;
for (unsigned long int i=0; i<file->size; i++)
{
entry.key = file->str[i];
entry.data = (void*)i;
// no errors should be here
hsearch(entry, ENTER);
}
/* Initialize array for found ebuilds */
size_t len_earr = file_arr.size * sizeof(struct str_t);
ebuild_arr = xmalloc(len_earr);
memset(ebuild_arr, 0, len_earr);
if (opt.verb >= V_VERBOSE)
puts("--> Searching portage database");
/***** portage DB tree loop data declarations *****/
struct dirent **package; // for packages in each category
int packages; // number of packages in category
char *contents, // full name of CONTENTS file
*tmpstr; // temporary pointer for string operations
size_t contents_len = 128, // max pool for length of CONTENTS full file name
new_len, // length of new CONTENTS file name
category_len, // category name length
package_len; // package name length
int list; // file descriptor for listing file
struct stat list_stat; // to fstat() listing file
char *mbuf; // mmap buffer
contents = xmalloc(contents_len);
/* loop through portage categories */
for (int i=0; i<categories; i++)
{
packages = scandir(category[i]->d_name, &package, filter_directory, 0);
if (packages == -1)
{
if (opt.verb)
error(0, errno, "error: cannot open portage category %s.",
category[i]->d_name);
}
else
{
category_len = _D_EXACT_NAMLEN(category[i]);
/* loop through packages/CONTENTS */
for (int j=0; j<packages; j++)
{
package_len = _D_EXACT_NAMLEN(package[j]);
// new len = category + '/' + package + "/CONTENTS" + '\0'
new_len = category_len + 1 + package_len + CONTENTS_LEN;
if (new_len > contents_len)
contents = xrealloc(contents, contents_len * 2);
// construct file name
memcpy(contents, category[i]->d_name, category_len);
tmpstr = contents + category_len;
*tmpstr++ = '/';
memcpy(tmpstr, package[j]->d_name, package_len);
tmpstr += package_len;
memcpy(tmpstr, CONTENTS_NAME, CONTENTS_LEN);
/* open file */
list = open(contents, 0, O_RDONLY);
if (list != -1)
{
// stat to obtain size
if (fstat(list, &list_stat))
{
if (opt.verb)
error(0, errno, "error: can't stat file %s, "
"check your portage DB!", contents);
}
else
{
// zero size is normal for at least virtual packages
if (list_stat.st_size > 0)
{
/* mmap now */
mbuf = mmap(NULL, list_stat.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_POPULATE, list, 0);
if (mmap == MAP_FAILED)
{
if (opt.verb)
error(0, errno, "error: can't mmap file %s %li bytes long!",
contents, list_stat.st_size);
}
else
{
// process CONTENTS file
process_list(mbuf, mbuf + list_stat.st_size,
contents, category_len + 1 + package_len);
if (munmap(mbuf, list_stat.st_size) && opt.verb)
error(0, errno, "warning: can't unmap file %s %li bytes long!",
contents, list_stat.st_size);
}
}
}
// check if file is closed correctly
if (close(list) && opt.verb)
error(0, errno, "warning: can't close file %s", contents);
}
else
{
if (opt.verb)
error(0, errno, "error: can't open file %s for reading, "
"check your portage DB!", contents);
}
// clean memory
free(package[j]);
}
// clean memory
free(package);
}
// clean memory
free(category[i]);
}
// clean memory
free(contents);
free(category);
hdestroy();
/* Fill ebuild field in match array.
* Take advantage of sequential file filling in match array */
unsigned int idx = 0; // index in ebuild and file arrays
// remember current end of match array, it may grow further due
// to several owners of single file
const unsigned int match_end = match_arr.count;
// fill first element
fill_ebuild(0,0);
for (unsigned int i=1; i<match_end; i++)
{
// switch to next element in ebuild array if file from
// match array changed (we can use this because file
// records are filled sequentially)
if (match_arr.match[i][mtype.file] != match_arr.match[i-1][mtype.file])
idx++;
fill_ebuild(i, idx);
}
}
#endif //HAVE_PORTAGE