-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
1257 lines (1114 loc) · 33.4 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
/* $Id$ */
/*
* Copyright (c) 2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <err.h>
#include <inttypes.h>
#include <math.h> /* floor */
#include <md5.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <kcgi.h>
#include <kcgihtml.h>
#include "extern.h"
#ifndef REPO_BASE
#define REPO_BASE "https://github.com/kristapsdz"
#endif
#ifndef COMMIT_BASE
#define COMMIT_BASE REPO_BASE
#endif
enum page {
PAGE_INDEX,
PAGE__MAX
};
/*
* Passed to each iterated row of listing.
*/
struct req {
struct kreq *r;
struct khtmlreq html;
char *nhash; /* newest hash of the set */
int checkhash; /* mark newest hash */
};
static const char *const pages[PAGE__MAX] = {
"index", /* PAGE_INDEX */
};
/*
* When computing the main dashboard, use this structure to winnow out
* statistics for each project.
*/
struct dash {
const struct project *proj; /* project in question */
const char *nhash; /* newest commit hash */
time_t nctime; /* newest ctime of nhash */
size_t finished; /* members of nhash */
size_t success; /* ...and success members */
size_t pending; /* non-members */
};
/*
* Open our HTTP document by emitting all headers.
* If mime isn't KMIME__MAX, use its content type.
* If last is non-zero, use it as the last-modified time.
*/
static void
http_open(struct kreq *r,
enum khttp code, enum kmime mime, time_t last)
{
char datebuf[32];
if (last)
kutil_epoch2str(last, datebuf, sizeof(datebuf));
khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[code]);
if (mime != KMIME__MAX)
khttp_head(r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[mime]);
if (last)
khttp_head(r, kresps[KRESP_LAST_MODIFIED],
"%s", datebuf);
khttp_body(r);
}
/*
* Open our HTML document with the correct document type, HTML envelope,
* and header element.
* This concludes with an open body envelope.
*/
static void
html_open(struct khtmlreq *req, const char *title)
{
khtml_elem(req, KELEM_DOCTYPE);
khtml_elem(req, KELEM_HTML);
khtml_elem(req, KELEM_HEAD);
khtml_elem(req, KELEM_TITLE);
khtml_puts(req, "Minimal CI: ");
khtml_puts(req, title);
khtml_closeelem(req, 1); /* title */
khtml_attr(req, KELEM_META,
KATTR_NAME, "viewport",
KATTR_CONTENT, "width=device-width, initial-scale=1",
KATTR__MAX);
khtml_attr(req, KELEM_META,
KATTR_CHARSET, "utf-8",
KATTR__MAX);
khtml_attrx(req, KELEM_LINK,
KATTR_REL, KATTRX_STRING, "stylesheet",
KATTR_HREF, KATTRX_STRING, "/minci.css",
KATTR__MAX);
khtml_closeelem(req, 1); /* head */
khtml_elem(req, KELEM_BODY);
}
/*
* Format the uname fields to have useful (and short) output.
*/
static void
get_html_uname(struct khtmlreq *req, const struct report *p)
{
khtml_puts(req, p->unames);
khtml_puts(req, " ");
khtml_puts(req, p->unamer);
khtml_puts(req, " ");
khtml_puts(req, p->unamem);
/* This isn't particularly useful information. */
#if 0
khtml_puts(req, "|");
khtml_puts(req, p->unamev);
#endif
}
/*
* Print a block with a time offset of "start" to "given".
* If "given" is zero, suppress any content printing.
*/
static void
get_html_offs(struct khtmlreq *req,
const char *classes, int64_t start, int64_t given)
{
khtml_attr(req, KELEM_DIV,
KATTR_CLASS, classes, KATTR__MAX);
if (given != 0) {
khtml_attrx(req, KELEM_TIME,
KATTR_CLASS, KATTRX_STRING, "success",
KATTR_DATETIME, KATTRX_INT, given,
KATTR__MAX);
khtml_int(req, given - start);
khtml_closeelem(req, 1); /* time */
} else {
khtml_attr(req, KELEM_SPAN,
KATTR_CLASS, "fail", KATTR__MAX);
khtml_closeelem(req, 1); /* span */
}
khtml_closeelem(req, 1); /* div */
}
/*
* Print the first row of a report table.
*/
static void
get_html_last_header(struct khtmlreq *req)
{
khtml_attr(req, KELEM_DIV,
KATTR_CLASS, "row", KATTR__MAX);
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-passfail", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-id", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-commit", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-start", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head project-name", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-system", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV,
KATTR_CLASS, "cellgroup", KATTR__MAX);
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-env", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-deps", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-build", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-regress", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-install", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_attr(req, KELEM_DIV, KATTR_CLASS,
"head report-dist", KATTR__MAX);
khtml_closeelem(req, 1); /* cell */
khtml_closeelem(req, 1); /* cellgroup */
khtml_closeelem(req, 1); /* row */
}
/*
* Print a record as it would appear in an HTML table.
*/
static void
get_html_last_report(const struct report *p, void *arg)
{
struct req *r = arg;
struct tm tm;
int64_t date;
char *urlid, *urlproj, *urldate, *urlcommit,
*urluname;
char commitshort[8];
if (r->nhash == NULL &&
r->checkhash && p->fetchhead[0] != '\0')
r->nhash = kstrdup(p->fetchhead);
memset(&tm, 0, sizeof(struct tm));
KUTIL_EPOCH2TM(p->ctime, &tm);
date = kutil_date2epoch
(tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
urlid = khttp_urlpartx(r->r->pname,
ksuffixes[KMIME_TEXT_HTML],
pages[PAGE_INDEX],
valid_keys[VALID_REPORT_ID].name,
KATTRX_INT, p->id, NULL);
urlproj = khttp_urlpartx(r->r->pname,
ksuffixes[KMIME_TEXT_HTML],
pages[PAGE_INDEX],
valid_keys[VALID_PROJECT_NAME].name,
KATTRX_STRING, p->project.name, NULL);
urldate = khttp_urlpartx(r->r->pname,
ksuffixes[KMIME_TEXT_HTML],
pages[PAGE_INDEX],
valid_keys[VALID_REPORT_CTIME].name,
KATTRX_INT, date, NULL);
kasprintf(&urlcommit, "%s/%s/tree/%s",
COMMIT_BASE, p->project.name,
p->fetchhead);
urluname = khttp_urlpart(r->r->pname,
ksuffixes[KMIME_TEXT_HTML],
pages[PAGE_INDEX],
valid_keys[VALID_REPORT_UNAMEHASH].name,
p->unamehash, NULL);
memset(&tm, 0, sizeof(struct tm));
KUTIL_EPOCH2TM(p->start, &tm);
if (r->nhash != NULL &&
strcmp(r->nhash, p->fetchhead))
khtml_attr(&r->html, KELEM_DIV,
KATTR_CLASS, "row notnewest", KATTR__MAX);
else
khtml_attr(&r->html, KELEM_DIV,
KATTR_CLASS, "row", KATTR__MAX);
khtml_attr(&r->html, KELEM_DIV, KATTR_CLASS,
"cell report-passfail", KATTR__MAX);
khtml_attr(&r->html, KELEM_SPAN, KATTR_CLASS,
p->distcheck ? "report-pass" : "report-fail",
KATTR__MAX);
khtml_ncr(&r->html, p->distcheck ? 0x2714 : 0x2717);
khtml_closeelem(&r->html, 1); /* span */
khtml_closeelem(&r->html, 1); /* cell */
khtml_attr(&r->html, KELEM_DIV, KATTR_CLASS,
"cell report-id", KATTR__MAX);
khtml_attr(&r->html, KELEM_A,
KATTR_HREF, urlid, KATTR__MAX);
if (p->id < 1000)
khtml_int(&r->html, 0);
if (p->id < 100)
khtml_int(&r->html, 0);
if (p->id < 10)
khtml_int(&r->html, 0);
khtml_int(&r->html, p->id);
khtml_closeelem(&r->html, 1); /* link */
khtml_closeelem(&r->html, 1); /* cell */
khtml_attr(&r->html, KELEM_DIV, KATTR_CLASS,
"cell report-commit", KATTR__MAX);
khtml_attr(&r->html, KELEM_A, KATTR_HREF,
urlcommit, KATTR__MAX);
strlcpy(commitshort, p->fetchhead, sizeof(commitshort));
khtml_puts(&r->html, commitshort);
khtml_closeelem(&r->html, 1); /* a */
khtml_closeelem(&r->html, 1); /* cell */
khtml_attr(&r->html, KELEM_DIV, KATTR_CLASS,
"cell report-start", KATTR__MAX);
khtml_attr(&r->html, KELEM_A,
KATTR_HREF, urldate, KATTR__MAX);
khtml_attrx(&r->html, KELEM_TIME,
KATTR_DATETIME, KATTRX_INT, p->start, KATTR__MAX);
khtml_int(&r->html, tm.tm_year + 1900);
khtml_puts(&r->html, "-");
if (tm.tm_mon < 9)
khtml_int(&r->html, 0);
khtml_int(&r->html, tm.tm_mon + 1);
khtml_puts(&r->html, "-");
if (tm.tm_mday < 10)
khtml_int(&r->html, 0);
khtml_int(&r->html, tm.tm_mday);
khtml_closeelem(&r->html, 1); /* time */
khtml_closeelem(&r->html, 1); /* a */
khtml_closeelem(&r->html, 1); /* cell */
khtml_attr(&r->html, KELEM_DIV, KATTR_CLASS,
"cell project-name", KATTR__MAX);
khtml_attr(&r->html, KELEM_A,
KATTR_HREF, urlproj, KATTR__MAX);
khtml_puts(&r->html, p->project.name);
khtml_closeelem(&r->html, 1); /* a */
khtml_closeelem(&r->html, 1); /* cell */
khtml_attr(&r->html, KELEM_DIV, KATTR_CLASS,
"cell report-system", KATTR__MAX);
khtml_attr(&r->html, KELEM_A,
KATTR_HREF, urluname, KATTR__MAX);
get_html_uname(&r->html, p);
khtml_closeelem(&r->html, 1); /* a */
khtml_closeelem(&r->html, 1); /* cell */
khtml_attr(&r->html, KELEM_DIV,
KATTR_CLASS, "cellgroup", KATTR__MAX);
get_html_offs(&r->html, "cell "
"report-env", p->start, p->env);
get_html_offs(&r->html, "cell "
"report-deps", p->env, p->depend);
get_html_offs(&r->html, "cell "
"report-build", p->depend, p->build);
get_html_offs(&r->html, "cell "
"report-regress", p->build, p->test);
get_html_offs(&r->html, "cell "
"report-install", p->test, p->install);
get_html_offs(&r->html, "cell "
"report-dist", p->install, p->distcheck);
khtml_closeelem(&r->html, 1); /* cellgroup */
khtml_closeelem(&r->html, 1); /* row */
free(urlid);
free(urlproj);
free(urldate);
free(urlcommit);
free(urluname);
}
/*
* Output only the log (which may be zero-length).
*/
static void
get_single_text(struct kreq *r, const struct report *p)
{
khttp_puts(r, p->log);
}
/*
* List a single record as text/html.
*/
static void
get_single_html(struct kreq *r, const struct report *p)
{
struct khtmlreq req;
char buf[64];
char commitshort[8];
char *url = NULL, *urlcommit, *urlproj, *urluname;
const char *cp;
size_t count;
urlproj = khttp_urlpartx(r->pname,
ksuffixes[KMIME_TEXT_HTML],
pages[PAGE_INDEX],
valid_keys[VALID_PROJECT_NAME].name,
KATTRX_STRING, p->project.name, NULL);
kasprintf(&urlcommit, "%s/%s/tree/%s",
COMMIT_BASE, p->project.name,
p->fetchhead);
urluname = khttp_urlpart(r->pname,
ksuffixes[KMIME_TEXT_HTML],
pages[PAGE_INDEX],
valid_keys[VALID_REPORT_UNAMEHASH].name,
p->unamehash, NULL);
khtml_open(&req, r, 0);
kcgi_writer_disable(r);
html_open(&req, "Report");
/* Heading. */
khtml_elem(&req, KELEM_HEADER);
khtml_attr(&req, KELEM_H1,
KATTR_CLASS, "singleton", KATTR__MAX);
khtml_attr(&req, KELEM_A,
KATTR_HREF, "index.html", KATTR__MAX);
khtml_puts(&req, "Dashboard");
khtml_closeelem(&req, 1); /* a */
khtml_ncr(&req, 0x203a);
khtml_elem(&req, KELEM_SPAN);
khtml_puts(&req, "Reports");
khtml_closeelem(&req, 1); /* span */
khtml_ncr(&req, 0x203a);
khtml_attr(&req, KELEM_SPAN,
KATTR_CLASS, "report-id", KATTR__MAX);
if (p->id < 1000)
khtml_int(&req, 0);
if (p->id < 100)
khtml_int(&req, 0);
if (p->id < 10)
khtml_int(&req, 0);
khtml_int(&req, p->id);
khtml_closeelem(&req, 1); /* span */
khtml_closeelem(&req, 1); /* h1 */
khtml_closeelem(&req, 1); /* header */
/* Body. */
khtml_attr(&req, KELEM_DIV,
KATTR_CLASS, "singleton", KATTR__MAX);
khtml_attr(&req, KELEM_SPAN, KATTR_CLASS,
"lefthead report-id", KATTR__MAX);
if (p->id < 1000)
khtml_int(&req, 0);
if (p->id < 100)
khtml_int(&req, 0);
if (p->id < 10)
khtml_int(&req, 0);
khtml_int(&req, p->id);
khtml_closeelem(&req, 1); /* div */
khtml_attr(&req, KELEM_SPAN, KATTR_CLASS,
"lefthead project-name", KATTR__MAX);
khtml_attr(&req, KELEM_A, KATTR_HREF, urlproj, KATTR__MAX);
khtml_puts(&req, p->project.name);
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* span */
khtml_attr(&req, KELEM_SPAN, KATTR_CLASS,
"lefthead project-repo", KATTR__MAX);
khtml_attr(&req, KELEM_A, KATTR_CLASS,
"lefthead report-commit",
KATTR_HREF, urlcommit, KATTR__MAX);
strlcpy(commitshort, p->fetchhead, sizeof(commitshort));
khtml_puts(&req, commitshort);
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* span */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"lefthead report-start", KATTR__MAX);
khtml_attrx(&req, KELEM_TIME,
KATTR_DATETIME, KATTRX_INT,
p->start, KATTR__MAX);
kutil_epoch2str(p->start, buf, sizeof(buf));
khtml_puts(&req, buf);
khtml_closeelem(&req, 1); /* time */
khtml_closeelem(&req, 1); /* div */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"lefthead report-system", KATTR__MAX);
khtml_attr(&req, KELEM_A,
KATTR_HREF, urluname, KATTR__MAX);
get_html_uname(&req, p);
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* div */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"lefthead report-system-ext", KATTR__MAX);
khtml_puts(&req, p->unamev);
khtml_closeelem(&req, 1); /* div */
khtml_attr(&req, KELEM_DIV,
KATTR_CLASS, "leftgroup", KATTR__MAX);
get_html_offs(&req, "lefthead "
"report-env", p->start, p->env);
get_html_offs(&req, "lefthead "
"report-deps", p->env, p->depend);
get_html_offs(&req, "lefthead "
"report-build", p->depend, p->build);
get_html_offs(&req, "lefthead "
"report-regress", p->build, p->test);
get_html_offs(&req, "lefthead "
"report-install", p->test, p->install);
get_html_offs(&req, "lefthead "
"report-dist", p->install, p->distcheck);
khtml_closeelem(&req, 1); /* div */
if (p->distcheck == 0)
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"report-failure", KATTR__MAX);
else
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"report-success", KATTR__MAX);
khtml_closeelem(&req, 1); /* div */
/* Emit the log tail only if it's non-empty. */
if (p->log[0] != '\0') {
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"report-log-box", KATTR__MAX);
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"report-log", KATTR__MAX);
count = 0;
cp = p->log + strlen(p->log);
while (cp > p->log) {
if (*cp == '\n' && count++ == 16) {
cp++;
break;
}
cp--;
}
khtml_puts(&req, cp);
khtml_closeelem(&req, 1); /* div */
url = khttp_urlpartx(r->pname,
ksuffixes[KMIME_TEXT_PLAIN],
pages[PAGE_INDEX],
valid_keys[VALID_REPORT_ID].name,
KATTRX_INT, p->id, NULL);
khtml_attr(&req, KELEM_A,
KATTR_CLASS, "report-log-link",
KATTR_HREF, url, KATTR__MAX);
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* div */
}
khtml_closeelem(&req, 1); /* div */
khtml_elem(&req, KELEM_FOOTER);
khtml_attr(&req, KELEM_A,
KATTR_HREF, REPO_BASE "/minci", KATTR__MAX);
khtml_puts(&req, "minci");
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* footer */
khtml_closeelem(&req, 1); /* body */
khtml_closeelem(&req, 1); /* html */
khtml_close(&req);
free(url);
free(urlproj);
free(urluname);
free(urlcommit);
}
/*
* Routes a record to its MIME type output.
* Outputs HTTP 404 (error) or 200 (success).
*/
static void
get_single(struct kreq *r, time_t mtime)
{
struct report *p;
struct kpair *kp;
kp = r->fieldmap[VALID_REPORT_ID];
assert(kp != NULL);
p = db_report_get_byid(r->arg,
kp->parsed.i); /* id */
if (p == NULL) {
http_open(r, KHTTP_404, KMIME__MAX, mtime);
return;
}
/* Emit either our log or the full HTML record. */
http_open(r, KHTTP_200, r->mime, mtime);
if (r->mime == KMIME_TEXT_PLAIN)
get_single_text(r, p);
else
get_single_html(r, p);
db_report_free(p);
}
/*
* List the last *n* records, sorted by time of accept.
* Always outputs HTTP 200.
*/
static void
get_dash(struct kreq *r, time_t mtime)
{
struct khtmlreq req;
struct report_q *rq;
struct report *rn;
struct dash *dash = NULL, *curdash;
size_t i, dashsz = 0, maxdone = 0;
struct tm tm;
char *urlproj, *urlcommit;
char datebuf[32], commitshort[8];
/* Open output page. */
http_open(r, KHTTP_200, r->mime, mtime);
khtml_open(&req, r, 0);
kcgi_writer_disable(r);
html_open(&req, "Reports");
/* Output header. */
khtml_elem(&req, KELEM_HEADER);
khtml_attr(&req, KELEM_H1,
KATTR_CLASS, "table", KATTR__MAX);
khtml_elem(&req, KELEM_SPAN);
khtml_puts(&req, "Dashboard");
khtml_closeelem(&req, 1); /* span */
khtml_ncr(&req, 0x203a);
khtml_elem(&req, KELEM_SPAN);
khtml_puts(&req, "All Projects");
khtml_closeelem(&req, 1); /* span */
khtml_closeelem(&req, 1); /* h1 */
khtml_closeelem(&req, 1); /* header */
/* Output data. */
khtml_attr(&req, KELEM_DIV,
KATTR_CLASS, "table alltable", KATTR__MAX);
rq = db_report_list_dash(r->arg);
/* Establish the newest report hash. */
TAILQ_FOREACH(rn, rq, _entries) {
for (i = 0; i < dashsz; i++)
if (dash[i].proj->id == rn->projectid)
break;
if (i == dashsz) {
dash = kreallocarray(dash,
dashsz + 1, sizeof(struct dash));
dashsz++;
curdash = &dash[i];
memset(curdash, 0, sizeof(struct dash));
curdash->proj = &rn->project;
curdash->nhash = rn->fetchhead;
curdash->nctime = rn->ctime;
} else
curdash = &dash[i];
if (rn->ctime > curdash->nctime) {
curdash->nhash = rn->fetchhead;
curdash->nctime = rn->ctime;
}
}
/*
* Compute how many have completed the newest hash.
* The empty hash is always considered old.
*/
TAILQ_FOREACH(rn, rq, _entries) {
for (i = 0; i < dashsz; i++)
if (dash[i].proj->id == rn->projectid)
break;
assert(i < dashsz);
if (dash[i].nhash[0] != '\0' &&
strcmp(rn->fetchhead, dash[i].nhash) == 0) {
if (++dash[i].finished > maxdone)
maxdone = dash[i].finished;
dash[i].success += rn->distcheck != 0;
} else {
dash[i].pending++;
}
}
/* Header row. */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"row", KATTR__MAX);
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"head report-successrate", KATTR__MAX);
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"head project-name", KATTR__MAX);
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"head report-finished-pct", KATTR__MAX);
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"head report-pending", KATTR__MAX);
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"head report-newest", KATTR__MAX);
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"head report-commit", KATTR__MAX);
khtml_closeelem(&req, 1); /* cell */
khtml_closeelem(&req, 1); /* row */
/* Now each project's data. */
for (i = 0; i < dashsz; i++) {
urlproj = khttp_urlpartx(r->pname,
ksuffixes[KMIME_TEXT_HTML],
pages[PAGE_INDEX],
valid_keys[VALID_PROJECT_NAME].name,
KATTRX_STRING, dash[i].proj->name, NULL);
kasprintf(&urlcommit, "%s/%s/tree/%s",
COMMIT_BASE, dash[i].proj->name,
dash[i].nhash);
assert(dash[i].finished + dash[i].pending > 0);
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"row", KATTR__MAX);
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"cell report-successrate", KATTR__MAX);
khtml_attr(&req, KELEM_SPAN, KATTR_CLASS,
dash[i].success == dash[i].finished ?
"report-pass" : "report-fail",
KATTR__MAX);
khtml_int(&req,
dash[i].finished == 0 ? 0 : floor
(100 * dash[i].success / dash[i].finished));
khtml_closeelem(&req, 1); /* span */
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"cell project-name", KATTR__MAX);
khtml_attr(&req, KELEM_A, KATTR_HREF,
urlproj, KATTR__MAX);
khtml_puts(&req, dash[i].proj->name);
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"cell report-finished-pct", KATTR__MAX);
khtml_int(&req, floor
(100 * dash[i].finished / maxdone));
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"cell report-pending", KATTR__MAX);
khtml_int(&req, dash[i].finished);
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"cell report-newest", KATTR__MAX);
gmtime_r(&dash[i].nctime, &tm);
strftime(datebuf, sizeof(datebuf), "%F %T", &tm);
khtml_puts(&req, datebuf);
khtml_closeelem(&req, 1); /* cell */
khtml_attr(&req, KELEM_DIV, KATTR_CLASS,
"cell report-commit", KATTR__MAX);
khtml_attr(&req, KELEM_A, KATTR_HREF,
urlcommit, KATTR__MAX);
strlcpy(commitshort, dash[i].nhash, sizeof(commitshort));
khtml_puts(&req, commitshort);
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* cell */
khtml_closeelem(&req, 1); /* row */
free(urlproj);
free(urlcommit);
}
khtml_closeelem(&req, 1); /* table */
khtml_elem(&req, KELEM_FOOTER);
khtml_attr(&req, KELEM_A,
KATTR_HREF, REPO_BASE "/minci", KATTR__MAX);
khtml_puts(&req, "minci");
khtml_closeelem(&req, 1); /* a */
khtml_closeelem(&req, 1); /* footer */
khtml_closeelem(&req, 1); /* body */
khtml_closeelem(&req, 1); /* html */
khtml_close(&req);
db_report_freeq(rq);
free(dash);
}
/*
* List the last *n* records, sorted by time of accept.
* Always outputs HTTP 200.
*/
static void
get_last(struct kreq *r, time_t mtime)
{
struct req req;
struct kpair *kpn, *kpd, *kph;
time_t t;
struct tm tm;
char datebuf[32];
memset(&req, 0, sizeof(struct req));
kpn = r->fieldmap[VALID_PROJECT_NAME];
kpd = r->fieldmap[VALID_REPORT_CTIME];
kph = r->fieldmap[VALID_REPORT_UNAMEHASH];
assert(kpn != NULL || kpd != NULL || kph != NULL);
/* Open output page. */
http_open(r, KHTTP_200, r->mime, mtime);
req.r = r;
khtml_open(&req.html, r, 0);
kcgi_writer_disable(r);
html_open(&req.html, "Reports");
/* Output header. */
khtml_elem(&req.html, KELEM_HEADER);
khtml_attr(&req.html, KELEM_H1,
KATTR_CLASS, "table", KATTR__MAX);
if (kpn != NULL) {
khtml_attr(&req.html, KELEM_A,
KATTR_HREF, "index.html", KATTR__MAX);
khtml_puts(&req.html, "Dashboard");
khtml_closeelem(&req.html, 1); /* a */
khtml_ncr(&req.html, 0x203a);
khtml_elem(&req.html, KELEM_SPAN);
khtml_puts(&req.html, kpn->parsed.s);
khtml_closeelem(&req.html, 1); /* span */
khtml_closeelem(&req.html, 1); /* h1 */
req.checkhash = 1;
} else if (kph != NULL) {
khtml_attr(&req.html, KELEM_A,
KATTR_HREF, "index.html", KATTR__MAX);
khtml_puts(&req.html, "Dashboard");
khtml_closeelem(&req.html, 1); /* a */
khtml_ncr(&req.html, 0x203a);
khtml_elem(&req.html, KELEM_SPAN);
khtml_puts(&req.html, "Machine Dashboard");
khtml_closeelem(&req.html, 1); /* span */
khtml_closeelem(&req.html, 1); /* h1 */
} else {
khtml_attr(&req.html, KELEM_A,
KATTR_HREF, "index.html", KATTR__MAX);
khtml_puts(&req.html, "Dashboard");
khtml_closeelem(&req.html, 1); /* a */
khtml_ncr(&req.html, 0x203a);
khtml_elem(&req.html, KELEM_SPAN);
t = kpd->parsed.i;
gmtime_r(&t, &tm);
strftime(datebuf, sizeof(datebuf), "%F", &tm);
khtml_puts(&req.html, datebuf);
khtml_closeelem(&req.html, 1); /* span */
khtml_closeelem(&req.html, 1); /* h1 */
}
khtml_closeelem(&req.html, 1); /* h1 */
khtml_closeelem(&req.html, 1); /* header */
/* Output data. */
if (kpn != NULL)
khtml_attr(&req.html, KELEM_DIV,
KATTR_CLASS, "table projtable", KATTR__MAX);
else if (kph != NULL)
khtml_attr(&req.html, KELEM_DIV,
KATTR_CLASS, "table unametable", KATTR__MAX);
else
khtml_attr(&req.html, KELEM_DIV,
KATTR_CLASS, "table datetable", KATTR__MAX);
get_html_last_header(&req.html);
if (kpn != NULL)
db_report_iterate_dashname(r->arg,
get_html_last_report, &req,
kpn->parsed.s); /* project.name */
else if (kph != NULL)
db_report_iterate_dashuname(r->arg,
get_html_last_report, &req,
kph->parsed.s); /* report.unamehash */
else
db_report_iterate_lastdate(r->arg,
get_html_last_report, &req,
kpd->parsed.i, /* ctime ge */
kpd->parsed.i + 86400); /* ctime le */
khtml_closeelem(&req.html, 1); /* table */
khtml_elem(&req.html, KELEM_FOOTER);
khtml_attr(&req.html, KELEM_A,
KATTR_HREF, REPO_BASE "/minci", KATTR__MAX);
khtml_puts(&req.html, "minci");
khtml_closeelem(&req.html, 1); /* a */
khtml_closeelem(&req.html, 1); /* footer */
khtml_closeelem(&req.html, 1); /* body */
khtml_closeelem(&req.html, 1); /* html */
khtml_close(&req.html);
free(req.nhash);
}
/*
* List one or more records.
*/
static void
get(struct kreq *r, time_t mtime)
{
if (r->fieldmap[VALID_REPORT_ID] != NULL)
get_single(r, mtime);
else if (r->fieldmap[VALID_PROJECT_NAME] != NULL)
get_last(r, mtime);
else if (r->fieldmap[VALID_REPORT_UNAMEHASH] != NULL)
get_last(r, mtime);
else if (r->fieldmap[VALID_REPORT_CTIME] != NULL)
get_last(r, mtime);
else
get_dash(r, mtime);
}
/*
* Process a record submission.
* Records are signed into a non-ORT field "signature".
* This performs all sanity checks: failure is sequentially consistent,
* timestamps are increasing, etc.
* It outputs only HTTP 403 (error) and 201 (success).
*/
static void
post(struct kreq *r)
{
struct project *proj = NULL;
struct user *user = NULL;
struct kpair *kps, *kpe, *kpd, *kpb, *kpt,
*kpi, *kpc, *kpn, *kpl, *sig,
*kpu, *kpum, *kpun, *kpur, *kpus,
*kpuv, *kpf;
size_t i, sz;
MD5_CTX ctx;
char *buf = NULL;
char digest[MD5_DIGEST_STRING_LENGTH],
unamedigest[MD5_DIGEST_STRING_LENGTH],
projunamedigest[MD5_DIGEST_STRING_LENGTH],
logdigest[MD5_DIGEST_STRING_LENGTH];
/*
* Check our non-ORT signature field was given.
* It must be a 32-byte string.
*/
for (sig = NULL, i = 0; i < r->fieldsz; i++)
if (strcmp(r->fields[i].key, "signature") == 0 &&
kvalid_stringne(&r->fields[i]) &&
r->fields[i].valsz == 32) {
sig = &r->fields[i];
break;
}
/* Check our ORT fields were given. */
if (sig == NULL ||
(kpn = r->fieldmap[VALID_PROJECT_NAME]) == NULL ||
(kpd = r->fieldmap[VALID_REPORT_DEPEND]) == NULL ||
(kpc = r->fieldmap[VALID_REPORT_DISTCHECK]) == NULL ||
(kpe = r->fieldmap[VALID_REPORT_ENV]) == NULL ||
(kpf = r->fieldmap[VALID_REPORT_FETCHHEAD]) == NULL ||
(kpi = r->fieldmap[VALID_REPORT_INSTALL]) == NULL ||
(kpl = r->fieldmap[VALID_REPORT_LOG]) == NULL ||
(kps = r->fieldmap[VALID_REPORT_START]) == NULL ||
(kpb = r->fieldmap[VALID_REPORT_BUILD]) == NULL ||
(kpt = r->fieldmap[VALID_REPORT_TEST]) == NULL ||
(kpum = r->fieldmap[VALID_REPORT_UNAMEM]) == NULL ||
(kpun = r->fieldmap[VALID_REPORT_UNAMEN]) == NULL ||
(kpur = r->fieldmap[VALID_REPORT_UNAMER]) == NULL ||
(kpus = r->fieldmap[VALID_REPORT_UNAMES]) == NULL ||
(kpuv = r->fieldmap[VALID_REPORT_UNAMEV]) == NULL ||
(kpu = r->fieldmap[VALID_USER_APIKEY]) == NULL) {
kutil_warnx(r, NULL, "invalid request");
http_open(r, KHTTP_403, KMIME__MAX, 0);
goto out;
}
/*
* Check that if stages fail, subsequent must also fail.
* Also, the log should only be specified on failure.
*/
if ((kpe->parsed.i == 0 &&