2 * html.c: implementation of html.h.
15 size_t buflen
, bufsize
;
17 unsigned long long totalsize
, oldest
, newest
;
21 const char *format
, *rootpage
;
22 unsigned long long thresholds
[MAXCOLOUR
];
23 char *titletexts
[MAXCOLOUR
+1];
27 static void vhtprintf(struct html
*ctx
, const char *fmt
, va_list ap
)
35 * Some C libraries (Solaris, I'm looking at you) don't like
36 * an output buffer size of zero in vsnprintf, but will return
37 * sensible values given any non-zero buffer size. Hence, we
38 * use testbuf to gauge the length of the string.
40 size
= vsnprintf(testbuf
, 1, fmt
, ap2
);
43 if (ctx
->buflen
+ size
>= ctx
->bufsize
) {
44 ctx
->bufsize
= (ctx
->buflen
+ size
) * 3 / 2 + 1024;
45 ctx
->buf
= sresize(ctx
->buf
, ctx
->bufsize
, char);
47 size2
= vsnprintf(ctx
->buf
+ ctx
->buflen
, ctx
->bufsize
- ctx
->buflen
,
49 assert(size
== size2
);
53 static void htprintf(struct html
*ctx
, const char *fmt
, ...)
57 vhtprintf(ctx
, fmt
, ap
);
61 static unsigned long long round_and_format_age(struct html
*ctx
,
62 unsigned long long age
,
63 char *buf
, int direction
)
67 unsigned long long ret
, newret
;
70 static const int minutes
[] = { 5, 10, 15, 30, 45 };
72 tm
= *localtime(&ctx
->now
);
73 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
78 for (i
= 0; i
< lenof(minutes
); i
++) {
79 newret
= ctx
->now
- minutes
[i
] * 60;
80 sprintf(newbuf
, "%d minutes", minutes
[i
]);
87 for (i
= 1; i
< 24; i
++) {
88 newret
= ctx
->now
- i
* (60*60);
89 sprintf(newbuf
, "%d hour%s", i
, i
==1 ?
"" : "s");
96 for (i
= 1; i
< 7; i
++) {
97 newret
= ctx
->now
- i
* (24*60*60);
98 sprintf(newbuf
, "%d day%s", i
, i
==1 ?
"" : "s");
105 for (i
= 1; i
< 4; i
++) {
106 newret
= ctx
->now
- i
* (7*24*60*60);
107 sprintf(newbuf
, "%d week%s", i
, i
==1 ?
"" : "s");
114 for (i
= 1; i
< 11; i
++) {
115 tm2
= tm
; /* structure copy */
116 tm2
.tm_year
= (ym
- i
) / 12;
117 tm2
.tm_mon
= (ym
- i
) % 12;
118 newret
= mktime(&tm2
);
119 sprintf(newbuf
, "%d month%s", i
, i
==1 ?
"" : "s");
127 tm2
= tm
; /* structure copy */
128 tm2
.tm_year
= (ym
- i
*12) / 12;
129 tm2
.tm_mon
= (ym
- i
*12) % 12;
130 newret
= mktime(&tm2
);
131 sprintf(newbuf
, "%d year%s", i
, i
==1 ?
"" : "s");
141 * Round toward newest, i.e. use the existing (buf,ret).
143 } else if (direction
< 0) {
145 * Round toward oldest, i.e. use (newbuf,newret);
153 if (ret
- age
> age
- newret
) {
161 static void get_indices(const void *t
, char *path
,
162 unsigned long *xi1
, unsigned long *xi2
)
164 size_t pathlen
= strlen(path
);
165 int c1
= path
[pathlen
], c2
= (pathlen
> 0 ? path
[pathlen
-1] : 0);
167 *xi1
= trie_before(t
, path
);
168 make_successor(path
);
169 *xi2
= trie_before(t
, path
);
172 path
[pathlen
-1] = c2
;
175 static unsigned long long fetch_size(const void *t
,
176 unsigned long xi1
, unsigned long xi2
,
177 unsigned long long atime
)
179 if (xi2
- xi1
== 1) {
181 * We are querying an individual file, so we should not
182 * depend on the index entries either side of the node,
183 * since they almost certainly don't both exist. Instead,
184 * just look up the file's size and atime in the main trie.
186 const struct trie_file
*f
= trie_getfile(t
, xi1
);
187 if (f
->atime
< atime
)
192 return index_query(t
, xi2
, atime
) - index_query(t
, xi1
, atime
);
196 static void htescape(struct html
*ctx
, const char *s
, int n
, int italics
)
198 while (n
> 0 && *s
) {
199 unsigned char c
= (unsigned char)*s
++;
202 htprintf(ctx
, "&");
204 htprintf(ctx
, "<");
206 htprintf(ctx
, ">");
207 else if (c
>= ' ' && c
< '\177')
208 htprintf(ctx
, "%c", c
);
210 if (italics
) htprintf(ctx
, "<i>");
211 htprintf(ctx
, "[%02x]", c
);
212 if (italics
) htprintf(ctx
, "</i>");
219 static void begin_colour_bar(struct html
*ctx
)
221 htprintf(ctx
, "<table cellspacing=0 cellpadding=0"
222 " style=\"border:0\">\n<tr>\n");
225 static void add_to_colour_bar(struct html
*ctx
, int colour
, int pixels
)
229 if (colour
>= 0 && colour
< 256) /* red -> yellow fade */
230 r
= 255, g
= colour
, b
= 0;
231 else if (colour
>= 256 && colour
<= 511) /* yellow -> green fade */
232 r
= 511 - colour
, g
= 255, b
= 0;
233 else /* background grey */
237 htprintf(ctx
, "<td style=\"width:%dpx; height:1em; "
238 "background-color:#%02x%02x%02x\"",
241 htprintf(ctx
, " title=\"%s\"", ctx
->titletexts
[colour
]);
242 htprintf(ctx
, "></td>\n");
246 static void end_colour_bar(struct html
*ctx
)
248 htprintf(ctx
, "</tr>\n</table>\n");
252 int want_href
, essential
;
254 int literal
; /* should the name be formatted in fixed-pitch? */
256 unsigned long long sizes
[MAXCOLOUR
+1];
259 int vec_compare(const void *av
, const void *bv
)
261 const struct vector
*a
= *(const struct vector
**)av
;
262 const struct vector
*b
= *(const struct vector
**)bv
;
264 if (a
->sizes
[MAXCOLOUR
] > b
->sizes
[MAXCOLOUR
])
266 else if (a
->sizes
[MAXCOLOUR
] < b
->sizes
[MAXCOLOUR
])
268 else if (a
->want_href
< b
->want_href
)
270 else if (a
->want_href
> b
->want_href
)
272 else if (a
->want_href
)
273 return strcmp(a
->name
, b
->name
);
274 else if (a
->index
< b
->index
)
276 else if (a
->index
> b
->index
)
278 else if (a
->essential
< b
->essential
)
280 else if (a
->essential
> b
->essential
)
285 static struct vector
*make_vector(struct html
*ctx
, char *path
,
286 int want_href
, int essential
,
287 char *name
, int literal
)
289 unsigned long xi1
, xi2
;
290 struct vector
*vec
= snew(struct vector
);
293 vec
->want_href
= want_href
;
294 vec
->essential
= essential
;
295 vec
->name
= name ?
dupstr(name
) : NULL
;
296 vec
->literal
= literal
;
298 get_indices(ctx
->t
, path
, &xi1
, &xi2
);
302 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
303 unsigned long long atime
;
307 atime
= ctx
->thresholds
[i
];
308 vec
->sizes
[i
] = fetch_size(ctx
->t
, xi1
, xi2
, atime
);
314 static void print_heading(struct html
*ctx
, const char *title
)
316 htprintf(ctx
, "<tr style=\"padding: 0.2em; background-color:#e0e0e0\">\n"
317 "<td colspan=4 align=center>%s</td>\n</tr>\n", title
);
320 static void compute_display_size(unsigned long long size
,
321 const char **fmt
, double *display_size
)
323 static const char *const fmts
[] = {
324 "%g b", "%g Kb", "%#.1f Mb", "%#.1f Gb", "%#.1f Tb",
325 "%#.1f Pb", "%#.1f Eb", "%#.1f Zb", "%#.1f Yb"
329 while (size
>= 1024 && shift
< lenof(fmts
)-1) {
333 *display_size
= (double)size
;
337 static void make_filename(char *buf
, size_t buflen
,
338 const char *format
, const char *rootpage
,
341 if (index
== 0 && rootpage
)
342 snprintf(buf
, buflen
, "%s", rootpage
);
344 snprintf(buf
, buflen
, format
, index
);
347 #define PIXEL_SIZE 600 /* FIXME: configurability? */
348 static void write_report_line(struct html
*ctx
, struct vector
*vec
)
350 unsigned long long size
, asize
, divisor
;
354 const char *unitsfmt
;
357 * A line with literally zero space usage should not be
358 * printed at all if it's a link to a subdirectory (since it
359 * probably means the whole thing was excluded by some
360 * --exclude-path wildcard). If it's [files] or the top-level
361 * line, though, we must always print _something_, and in that
362 * case we must fiddle about to prevent divisions by zero in
365 if (!vec
->sizes
[MAXCOLOUR
] && !vec
->essential
)
367 divisor
= ctx
->totalsize
;
373 * Find the total size of this subdirectory.
375 size
= vec
->sizes
[MAXCOLOUR
];
376 compute_display_size(size
, &unitsfmt
, &display_size
);
377 htprintf(ctx
, "<tr>\n"
378 "<td style=\"padding: 0.2em; text-align: right\">");
379 htprintf(ctx
, unitsfmt
, display_size
);
380 htprintf(ctx
, "</td>\n");
383 * Generate a colour bar.
385 htprintf(ctx
, "<td style=\"padding: 0.2em\">\n");
386 begin_colour_bar(ctx
);
388 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
389 asize
= vec
->sizes
[i
];
390 newpix
= asize
* PIXEL_SIZE
/ divisor
;
391 add_to_colour_bar(ctx
, i
, newpix
- pix
);
394 add_to_colour_bar(ctx
, -1, PIXEL_SIZE
- pix
);
396 htprintf(ctx
, "</td>\n");
399 * Output size as a percentage of totalsize.
401 htprintf(ctx
, "<td style=\"padding: 0.2em; text-align: right\">"
402 "%.2f%%</td>\n", (double)size
/ divisor
* 100.0);
405 * Output a subdirectory marker.
407 htprintf(ctx
, "<td style=\"padding: 0.2em\">");
411 if (ctx
->format
&& vec
->want_href
) {
412 make_filename(ctx
->href
, ctx
->hreflen
,
413 ctx
->format
, ctx
->rootpage
,
415 htprintf(ctx
, "<a href=\"%s\">", ctx
->href
);
419 htprintf(ctx
, "<code>");
420 htescape(ctx
, vec
->name
, strlen(vec
->name
), 1);
422 htprintf(ctx
, "</code>");
424 htprintf(ctx
, "</a>");
426 htprintf(ctx
, "</td>\n</tr>\n");
429 int strcmptrailingpathsep(const char *a
, const char *b
)
431 while (*a
== *b
&& *a
)
434 if ((*a
== pathsep
&& !a
[1] && !*b
) ||
435 (*b
== pathsep
&& !b
[1] && !*a
))
438 return (int)(unsigned char)*a
- (int)(unsigned char)*b
;
441 char *html_query(const void *t
, unsigned long index
,
442 const struct html_config
*cfg
, int downlink
)
444 struct html actx
, *ctx
= &actx
;
445 char *path
, *path2
, *p
, *q
, *href
;
446 char agebuf1
[80], agebuf2
[80];
447 size_t pathlen
, subdirpos
, hreflen
;
448 unsigned long index2
;
450 struct vector
**vecs
;
452 unsigned long xi1
, xi2
, xj1
, xj2
;
454 if (index
>= trie_count(t
))
458 ctx
->buflen
= ctx
->bufsize
= 0;
460 ctx
->format
= cfg
->format
;
461 ctx
->rootpage
= cfg
->rootpage
;
462 htprintf(ctx
, "<html>\n");
464 path
= snewn(1+trie_maxpathlen(t
), char);
465 ctx
->path2
= path2
= snewn(1+trie_maxpathlen(t
), char);
467 hreflen
= strlen(cfg
->format
) + 100;
468 href
= snewn(hreflen
, char);
473 ctx
->hreflen
= hreflen
;
479 htprintf(ctx
, "<head>\n");
480 trie_getpath(t
, index
, path
);
481 htprintf(ctx
, "<title>%s: ", PNAME
);
482 htescape(ctx
, path
, strlen(path
), 0);
483 htprintf(ctx
, "</title>\n");
484 htprintf(ctx
, "</head>\n");
487 * Begin BODY section.
489 htprintf(ctx
, "<body>\n");
490 htprintf(ctx
, "<h3 align=center>Disk space breakdown by"
491 " last-access time</h3>\n");
494 * Show the pathname we're centred on, with hyperlinks to
495 * parent directories where available.
497 htprintf(ctx
, "<p align=center>\n<code>");
499 for (p
= strchr(path
, pathsep
); p
&& p
[1]; p
= strchr(p
, pathsep
)) {
504 * See if this path prefix exists in the trie. If so,
505 * generate a hyperlink.
508 if (p
== path
) /* special case for "/" at start */
515 index2
= trie_before(t
, path
);
516 trie_getpath(t
, index2
, path2
);
517 if (!strcmptrailingpathsep(path
, path2
) && cfg
->format
) {
518 make_filename(href
, hreflen
, cfg
->format
, cfg
->rootpage
, index2
);
519 if (!*href
) /* special case that we understand */
521 htprintf(ctx
, "<a href=\"%s\">", href
);
525 htescape(ctx
, q
, zp
- q
, 1);
527 htprintf(ctx
, "</a>");
528 htescape(ctx
, zp
, p
- zp
, 1);
531 htescape(ctx
, q
, strlen(q
), 1);
532 htprintf(ctx
, "</code>\n");
535 * Decide on the age limit of our colour coding, establish the
536 * colour thresholds, and write out a key.
538 ctx
->now
= time(NULL
);
540 ctx
->oldest
= index_order_stat(t
, 0.05);
541 ctx
->newest
= index_order_stat(t
, 1.0);
542 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, -1);
543 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, +1);
545 ctx
->oldest
= cfg
->oldest
;
546 ctx
->newest
= cfg
->newest
;
547 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, 0);
548 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, 0);
550 for (i
= 0; i
< MAXCOLOUR
; i
++) {
552 ctx
->oldest
+ (ctx
->newest
- ctx
->oldest
) * i
/ (MAXCOLOUR
-1);
554 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
558 strcpy(buf
, "< ");
559 round_and_format_age(ctx
, ctx
->thresholds
[0], buf
+5, 0);
560 } else if (i
== MAXCOLOUR
) {
561 strcpy(buf
, "> ");
562 round_and_format_age(ctx
, ctx
->thresholds
[MAXCOLOUR
-1], buf
+5, 0);
564 unsigned long long midrange
=
565 (ctx
->thresholds
[i
-1] + ctx
->thresholds
[i
]) / 2;
566 round_and_format_age(ctx
, midrange
, buf
, 0);
569 ctx
->titletexts
[i
] = dupstr(buf
);
571 htprintf(ctx
, "<p align=center>Key to colour coding (mouse over for more detail):\n");
572 htprintf(ctx
, "<p align=center style=\"padding: 0; margin-top:0.4em; "
573 "margin-bottom:1em\">");
574 begin_colour_bar(ctx
);
575 htprintf(ctx
, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1
);
576 for (i
= 0; i
< MAXCOLOUR
; i
++)
577 add_to_colour_bar(ctx
, i
, 1);
578 htprintf(ctx
, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2
);
582 * Begin the main table.
584 htprintf(ctx
, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
587 * Find the total size of our entire subdirectory. We'll use
588 * that as the scale for all the colour bars in this report.
590 get_indices(t
, path
, &xi1
, &xi2
);
591 ctx
->totalsize
= fetch_size(t
, xi1
, xi2
, ULLONG_MAX
);
594 * Generate a report line for the whole subdirectory.
597 vecs
= snewn(vecsize
, struct vector
*);
599 vecs
[0] = make_vector(ctx
, path
, 0, 1, NULL
, 0);
600 print_heading(ctx
, "Overall");
601 write_report_line(ctx
, vecs
[0]);
604 * Now generate report lines for all its children, and the
605 * files contained in it.
607 print_heading(ctx
, "Subdirectories");
609 vecs
[0]->name
= dupstr("[files]");
610 get_indices(t
, path
, &xi1
, &xi2
);
612 pathlen
= strlen(path
);
613 subdirpos
= pathlen
+ 1;
614 if (pathlen
> 0 && path
[pathlen
-1] == pathsep
)
617 trie_getpath(t
, xi1
, path2
);
618 get_indices(t
, ctx
->path2
, &xj1
, &xj2
);
620 if (!cfg
->showfiles
&& xj2
- xj1
<= 1)
621 continue; /* skip individual files */
622 if (nvecs
>= vecsize
) {
623 vecsize
= nvecs
* 3 / 2 + 64;
624 vecs
= sresize(vecs
, vecsize
, struct vector
*);
626 assert(strlen(path2
) > pathlen
);
627 vecs
[nvecs
] = make_vector(ctx
, path2
, downlink
&& (xj2
- xj1
> 1), 0,
628 path2
+ subdirpos
, 1);
629 for (i
= 0; i
<= MAXCOLOUR
; i
++)
630 vecs
[0]->sizes
[i
] -= vecs
[nvecs
]->sizes
[i
];
634 qsort(vecs
, nvecs
, sizeof(vecs
[0]), vec_compare
);
636 for (i
= 0; i
< nvecs
; i
++)
637 write_report_line(ctx
, vecs
[i
]);
640 * Close the main table.
642 htprintf(ctx
, "</table>\n");
645 * Finish up and tidy up.
647 htprintf(ctx
, "</body>\n");
648 htprintf(ctx
, "</html>\n");
652 for (i
= 0; i
< nvecs
; i
++) {
653 sfree(vecs
[i
]->name
);
661 int html_dump(const void *t
, unsigned long index
, unsigned long endindex
,
662 int maxdepth
, const struct html_config
*cfg
,
663 const char *pathprefix
)
666 * Determine the filename for this file.
668 assert(cfg
->format
!= NULL
);
669 int prefixlen
= strlen(pathprefix
);
670 int fnmax
= strlen(pathprefix
) + strlen(cfg
->format
) + 100;
671 char filename
[fnmax
];
672 strcpy(filename
, pathprefix
);
673 make_filename(filename
+ prefixlen
, fnmax
- prefixlen
,
674 cfg
->format
, cfg
->rootpage
, index
);
677 * Create the HTML itself. Don't write out downlinks from our
680 char *html
= html_query(t
, index
, cfg
, maxdepth
!= 0);
685 FILE *fp
= fopen(filename
, "w");
687 fprintf(stderr
, "%s: %s: open: %s\n", PNAME
,
688 filename
, strerror(errno
));
691 if (fputs(html
, fp
) < 0) {
692 fprintf(stderr
, "%s: %s: write: %s\n", PNAME
,
693 filename
, strerror(errno
));
697 if (fclose(fp
) < 0) {
698 fprintf(stderr
, "%s: %s: fclose: %s\n", PNAME
,
699 filename
, strerror(errno
));
707 unsigned long subindex
, subendindex
;
708 int newdepth
= (maxdepth
> 0 ? maxdepth
- 1 : maxdepth
);
709 char path
[1+trie_maxpathlen(t
)];
712 while (index
< endindex
) {
713 trie_getpath(t
, index
, path
);
714 get_indices(t
, path
, &subindex
, &subendindex
);
716 if (subendindex
- subindex
> 1) {
717 if (html_dump(t
, subindex
, subendindex
, newdepth
,