0d80eae58d696e1b6ac8e5b83fd2b000c1fa5eb8
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
);
169 int c1
= path
[pathlen
], c2
= (pathlen
> 0 ? path
[pathlen
-1] : 0);
171 *xi1
= trie_before(t
, path
);
172 make_successor(path
);
173 *xi2
= trie_before(t
, path
);
176 path
[pathlen
-1] = c2
;
179 static unsigned long long fetch_size(const void *t
, char *path
,
180 unsigned long long atime
)
182 unsigned long xi1
, xi2
;
184 get_indices(t
, path
, &xi1
, &xi2
);
186 return index_query(t
, xi2
, atime
) - index_query(t
, xi1
, atime
);
189 static void htescape(struct html
*ctx
, const char *s
, int n
, int italics
)
191 while (n
> 0 && *s
) {
192 unsigned char c
= (unsigned char)*s
++;
195 htprintf(ctx
, "&");
197 htprintf(ctx
, "<");
199 htprintf(ctx
, ">");
200 else if (c
>= ' ' && c
< '\177')
201 htprintf(ctx
, "%c", c
);
203 if (italics
) htprintf(ctx
, "<i>");
204 htprintf(ctx
, "[%02x]", c
);
205 if (italics
) htprintf(ctx
, "</i>");
212 static void begin_colour_bar(struct html
*ctx
)
214 htprintf(ctx
, "<table cellspacing=0 cellpadding=0"
215 " style=\"border:0\">\n<tr>\n");
218 static void add_to_colour_bar(struct html
*ctx
, int colour
, int pixels
)
223 if (colour
>= 0 && colour
< 256) /* red -> yellow fade */
224 r
= 255, g
= colour
, b
= 0;
225 else if (colour
>= 256 && colour
<= 511) /* yellow -> green fade */
226 r
= 511 - colour
, g
= 255, b
= 0;
227 else /* background grey */
231 /* no title text here */
232 } else if (colour
== 0) {
233 strcpy(buf
, "< ");
234 round_and_format_age(ctx
, ctx
->thresholds
[0], buf
+5, 0);
235 } else if (colour
== MAXCOLOUR
) {
236 strcpy(buf
, "> ");
237 round_and_format_age(ctx
, ctx
->thresholds
[MAXCOLOUR
-1], buf
+5, 0);
239 unsigned long long midrange
=
240 (ctx
->thresholds
[colour
] + ctx
->thresholds
[colour
+1]) / 2;
241 round_and_format_age(ctx
, midrange
, buf
, 0);
245 htprintf(ctx
, "<td style=\"width:%dpx; height:1em; "
246 "background-color:#%02x%02x%02x\"",
249 htprintf(ctx
, " title=\"%s\"", buf
);
250 htprintf(ctx
, "></td>\n");
254 static void end_colour_bar(struct html
*ctx
)
256 htprintf(ctx
, "</tr>\n</table>\n");
263 unsigned long long sizes
[MAXCOLOUR
+1];
266 int vec_compare(const void *av
, const void *bv
)
268 const struct vector
*a
= *(const struct vector
**)av
;
269 const struct vector
*b
= *(const struct vector
**)bv
;
271 if (a
->sizes
[MAXCOLOUR
] > b
->sizes
[MAXCOLOUR
])
273 else if (a
->sizes
[MAXCOLOUR
] < b
->sizes
[MAXCOLOUR
])
275 else if (a
->want_href
< b
->want_href
)
277 else if (a
->want_href
> b
->want_href
)
279 else if (a
->want_href
)
280 return strcmp(a
->name
, b
->name
);
281 else if (a
->index
< b
->index
)
283 else if (a
->index
> b
->index
)
288 static struct vector
*make_vector(struct html
*ctx
, char *path
,
289 int want_href
, char *name
)
291 unsigned long xi1
, xi2
;
292 struct vector
*vec
= snew(struct vector
);
295 vec
->want_href
= want_href
;
296 vec
->name
= name ?
dupstr(name
) : NULL
;
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
, path
, 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 #define PIXEL_SIZE 600 /* FIXME: configurability? */
321 static void write_report_line(struct html
*ctx
, struct vector
*vec
)
323 unsigned long long size
, asize
, divisor
;
328 * A line with literally zero space usage should not be
329 * printed at all if it's a link to a subdirectory (since it
330 * probably means the whole thing was excluded by some
331 * --exclude-path wildcard). If it's [files] or the top-level
332 * line, though, we must always print _something_, and in that
333 * case we must fiddle about to prevent divisions by zero in
336 if (!vec
->sizes
[MAXCOLOUR
] && vec
->want_href
)
338 divisor
= ctx
->totalsize
;
344 * Find the total size of this subdirectory.
346 size
= vec
->sizes
[MAXCOLOUR
];
347 htprintf(ctx
, "<tr>\n"
348 "<td style=\"padding: 0.2em; text-align: right\">%lluMb</td>\n",
349 ((size
+ ((1<<20)-1)) >> 20)); /* convert to Mb, rounding up */
352 * Generate a colour bar.
354 htprintf(ctx
, "<td style=\"padding: 0.2em\">\n");
355 begin_colour_bar(ctx
);
357 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
358 asize
= vec
->sizes
[i
];
359 newpix
= asize
* PIXEL_SIZE
/ divisor
;
360 add_to_colour_bar(ctx
, i
, newpix
- pix
);
363 add_to_colour_bar(ctx
, -1, PIXEL_SIZE
- pix
);
365 htprintf(ctx
, "</td>\n");
368 * Output size as a percentage of totalsize.
370 htprintf(ctx
, "<td style=\"padding: 0.2em; text-align: right\">"
371 "%.2f%%</td>\n", (double)size
/ divisor
* 100.0);
374 * Output a subdirectory marker.
376 htprintf(ctx
, "<td style=\"padding: 0.2em\">");
380 if (ctx
->format
&& vec
->want_href
) {
381 snprintf(ctx
->href
, ctx
->hreflen
, ctx
->format
, vec
->index
);
382 htprintf(ctx
, "<a href=\"%s\">", ctx
->href
);
385 htescape(ctx
, vec
->name
, strlen(vec
->name
), 1);
387 htprintf(ctx
, "</a>");
389 htprintf(ctx
, "</td>\n</tr>\n");
392 char *html_query(const void *t
, unsigned long index
,
393 const struct html_config
*cfg
)
395 struct html actx
, *ctx
= &actx
;
396 char *path
, *path2
, *p
, *q
, *href
;
397 char agebuf1
[80], agebuf2
[80];
398 size_t pathlen
, subdirpos
, hreflen
;
399 unsigned long index2
;
401 struct vector
**vecs
;
403 unsigned long xi1
, xi2
, xj1
, xj2
;
405 if (index
>= trie_count(t
))
409 ctx
->buflen
= ctx
->bufsize
= 0;
411 ctx
->format
= cfg
->format
;
412 htprintf(ctx
, "<html>\n");
414 path
= snewn(1+trie_maxpathlen(t
), char);
415 ctx
->path2
= path2
= snewn(1+trie_maxpathlen(t
), char);
417 hreflen
= strlen(cfg
->format
) + 100;
418 href
= snewn(hreflen
, char);
423 ctx
->hreflen
= hreflen
;
429 htprintf(ctx
, "<head>\n");
430 trie_getpath(t
, index
, path
);
431 htprintf(ctx
, "<title>agedu: ");
432 htescape(ctx
, path
, strlen(path
), 0);
433 htprintf(ctx
, "</title>\n");
434 htprintf(ctx
, "</head>\n");
437 * Begin BODY section.
439 htprintf(ctx
, "<body>\n");
440 htprintf(ctx
, "<h3 align=center>Disk space breakdown by"
441 " last-access time</h3>\n");
444 * Show the pathname we're centred on, with hyperlinks to
445 * parent directories where available.
447 htprintf(ctx
, "<p align=center>\n<code>");
449 for (p
= strchr(path
, pathsep
); p
&& p
[1]; p
= strchr(p
, pathsep
)) {
454 * See if this path prefix exists in the trie. If so,
455 * generate a hyperlink.
458 if (p
== path
) /* special case for "/" at start */
465 index2
= trie_before(t
, path
);
466 trie_getpath(t
, index2
, path2
);
467 if (!strcmp(path
, path2
) && cfg
->format
) {
468 snprintf(href
, hreflen
, cfg
->format
, index2
);
469 if (!*href
) /* special case that we understand */
471 htprintf(ctx
, "<a href=\"%s\">", href
);
475 htescape(ctx
, q
, zp
- q
, 1);
477 htprintf(ctx
, "</a>");
478 htescape(ctx
, zp
, p
- zp
, 1);
481 htescape(ctx
, q
, strlen(q
), 1);
482 htprintf(ctx
, "</code>\n");
485 * Decide on the age limit of our colour coding, establish the
486 * colour thresholds, and write out a key.
488 ctx
->now
= time(NULL
);
490 ctx
->oldest
= index_order_stat(t
, 0.05);
491 ctx
->newest
= index_order_stat(t
, 1.0);
492 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, -1);
493 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, +1);
495 ctx
->oldest
= cfg
->oldest
;
496 ctx
->newest
= cfg
->newest
;
497 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, 0);
498 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, 0);
500 for (i
= 0; i
< MAXCOLOUR
-1; i
++) {
502 ctx
->oldest
+ (ctx
->newest
- ctx
->oldest
) * i
/ MAXCOLOUR
;
504 htprintf(ctx
, "<p align=center>Key to colour coding (mouse over for more detail):\n");
505 htprintf(ctx
, "<p align=center style=\"padding: 0; margin-top:0.4em; "
506 "margin-bottom:1em\"");
507 begin_colour_bar(ctx
);
508 htprintf(ctx
, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1
);
509 for (i
= 0; i
< MAXCOLOUR
; i
++)
510 add_to_colour_bar(ctx
, i
, 1);
511 htprintf(ctx
, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2
);
515 * Begin the main table.
517 htprintf(ctx
, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
520 * Find the total size of our entire subdirectory. We'll use
521 * that as the scale for all the colour bars in this report.
523 ctx
->totalsize
= fetch_size(t
, path
, ULLONG_MAX
);
526 * Generate a report line for the whole subdirectory.
529 vecs
= snewn(vecsize
, struct vector
*);
531 vecs
[0] = make_vector(ctx
, path
, 0, NULL
);
532 print_heading(ctx
, "Overall");
533 write_report_line(ctx
, vecs
[0]);
536 * Now generate report lines for all its children, and the
537 * files contained in it.
539 print_heading(ctx
, "Subdirectories");
541 vecs
[0]->name
= dupstr("[files]");
542 get_indices(t
, path
, &xi1
, &xi2
);
544 pathlen
= strlen(path
);
545 subdirpos
= pathlen
+ 1;
546 if (pathlen
> 0 && path
[pathlen
-1] == pathsep
)
549 trie_getpath(t
, xi1
, path2
);
550 get_indices(t
, ctx
->path2
, &xj1
, &xj2
);
553 continue; /* skip individual files */
554 if (nvecs
>= vecsize
) {
555 vecsize
= nvecs
* 3 / 2 + 64;
556 vecs
= sresize(vecs
, vecsize
, struct vector
*);
558 assert(strlen(path2
) > pathlen
);
559 vecs
[nvecs
] = make_vector(ctx
, path2
, 1, path2
+ subdirpos
);
560 for (i
= 0; i
<= MAXCOLOUR
; i
++)
561 vecs
[0]->sizes
[i
] -= vecs
[nvecs
]->sizes
[i
];
565 qsort(vecs
, nvecs
, sizeof(vecs
[0]), vec_compare
);
567 for (i
= 0; i
< nvecs
; i
++)
568 write_report_line(ctx
, vecs
[i
]);
571 * Close the main table.
573 htprintf(ctx
, "</table>\n");
576 * Finish up and tidy up.
578 htprintf(ctx
, "</body>\n");
579 htprintf(ctx
, "</html>\n");
583 for (i
= 0; i
< nvecs
; i
++) {
584 sfree(vecs
[i
]->name
);