forked from gentoo/portage-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlop.c
1804 lines (1657 loc) · 49.3 KB
/
qlop.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-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 <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <sys/stat.h>
#include "atom.h"
#include "eat_file.h"
#include "scandirat.h"
#include "set.h"
#include "xarray.h"
#include "xasprintf.h"
#define QLOP_DEFAULT_LOGFILE "emerge.log"
#define QLOP_FLAGS "ctapHMmuUsElerd:f:w:F:" COMMON_FLAGS
static struct option const qlop_long_opts[] = {
{"summary", no_argument, NULL, 'c'},
{"time", no_argument, NULL, 't'},
{"average", no_argument, NULL, 'a'},
{"predict", no_argument, NULL, 'p'},
{"human", no_argument, NULL, 'H'},
{"machine", no_argument, NULL, 'M'},
{"merge", no_argument, NULL, 'm'},
{"unmerge", no_argument, NULL, 'u'},
{"autoclean", no_argument, NULL, 'U'},
{"sync", no_argument, NULL, 's'},
{"emerge", no_argument, NULL, 'E'},
{"endtime", no_argument, NULL, 'e'},
{"running", no_argument, NULL, 'r'},
{"date", a_argument, NULL, 'd'},
{"lastmerge", no_argument, NULL, 'l'},
{"logfile", a_argument, NULL, 'f'},
{"atoms", a_argument, NULL, 'w'},
{"format", a_argument, NULL, 'F'},
COMMON_LONG_OPTS
};
static const char * const qlop_opts_help[] = {
"Print summary of average merges (implies -a)",
"Print time taken to complete action",
"Print average time taken to complete action",
"Print prediction of time it takes to complete action",
"Print elapsed time in human readable format (use with -t or -a)",
"Print start/elapsed time as seconds with no formatting",
"Show merge history",
"Show unmerge history",
"Show autoclean unmerge history",
"Show sync history",
"Show last merge similar to how emerge(1) -v would show it",
"Report time at which the operation finished (iso started)",
"Show current emerging packages",
"Limit selection to this time (1st -d is start, 2nd -d is end)",
"Limit selection to last Portage emerge action",
"Read emerge logfile instead of $EMERGE_LOG_DIR/" QLOP_DEFAULT_LOGFILE,
"Read package atoms to report from file",
"Print matched atom using given format string",
COMMON_OPTS_HELP
};
static const char qlop_desc[] =
"The --date option can take a few forms:\n"
" -d '# <day|week|month|year>[s] [ago]' (e.g. '3 days ago')\n"
"Or using strptime(3) formats:\n"
" -d '2015-12-25' (detected as %Y-%m-%d)\n"
" -d '1459101740' (detected as %s)\n"
" -d '%d.%m.%Y|25.12.2015' (format is specified)";
#define qlop_usage(ret) usage(ret, QLOP_FLAGS, qlop_long_opts, qlop_opts_help, qlop_desc, lookup_applet_idx("qlop"))
struct qlop_mode {
char do_time:1;
char do_merge:1;
char do_unmerge:1;
char do_autoclean:1;
char do_sync:1;
char do_running:1;
char do_average:1;
char do_predict:1;
char do_summary:1;
char do_human:1;
char do_machine:1;
char do_endtime:1;
char show_lastmerge:1;
char show_emerge:1;
const char *fmt;
};
static bool
parse_date(const char *sdate, time_t *t)
{
struct tm tm;
const char *s;
memset(&tm, 0, sizeof(tm));
s = strchr(sdate, '|');
if (s) {
/* Handle custom format like "%Y|2012". */
size_t fmtlen = s - sdate;
char fmt[fmtlen + 1];
memcpy(fmt, sdate, fmtlen);
fmt[fmtlen] = '\0';
sdate = s + 1;
s = strptime(sdate, fmt, &tm);
if (s == NULL || s[0] != '\0')
return false;
} else {
/* Handle automatic formats:
* - "12315128" -> %s
* - "@12315128" -> %s
* - "2015-12-24" -> %Y-%m-%d
* - "2019-03-28T13:52:31" -> %Y-%m-%dT%H:%M:%s"
* - human readable format (see below)
*/
size_t len = strspn(sdate, "0123456789-:T@");
if (sdate[len] == '\0') {
if (sdate[0] == '@') {
time_t d = (time_t)strtoll(&sdate[1], (char **)&s, 10);
localtime_r(&d, &tm);
} else if (strchr(sdate, '-') == NULL) {
time_t d = (time_t)strtoll(sdate, (char **)&s, 10);
localtime_r(&d, &tm);
} else if ((s = strchr(sdate, 'T')) == NULL) {
s = strptime(sdate, "%Y-%m-%d", &tm);
} else {
s = strptime(sdate, "%Y-%m-%dT%H:%M:%S", &tm);
}
if (s == NULL || s[0] != '\0')
return false;
} else {
/* Handle the formats:
* <#> <day|week|month|year>[s] [ago]
*/
len = strlen(sdate) + 1;
unsigned long num;
char dur[len];
char ago[len];
int ret = sscanf(sdate, "%lu %s %s", &num, dur, ago);
if (ret < 2) {
if (strcmp(sdate, "today") == 0) {
num = 1;
snprintf(dur, len, "%s", "day");
} else if (strcmp(sdate, "yesterday") == 0) {
num = 2;
snprintf(dur, len, "%s", "day");
} else {
return false;
}
}
if (ret == 3 && strcmp(ago, "ago") != 0)
return false;
if (time(t) == -1)
return false;
if (localtime_r(t, &tm) == NULL)
return false;
/* Chop and trailing "s" sizes. */
len = strlen(dur);
if (dur[len - 1] == 's')
dur[len - 1] = '\0';
/* Step down the current time. */
if (!strcmp(dur, "year")) {
tm.tm_year -= num;
} else if (!strcmp(dur, "month")) {
if (num >= 12) {
tm.tm_year -= (num / 12);
num %= 12;
}
tm.tm_mon -= num;
if (tm.tm_mon < 0) {
tm.tm_mon += 12;
tm.tm_year -= 1;
}
} else if (!strcmp(dur, "week")) {
num *= 7;
goto days;
} else if (!strcmp(dur, "day")) {
days:
/* This is in seconds, so scale w/that. */
*t -= (num * 24 * 60 * 60);
if (localtime_r(t, &tm) == NULL)
return false;
} else
return false;
}
}
*t = mktime(&tm);
return (*t == -1) ? false : true;
}
static char _date_buf[48];
static char *fmt_date(struct qlop_mode *flags, time_t ts, time_t te)
{
time_t t = flags->do_endtime ? te : ts;
struct tm lt;
if (flags->do_machine || localtime_r(&t, <) == NULL)
snprintf(_date_buf, sizeof(_date_buf),
"%zd", (size_t)t);
else
strftime(_date_buf, sizeof(_date_buf),
"%Y-%m-%dT%H:%M:%S", <);
return _date_buf;
}
static char _elapsed_buf[256];
static char *fmt_elapsedtime(struct qlop_mode *flags, time_t e)
{
time_t dd;
time_t hh;
time_t mm;
time_t ss;
size_t bufpos = 0;
if (flags->do_machine || e < 0) {
snprintf(_elapsed_buf, sizeof(_elapsed_buf),
"%s%zd%s",
GREEN, (size_t)e, NORM);
return _elapsed_buf;
}
ss = e % 60;
e /= 60;
mm = e % 60;
e /= 60;
hh = e % 24;
e /= 24;
dd = e;
if (flags->do_human) {
if (dd > 0)
bufpos += snprintf(_elapsed_buf + bufpos,
sizeof(_elapsed_buf) - bufpos,
"%s%zd%s day%s",
GREEN, (size_t)dd, NORM, dd == 1 ? "" : "s");
if (hh > 0)
bufpos += snprintf(_elapsed_buf + bufpos,
sizeof(_elapsed_buf) - bufpos,
"%s%s%zd%s hour%s",
bufpos == 0 ? "" : ", ",
GREEN, (size_t)hh, NORM, hh == 1 ? "" : "s");
if (mm > 0)
bufpos += snprintf(_elapsed_buf + bufpos,
sizeof(_elapsed_buf) - bufpos,
"%s%s%zd%s minute%s",
bufpos == 0 ? "" : ", ",
GREEN, (size_t)mm, NORM, mm == 1 ? "" : "s");
if (ss > 0 || (mm + hh + dd) == 0)
bufpos += snprintf(_elapsed_buf + bufpos,
sizeof(_elapsed_buf) - bufpos,
"%s%s%zd%s second%s",
bufpos == 0 ? "" : ", ",
GREEN, (size_t)ss, NORM, ss == 1 ? "" : "s");
} else {
hh += 24 * dd;
if (hh > 0) {
snprintf(_elapsed_buf, sizeof(_elapsed_buf),
"%s%zd%s:%s%02zd%s:%s%02zd%s",
GREEN, (size_t)hh, NORM,
GREEN, (size_t)mm, NORM,
GREEN, (size_t)ss, NORM);
} else if (mm > 0) {
snprintf(_elapsed_buf, sizeof(_elapsed_buf),
"%s%zd%s′%s%02zd%s″",
GREEN, (size_t)mm, NORM,
GREEN, (size_t)ss, NORM);
} else {
snprintf(_elapsed_buf, sizeof(_elapsed_buf),
"%s%zd%ss",
GREEN, (size_t)ss, NORM);
}
}
return _elapsed_buf;
}
struct pkg_match {
char id[BUFSIZ];
depend_atom *atom;
time_t tbegin;
time_t time;
size_t cnt;
};
static int
pkg_sort_cb(const void *l, const void *r)
{
struct pkg_match *pl = *(struct pkg_match **)l;
struct pkg_match *pr = *(struct pkg_match **)r;
depend_atom *al = pl->atom;
depend_atom *ar = pr->atom;
return atom_compar_cb(al, ar);
}
/* The format of the sync log has changed over time.
Old format:
1106804103: Started emerge on: Jan 27, 2005 05:35:03
1106804103: *** emerge sync
1106804103: === sync
1106804103: >>> starting rsync with rsync://192.168.0.5/gentoo-portage
1106804537: === Sync completed with rsync://192.168.0.5/gentoo-portage
1106804538: *** terminating.
New format:
1431764402: Started emerge on: May 16, 2015 04:20:01
1431764402: *** emerge --quiet --keep-going --verbose --nospinner --oneshot --quiet-build=n --sync
1431764402: === sync
1431764402: >>> Syncing repository 'gentoo' into '/usr/portage'...
1431764402: >>> Starting rsync with rsync://[2a01:90:200:10::1a]/gentoo-portage
1431764460: === Sync completed for gentoo
1431764493: *** terminating.
*** packages
1547475773: >>> emerge (53 of 74) app-shells/bash-5.0 to /gentoo/prefix64/
1547475774: === (53 of 74) Cleaning (app-shells/bash-5.0::/path/to/app-shells/bash/bash-5.0.ebuild)
1547475774: === (53 of 74) Compiling/Merging (app-shells/bash-5.0::/path/to/app-shells/bash/bash-5.0.ebuild)
1547475913: === (53 of 74) Merging (app-shells/bash-5.0::/path/to/app-shells/bash/bash-5.0.ebuild)
1547475916: >>> AUTOCLEAN: app-shells/bash:0
1547475916: === Unmerging... (app-shells/bash-4.4_p23)
1547475918: >>> unmerge success: app-shells/bash-4.4_p23
1547475921: === (53 of 74) Post-Build Cleaning (app-shells/bash-5.0::/path/to/app-shells/bash/bash-5.0.ebuild)
1547475921: ::: completed emerge (53 of 74) app-shells/bash-5.0 to /gentoo/prefix64/
1550953093: Started emerge on: Feb 23, 2019 21:18:12
1550953093: *** emerge --ask --verbose --depclean pwgen
1550953093: >>> depclean
1550953118: === Unmerging... (app-admin/pwgen-2.08)
1550953125: >>> unmerge success: app-admin/pwgen-2.08
1550953125: *** exiting successfully.
1550953125: *** terminating.
Currently running merges can be found in the /proc filesystem:
- Linux: readlink(/proc/<pid>/fd/X)
- Solaris: readlink(/proc/<pid>/path/X)
from here a file should be there that points to the build.log file
$CAT/$P/work/build.log. If so, it's running for $CAT/$P.
This requires being the portage user though, or root.
Should there be no /proc, we can deduce from the log whether a package
is being emerged, if and only if, there are no parallel merges, and
portage never got interrupted in a way where it could not write its
interruption to the log. Unfortunately these scenarios happen a lot.
As such, we can try to remedy this somewhat by using a rule of thumb
that currently merging packages need to be withinin the last 10 days.
Averages
Compile time of packages typically changes over time (assuming the
machine stays the same). New major releases introduce extra code, or
improve stuff considerably. New compilers might take more or less time
compiling the code. The environment (like the toolchain) we cannot take
into account, packages themselves, we could do something with.
Portage unfortunately does not record the SLOT of the package it
emerges, so we cannot use SLOT.
- Gentoo revisions (-rX) typically do not change the code a lot
- minor and below revisions usually are part of the same code branch
- major revisions typically indicate a next generation of the code
So when there is a running emerge:
1. we value most an identical version, minus Gentoo revisions
2. we look for merges with the same major version
3. when there's still not enough (or anything) to balance an average
off, we look at merges for the same package (older major versions)
Think of this value system as something like the weight of 1 is 5x, 2 is
3x and 3 is just 1x.
While this might sound really cool, it often results in the same average
as a normal average, and so it is useless. In particular: the best
prediction is the most recent matching version. If there is no exact
match (ignoring Gentoo revisions), a projection of runtimes for previous
merges is the next best thing, ignoring major or minor revisions, e.g.
for GCC major versions would hold very well, but for binutils, the
minors differ a lot (for it's on major version 2 for ages).
So it seems what it boils down to, is that prediction of the simplest
kind is probably the best here. What we do, is we order the merge
history of the package, and try to extrapolate the growth between the
two last merges, e.g. 10 and 12 becomes 12 + (12 - 10) = 14.
Now if there is only one point of history here, there's nothing to
predict, and we best just take the value and add a 10% fuzz.
There's two cases in this setup:
1. predict for a version that we already have merge history for,
ignoring revisions, or
2. predict for a version that's different from what we've seen before
In case of 1. we only look at the same versions, and apply extrapolation
based on the revisions, e.g. -r2 -> -r4. In case of 2. we only look at
versions that are older than the version we're predicting for. This to
deal with e.g. merging python-3.6 with 3.9 installed as well.
*/
static int do_emerge_log(
const char *log,
struct qlop_mode *flags,
array_t *atoms,
time_t tbegin,
time_t tend)
{
FILE *fp;
char buf[BUFSIZ];
char *p;
char *q;
time_t tstart = LONG_MAX;
time_t tlast = tbegin;
time_t tstart_emerge = 0;
time_t last_merge = 0;
time_t last_exit = 0;
time_t sync_start = 0;
time_t sync_time = 0;
size_t sync_cnt = 0;
time_t elapsed;
depend_atom *atom;
depend_atom *atomw;
depend_atom *upgrade_atom = NULL;
DECLARE_ARRAY(merge_matches);
set *merge_averages = create_set();
DECLARE_ARRAY(unmerge_matches);
set *unmerge_averages = create_set();
set *atomset = NULL;
size_t i;
size_t parallel_emerge = 0;
bool all_atoms = false;
bool emerge_line;
char afmt[BUFSIZ];
struct pkg_match *pkg;
struct pkg_match *pkgw;
#define strpfx(X, Y) strncmp(X, Y, sizeof(Y) - 1)
/* support relative path in here and now, when using ROOT, stick to
* it, turning relative into a moot point */
if (portroot[1] == '\0')
snprintf(buf, sizeof(buf), "%s", log);
else
snprintf(buf, sizeof(buf), "%s%s", portroot, log);
if ((fp = fopen(buf, "r")) == NULL)
{
warnp("Could not open logfile '%s'", log);
return 1;
}
all_atoms = array_cnt(atoms) == 0;
if (all_atoms || flags->show_lastmerge) {
atomset = create_set();
/* assemble list of atoms */
while (fgets(buf, sizeof(buf), fp) != NULL) {
if ((p = strchr(buf, ':')) == NULL)
continue;
*p++ = '\0';
tstart = atol(buf);
if (tstart < tbegin || tstart > tend)
continue;
if (flags->show_lastmerge) {
if (strpfx(p, " *** emerge ") == 0)
tstart_emerge = tstart;
if (!all_atoms)
continue;
}
atom = NULL;
if (strpfx(p, " >>> emerge ") == 0 &&
(q = strchr(p + 13, ')')) != NULL)
{
p = q + 2;
q = strchr(p, ' ');
if (q != NULL) {
*q = '\0';
atom = atom_explode(p);
}
} else if (strpfx(p, " === Unmerging... (") == 0 ||
strpfx(p, " === Unmerging... (") == 0)
{
if (p[1] == ' ')
p++;
p += 19;
q = strchr(p, ')');
if (q != NULL) {
*q = '\0';
atom = atom_explode(p);
}
}
if (atom != NULL) {
/* strip off version info, if we generate a list
* ourselves, we will always print everything, so as
* well can keep memory footprint a bit lower by only
* having package matches */
atom->PV = NULL;
atom->PVR = NULL;
atom->PR_int = 0;
snprintf(afmt, sizeof(afmt), "%s/%s", atom->CATEGORY, atom->PN);
/* now we found a package, register this merge as a
* "valid" one, such that dummy emerge calls (e.g.
* emerge -pv foo) are ignored */
if (last_merge != tstart_emerge) {
array_t vals;
values_set(atomset, &vals);
array_for_each(&vals, i, atomw)
atom_implode(atomw);
xarrayfree_int(&vals);
clear_set(atomset);
last_merge = tstart_emerge;
}
atomset = add_set_value(afmt, atom, (void **)&atomw, atomset);
if (atomw != NULL)
atom_implode(atom);
}
}
rewind(fp);
}
if (flags->show_lastmerge) {
tbegin = last_merge;
tend = tstart;
}
/* loop over lines searching for atoms */
while (fgets(buf, sizeof(buf), fp) != NULL) {
if ((p = strchr(buf, ':')) == NULL)
continue;
*p++ = '\0';
tstart = atol(buf);
emerge_line = false;
if (((flags->do_running ||
(flags->show_emerge && verbose)) &&
strpfx(p, " *** emerge ") == 0) ||
(flags->do_running &&
(strpfx(p, " *** exiting ") == 0 ||
strpfx(p, " *** terminating.") == 0)))
{
emerge_line = true;
}
/* keeping track of parallel merges needs to be done before
* applying dates, for a subset of the log might show emerge
* finished without knowledge of another instance */
if (emerge_line && flags->do_running) {
if (p[7] == 'm') {
parallel_emerge++;
} else if (parallel_emerge > 0) {
if (p[7] != 'e' || (tstart - 4) <= last_exit)
parallel_emerge--;
if (p[7] == 'x')
last_exit = tstart;
}
/* for bug #687508, this cannot be in the else if case
* above, if the log is truncated somehow, the leading
* *** emerge might be missing, but a termination in that
* case better means we forget about everything that was
* unfinished not to keep reporting some packages forever */
if (parallel_emerge == 0) {
/* we just finished the only emerge we found to be
* running, so if there were "running" (unfinished)
* merges, they must have been terminated */
sync_start = 0;
while ((i = array_cnt(merge_matches)) > 0) {
i--;
pkgw = xarrayget(merge_matches, i);
atom_implode(pkgw->atom);
xarraydelete(merge_matches, i);
}
while ((i = array_cnt(unmerge_matches)) > 0) {
i--;
pkgw = xarrayget(unmerge_matches, i);
atom_implode(pkgw->atom);
xarraydelete(unmerge_matches, i);
}
}
}
if (tstart < tlast)
continue;
tlast = tstart;
if (tstart < tbegin || tstart > tend)
continue;
/* are we interested in this line? */
if (emerge_line && flags->show_emerge && verbose) {
char shortopts[8]; /* must hold as many opts converted below */
int numopts = 0;
printf("%semerge%s", DKBLUE, NORM);
for (p += 13; (q = strtok(p, " \n")) != NULL; p = NULL) {
if (strpfx(q, "--") == 0) {
/* portage seems to normalise options given into
* their long forms always; I don't want to keep a
* mapping table to short forms here, but it's
* tempting, so I just do a few of the often used
* ones */
q += 2;
if (strcmp(q, "ask") == 0) {
shortopts[numopts++] = 'a';
q = NULL;
} else if (strcmp(q, "verbose") == 0) {
shortopts[numopts++] = 'v';
q = NULL;
} else if (strcmp(q, "oneshot") == 0) {
shortopts[numopts++] = '1';
q = NULL;
} else if (strcmp(q, "deep") == 0) {
shortopts[numopts++] = 'D';
q = NULL;
} else if (strcmp(q, "update") == 0) {
shortopts[numopts++] = 'u';
q = NULL;
} else if (strcmp(q, "depclean") == 0) {
shortopts[numopts++] = 'c';
q = NULL;
} else if (strcmp(q, "unmerge") == 0) {
shortopts[numopts++] = 'C';
q = NULL;
} else {
q -= 2;
}
/* process next token */
if (q == NULL)
continue;
}
/* if we're here, we've assembled opts whatever we could */
if (numopts > 0) {
printf(" %s-%.*s%s",
GREEN, numopts, shortopts, NORM);
numopts = 0;
}
if (*q == '\0') {
/* skip empty token, likely the trailing \n */
continue;
}
if (strpfx(q, "--") == 0) {
printf(" %s%s%s", GREEN, q, NORM);
} else if (q[0] == '@' ||
strcmp(q, "world") == 0 ||
strcmp(q, "system") == 0)
{
printf(" %s%s%s", YELLOW, q, NORM);
} else {
/* this should be an atom */
atom = atom_explode(q);
if (atom == NULL) {
/* or not ... just print it */
printf(" %s", q);
} else {
printf(" %s", atom_format(
"%[pfx]%[CAT]%[PF]%[sfx]"
"%[SLOT]%[SUBSLOT]%[REPO]", atom));
atom_implode(atom);
}
}
}
printf("\n");
} else if (flags->do_sync && (
strpfx(p, " === Sync completed ") == 0 ||
strcmp(p, " === sync\n") == 0))
{
/* sync start or stop, we have nothing to detect parallel
* syncs with, so don't bother and assume this doesn't
* happen */
if (p[6] == 's') {
sync_start = tstart;
} else {
if (sync_start == 0)
continue; /* sync without start, exclude */
elapsed = tstart - sync_start;
p += 20;
if (strpfx(p, "for ") == 0) {
p += 4;
} else { /* "with " */
p += 5;
}
if ((q = strchr(p, '\n')) != NULL)
*q = '\0';
if (flags->do_predict || flags->do_average ||
flags->do_running)
{
sync_cnt++;
sync_time += elapsed;
sync_start = 0; /* reset */
continue;
}
if (quiet) {
printf("%s%s%s%s%s\n",
GREEN, p, NORM,
flags->do_time ? ": " : "",
flags->do_time ?
fmt_elapsedtime(flags, elapsed) : "");
} else if (flags->do_time) {
printf("%s *** %s%s%s: %s\n",
fmt_date(flags, sync_start, tstart),
GREEN, p, NORM,
fmt_elapsedtime(flags, elapsed));
} else {
printf("%s *** %s%s%s\n",
fmt_date(flags, sync_start, tstart),
GREEN, p, NORM);
}
sync_start = 0; /* reset */
}
} else if (flags->do_merge && (
strpfx(p, " >>> emerge (") == 0 ||
strpfx(p, " ::: completed emerge (") == 0))
{
/* merge start/stop (including potential unmerge of old pkg) */
if (p[3] == '>') { /* >>> emerge */
char *id;
q = strchr(p + 14, ')');
if (q == NULL)
continue;
/* keep a copy of the relevant string in case we need to
* match this */
id = p + 6;
q += 2; /* ") " */
p = strchr(q, ' ');
if (p == NULL)
continue;
*p = '\0';
atom = atom_explode(q);
*p = ' ';
if (atom == NULL)
continue;
/* see if we need this atom */
atomw = NULL;
if (atomset == NULL) {
/* match without revisions when we try to predict,
* such that our set remains rich enough to cover
* various predictions */
array_for_each(atoms, i, atomw) {
if (atom_compare_flg(atom, atomw,
flags->do_predict
? ATOM_COMP_NOREV
: ATOM_COMP_DEFAULT) == EQUAL)
break;
atomw = NULL;
}
} else {
snprintf(afmt, sizeof(afmt), "%s/%s",
atom->CATEGORY, atom->PN);
atomw = get_set(afmt, atomset);
}
if (atomw == NULL) {
atom_implode(atom);
continue;
}
pkg = xmalloc(sizeof(struct pkg_match));
snprintf(pkg->id, sizeof(pkg->id), "%s", id);
pkg->atom = atom;
pkg->tbegin = tstart;
pkg->time = (time_t)0;
pkg->cnt = 0;
xarraypush_ptr(merge_matches, pkg);
} else { /* ::: completed */
array_for_each_rev(merge_matches, i, pkgw) {
if (strcmp(p + 16, pkgw->id) != 0)
continue;
/* found, do report */
elapsed = tstart - pkgw->tbegin;
if (flags->do_average || flags->do_predict
|| flags->do_running)
{
/* find in list of averages */
if (flags->do_predict ||
(verbose && !flags->do_running))
{
snprintf(afmt, sizeof(afmt), "%s/%s",
pkgw->atom->CATEGORY, pkgw->atom->PF);
} else {
snprintf(afmt, sizeof(afmt), "%s/%s",
pkgw->atom->CATEGORY, pkgw->atom->PN);
}
merge_averages =
add_set_value(afmt, pkgw,
(void **)&pkg, merge_averages);
if (pkg != NULL) {
pkg->cnt++;
pkg->time += elapsed;
/* store max time for do_running */
if (elapsed > pkg->tbegin)
pkg->tbegin = elapsed;
atom_implode(pkgw->atom);
xarraydelete(merge_matches, i);
} else {
/* new entry */
pkgw->id[0] = '\0';
pkgw->cnt = 1;
pkgw->time = elapsed;
pkgw->tbegin = elapsed;
xarraydelete_ptr(merge_matches, i);
}
break;
}
if (quiet && !flags->do_average) {
printf("%s%s%s\n",
atom_format(flags->fmt, pkgw->atom),
flags->do_time ? ": " : "",
flags->do_time ?
fmt_elapsedtime(flags, elapsed) : "");
} else if (flags->show_emerge) {
int state = NOT_EQUAL;
if (upgrade_atom != NULL)
state = atom_compare(pkgw->atom, upgrade_atom);
switch (state) {
/* "NRUD " */
case EQUAL:
printf(" %sR%s ", YELLOW, NORM);
break;
case NOT_EQUAL:
printf("%sN%s ", GREEN, NORM);
break;
case NEWER:
printf(" %sU%s ", BLUE, NORM);
break;
case OLDER:
printf(" %sU%sD%s ", BLUE, DKBLUE, NORM);
break;
}
printf("%s", atom_format("%[CAT]%[PF]", pkgw->atom));
if (state == NEWER || state == OLDER)
printf(" %s[%s%s%s]%s", DKBLUE, NORM,
atom_format("%[PVR]", upgrade_atom),
DKBLUE, NORM);
if (flags->do_time)
printf(": %s\n", fmt_elapsedtime(flags, elapsed));
else
printf("\n");
} else if (flags->do_time) {
printf("%s >>> %s: %s\n",
fmt_date(flags, pkgw->tbegin, tstart),
atom_format(flags->fmt, pkgw->atom),
fmt_elapsedtime(flags, elapsed));
} else if (!flags->do_average) {
printf("%s >>> %s\n",
fmt_date(flags, pkgw->tbegin, tstart),
atom_format(flags->fmt, pkgw->atom));
}
atom_implode(pkgw->atom);
xarraydelete(merge_matches, i);
break;
}
}
} else if (
(flags->do_unmerge &&
strpfx(p, " === Unmerging... (") == 0) ||
(flags->do_autoclean &&
strpfx(p, " === Unmerging... (") == 0) ||
((flags->do_unmerge || flags->do_autoclean) &&
strpfx(p, " >>> unmerge success: ") == 0))
{
/* unmerge action */
if (p[2] == '=') {
if (p[1] == ' ')
p++;
p += 19;
if ((q = strchr(p, ')')) == NULL)
continue;
*q = '\0';
atom = atom_explode(p);
if (atom == NULL)
continue;
if (p[-20] == ' ' && flags->show_emerge)
{
if (upgrade_atom != NULL)
atom_implode(upgrade_atom);
upgrade_atom = atom;
continue;
}
/* see if we need this atom */
atomw = NULL;
if (atomset == NULL) {
array_for_each(atoms, i, atomw) {
if (atom_compare(atom, atomw) == EQUAL)
break;
atomw = NULL;
}
} else {
snprintf(afmt, sizeof(afmt), "%s/%s",
atom->CATEGORY, atom->PN);
atomw = get_set(afmt, atomset);
}
if (atomw == NULL) {
atom_implode(atom);
continue;
}
pkg = xmalloc(sizeof(struct pkg_match));
snprintf(pkg->id, sizeof(pkg->id), "%s\n", p); /* \n !!! */
pkg->atom = atom;
pkg->tbegin = tstart;
pkg->time = (time_t)0;
pkg->cnt = 0;
xarraypush_ptr(unmerge_matches, pkg);
} else {
array_for_each_rev(unmerge_matches, i, pkgw) {
if (strcmp(p + 23, pkgw->id) != 0)
continue;
/* found, do report */
elapsed = tstart - pkgw->tbegin;
if (flags->do_average || flags->do_predict
|| flags->do_running)
{
/* find in list of averages */
if (flags->do_predict ||
(verbose && !flags->do_running))
{
snprintf(afmt, sizeof(afmt), "%s/%s",
pkgw->atom->CATEGORY, pkgw->atom->PF);
} else {
snprintf(afmt, sizeof(afmt), "%s/%s",
pkgw->atom->CATEGORY, pkgw->atom->PN);
}
unmerge_averages =
add_set_value(afmt, pkgw,
(void **)&pkg, unmerge_averages);
if (pkg != NULL) {
pkg->cnt++;
pkg->time += elapsed;
/* store max time for do_running */
if (elapsed > pkg->tbegin)
pkg->tbegin = elapsed;
atom_implode(pkgw->atom);
xarraydelete(unmerge_matches, i);
} else {
/* new entry */
pkgw->id[0] = '\0';
pkgw->cnt = 1;
pkgw->time = elapsed;
pkgw->tbegin = elapsed;
xarraydelete_ptr(unmerge_matches, i);
}
break;
}
if (quiet && !flags->do_average) {
printf("%s%s%s\n",
atom_format(flags->fmt, pkgw->atom),
flags->do_time ? ": " : "",
flags->do_time ?
fmt_elapsedtime(flags, elapsed) : "");
} else if (flags->show_emerge) {
/* emerge never lists packages it unmerges, only
* when part of installation of another package,
* so there is nothing to follow here. We could
* use D(elete), R(emove) or (U)nmerge here,
* which all three are already in use. I've
* chosen to use D in the first column here,
* because D is only used together with U, so it
* is the only distinquishable choice, appearing
* in the place of N(ew). */
printf("%sD%s %s", RED, NORM,
atom_format("%[CAT]%[PF]", pkgw->atom));
if (flags->do_time)
printf(": %s\n", fmt_elapsedtime(flags, elapsed));
else
printf("\n");
} else if (flags->do_time) {