Protect against division by zero when handling directories with
[sgt/agedu] / html.c
1 /*
2 * html.c: implementation of html.h.
3 */
4
5 #include <assert.h>
6 #include <stddef.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <limits.h>
12 #include <time.h>
13
14 #include "html.h"
15 #include "malloc.h"
16 #include "trie.h"
17 #include "index.h"
18
19 #define lenof(x) ( sizeof((x)) / sizeof(*(x)) )
20
21 #define MAXCOLOUR 511
22
23 struct html {
24 char *buf;
25 size_t buflen, bufsize;
26 const void *t;
27 unsigned long long totalsize, oldest, newest;
28 char *path2;
29 char *href;
30 size_t hreflen;
31 const char *format;
32 unsigned long long thresholds[MAXCOLOUR-1];
33 time_t now;
34 };
35
36 static void vhtprintf(struct html *ctx, char *fmt, va_list ap)
37 {
38 va_list ap2;
39 int size, size2;
40
41 va_copy(ap2, ap);
42 size = vsnprintf(NULL, 0, fmt, ap2);
43 va_end(ap2);
44
45 if (ctx->buflen + size >= ctx->bufsize) {
46 ctx->bufsize = (ctx->buflen + size) * 3 / 2 + 1024;
47 ctx->buf = sresize(ctx->buf, ctx->bufsize, char);
48 }
49 size2 = vsnprintf(ctx->buf + ctx->buflen, ctx->bufsize - ctx->buflen,
50 fmt, ap);
51 assert(size == size2);
52 ctx->buflen += size;
53 }
54
55 static void htprintf(struct html *ctx, char *fmt, ...)
56 {
57 va_list ap;
58 va_start(ap, fmt);
59 vhtprintf(ctx, fmt, ap);
60 va_end(ap);
61 }
62
63 static unsigned long long round_and_format_age(struct html *ctx,
64 unsigned long long age,
65 char *buf, int direction)
66 {
67 struct tm tm, tm2;
68 char newbuf[80];
69 unsigned long long ret, newret;
70 int i;
71 int ym;
72 static const int minutes[] = { 5, 10, 15, 30, 45 };
73
74 tm = *localtime(&ctx->now);
75 ym = tm.tm_year * 12 + tm.tm_mon;
76
77 ret = ctx->now;
78 strcpy(buf, "Now");
79
80 for (i = 0; i < lenof(minutes); i++) {
81 newret = ctx->now - minutes[i] * 60;
82 sprintf(newbuf, "%d minutes", minutes[i]);
83 if (newret < age)
84 goto finish;
85 strcpy(buf, newbuf);
86 ret = newret;
87 }
88
89 for (i = 1; i < 24; i++) {
90 newret = ctx->now - i * (60*60);
91 sprintf(newbuf, "%d hour%s", i, i==1 ? "" : "s");
92 if (newret < age)
93 goto finish;
94 strcpy(buf, newbuf);
95 ret = newret;
96 }
97
98 for (i = 1; i < 7; i++) {
99 newret = ctx->now - i * (24*60*60);
100 sprintf(newbuf, "%d day%s", i, i==1 ? "" : "s");
101 if (newret < age)
102 goto finish;
103 strcpy(buf, newbuf);
104 ret = newret;
105 }
106
107 for (i = 1; i < 4; i++) {
108 newret = ctx->now - i * (7*24*60*60);
109 sprintf(newbuf, "%d week%s", i, i==1 ? "" : "s");
110 if (newret < age)
111 goto finish;
112 strcpy(buf, newbuf);
113 ret = newret;
114 }
115
116 for (i = 1; i < 11; i++) {
117 tm2 = tm; /* structure copy */
118 tm2.tm_year = (ym - i) / 12;
119 tm2.tm_mon = (ym - i) % 12;
120 newret = mktime(&tm2);
121 sprintf(newbuf, "%d month%s", i, i==1 ? "" : "s");
122 if (newret < age)
123 goto finish;
124 strcpy(buf, newbuf);
125 ret = newret;
126 }
127
128 for (i = 1;; i++) {
129 tm2 = tm; /* structure copy */
130 tm2.tm_year = (ym - i*12) / 12;
131 tm2.tm_mon = (ym - i*12) % 12;
132 newret = mktime(&tm2);
133 sprintf(newbuf, "%d year%s", i, i==1 ? "" : "s");
134 if (newret < age)
135 goto finish;
136 strcpy(buf, newbuf);
137 ret = newret;
138 }
139
140 finish:
141 if (direction > 0) {
142 /*
143 * Round toward newest, i.e. use the existing (buf,ret).
144 */
145 } else if (direction < 0) {
146 /*
147 * Round toward oldest, i.e. use (newbuf,newret);
148 */
149 strcpy(buf, newbuf);
150 ret = newret;
151 } else {
152 /*
153 * Round to nearest.
154 */
155 if (ret - age > age - newret) {
156 strcpy(buf, newbuf);
157 ret = newret;
158 }
159 }
160 return ret;
161 }
162
163 static void get_indices(const void *t, char *path,
164 unsigned long *xi1, unsigned long *xi2)
165 {
166 size_t pathlen = strlen(path);
167
168 *xi1 = trie_before(t, path);
169 path[pathlen] = '\001';
170 path[pathlen+1] = '\0';
171 *xi2 = trie_before(t, path);
172 path[pathlen] = '\0';
173 }
174
175 static unsigned long long fetch_size(const void *t, char *path,
176 unsigned long long atime)
177 {
178 unsigned long xi1, xi2;
179
180 get_indices(t, path, &xi1, &xi2);
181
182 return index_query(t, xi2, atime) - index_query(t, xi1, atime);
183 }
184
185 static void htescape(struct html *ctx, const char *s, int n, int italics)
186 {
187 while (n > 0 && *s) {
188 unsigned char c = (unsigned char)*s++;
189
190 if (c == '&')
191 htprintf(ctx, "&amp;");
192 else if (c == '<')
193 htprintf(ctx, "&lt;");
194 else if (c == '>')
195 htprintf(ctx, "&gt;");
196 else if (c >= ' ' && c < '\177')
197 htprintf(ctx, "%c", c);
198 else {
199 if (italics) htprintf(ctx, "<i>");
200 htprintf(ctx, "[%02x]", c);
201 if (italics) htprintf(ctx, "</i>");
202 }
203
204 n--;
205 }
206 }
207
208 static void begin_colour_bar(struct html *ctx)
209 {
210 htprintf(ctx, "<table cellspacing=0 cellpadding=0"
211 " style=\"border:0\">\n<tr>\n");
212 }
213
214 static void add_to_colour_bar(struct html *ctx, int colour, int pixels)
215 {
216 int r, g, b;
217 char buf[80];
218
219 if (colour >= 0 && colour < 256) /* red -> yellow fade */
220 r = 255, g = colour, b = 0;
221 else if (colour >= 256 && colour <= 511) /* yellow -> green fade */
222 r = 511 - colour, g = 255, b = 0;
223 else /* background grey */
224 r = g = b = 240;
225
226 if (colour < 0) {
227 /* no title text here */
228 } else if (colour == 0) {
229 strcpy(buf, "&lt; ");
230 round_and_format_age(ctx, ctx->thresholds[0], buf+5, 0);
231 } else if (colour == MAXCOLOUR) {
232 strcpy(buf, "&gt; ");
233 round_and_format_age(ctx, ctx->thresholds[MAXCOLOUR-1], buf+5, 0);
234 } else {
235 unsigned long long midrange =
236 (ctx->thresholds[colour] + ctx->thresholds[colour+1]) / 2;
237 round_and_format_age(ctx, midrange, buf, 0);
238 }
239
240 if (pixels > 0) {
241 htprintf(ctx, "<td style=\"width:%dpx; height:1em; "
242 "background-color:#%02x%02x%02x\"",
243 pixels, r, g, b);
244 if (colour >= 0)
245 htprintf(ctx, " title=\"%s\"", buf);
246 htprintf(ctx, "></td>\n");
247 }
248 }
249
250 static void end_colour_bar(struct html *ctx)
251 {
252 htprintf(ctx, "</tr>\n</table>\n");
253 }
254
255 struct vector {
256 int want_href;
257 char *name;
258 unsigned long index;
259 unsigned long long sizes[MAXCOLOUR+1];
260 };
261
262 int vec_compare(const void *av, const void *bv)
263 {
264 const struct vector *a = *(const struct vector **)av;
265 const struct vector *b = *(const struct vector **)bv;
266
267 if (a->sizes[MAXCOLOUR] > b->sizes[MAXCOLOUR])
268 return -1;
269 else if (a->sizes[MAXCOLOUR] < b->sizes[MAXCOLOUR])
270 return +1;
271 else if (a->want_href < b->want_href)
272 return +1;
273 else if (a->want_href > b->want_href)
274 return -1;
275 else if (a->want_href)
276 return strcmp(a->name, b->name);
277 else if (a->index < b->index)
278 return -1;
279 else if (a->index > b->index)
280 return +1;
281 return 0;
282 }
283
284 static struct vector *make_vector(struct html *ctx, char *path,
285 int want_href, char *name)
286 {
287 unsigned long xi1, xi2;
288 struct vector *vec = snew(struct vector);
289 int i;
290
291 vec->want_href = want_href;
292 vec->name = name ? dupstr(name) : NULL;
293
294 get_indices(ctx->t, path, &xi1, &xi2);
295
296 vec->index = xi1;
297
298 for (i = 0; i <= MAXCOLOUR; i++) {
299 unsigned long long atime;
300 if (i == MAXCOLOUR)
301 atime = ULLONG_MAX;
302 else
303 atime = ctx->thresholds[i];
304 vec->sizes[i] = fetch_size(ctx->t, path, atime);
305 }
306
307 return vec;
308 }
309
310 static void print_heading(struct html *ctx, const char *title)
311 {
312 htprintf(ctx, "<tr style=\"padding: 0.2em; background-color:#e0e0e0\">\n"
313 "<td colspan=4 align=center>%s</td>\n</tr>\n", title);
314 }
315
316 #define PIXEL_SIZE 600 /* FIXME: configurability? */
317 static void write_report_line(struct html *ctx, struct vector *vec)
318 {
319 unsigned long long size, asize, divisor;
320 int pix, newpix;
321 int i;
322
323 /*
324 * Prevent divisions by zero.
325 */
326 divisor = ctx->totalsize;
327 if (!divisor)
328 divisor = 1;
329
330 /*
331 * Find the total size of this subdirectory.
332 */
333 size = vec->sizes[MAXCOLOUR];
334 htprintf(ctx, "<tr>\n"
335 "<td style=\"padding: 0.2em; text-align: right\">%lluMb</td>\n",
336 ((size + ((1<<11)-1)) >> 11)); /* convert to Mb, rounding up */
337
338 /*
339 * Generate a colour bar.
340 */
341 htprintf(ctx, "<td style=\"padding: 0.2em\">\n");
342 begin_colour_bar(ctx);
343 pix = 0;
344 for (i = 0; i <= MAXCOLOUR; i++) {
345 asize = vec->sizes[i];
346 newpix = asize * PIXEL_SIZE / divisor;
347 add_to_colour_bar(ctx, i, newpix - pix);
348 pix = newpix;
349 }
350 add_to_colour_bar(ctx, -1, PIXEL_SIZE - pix);
351 end_colour_bar(ctx);
352 htprintf(ctx, "</td>\n");
353
354 /*
355 * Output size as a percentage of totalsize.
356 */
357 htprintf(ctx, "<td style=\"padding: 0.2em; text-align: right\">"
358 "%.2f%%</td>\n", (double)size / divisor * 100.0);
359
360 /*
361 * Output a subdirectory marker.
362 */
363 htprintf(ctx, "<td style=\"padding: 0.2em\">");
364 if (vec->name) {
365 int doing_href = 0;
366
367 if (ctx->format && vec->want_href) {
368 snprintf(ctx->href, ctx->hreflen, ctx->format, vec->index);
369 htprintf(ctx, "<a href=\"%s\">", ctx->href);
370 doing_href = 1;
371 }
372 htescape(ctx, vec->name, strlen(vec->name), 1);
373 if (doing_href)
374 htprintf(ctx, "</a>");
375 }
376 htprintf(ctx, "</td>\n</tr>\n");
377 }
378
379 char *html_query(const void *t, unsigned long index, const char *format)
380 {
381 struct html actx, *ctx = &actx;
382 char *path, *path2, *p, *q, *href;
383 char agebuf1[80], agebuf2[80];
384 size_t pathlen, hreflen;
385 unsigned long index2;
386 int i;
387 struct vector **vecs;
388 int nvecs, vecsize;
389 unsigned long xi1, xi2, xj1, xj2;
390
391 if (index >= trie_count(t))
392 return NULL;
393
394 ctx->buf = NULL;
395 ctx->buflen = ctx->bufsize = 0;
396 ctx->t = t;
397 ctx->format = format;
398 htprintf(ctx, "<html>\n");
399
400 path = snewn(1+trie_maxpathlen(t), char);
401 ctx->path2 = path2 = snewn(1+trie_maxpathlen(t), char);
402 if (format) {
403 hreflen = strlen(format) + 100;
404 href = snewn(hreflen, char);
405 } else {
406 hreflen = 0;
407 href = NULL;
408 }
409 ctx->hreflen = hreflen;
410 ctx->href = href;
411
412 /*
413 * HEAD section.
414 */
415 htprintf(ctx, "<head>\n");
416 trie_getpath(t, index, path);
417 htprintf(ctx, "<title>agedu: ");
418 htescape(ctx, path, strlen(path), 0);
419 htprintf(ctx, "</title>\n");
420 htprintf(ctx, "</head>\n");
421
422 /*
423 * Begin BODY section.
424 */
425 htprintf(ctx, "<body>\n");
426 htprintf(ctx, "<h3 align=center>Disk space breakdown by"
427 " last-access time</h3>\n");
428
429 /*
430 * Show the pathname we're centred on, with hyperlinks to
431 * parent directories where available.
432 */
433 htprintf(ctx, "<p align=center>\n<code>");
434 q = path;
435 for (p = strchr(path, '/'); p; p = strchr(p+1, '/')) {
436 int doing_href = 0;
437 /*
438 * See if this path prefix exists in the trie. If so,
439 * generate a hyperlink.
440 */
441 *p = '\0';
442 index2 = trie_before(t, path);
443 trie_getpath(t, index2, path2);
444 if (!strcmp(path, path2) && format) {
445 snprintf(href, hreflen, format, index2);
446 htprintf(ctx, "<a href=\"%s\">", href);
447 doing_href = 1;
448 }
449 *p = '/';
450 htescape(ctx, q, p - q, 1);
451 q = p + 1;
452 if (doing_href)
453 htprintf(ctx, "</a>");
454 htprintf(ctx, "/");
455 }
456 htescape(ctx, q, strlen(q), 1);
457 htprintf(ctx, "</code>\n");
458
459 /*
460 * Decide on the age limit of our colour coding, establish the
461 * colour thresholds, and write out a key.
462 */
463 ctx->oldest = index_order_stat(t, 0.05); /* FIXME: configurability? */
464 ctx->newest = index_order_stat(t, 1.0);
465 ctx->now = time(NULL);
466 ctx->oldest = round_and_format_age(ctx, ctx->oldest, agebuf1, -1);
467 ctx->newest = round_and_format_age(ctx, ctx->newest, agebuf2, +1);
468 for (i = 0; i < MAXCOLOUR-1; i++) {
469 ctx->thresholds[i] =
470 ctx->oldest + (ctx->newest - ctx->oldest) * i / MAXCOLOUR;
471 }
472 htprintf(ctx, "<p align=center>Key to colour coding (mouse over for more detail):\n");
473 htprintf(ctx, "<p align=center style=\"padding: 0; margin-top:0.4em; "
474 "margin-bottom:1em\"");
475 begin_colour_bar(ctx);
476 htprintf(ctx, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1);
477 for (i = 0; i < MAXCOLOUR; i++)
478 add_to_colour_bar(ctx, i, 1);
479 htprintf(ctx, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2);
480 end_colour_bar(ctx);
481
482 /*
483 * Begin the main table.
484 */
485 htprintf(ctx, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
486
487 /*
488 * Find the total size of our entire subdirectory. We'll use
489 * that as the scale for all the colour bars in this report.
490 */
491 ctx->totalsize = fetch_size(t, path, ULLONG_MAX);
492
493 /*
494 * Generate a report line for the whole subdirectory.
495 */
496 vecsize = 64;
497 vecs = snewn(vecsize, struct vector *);
498 nvecs = 1;
499 vecs[0] = make_vector(ctx, path, 0, NULL);
500 print_heading(ctx, "Overall");
501 write_report_line(ctx, vecs[0]);
502
503 /*
504 * Now generate report lines for all its children, and the
505 * files contained in it.
506 */
507 print_heading(ctx, "Subdirectories");
508
509 vecs[0]->name = dupstr("[files]");
510 get_indices(t, path, &xi1, &xi2);
511 xi1++;
512 pathlen = strlen(path);
513 while (xi1 < xi2) {
514 trie_getpath(t, xi1, path2);
515 get_indices(t, ctx->path2, &xj1, &xj2);
516 xi1 = xj2;
517 if (xj2 - xj1 <= 1)
518 continue; /* skip individual files */
519 if (nvecs >= vecsize) {
520 vecsize = nvecs * 3 / 2 + 64;
521 vecs = sresize(vecs, vecsize, struct vector *);
522 }
523 assert(strlen(path2) > pathlen);
524 vecs[nvecs] = make_vector(ctx, path2, 1, path2 + pathlen + 1);
525 for (i = 0; i <= MAXCOLOUR; i++)
526 vecs[0]->sizes[i] -= vecs[nvecs]->sizes[i];
527 nvecs++;
528 }
529
530 qsort(vecs, nvecs, sizeof(vecs[0]), vec_compare);
531
532 for (i = 0; i < nvecs; i++)
533 write_report_line(ctx, vecs[i]);
534
535 /*
536 * Close the main table.
537 */
538 htprintf(ctx, "</table>\n");
539
540 /*
541 * Finish up and tidy up.
542 */
543 htprintf(ctx, "</body>\n");
544 htprintf(ctx, "</html>\n");
545 sfree(href);
546 sfree(path2);
547 sfree(path);
548 for (i = 0; i < nvecs; i++) {
549 sfree(vecs[i]->name);
550 sfree(vecs[i]);
551 }
552 sfree(vecs);
553
554 return ctx->buf;
555 }