bc9905fbf17ea1f4a827aa3ff57cb987f4178dc5
2 * html.c: implementation of html.h.
19 #define lenof(x) ( sizeof((x)) / sizeof(*(x)) )
27 size_t buflen
, bufsize
;
29 unsigned long long totalsize
, oldest
, newest
;
34 unsigned long long thresholds
[MAXCOLOUR
-1];
38 static void vhtprintf(struct html
*ctx
, char *fmt
, va_list ap
)
44 size
= vsnprintf(NULL
, 0, fmt
, ap2
);
47 if (ctx
->buflen
+ size
>= ctx
->bufsize
) {
48 ctx
->bufsize
= (ctx
->buflen
+ size
) * 3 / 2 + 1024;
49 ctx
->buf
= sresize(ctx
->buf
, ctx
->bufsize
, char);
51 size2
= vsnprintf(ctx
->buf
+ ctx
->buflen
, ctx
->bufsize
- ctx
->buflen
,
53 assert(size
== size2
);
57 static void htprintf(struct html
*ctx
, char *fmt
, ...)
61 vhtprintf(ctx
, fmt
, ap
);
65 static unsigned long long round_and_format_age(struct html
*ctx
,
66 unsigned long long age
,
67 char *buf
, int direction
)
71 unsigned long long ret
, newret
;
74 static const int minutes
[] = { 5, 10, 15, 30, 45 };
76 tm
= *localtime(&ctx
->now
);
77 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
82 for (i
= 0; i
< lenof(minutes
); i
++) {
83 newret
= ctx
->now
- minutes
[i
] * 60;
84 sprintf(newbuf
, "%d minutes", minutes
[i
]);
91 for (i
= 1; i
< 24; i
++) {
92 newret
= ctx
->now
- i
* (60*60);
93 sprintf(newbuf
, "%d hour%s", i
, i
==1 ?
"" : "s");
100 for (i
= 1; i
< 7; i
++) {
101 newret
= ctx
->now
- i
* (24*60*60);
102 sprintf(newbuf
, "%d day%s", i
, i
==1 ?
"" : "s");
109 for (i
= 1; i
< 4; i
++) {
110 newret
= ctx
->now
- i
* (7*24*60*60);
111 sprintf(newbuf
, "%d week%s", i
, i
==1 ?
"" : "s");
118 for (i
= 1; i
< 11; i
++) {
119 tm2
= tm
; /* structure copy */
120 tm2
.tm_year
= (ym
- i
) / 12;
121 tm2
.tm_mon
= (ym
- i
) % 12;
122 newret
= mktime(&tm2
);
123 sprintf(newbuf
, "%d month%s", i
, i
==1 ?
"" : "s");
131 tm2
= tm
; /* structure copy */
132 tm2
.tm_year
= (ym
- i
*12) / 12;
133 tm2
.tm_mon
= (ym
- i
*12) % 12;
134 newret
= mktime(&tm2
);
135 sprintf(newbuf
, "%d year%s", i
, i
==1 ?
"" : "s");
145 * Round toward newest, i.e. use the existing (buf,ret).
147 } else if (direction
< 0) {
149 * Round toward oldest, i.e. use (newbuf,newret);
157 if (ret
- age
> age
- newret
) {
165 static void get_indices(const void *t
, char *path
,
166 unsigned long *xi1
, unsigned long *xi2
)
168 size_t pathlen
= strlen(path
);
170 *xi1
= trie_before(t
, path
);
171 path
[pathlen
] = '\001';
172 path
[pathlen
+1] = '\0';
173 *xi2
= trie_before(t
, path
);
174 path
[pathlen
] = '\0';
177 static unsigned long long fetch_size(const void *t
, char *path
,
178 unsigned long long atime
)
180 unsigned long xi1
, xi2
;
182 get_indices(t
, path
, &xi1
, &xi2
);
184 return index_query(t
, xi2
, atime
) - index_query(t
, xi1
, atime
);
187 static void htescape(struct html
*ctx
, const char *s
, int n
, int italics
)
189 while (n
> 0 && *s
) {
190 unsigned char c
= (unsigned char)*s
++;
193 htprintf(ctx
, "&");
195 htprintf(ctx
, "<");
197 htprintf(ctx
, ">");
198 else if (c
>= ' ' && c
< '\177')
199 htprintf(ctx
, "%c", c
);
201 if (italics
) htprintf(ctx
, "<i>");
202 htprintf(ctx
, "[%02x]", c
);
203 if (italics
) htprintf(ctx
, "</i>");
210 static void begin_colour_bar(struct html
*ctx
)
212 htprintf(ctx
, "<table cellspacing=0 cellpadding=0"
213 " style=\"border:0\">\n<tr>\n");
216 static void add_to_colour_bar(struct html
*ctx
, int colour
, int pixels
)
221 if (colour
>= 0 && colour
< 256) /* red -> yellow fade */
222 r
= 255, g
= colour
, b
= 0;
223 else if (colour
>= 256 && colour
<= 511) /* yellow -> green fade */
224 r
= 511 - colour
, g
= 255, b
= 0;
225 else /* background grey */
229 /* no title text here */
230 } else if (colour
== 0) {
231 strcpy(buf
, "< ");
232 round_and_format_age(ctx
, ctx
->thresholds
[0], buf
+5, 0);
233 } else if (colour
== MAXCOLOUR
) {
234 strcpy(buf
, "> ");
235 round_and_format_age(ctx
, ctx
->thresholds
[MAXCOLOUR
-1], buf
+5, 0);
237 unsigned long long midrange
=
238 (ctx
->thresholds
[colour
] + ctx
->thresholds
[colour
+1]) / 2;
239 round_and_format_age(ctx
, midrange
, buf
, 0);
243 htprintf(ctx
, "<td style=\"width:%dpx; height:1em; "
244 "background-color:#%02x%02x%02x\"",
247 htprintf(ctx
, " title=\"%s\"", buf
);
248 htprintf(ctx
, "></td>\n");
252 static void end_colour_bar(struct html
*ctx
)
254 htprintf(ctx
, "</tr>\n</table>\n");
261 unsigned long long sizes
[MAXCOLOUR
+1];
264 int vec_compare(const void *av
, const void *bv
)
266 const struct vector
*a
= *(const struct vector
**)av
;
267 const struct vector
*b
= *(const struct vector
**)bv
;
269 if (a
->sizes
[MAXCOLOUR
] > b
->sizes
[MAXCOLOUR
])
271 else if (a
->sizes
[MAXCOLOUR
] < b
->sizes
[MAXCOLOUR
])
273 else if (a
->want_href
< b
->want_href
)
275 else if (a
->want_href
> b
->want_href
)
277 else if (a
->want_href
)
278 return strcmp(a
->name
, b
->name
);
279 else if (a
->index
< b
->index
)
281 else if (a
->index
> b
->index
)
286 static struct vector
*make_vector(struct html
*ctx
, char *path
,
287 int want_href
, char *name
)
289 unsigned long xi1
, xi2
;
290 struct vector
*vec
= snew(struct vector
);
293 vec
->want_href
= want_href
;
294 vec
->name
= name ?
dupstr(name
) : NULL
;
296 get_indices(ctx
->t
, path
, &xi1
, &xi2
);
300 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
301 unsigned long long atime
;
305 atime
= ctx
->thresholds
[i
];
306 vec
->sizes
[i
] = fetch_size(ctx
->t
, path
, atime
);
312 static void print_heading(struct html
*ctx
, const char *title
)
314 htprintf(ctx
, "<tr style=\"padding: 0.2em; background-color:#e0e0e0\">\n"
315 "<td colspan=4 align=center>%s</td>\n</tr>\n", title
);
318 #define PIXEL_SIZE 600 /* FIXME: configurability? */
319 static void write_report_line(struct html
*ctx
, struct vector
*vec
)
321 unsigned long long size
, asize
, divisor
;
326 * A line with literally zero space usage should not be
327 * printed at all if it's a link to a subdirectory (since it
328 * probably means the whole thing was excluded by some
329 * --exclude-path wildcard). If it's [files] or the top-level
330 * line, though, we must always print _something_, and in that
331 * case we must fiddle about to prevent divisions by zero in
334 if (!vec
->sizes
[MAXCOLOUR
] && vec
->want_href
)
336 divisor
= ctx
->totalsize
;
342 * Find the total size of this subdirectory.
344 size
= vec
->sizes
[MAXCOLOUR
];
345 htprintf(ctx
, "<tr>\n"
346 "<td style=\"padding: 0.2em; text-align: right\">%lluMb</td>\n",
347 ((size
+ ((1<<20)-1)) >> 20)); /* convert to Mb, rounding up */
350 * Generate a colour bar.
352 htprintf(ctx
, "<td style=\"padding: 0.2em\">\n");
353 begin_colour_bar(ctx
);
355 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
356 asize
= vec
->sizes
[i
];
357 newpix
= asize
* PIXEL_SIZE
/ divisor
;
358 add_to_colour_bar(ctx
, i
, newpix
- pix
);
361 add_to_colour_bar(ctx
, -1, PIXEL_SIZE
- pix
);
363 htprintf(ctx
, "</td>\n");
366 * Output size as a percentage of totalsize.
368 htprintf(ctx
, "<td style=\"padding: 0.2em; text-align: right\">"
369 "%.2f%%</td>\n", (double)size
/ divisor
* 100.0);
372 * Output a subdirectory marker.
374 htprintf(ctx
, "<td style=\"padding: 0.2em\">");
378 if (ctx
->format
&& vec
->want_href
) {
379 snprintf(ctx
->href
, ctx
->hreflen
, ctx
->format
, vec
->index
);
380 htprintf(ctx
, "<a href=\"%s\">", ctx
->href
);
383 htescape(ctx
, vec
->name
, strlen(vec
->name
), 1);
385 htprintf(ctx
, "</a>");
387 htprintf(ctx
, "</td>\n</tr>\n");
390 char *html_query(const void *t
, unsigned long index
,
391 const struct html_config
*cfg
)
393 struct html actx
, *ctx
= &actx
;
394 char *path
, *path2
, *p
, *q
, *href
;
395 char agebuf1
[80], agebuf2
[80];
396 size_t pathlen
, hreflen
;
397 unsigned long index2
;
399 struct vector
**vecs
;
401 unsigned long xi1
, xi2
, xj1
, xj2
;
403 if (index
>= trie_count(t
))
407 ctx
->buflen
= ctx
->bufsize
= 0;
409 ctx
->format
= cfg
->format
;
410 htprintf(ctx
, "<html>\n");
412 path
= snewn(1+trie_maxpathlen(t
), char);
413 ctx
->path2
= path2
= snewn(1+trie_maxpathlen(t
), char);
415 hreflen
= strlen(cfg
->format
) + 100;
416 href
= snewn(hreflen
, char);
421 ctx
->hreflen
= hreflen
;
427 htprintf(ctx
, "<head>\n");
428 trie_getpath(t
, index
, path
);
429 htprintf(ctx
, "<title>agedu: ");
430 htescape(ctx
, path
, strlen(path
), 0);
431 htprintf(ctx
, "</title>\n");
432 htprintf(ctx
, "</head>\n");
435 * Begin BODY section.
437 htprintf(ctx
, "<body>\n");
438 htprintf(ctx
, "<h3 align=center>Disk space breakdown by"
439 " last-access time</h3>\n");
442 * Show the pathname we're centred on, with hyperlinks to
443 * parent directories where available.
445 htprintf(ctx
, "<p align=center>\n<code>");
447 for (p
= strchr(path
, pathsep
); p
; p
= strchr(p
+1, pathsep
)) {
450 * See if this path prefix exists in the trie. If so,
451 * generate a hyperlink.
454 index2
= trie_before(t
, path
);
455 trie_getpath(t
, index2
, path2
);
456 if (!strcmp(path
, path2
) && cfg
->format
) {
457 snprintf(href
, hreflen
, cfg
->format
, index2
);
458 htprintf(ctx
, "<a href=\"%s\">", href
);
462 htescape(ctx
, q
, p
- q
, 1);
465 htprintf(ctx
, "</a>");
466 htescape(ctx
, p
, 1, 1);
468 htescape(ctx
, q
, strlen(q
), 1);
469 htprintf(ctx
, "</code>\n");
472 * Decide on the age limit of our colour coding, establish the
473 * colour thresholds, and write out a key.
475 ctx
->now
= time(NULL
);
477 ctx
->oldest
= index_order_stat(t
, 0.05);
478 ctx
->newest
= index_order_stat(t
, 1.0);
479 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, -1);
480 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, +1);
482 ctx
->oldest
= cfg
->oldest
;
483 ctx
->newest
= cfg
->newest
;
484 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, 0);
485 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, 0);
487 for (i
= 0; i
< MAXCOLOUR
-1; i
++) {
489 ctx
->oldest
+ (ctx
->newest
- ctx
->oldest
) * i
/ MAXCOLOUR
;
491 htprintf(ctx
, "<p align=center>Key to colour coding (mouse over for more detail):\n");
492 htprintf(ctx
, "<p align=center style=\"padding: 0; margin-top:0.4em; "
493 "margin-bottom:1em\"");
494 begin_colour_bar(ctx
);
495 htprintf(ctx
, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1
);
496 for (i
= 0; i
< MAXCOLOUR
; i
++)
497 add_to_colour_bar(ctx
, i
, 1);
498 htprintf(ctx
, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2
);
502 * Begin the main table.
504 htprintf(ctx
, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
507 * Find the total size of our entire subdirectory. We'll use
508 * that as the scale for all the colour bars in this report.
510 ctx
->totalsize
= fetch_size(t
, path
, ULLONG_MAX
);
513 * Generate a report line for the whole subdirectory.
516 vecs
= snewn(vecsize
, struct vector
*);
518 vecs
[0] = make_vector(ctx
, path
, 0, NULL
);
519 print_heading(ctx
, "Overall");
520 write_report_line(ctx
, vecs
[0]);
523 * Now generate report lines for all its children, and the
524 * files contained in it.
526 print_heading(ctx
, "Subdirectories");
528 vecs
[0]->name
= dupstr("[files]");
529 get_indices(t
, path
, &xi1
, &xi2
);
531 pathlen
= strlen(path
);
533 trie_getpath(t
, xi1
, path2
);
534 get_indices(t
, ctx
->path2
, &xj1
, &xj2
);
537 continue; /* skip individual files */
538 if (nvecs
>= vecsize
) {
539 vecsize
= nvecs
* 3 / 2 + 64;
540 vecs
= sresize(vecs
, vecsize
, struct vector
*);
542 assert(strlen(path2
) > pathlen
);
543 vecs
[nvecs
] = make_vector(ctx
, path2
, 1, path2
+ pathlen
+ 1);
544 for (i
= 0; i
<= MAXCOLOUR
; i
++)
545 vecs
[0]->sizes
[i
] -= vecs
[nvecs
]->sizes
[i
];
549 qsort(vecs
, nvecs
, sizeof(vecs
[0]), vec_compare
);
551 for (i
= 0; i
< nvecs
; i
++)
552 write_report_line(ctx
, vecs
[i
]);
555 * Close the main table.
557 htprintf(ctx
, "</table>\n");
560 * Finish up and tidy up.
562 htprintf(ctx
, "</body>\n");
563 htprintf(ctx
, "</html>\n");
567 for (i
= 0; i
< nvecs
; i
++) {
568 sfree(vecs
[i
]->name
);