forked from gentoo/portage-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1292 lines (1147 loc) · 31.8 KB
/
main.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2005-2023 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
*
* Copyright 2005-2008 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2014 Mike Frysinger - <vapier@gentoo.org>
* Copyright 2018- Fabian Groffen - <grobian@gentoo.org>
*/
#include "main.h"
#include "applets.h"
#include <xalloc.h>
#include <assert.h>
#include <ctype.h>
#include <sys/time.h>
#include <limits.h>
#include <termios.h>
#include <sys/ioctl.h>
#include "eat_file.h"
#include "rmspace.h"
#include "scandirat.h"
#include "set.h"
#include "xasprintf.h"
/* variables to control runtime behavior */
char *main_overlay;
char *module_name = NULL;
int verbose = 0;
int quiet = 0;
int twidth;
bool nocolor;
char pretend = 0;
char *portarch;
char *portroot;
char *config_protect;
char *config_protect_mask;
char *portvdb;
char *portlogdir;
char *pkg_install_mask;
char *binhost;
char *pkgdir;
char *port_tmpdir;
set *features;
set *ev_use;
char *install_mask;
DECLARE_ARRAY(overlays);
DECLARE_ARRAY(overlay_names);
DECLARE_ARRAY(overlay_src);
static char *portedb;
static char *eprefix;
static char *accept_license;
#define STR_DEFAULT "built-in default"
/* helper functions for showing errors */
const char *argv0;
FILE *warnout;
#ifdef EBUG
# include <sys/resource.h>
static void
init_coredumps(void)
{
struct rlimit rl;
rl.rlim_cur = RLIM_INFINITY;
rl.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &rl);
}
#endif
void
setup_quiet(void)
{
/* "e" for FD_CLOEXEC */
if (quiet == 0)
warnout = fopen("/dev/null", "we");
++quiet;
}
/* display usage and exit */
void
usage(int status, const char *flags, struct option const opts[],
const char * const help[], const char *desc, int blabber)
{
const char opt_arg[] = "[arg]";
const char a_arg[] = "<arg>";
size_t a_arg_len = strlen(a_arg) + 1;
size_t i;
size_t optlen;
size_t l;
size_t prefixlen;
const char *hstr;
FILE *fp = status == EXIT_SUCCESS ? stdout : warnout;
if (blabber == 0) {
fprintf(fp, "%susage:%s %sq%s %s<applet> <args>%s : %s"
"invoke a portage utility applet\n\n", GREEN,
NORM, YELLOW, NORM, DKBLUE, RED, NORM);
fprintf(fp, "%scurrently defined applets:%s\n", GREEN, NORM);
for (i = 0; applets[i].desc; ++i)
if (applets[i].func)
fprintf(fp, " %s%9s%s %s%-16s%s%s:%s %s\n",
YELLOW, applets[i].name, NORM,
DKBLUE, applets[i].opts, NORM,
RED, NORM, _(applets[i].desc));
} else if (blabber > 0) {
fprintf(fp, "%susage:%s %s%s%s [opts] %s%s%s %s:%s %s\n",
GREEN, NORM,
YELLOW, applets[blabber].name, NORM,
DKBLUE, applets[blabber].opts, NORM,
RED, NORM, _(applets[blabber].desc));
if (desc)
fprintf(fp, "\n%s\n", desc);
}
if (module_name != NULL)
fprintf(fp, "%sloaded module:%s\n%s%8s%s %s<args>%s\n",
GREEN, NORM, YELLOW, module_name, NORM, DKBLUE, NORM);
/* Prescan the --long opt length to auto-align. */
optlen = 0;
for (i = 0; opts[i].name; ++i) {
l = strlen(opts[i].name);
if (opts[i].has_arg != no_argument)
l += a_arg_len;
optlen = MAX(l, optlen);
}
fprintf(fp, "\n%soptions:%s -[%s]\n", GREEN, NORM, flags);
for (i = 0; opts[i].name; ++i) {
/* this assert is a life saver when adding new applets. */
assert(help[i] != NULL);
/* first output the short flag if it has one */
if (opts[i].val > '~' || opts[i].val < ' ')
fprintf(fp, " ");
else
fprintf(fp, " -%c, ", opts[i].val);
/* then the long flag */
if (opts[i].has_arg == no_argument)
fprintf(fp, "--%-*s %s*%s ", (int)optlen, opts[i].name,
RED, NORM);
else
fprintf(fp, "--%s %s%s%s%*s %s*%s ",
opts[i].name,
DKBLUE, (opts[i].has_arg == a_argument ? a_arg : opt_arg), NORM,
(int)(optlen - strlen(opts[i].name) - a_arg_len), "",
RED, NORM);
/* then wrap the help text, if necessary */
prefixlen = 6 + 2 + optlen + 1 + 1 + 1;
if ((size_t)twidth < prefixlen + 10) {
fprintf(fp, "%s\n", _(help[i]));
} else {
const char *t;
hstr = _(help[i]);
l = strlen(hstr);
while (twidth - prefixlen < l) {
/* search backwards for a space */
t = &hstr[twidth - prefixlen];
while (t > hstr && !isspace((int)*t))
t--;
if (t == hstr)
break;
fprintf(fp, "%.*s\n%*s",
(int)(t - hstr), hstr, (int)prefixlen, "");
l -= t + 1 - hstr;
hstr = t + 1; /* skip space */
}
fprintf(fp, "%s\n", hstr);
}
}
exit(status);
}
void
version_barf(void)
{
const char *vcsid = "";
const char *eprefixid = "";
#ifndef VERSION
# define VERSION "git"
#endif
#ifdef VCSID
vcsid = " (" VCSID ")";
#endif
if (strlen(CONFIG_EPREFIX) > 1)
eprefixid = "configured for " CONFIG_EPREFIX "\n";
printf("portage-utils-%s%s\n"
"%s"
"written for Gentoo by solar, vapier and grobian\n",
VERSION, vcsid, eprefixid);
exit(EXIT_SUCCESS);
}
void
freeargv(int argc, char **argv)
{
while (argc--)
free(argv[argc]);
free(argv);
}
void
makeargv(const char *string, int *argc, char ***argv)
{
int curc = 2;
char *q, *p, *str;
(*argv) = xmalloc(sizeof(char *) * curc);
*argc = 1;
(*argv)[0] = xstrdup(argv0);
/* shortcut empty strings */
while (isspace((int)*string))
string++;
if (*string == '\0')
return;
q = xstrdup(string);
str = q;
remove_extra_space(str);
rmspace(str);
while (str) {
if ((p = strchr(str, ' ')) != NULL)
*(p++) = '\0';
if (*argc == curc) {
curc *= 2;
(*argv) = xrealloc(*argv, sizeof(char *) * curc);
}
(*argv)[*argc] = xstrdup(str);
(*argc)++;
str = p;
}
free(q);
}
static void
strincr_var(const char *name, const char *s, char **value, size_t *value_len)
{
size_t len;
char *p;
char *nv;
char brace;
bool haddashstar;
/* find/skip any -* instances */
nv = (char *)s;
while ((p = strstr(nv, "-*")) != NULL)
nv = p + 2;
haddashstar = nv != (char *)s;
len = strlen(nv);
if (haddashstar && len < *value_len) {
p = *value;
*p = '\0'; /* in case len == 0 */
} else if (haddashstar) {
p = *value = xrealloc(*value, len + 1);
} else {
*value = xrealloc(*value, *value_len + 1 + len + 1);
p = &(*value)[*value_len];
if (*value_len > 0)
*p++ = ' ';
}
memcpy(p, nv, len + 1);
/* This function is mainly used by the startup code for parsing
make.conf and stacking variables remove.
variables can be in the form of ${v} or $v
works:
FEATURES="${FEATURES} foo"
FEATURES="$FEATURES foo"
FEATURES="baz bar -* foo"
wont work:
FEATURES="${OTHERVAR} foo"
FEATURES="-nls nls -nls"
FEATURES="nls nls nls"
*/
len = strlen(name);
while ((p = strchr(p, '$')) != NULL) {
nv = p;
p++; /* skip $ */
brace = *p == '{';
if (brace)
p++;
if (strncmp(p, name, len) == 0) {
p += len;
if (brace && *p == '}') {
p++;
memset(nv, ' ', p - nv);
} else if (!brace && (*p == '\0' || isspace((int)*p))) {
memset(nv, ' ', p - nv);
}
}
}
remove_extra_space(*value);
*value_len = strlen(*value);
/* we should sort here */
}
static void
setincr_var(const char *s, set **vals)
{
int i;
int argc;
char **argv;
bool ignore;
/* This tries to mimick parsing of portage envvars and the stacking
* thereof. In particular USE and FEATURES, where also negation can
* happen (- prefix). Variables are supported in form of ${v} or
* $v, but there's no actual replacement happening, we just ignore
* any of such forms.
* works:
* FEATURES="${FEATURES} foo"
* FEATURES="$FEATURES foo"
* FEATURES="baz bar -* foo"
*
* wont work:
* FEATURES="${OTHERVAR} foo"
* FEATURES="-* ${FEATURES}"
*/
if (s == NULL || *s == '\0')
return;
/* break up input */
makeargv(s, &argc, &argv);
for (i = 1 /* skip executable name */; i < argc; i++) {
if (argv[i][0] == '-') {
if (*vals != NULL) {
/* handle negation, when the respective value isn't set, we
* simply ignore/drop it */
if (argv[i][1] == '*') {
clear_set(*vals);
} else {
del_set(&argv[i][1], *vals, &ignore);
}
}
} else if (argv[i][0] == '$') {
/* detect ${var} or $var, simply ignore it completely, for
* all of these should be stacked, so re-including whatever
* there is shouldn't make much sense */
} else {
*vals = add_set_unique(argv[i], *vals, &ignore);
}
}
freeargv(argc, argv);
}
static env_vars *
get_portage_env_var(env_vars *vars, const char *name)
{
size_t i;
for (i = 0; vars[i].name; ++i)
if (!strcmp(vars[i].name, name))
return &vars[i];
return NULL;
}
static void
set_portage_env_var(env_vars *var, const char *value, const char *src)
{
switch (var->type) {
case _Q_BOOL:
*var->value.b = 0;
if (strcasecmp(value, "true") == 0 ||
strcasecmp(value, "yes") == 0 ||
strcmp(value, "1") == 0)
*var->value.b = 1;
free(var->src);
var->src = xstrdup(src);
break;
case _Q_STR:
free(*var->value.s);
*var->value.s = xstrdup(value);
var->value_len = strlen(value);
free(var->src);
var->src = xstrdup(src);
break;
case _Q_ISTR:
case _Q_ISET:
if (strcmp(var->src, STR_DEFAULT) != 0) {
size_t l = strlen(var->src) + 2 + strlen(src) + 1;
char *p = xmalloc(sizeof(char) * l);
snprintf(p, l, "%s, %s", var->src, src);
free(var->src);
var->src = p;
} else {
free(*var->value.s);
*var->value.s = NULL;
var->value_len = 0;
free(var->src);
var->src = xstrdup(src);
}
if (var->type == _Q_ISTR)
strincr_var(var->name, value, var->value.s, &var->value_len);
else
setincr_var(value, var->value.t);
break;
}
}
/* Helper to read a portage file (e.g. make.conf, package.mask), or
* recursively if it points to a directory (we don't care about EAPI for
* dirs, basically PMS 5.2.5 EAPI restriction is ignored) */
enum portage_file_type { ENV_FILE, PMASK_FILE };
static void
read_portage_file(const char *file, enum portage_file_type type, void *data)
{
FILE *fp;
struct dirent **dents;
int dentslen;
char *s;
char *p;
char *buf = NULL;
size_t buflen = 0;
size_t line;
bool incomment;
size_t cbeg;
size_t cend;
char npath[_Q_PATH_MAX * 2];
int i;
env_vars *vars = data;
set *masks = data;
if ((dentslen = scandir(file, &dents, NULL, alphasort)) > 0) {
int di;
struct dirent *d;
/* recurse through all files */
for (di = 0; di < dentslen; di++) {
d = dents[di];
if (d->d_name[0] == '.' || d->d_name[0] == '\0' ||
d->d_name[strlen(d->d_name) - 1] == '~')
continue;
snprintf(npath, sizeof(npath), "%s/%s", file, d->d_name);
read_portage_file(npath, type, data);
}
scandir_free(dents, dentslen);
goto done;
}
fp = fopen(file, "r");
if (fp == NULL)
goto done;
line = 0;
incomment = false;
cbeg = 0;
cend = 0;
while (getline(&buf, &buflen, fp) != -1) {
line++;
rmspace(buf);
if (*buf == '#') {
if (!incomment)
cbeg = cend = line;
else
cend = line;
incomment = true;
continue;
}
incomment = false;
if (*buf == '\0') {
cbeg = cend = 0;
continue;
}
if (type == ENV_FILE) {
size_t curline = line;
/* Handle "source" keyword */
if (strncmp(buf, "source ", 7) == 0) {
const char *sfile = buf + 7;
if (sfile[0] != '/') {
/* handle relative paths */
size_t file_path_len;
s = strrchr(file, '/');
file_path_len = s - file + 1;
snprintf(npath, sizeof(npath), "%.*s/%s",
(int)file_path_len, file, sfile);
sfile = npath;
}
read_portage_file(sfile, type, data);
continue;
}
/* look for our desired variables and grab their value */
for (i = 0; vars[i].name; i++) {
if (buf[vars[i].name_len] != '=' &&
buf[vars[i].name_len] != ' ')
continue;
if (strncmp(buf, vars[i].name, vars[i].name_len))
continue;
/* make sure we handle spaces between the varname, the =,
* and the value:
* VAR=val VAR = val VAR="val"
*/
s = buf + vars[i].name_len;
if ((p = strchr(s, '=')) != NULL)
s = p + 1;
while (isspace(*s))
s++;
if (*s == '"' || *s == '\'') {
char *endq;
char q = *s;
/* make sure we handle spacing/comments after the quote */
endq = strchr(s + 1, q);
if (!endq) {
/* if the last char is not a quote,
* then we span lines */
size_t abuflen;
char *abuf;
abuf = NULL;
while (getline(&abuf, &abuflen, fp) != -1) {
line++;
buf = xrealloc(buf, buflen + abuflen);
endq = strchr(abuf, q);
if (endq)
*endq = '\0';
strcat(buf, abuf);
buflen += abuflen;
if (endq)
break;
}
free(abuf);
if (!endq)
warn("%s:%zu: %s: quote mismatch",
file, line, vars[i].name);
s = buf + vars[i].name_len + 2;
} else {
*endq = '\0';
s++;
}
} else {
/* no quotes, so chop the spacing/comments ourselves */
size_t off = strcspn(s, "# \t\n");
s[off] = '\0';
}
snprintf(npath, sizeof(npath), "%s:%zu:%zu-%zu",
file, curline, cbeg, cend);
set_portage_env_var(&vars[i], s, npath);
}
} else if (type == PMASK_FILE) {
if (*buf == '-') {
/* negation/removal, lookup and drop mask if it exists;
* note that this only supports exact matches (PMS
* 5.2.5) so we don't even have to parse and use
* atom-compare here */
if ((p = del_set(buf + 1, masks, NULL)) != NULL)
free(p);
} else {
void *e;
snprintf(npath, sizeof(npath), "%s:%zu:%zu-%zu",
file, line, cbeg, cend);
/* if not necessary, but do it for static code analysers
* which take into accound that add_set_value might
* allocate a new set when masks would be NULL -- a case
* which would never happen */
if (masks != NULL) {
p = xstrdup(npath);
add_set_value(buf, p, &e, masks);
if (e != NULL)
free(p);
}
}
}
}
fclose(fp);
done:
free(buf);
if (getenv("DEBUG"))
fprintf(stderr, "read profile %s\n", file);
}
/* Helper to check if a string starts with a prefix. If so, returns
* true and gets the length of the prefix. Otherwise returns false,
* leaving the prefix length unmodified. */
static bool
starts_with(const char *str, const char *prefix, size_t *prefix_len)
{
const char *s;
const char *p;
size_t len;
if (prefix == NULL) {
/* every string starts with a null string */
if (prefix_len != NULL)
*prefix_len = 0;
return true;
}
if (str == NULL)
/* null string only starts with a null string, and prefix isn't null */
return false;
len = 0;
for (s = str, p = prefix; *s != '\0' && *p != '\0'; ++s, ++p, ++len) {
if (*s != *p)
return false;
}
if (*p == '\0') {
if (prefix_len != NULL)
*prefix_len = len;
return true;
}
return false;
}
/* Helper to figure out inside of which overlay a path is. Returns
* null if nonesuch is found. */
static const char *
overlay_from_path (const char *path)
{
size_t n;
char *overlay;
size_t max_match = 0;
const char *found_overlay = NULL;
array_for_each(overlays, n, overlay) {
size_t overlay_len;
if (!starts_with(path, overlay, &overlay_len))
continue;
if (overlay_len <= max_match)
continue;
max_match = overlay_len;
found_overlay = overlay;
}
return found_overlay;
}
/* Helper to recursively read stacked make.defaults in profiles */
static void
read_portage_profile(const char *profile, env_vars vars[], set *masks)
{
char profile_file[_Q_PATH_MAX * 3];
char rpath[_Q_PATH_MAX];
size_t profile_len;
char *s;
char *p;
char *buf = NULL;
size_t buf_len = 0;
char *saveptr;
/* create mutable/appendable copy */
profile_len = snprintf(profile_file, sizeof(profile_file), "%s/", profile);
/* check if we have enough space (should always be the case) */
if (sizeof(profile_file) - profile_len < sizeof("make.defaults"))
return;
/* first walk all the parents, PMS 5.2.1 defines that it should
* treat parent profiles as defaults, that can be overridden by
* *this* profile. */
strcpy(profile_file + profile_len, "parent");
if (eat_file(profile_file, &buf, &buf_len)) {
s = strtok_r(buf, "\n", &saveptr);
for (; s != NULL; s = strtok_r(NULL, "\n", &saveptr)) {
/* handle repo: notation (not in PMS, referenced in Wiki only?) */
if ((p = strchr(s, ':')) != NULL) {
char *overlay;
char *repo_name;
size_t n;
/* split repo from target */
*p++ = '\0';
if (s[0] == '\0') {
/* empty repo name means a repo where the profile is */
const char* current_overlay = overlay_from_path (profile);
if (current_overlay == NULL) {
/* bring back the colon to see the ignored
* parent line */
*(--p) = ':';
if (!quiet)
warn("could not figure out current repo "
"of profile %s, ignoring parent %s",
profile, s);
continue;
}
snprintf(profile_file, sizeof(profile_file),
"%s/profiles/%s", current_overlay, p);
} else {
/* match the repo */
repo_name = NULL;
array_for_each(overlays, n, overlay) {
repo_name = xarrayget(overlay_names, n);
if (strcmp(repo_name, s) == 0) {
snprintf(profile_file, sizeof(profile_file),
"%s/profiles/%s/", overlay, p);
break;
}
repo_name = NULL;
}
if (repo_name == NULL) {
/* bring back the colon to see the ignored
* parent line */
*(--p) = ':';
if (!quiet)
warn("ignoring parent with unknown repo "
"in profile %s: %s", profile, s);
continue;
}
}
} else {
snprintf(profile_file + profile_len,
sizeof(profile_file) - profile_len, "%s", s);
}
read_portage_profile(
realpath(profile_file, rpath) == NULL ?
profile_file : rpath, vars, masks);
/* restore original path in case we were repointed by profile */
if (p != NULL)
snprintf(profile_file, sizeof(profile_file), "%s/", profile);
}
}
if (buf != NULL)
free(buf);
/* now consume *this* profile's make.defaults and package.mask */
strcpy(profile_file + profile_len, "make.defaults");
read_portage_file(profile_file, ENV_FILE, vars);
strcpy(profile_file + profile_len, "package.mask");
read_portage_file(profile_file, PMASK_FILE, masks);
}
env_vars vars_to_read[] = {
#define _Q_EV(t, V, set, lset, d) \
{ \
.name = #V, \
.name_len = sizeof(#V) - 1, \
.type = _Q_##t, \
set, \
lset, \
.default_value = d, \
.src = NULL, \
},
#define _Q_EVS(t, V, v, D) \
_Q_EV(t, V, .value.s = &v, .value_len = sizeof(D) - 1, D)
#define _Q_EVB(t, V, v, D) \
_Q_EV(t, V, .value.b = &v, .value_len = 0, D)
#define _Q_EVT(T, V, v, D) \
_Q_EV(T, V, .value.t = &v, .value_len = 0, D)
_Q_EVS(STR, ROOT, portroot, "/")
_Q_EVS(STR, ACCEPT_LICENSE, accept_license, "")
_Q_EVS(ISTR, INSTALL_MASK, install_mask, "")
_Q_EVS(ISTR, PKG_INSTALL_MASK, pkg_install_mask, "")
_Q_EVS(STR, ARCH, portarch, "")
_Q_EVS(ISTR, CONFIG_PROTECT, config_protect, "/etc")
_Q_EVS(ISTR, CONFIG_PROTECT_MASK, config_protect_mask, "")
_Q_EVB(BOOL, NOCOLOR, nocolor, 0)
_Q_EVT(ISET, FEATURES, features, NULL)
_Q_EVT(ISET, USE, ev_use, NULL)
_Q_EVS(STR, EPREFIX, eprefix, CONFIG_EPREFIX)
_Q_EVS(STR, EMERGE_LOG_DIR, portlogdir, CONFIG_EPREFIX "var/log")
_Q_EVS(STR, PORTDIR, main_overlay, CONFIG_EPREFIX "var/db/repos/gentoo")
_Q_EVS(STR, PORTAGE_BINHOST, binhost, DEFAULT_PORTAGE_BINHOST)
_Q_EVS(STR, PORTAGE_TMPDIR, port_tmpdir, CONFIG_EPREFIX "var/tmp/portage/")
_Q_EVS(STR, PKGDIR, pkgdir, CONFIG_EPREFIX "var/cache/binpkgs/")
_Q_EVS(STR, Q_VDB, portvdb, CONFIG_EPREFIX "var/db/pkg")
_Q_EVS(STR, Q_EDB, portedb, CONFIG_EPREFIX "var/cache/edb")
{ NULL, 0, _Q_BOOL, { NULL }, 0, NULL, NULL, }
#undef _Q_EV
#undef _Q_EVS
#undef _Q_EVB
};
set *package_masks = NULL;
/* Handle a single file in the repos.conf format. */
static void
read_one_repos_conf(const char *repos_conf, char **primary)
{
char rrepo[_Q_PATH_MAX];
char *main_repo;
char *repo;
char *buf = NULL;
size_t buf_len = 0;
char *s = NULL; /* pacify compiler */
char *p;
char *q;
char *r;
char *e;
bool do_trim;
bool is_default;
if (getenv("DEBUG"))
fprintf(stderr, " parse %s\n", repos_conf);
if (!eat_file(repos_conf, &buf, &buf_len)) {
if (buf != NULL)
free(buf);
return;
}
main_repo = NULL;
repo = NULL;
for (p = strtok_r(buf, "\n", &s); p != NULL; p = strtok_r(NULL, "\n", &s))
{
/* trim trailing whitespace, remove comments, locate =, walking
* backwards to the front of the string */
do_trim = true;
e = NULL;
for (r = q = s - 2; q >= p; q--) {
if (do_trim && isspace((int)*q)) {
*q = '\0';
r = q - 1;
} else if (*q == '#') {
do_trim = true;
*q = '\0';
e = NULL;
r = q - 1;
} else {
if (*q == '=')
e = q;
do_trim = false;
}
}
/* make q point to the last char */
q = r;
if (*p == '[' && *q == ']') { /* section header */
repo = p + 1;
*q = '\0';
is_default = strcmp(repo, "DEFAULT") == 0;
continue;
} else if (*p == '\0') { /* empty line */
continue;
} else if (e == NULL) { /* missing = */
continue;
} else if (repo == NULL) { /* not in a section */
continue;
}
/* trim off whitespace before = */
for (r = e - 1; r >= p && isspace((int)*r); r--)
*r = '\0';
/* and after the = */
for (*e++ = '\0'; e < q && isspace((int)*e); e++)
;
if (is_default && strcmp(p, "main-repo") == 0) {
main_repo = e;
} else if (!is_default && strcmp(p, "location") == 0) {
void *ele;
size_t n;
char *overlay;
/* try not to get confused by symlinks etc. */
if (realpath(e, rrepo) != NULL)
e = rrepo;
array_for_each(overlay_names, n, overlay) {
if (strcmp(overlay, repo) == 0)
break;
overlay = NULL;
}
if (overlay != NULL) {
/* replace overlay */
ele = array_get_elem(overlay_src, n);
free(ele);
array_get_elem(overlay_src, n) = xstrdup(repos_conf);
ele = array_get_elem(overlays, n);
free(ele);
ele = array_get_elem(overlays, n) = xstrdup(e);
} else {
ele = xarraypush_str(overlays, e);
overlay = xarraypush_str(overlay_names, repo);
xarraypush_str(overlay_src, repos_conf);
}
if (main_repo && strcmp(repo, main_repo) == 0)
*primary = overlay;
}
}
free(buf);
}
/* Handle a possible directory of files. */
static void
read_repos_conf(const char *configroot, const char *repos_conf, char **primary)
{
char *top_conf, *sub_conf;
int i, count;
struct dirent **confs;
xasprintf(&top_conf, "%s%s", configroot, repos_conf);
if (getenv("DEBUG"))
fprintf(stderr, "repos.conf.d scanner %s\n", top_conf);
count = scandir(top_conf, &confs, NULL, alphasort);
if (count == -1) {
if (errno == ENOTDIR)
read_one_repos_conf(top_conf, primary);
} else {
for (i = 0; i < count; ++i) {
const char *name = confs[i]->d_name;
if (name[0] == '.' || name[0] == '\0')
continue;
/* Exclude backup files (aka files with ~ as postfix). */
if (name[strlen(name) - 1] == '~')
continue;
#ifdef DT_UNKNOWN
if (confs[i]->d_type != DT_UNKNOWN &&
confs[i]->d_type != DT_REG &&
confs[i]->d_type != DT_LNK)
continue;
#endif
xasprintf(&sub_conf, "%s/%s", top_conf, name);
#ifdef DT_UNKNOWN
if (confs[i]->d_type != DT_REG)
#endif
{
struct stat st;
if (stat(sub_conf, &st) || !S_ISREG(st.st_mode)) {
free(sub_conf);
continue;
}
}
read_one_repos_conf(sub_conf, primary);
free(sub_conf);
}
scandir_free(confs, count);
}
free(top_conf);
}
static void
initialize_portage_env(void)
{
size_t i;
const char *s;
env_vars *var;
char pathbuf[_Q_PATH_MAX];
char rpathbuf[_Q_PATH_MAX];
const char *configroot = getenv("PORTAGE_CONFIGROOT");
char *primary_overlay = NULL;
/* initialize all the properties with their default value */
for (i = 0; vars_to_read[i].name; ++i) {
var = &vars_to_read[i];
switch (var->type) {
case _Q_BOOL: *var->value.b = var->default_value; break;
case _Q_STR:
case _Q_ISTR: *var->value.s = xstrdup(var->default_value); break;
case _Q_ISET: *var->value.t = (set *)var->default_value; break;
}
var->src = xstrdup(STR_DEFAULT);
}
package_masks = create_set();
/* figure out where to find our config files */
if (!configroot)
configroot = CONFIG_EPREFIX;
/* rstrip /-es */