forked from gentoo/portage-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qmerge.c
2185 lines (1933 loc) · 57 KB
/
qmerge.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-2024 Gentoo Authors
* Distributed under the terms of the GNU General Public License v2
*
* Copyright 2005-2010 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 <stdio.h>
#include <xalloc.h>
#include <fnmatch.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include "stat-time.h"
#include "atom.h"
#include "copy_file.h"
#include "move_file.h"
#include "contents.h"
#include "eat_file.h"
#include "hash.h"
#include "human_readable.h"
#include "profile.h"
#include "rmspace.h"
#include "scandirat.h"
#include "set.h"
#include "tree.h"
#include "xasprintf.h"
#include "xchdir.h"
#include "xmkdir.h"
#include "xpak.h"
#include "xsystem.h"
#ifndef GLOB_BRACE
# define GLOB_BRACE (1 << 10) /* Expand "{a,b}" to "a" "b". */
#endif
/*
--nofiles don't verify files in package
--noscript don't execute pkg_{pre,post}{inst,rm} (if any)
*/
/* How things should work, ideally. This is not how it currently is
* implemented at all.
*
* invocation: qmerge ... pkg/a pkg/b pkg/c
* initial mergeset: pkg/a pkg/b pkg/c
* resolving:
* - for pkg, get dependencies
* * apply masks
* * extract depend, rdepend, bdepend, etc.
* * filter flags in use (libq/dep)
* * add found pkgs to mergeset if not present in resolvedset
* - while mergeset contains pkgs
* * resolve pkg (see above)
* * move from mergeset to resolvedset
* here (if all is well) mergeset is empty, and resolvedset contains the
* pkgs to be installed. If for instance pkg/c depends on pkg/b it
* won't occur double, for it is a set.
*
* Technically, because we deal with binpkgs, we don't have
* dependencies, but there can be pre/post scripts that actually use
* depended on pkgs, and if we would invoke ebuild(5) to compile instead
* of unpack, we would respect the order too, so the resolvedset here
* needs to be ordered into a list.
* While functionally one can re-evaluate the dependencies here,
* implementation wise, it probably is easier to build the final merge
* order list while resolving. This can also add the additional
* metadata of whether a pkg was requested (cmdline) or pulled in a dep,
* and for which package. That information can be leveraged to draw a
* tree (visuals) but also to determine possible parallel installation
* paths.
*
* For example, original invocation could lead to a resolved merge list
* of:
* M pkg/a
* m pkg/d
* R pkg/c
* M pkg/b
*
* After this, the pkgs need to be fetched and the pre phases need to be
* run. In interactive mode, a question probably needs to be inserted
* after the printing of the resolved merge list. Then if all checks
* out, the unpack and merge to live fs + vdb updates can be performed.
* Ideally the unpack (or compile via ebuild(5)) phase is separated from
* the final merge to live fs. The latter always has to be serial, but
* the former can run in parallel based on dependencies seen. For
* our example, pkg/d and pkg/b can be unpacked in parallel, merged in
* which order finished first, then pkg/a and pkg/c can commence. So
* the start set is pkg/d and pkg/b, and each unlocks pkg/a and pkg/c
* respectively, which are not constrained, other than the final merge
* logic.
*
* Errors
* There are multiple kinds of errors in multiple stages. Whether they
* are fatal depends on a number of factors.
* - resolution error
* Failing to resolve an atom basically makes the tree that that pkg
* is part of unmergable; that is, it needs to be discarded from the
* workset. In interactive mode after resolving we could ask if the
* user wants to continue (if there's anything else we *can* do),
* non-interactive we could decide to just go ahead with whatever we
* was possible, unless we add a flag that stops at resolution errors.
* - USE-dep resolution error
* The state of USE-flags must be respected, and can cause problems,
* in particular cyclic dependencies. Solution to those is to disable
* USE-flags temporary and re-merge without. For now, these errors
* are not resolved, but should be detected and treated as resolution
* errors above.
* - fetch error
* Either because fetching the binpkg or the source files fails. This
* discards the atom and its tree. It may be possible in this case to
* try and re-resolve using an older version of the pkg. But since
* this kind of error is pretty much in the foundation of the work, it
* seems more logical to exclude the tree the package belongs too,
* because at this point parallel execution happens, it makes no sense
* any more to ask the user to abort.
* - unpack or merge error
* Under these errors are the failures in the various pkg checks (run
* phases) and for source-based installs obviously compilation
* failures. These discard an entire tree, and like fetch errors,
* we don't have a clear opportunity anymore to ask whether or not to
* continue.
* - live fs + vdb error
* This should be rare, but most probably filesystem related errors,
* such as running out of diskspace or lacking certain permissions.
* Corrupting the VDB hopefully doesn't happen, but it is possible to
* encounter problems there as well. Like fetch and unpack errors, we
* should try to continue with whatever we can, but will not roll-back
* already merged packages. So a failure here, should result in
* dropping all children from the failed pkg.
*
* After merging qlop -Ev should show whatever was merged successfully,
* so qmerge should show what failed to merge (in what stage).
*/
/* #define BUSYBOX "/bin/busybox" */
#define BUSYBOX ""
#define QMERGE_FLAGS "fFsKUpuyO" COMMON_FLAGS
static struct option const qmerge_long_opts[] = {
{"fetch", no_argument, NULL, 'f'},
{"force", no_argument, NULL, 'F'},
{"search", no_argument, NULL, 's'},
{"install", no_argument, NULL, 'K'},
{"unmerge", no_argument, NULL, 'U'},
{"pretend", no_argument, NULL, 'p'},
{"update", no_argument, NULL, 'u'},
{"yes", no_argument, NULL, 'y'},
{"nodeps", no_argument, NULL, 'O'},
{"debug", no_argument, NULL, 128},
COMMON_LONG_OPTS
};
static const char * const qmerge_opts_help[] = {
"Fetch package and newest Packages metadata",
"Fetch package (skipping Packages)",
"Search available packages",
"Install package",
"Uninstall package",
"Pretend only",
"Update only",
"Don't prompt before overwriting",
"Don't merge dependencies",
"Run shell funcs with `set -x`",
COMMON_OPTS_HELP
};
#define qmerge_usage(ret) usage(ret, QMERGE_FLAGS, qmerge_long_opts, qmerge_opts_help, NULL, lookup_applet_idx("qmerge"))
char search_pkgs = 0;
char interactive = 1;
char install = 0;
char uninstall = 0;
char force_download = 0;
char follow_rdepends = 1;
char qmerge_strict = 0;
char update_only = 0;
bool debug = false;
const char Packages[] = "Packages";
struct llist_char_t {
char *data;
struct llist_char_t *next;
};
typedef struct llist_char_t llist_char;
static void pkg_fetch(int, const depend_atom *, const tree_match_ctx *);
static void pkg_merge(int, const depend_atom *, const tree_match_ctx *);
static int pkg_unmerge(tree_pkg_ctx *, depend_atom *, set *, int, char **, int, char **);
static bool
qmerge_prompt(const char *p)
{
printf("%s? [Y/n] ", p);
fflush(stdout);
switch (fgetc(stdin)) {
case '\n':
case 'y':
case 'Y':
return true;
default:
return false;
}
}
static void
fetch(const char *destdir, const char *src)
{
if (!binhost[0])
return;
fflush(NULL);
#if 0
if (getenv("FETCHCOMMAND") != NULL) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "(export DISTDIR='%s' URI='%s/%s'; %s)",
destdir, binhost, src, getenv("FETCHCOMMAND"));
xsystem(buf, AT_FDCWD);
} else
#endif
{
char *path = NULL;
/* wget -c -q -P <dir> <uri> */
const char *argv[] = {
"echo",
"wget",
"-c",
"-P",
destdir,
path,
quiet ? (char *)"-q" : NULL,
NULL,
};
xasprintf(&path, "%s/%s", binhost, src);
if (!pretend && (force_download || install))
xsystemv(&argv[1], AT_FDCWD); /* skip echo */
else
xsystemv(argv, AT_FDCWD);
free(path);
}
fflush(stdout);
fflush(stderr);
}
static void
qmerge_initialize(void)
{
char *buf;
if (strlen(BUSYBOX) > 0)
if (access(BUSYBOX, X_OK) != 0)
err(BUSYBOX " must be installed");
if (access("/bin/sh", X_OK) != 0)
err("/bin/sh must be installed");
if (pkgdir[0] != '/')
errf("PKGDIR='%s' does not appear to be valid", pkgdir);
if (!search_pkgs && !pretend) {
if (mkdir_p(pkgdir, 0755))
errp("could not setup PKGDIR: %s", pkgdir);
}
xasprintf(&buf, "%s/portage/", port_tmpdir);
mkdir_p(buf, 0755);
xchdir(buf);
if (force_download == 1 /* -f: fetch */) {
unlink(Packages);
fetch(buf, Packages);
}
free(buf);
}
static tree_ctx *_qmerge_vdb_tree = NULL;
static tree_ctx *_qmerge_binpkg_tree = NULL;
#define BV_INSTALLED BV_VDB
#define BV_BINARY BV_BINPKG
#define BV_EBUILD (1<<0) /* not yet supported */
#define BV_VDB (1<<1)
#define BV_BINPKG (1<<2)
static tree_match_ctx *
best_version(const depend_atom *atom, int mode)
{
tree_ctx *vdb = _qmerge_vdb_tree;
tree_ctx *binpkg = _qmerge_binpkg_tree;
tree_match_ctx *tmv = NULL;
tree_match_ctx *tmp = NULL;
tree_match_ctx *ret;
int r;
if (mode & BV_EBUILD) {
warn("BV_EBUILD not yet supported");
return NULL;
}
if (mode == 0) {
warn("mode needs to be set");
return NULL;
}
if (mode & BV_VDB) {
if (vdb == NULL) {
vdb = tree_open_vdb(portroot, portvdb);
if (vdb == NULL)
return NULL;
_qmerge_vdb_tree = vdb;
}
tmv = tree_match_atom(vdb, atom,
TREE_MATCH_LATEST | TREE_MATCH_FIRST |
TREE_MATCH_VIRTUAL | TREE_MATCH_ACCT);
}
if (mode & BV_BINPKG) {
if (binpkg == NULL) {
binpkg = tree_open_binpkg("/", pkgdir);
if (binpkg == NULL) {
if (tmv != NULL)
tree_match_close(tmv);
if (vdb != NULL)
tree_close(vdb);
return NULL;
}
_qmerge_binpkg_tree = binpkg;
}
tmp = tree_match_atom(binpkg, atom,
TREE_MATCH_LATEST | TREE_MATCH_FIRST | TREE_MATCH_METADATA |
TREE_MATCH_VIRTUAL | TREE_MATCH_ACCT);
}
if (tmv == NULL && tmp == NULL)
ret = NULL;
else if (tmv == NULL && tmp != NULL)
ret = tmp;
else if (tmv != NULL && tmp == NULL)
ret = tmv;
else {
if ((r = atom_compare(tmv->atom, tmp->atom)) == EQUAL || r == OLDER)
ret = tmp;
else
ret = tmv;
}
if (tmv != NULL && tmv != ret)
tree_match_close(tmv);
if (tmp != NULL && tmp != ret)
tree_match_close(tmp);
return ret;
}
static int
config_protected(const char *buf, int cp_argc, char **cp_argv,
int cpm_argc, char **cpm_argv)
{
int i;
/* Check CONFIG_PROTECT_MASK */
for (i = 1; i < cpm_argc; ++i)
if (strncmp(cpm_argv[i], buf, strlen(cpm_argv[i])) == 0)
return 0;
/* Check CONFIG_PROTECT */
for (i = 1; i < cp_argc; ++i)
if (strncmp(cp_argv[i], buf, strlen(cp_argv[i])) == 0)
return 1;
/* this would probably be bad */
if (strcmp(CONFIG_EPREFIX "bin/sh", buf) == 0)
return 1;
return 0;
}
static void
crossmount_rm(const char *fname, const struct stat * const st,
int fd, char *qpth)
{
struct stat lst;
if (fstatat(fd, fname, &lst, AT_SYMLINK_NOFOLLOW) == -1)
return;
if (lst.st_dev != st->st_dev) {
warn("skipping crossmount install masking: %s", fname);
return;
}
qprintf("%s<<<%s %s/%s (INSTALL_MASK)\n", YELLOW, NORM, qpth, fname);
rm_rf_at(fd, fname);
}
enum inc_exc { INCLUDE = 1, EXCLUDE = 2 };
static void
install_mask_check_dir(
char ***maskv,
int maskc,
const struct stat * const st,
int fd,
ssize_t level,
enum inc_exc parent_mode,
char *qpth)
{
struct dirent **files;
int cnt;
int i;
int j;
enum inc_exc mode;
enum inc_exc child_mode;
#ifndef DT_DIR
struct stat s;
#endif
char *npth = qpth + strlen(qpth);
cnt = scandirat(fd, ".", &files, filter_self_parent, alphasort);
for (j = 0; j < cnt; j++) {
mode = child_mode = parent_mode;
for (i = 0; i < maskc; i++) {
if ((ssize_t)maskv[i][0] < 0) {
/* relative matches need to be a "file", as the Portage
* implementation suggests, so that's easy for us here,
* since we can just match it against each component in
* the path */
if ((ssize_t)maskv[i][0] < -1)
continue; /* this is unsupported, so skip it */
/* this also works if maskv happens to be a glob */
if (fnmatch(maskv[i][1], files[j]->d_name, FNM_PERIOD) != 0)
continue;
mode = child_mode = maskv[i][2] ? INCLUDE : EXCLUDE;
} else if ((ssize_t)maskv[i][0] < level) {
/* either this is a mask that didn't match, or it
* matched, but a negative match exists for a deeper
* level, parent_mode should reflect this */
continue;
} else {
if (fnmatch(maskv[i][level], files[j]->d_name, FNM_PERIOD) != 0)
continue;
if ((ssize_t)maskv[i][0] == level) /* full mask match */
mode = child_mode =
(ssize_t)maskv[i][level + 1] ? INCLUDE : EXCLUDE;
else if (maskv[i][(ssize_t)maskv[i][0] + 1])
/* partial include mask */
mode = INCLUDE;
}
}
DBG("%s/%s: %s/%s", qpth, files[j]->d_name,
mode == EXCLUDE ? "EXCLUDE" : "INCLUDE",
child_mode == EXCLUDE ? "EXCLUDE" : "INCLUDE");
if (mode == EXCLUDE) {
crossmount_rm(files[j]->d_name, st, fd, qpth);
continue;
}
#ifdef DT_DIR
if (files[j]->d_type == DT_DIR) {
#else
if (fstatat(fd, files[j]->d_name, &s, AT_SYMLINK_NOFOLLOW) != 0)
continue;
if (S_ISDIR(s.st_mode)) {
#endif
int subfd = openat(fd, files[j]->d_name, O_RDONLY);
if (subfd < 0)
continue;
snprintf(npth, _Q_PATH_MAX - (npth - qpth),
"/%s", files[j]->d_name);
install_mask_check_dir(maskv, maskc, st, subfd,
level + 1, child_mode, qpth);
close(subfd);
*npth = '\0';
}
}
scandir_free(files, cnt);
}
static void
install_mask_pwd(int iargc, char **iargv, const struct stat * const st, int fd)
{
char *p;
int i;
size_t cnt;
size_t maxdirs;
char **masks;
size_t masksc;
char ***masksv;
char qpth[_Q_PATH_MAX];
/* we have to deal with "negative" masks, see
* https://archives.gentoo.org/gentoo-portage-dev/message/29e128a9f41122fa0420c1140f7b7f94
* which means we'll need to see what thing matches last
* (inclusion or exclusion) for *every* file :( */
/*
example package contents:
/e/t1
/u/b/t1
/u/b/t2
/u/l/lt1
/u/s/d/t1
/u/s/m/m1/t1
/u/s/m/m5/t2
masking rules: array encoding:
/u/s 2 u s 0 relative=0 include=0
-/u/s/m/m1 4 u s m m1 1 relative=0 include=1
e -1 e 0 relative=1 include=0
should result in:
/u/b/t1
/u/b/t2
/u/l/lt1
/u/s/m/m1/t1
strategy:
- for each dir level
- find if there is a match on that level in rules
- if the last match is the full mask
- if the mask is negated, do not remove entry
- else, remove entry
- if the last match is negated, partial and a full mask matched before
- do not remove entry
practice:
/e | matches "e" -> remove
/u | matches partial last negated -> continue
/b | doesn't match -> leave subtree
/l | doesn't match -> leave subtree
/s | match, partial last negated match -> remember match, continue
/d | doesn't match -> remembered match, remove subtree
/m | partial match negated -> continue
/m1 | match negated -> leave subtree
/m5 | doesn't match -> remembered match, remove subtree
*/
/* find the longest path so we can allocate a matrix */
maxdirs = 0;
for (i = 1; i < iargc; i++) {
char lastc = '/';
cnt = 1; /* we always have "something", right? */
p = iargv[i];
if (*p == '-')
p++;
for (; *p != '\0'; p++) {
/* eliminate duplicate /-es, also ignore the leading / in
* the count */
if (*p == '/' && *p != lastc)
cnt++;
lastc = *p;
}
if (cnt > maxdirs)
maxdirs = cnt;
}
maxdirs += 2; /* allocate plus relative and include elements */
/* allocate and populate matrix */
masksc = iargc - 1;
masks = xmalloc(sizeof(char *) * (maxdirs * masksc));
masksv = xmalloc(sizeof(char **) * (masksc));
for (i = 1; i < iargc; i++) {
masksv[i - 1] = &masks[(i - 1) * maxdirs];
p = iargv[i];
cnt = 1; /* start after count */
/* ignore include marker */
if (*p == '-')
p++;
/* strip of leading slash(es) */
while (*p == '/')
p++;
masks[((i - 1) * maxdirs) + cnt] = p;
for (; *p != '\0'; p++) {
if (*p == '/') {
/* fold duplicate slashes */
do {
*p++ = '\0';
} while (*p == '/');
cnt++;
masks[((i - 1) * maxdirs) + cnt] = p;
}
}
/* brute force cast below values, a pointer basically is size_t,
* which is large enough to store what we need here */
p = iargv[i];
/* set include bit */
if (*p == '-') {
masks[((i - 1) * maxdirs) + cnt + 1] = (char *)1;
p++;
} else {
masks[((i - 1) * maxdirs) + cnt + 1] = (char *)0;
}
/* set count */
masks[((i - 1) * maxdirs) + 0] =
(char *)((*p == '/' ? 1 : -1) * cnt);
}
#if EBUG
fprintf(warnout, "applying install masks:\n");
for (cnt = 0; cnt < masksc; cnt++) {
ssize_t plen = (ssize_t)masksv[cnt][0];
fprintf(warnout, "%3zd ", plen);
if (plen < 0)
plen = -plen;
for (i = 1; i <= plen; i++)
fprintf(warnout, "%s ", masksv[cnt][i]);
fprintf(warnout, " %zd\n", (size_t)masksv[cnt][i]);
}
#endif
cnt = snprintf(qpth, _Q_PATH_MAX, "%s", CONFIG_EPREFIX);
cnt--;
if (qpth[cnt] == '/')
qpth[cnt] = '\0';
install_mask_check_dir(masksv, masksc, st, fd, 1, INCLUDE, qpth);
free(masks);
free(masksv);
}
static char
qprint_tree_node(
int level,
const tree_match_ctx *mpkg,
const tree_match_ctx *bpkg,
int replacing)
{
char buf[1024];
int i;
char install_ver[126] = "";
char c = 'N';
const char *color;
if (!pretend)
return 0;
if (bpkg == NULL) {
c = 'N';
snprintf(buf, sizeof(buf), "%sN%s", GREEN, NORM);
} else {
if (bpkg != NULL) {
switch (replacing) {
case EQUAL: c = 'R'; break;
case NEWER: c = 'U'; break;
case OLDER: c = 'D'; break;
default: c = '?'; break;
}
snprintf(install_ver, sizeof(install_ver), "[%s%.*s%s] ",
DKBLUE,
(int)(sizeof(install_ver) - 4 -
sizeof(DKBLUE) - sizeof(NORM)),
bpkg->atom->PVR, NORM);
}
if (update_only && c != 'U')
return c;
if ((c == 'R' || c == 'D') && update_only && level)
return c;
switch (c) {
case 'R': color = YELLOW; break;
case 'U': color = BLUE; break;
case 'D': color = DKBLUE; break;
default: color = RED; break;
}
snprintf(buf, sizeof(buf), "%s%c%s", color, c, NORM);
#if 0
if (level) {
switch (c) {
case 'N':
case 'U': break;
default:
qprintf("[%c] %d %s\n", c, level, pkg->PF); return;
break;
}
}
#endif
}
printf("[%s] ", buf);
for (i = 0; i < level; ++i)
putchar(' ');
if (verbose) {
char *use = mpkg->meta->Q_USE; /* TODO: compute difference */
printf("%s %s%s%s%s%s%s\n",
atom_format("%[CAT]%[PF]", mpkg->atom),
install_ver, use != NULL ? "(" : "",
RED, use, NORM, use != NULL ? ")" : "");
} else {
printf("%s\n", atom_format("%[CAT]%[PF]", mpkg->atom));
}
return c;
}
/* PMS 9.2 Call order */
enum pkg_phases {
PKG_PRETEND = 1,
PKG_SETUP = 2,
/* skipping src_* */
PKG_PREINST = 3,
PKG_POSTINST = 4,
PKG_PRERM = 5,
PKG_POSTRM = 6
};
#define MAX_EAPI 8
static struct {
enum pkg_phases phase;
const char *phasestr;
unsigned char eapi[1 + MAX_EAPI];
} phase_table[] = {
{ 0, NULL, {0,0,0,0,0,0,0,0,0} }, /* align */
/* phase EAPI: 0 1 2 3 4 5 6 7 8 */
{ PKG_PRETEND, "pkg_pretend", {0,0,0,0,1,1,1,1,1} }, /* table 9.3 */
{ PKG_SETUP, "pkg_setup", {1,1,1,1,1,1,1,1,1} },
{ PKG_PREINST, "pkg_preinst", {1,1,1,1,1,1,1,1,1} },
{ PKG_POSTINST, "pkg_postinst", {1,1,1,1,1,1,1,1,1} },
{ PKG_PRERM, "pkg_prerm", {1,1,1,1,1,1,1,1,1} },
{ PKG_POSTRM, "pkg_postrm", {1,1,1,1,1,1,1,1,1} }
};
static struct {
enum pkg_phases phase;
const char *varname;
} phase_replacingvers[] = {
{ 0, NULL }, /* align */
/* phase varname PMS 11.1.2 */
{ PKG_PRETEND, "REPLACING_VERSIONS" },
{ PKG_SETUP, "REPLACING_VERSIONS" },
{ PKG_PREINST, "REPLACING_VERSIONS" },
{ PKG_POSTINST, "REPLACING_VERSIONS" },
{ PKG_PRERM, "REPLACED_BY_VERSION" },
{ PKG_POSTRM, "REPLACED_BY_VERSION" }
};
static void
pkg_run_func_at(
int dirfd,
const char *vdb_path,
const char *phases,
enum pkg_phases phaseidx,
const char *D,
const char *T,
const char *EAPI,
const char *replacing)
{
const char *func;
const char *phase;
char *script;
int eapi;
/* EAPI officially is a string, but since the official ones are only
* numbers, we'll just go with the numbers */
eapi = (int)strtol(EAPI, NULL, 10);
if (eapi > MAX_EAPI)
eapi = MAX_EAPI; /* let's hope latest known EAPI is closest */
/* see if this function should be run for the EAPI */
if (!phase_table[phaseidx].eapi[eapi])
return;
/* This assumes no func is a substring of another func.
* Today, that assumption is valid for all funcs ...
* The phases are the func with the "pkg_" chopped off. */
func = phase_table[phaseidx].phasestr;
phase = func + 4;
if (strstr(phases, phase) == NULL) {
qprintf("--- %s\n", func);
return;
}
qprintf("@@@ %s\n", func);
xasprintf(&script,
/* Provide funcs required by the PMS */
"EBUILD_PHASE=%3$s\n"
"debug-print() { :; }\n"
"debug-print-function() { :; }\n"
"debug-print-section() { :; }\n"
/* Not quite right */
"has_version() { [ -n \"$(qlist -ICqe \"$1\")\" ]; }\n"
"best_version() { qlist -ICqev \"$1\"; }\n"
"use() { useq \"$@\"; }\n"
"usex() { useq \"$1\" && echo \"${2-yes}$4\" || echo \"${3-no}$5\"; }\n"
"useq() { hasq \"$1\" ${USE}; }\n"
"usev() { hasv \"$1\" ${USE}; }\n"
"has() { hasq \"$@\"; }\n"
"hasq() { local h=$1; shift; case \" $* \" in *\" $h \"*) return 0;; *) return 1;; esac; }\n"
"hasv() { hasq \"$@\" && echo \"$1\"; }\n"
"elog() { printf ' * %%b\\n' \"$*\" >&2; }\n"
"einfon() { printf ' * %%b' \"$*\" >&2; }\n"
"einfo() { elog \"$@\"; }\n"
"ewarn() { elog \"$@\"; }\n"
"eqawarn() { elog \"QA: \"\"$@\"; }\n"
"eerror() { elog \"$@\"; }\n"
"die() { eerror \"$@\"; exit 1; }\n"
"fowners() { local f a=$1; shift; for f in \"$@\"; do chown $a \"${ED}/${f}\"; done; }\n"
"fperms() { local f a=$1; shift; for f in \"$@\"; do chmod $a \"${ED}/${f}\"; done; }\n"
/* TODO: This should suppress `die` */
"nonfatal() { \"$@\"; }\n"
"ebegin() { printf ' * %%b ...' \"$*\" >&2; }\n"
"eend() { local r=${1:-$?}; [ $# -gt 0 ] && shift; [ $r -eq 0 ] && echo ' [ ok ]' || echo \" $* \"'[ !! ]'; return $r; } >&2\n"
"dodir() { mkdir -p \"$@\"; }\n"
"keepdir() { dodir \"$@\" && touch \"$@\"/.keep_${CATEGORY}_${PN}-${SLOT%%/*}; }\n"
/* TODO: This should be fatal upon error */
"emake() { ${MAKE:-make} ${MAKEOPTS} \"$@\"; }\n"
/* Unpack the env */
"{ mkdir -p \"%6$s\"; "
"bzip2 -dc '%1$s/environment.bz2' > \"%6$s/environment\" "
"|| exit 1; }\n"
/* Load the main env */
". \"%6$s/environment\"\n"
/* Reload env vars that matter to us */
"export EBUILD_PHASE_FUNC='%2$s'\n"
"export FILESDIR=/.does/not/exist/anywhere\n"
"export MERGE_TYPE=binary\n"
"export ROOT='%4$s'\n"
"export EROOT=\"${ROOT%%/}${EPREFIX%%/}/\"\n"
/* BROOT, SYSROOT, ESYSROOT: PMS table 8.3 Prefix values for DEPEND */
"export BROOT=\n"
"export SYSROOT=\"${ROOT}\"\n"
"export ESYSROOT=\"${EROOT}\"\n"
"export D=\"%5$s\"\n"
"export ED=\"${D%%/}${EPREFIX%%/}/\"\n"
"export T=\"%6$s\"\n"
/* we do not support preserve-libs yet, so force
* preserve_old_lib instead */
"export FEATURES=\"${FEATURES/preserve-libs/}\"\n"
/* replacing versions: we ignore EAPI availability, for it will
* never hurt */
"export %7$s=\"%8$s\"\n"
/* Finally run the func */
"%9$s%2$s\n"
/* Ignore func return values (not exit values) */
":",
/*1*/ vdb_path,
/*2*/ func,
/*3*/ phase,
/*4*/ portroot,
/*5*/ D,
/*6*/ T,
/*7*/ phase_replacingvers[phaseidx].varname,
/*8*/ replacing,
/*9*/ debug ? "set -x;" : "");
xsystem(script, dirfd);
free(script);
}
#define pkg_run_func(...) pkg_run_func_at(AT_FDCWD, __VA_ARGS__)
/* Copy one tree (the single package) to another tree (ROOT) */
static int
merge_tree_at(int fd_src, const char *src, int fd_dst, const char *dst,
FILE *contents, size_t eprefix_len, set **objs, char **cpathp,
int cp_argc, char **cp_argv, int cpm_argc, char **cpm_argv)
{
int i, ret, subfd_src, subfd_dst;
DIR *dir;
struct dirent *de;
struct stat st;
char *cpath;
size_t clen, nlen, mnlen;
ret = -1;
/* Get handles to these subdirs */
/* Cannot use O_PATH as we want to use fdopendir() */
subfd_src = openat(fd_src, src, O_RDONLY|O_CLOEXEC);
if (subfd_src < 0)
return ret;
subfd_dst = openat(fd_dst, dst, O_RDONLY|O_CLOEXEC|O_PATH);
if (subfd_dst < 0) {
close(subfd_src);
return ret;
}
i = dup(subfd_src); /* fdopendir closes its argument */
dir = fdopendir(i);
if (!dir)
goto done;
cpath = *cpathp;
clen = strlen(cpath);
cpath[clen] = '/';
nlen = mnlen = 0;
while ((de = readdir(dir)) != NULL) {
const char *name = de->d_name;
if (filter_self_parent(de) == 0)
continue;
/* Build up the full path for this entry */
nlen = strlen(name);
if (mnlen < nlen) {
cpath = *cpathp = xrealloc(*cpathp, clen + 1 + nlen + 1);
mnlen = nlen;
}
strcpy(cpath + clen + 1, name);
/* Find out what the source path is */
if (fstatat(subfd_src, name, &st, AT_SYMLINK_NOFOLLOW)) {
warnp("could not read %s", cpath);
continue;
}
/* Migrate a directory */
if (S_ISDIR(st.st_mode)) {
if (!pretend && mkdirat(subfd_dst, name, st.st_mode)) {
if (errno != EEXIST) {
warnp("could not create %s", cpath);
continue;
}
/* XXX: update times of dir ? */
}
/* syntax: dir dirname */
if (!pretend)
fprintf(contents, "dir %s\n", cpath);
*objs = add_set(cpath, *objs);
qprintf("%s---%s %s%s%s/\n", GREEN, NORM, DKBLUE, cpath, NORM);
/* Copy all of these contents */
merge_tree_at(subfd_src, name,
subfd_dst, name, contents, eprefix_len,
objs, cpathp, cp_argc, cp_argv, cpm_argc, cpm_argv);
cpath = *cpathp;
mnlen = 0;
/* In case we didn't install anything, prune the empty dir */
if (!pretend)
unlinkat(subfd_dst, name, AT_REMOVEDIR);
} else if (S_ISREG(st.st_mode)) {
/* Migrate a file */
char *hash;
const char *dname;
char buf[_Q_PATH_MAX * 2];
struct stat ignore;
/* syntax: obj filename hash mtime */
hash = hash_file_at(subfd_src, name, HASH_MD5);
if (!pretend)
fprintf(contents, "obj %s %s %zu""\n",
cpath, hash ? hash : "xxx", (size_t)st.st_mtime);
/* Check CONFIG_PROTECT */
if (config_protected(cpath + eprefix_len,
cp_argc, cp_argv, cpm_argc, cpm_argv) &&
fstatat(subfd_dst, name, &ignore, AT_SYMLINK_NOFOLLOW) == 0)
{
/* ._cfg####_ */
char *num;
dname = buf;
snprintf(buf, sizeof(buf), "._cfg####_%s", name);
num = buf + 5;
for (i = 0; i < 10000; ++i) {
sprintf(num, "%04i", i);
num[4] = '_';
if (fstatat(subfd_dst, dname, &ignore, AT_SYMLINK_NOFOLLOW))
break;
}
qprintf("%s>>>%s %s (%s)\n", GREEN, NORM, cpath, dname);
} else {
dname = name;
qprintf("%s>>>%s %s\n", GREEN, NORM, cpath);
}
*objs = add_set(cpath, *objs);
if (pretend)
continue;
if (move_file(subfd_src, name, subfd_dst, dname, &st) != 0)
warnp("failed to move file from %s", cpath);
} else if (S_ISLNK(st.st_mode)) {
/* Migrate a symlink */
size_t len = st.st_size;
char sym[_Q_PATH_MAX];
/* Find out what we're pointing to */
if (readlinkat(subfd_src, name, sym, sizeof(sym)) == -1) {
warnp("could not read link %s", cpath);
continue;
}
sym[len < _Q_PATH_MAX ? len : _Q_PATH_MAX - 1] = '\0';
/* syntax: sym src -> dst mtime */
if (!pretend)
fprintf(contents, "sym %s -> %s %zu\n",
cpath, sym, (size_t)st.st_mtime);
qprintf("%s>>>%s %s%s -> %s%s\n", GREEN, NORM,
CYAN, cpath, sym, NORM);
*objs = add_set(cpath, *objs);