X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/agedu/blobdiff_plain/50e82fdc62d1e7c0747bdc8c17d7ef7b863e1460..a8d1009fa71020a958786761e08945db33e94010:/html.c diff --git a/html.c b/html.c index db6c25b..8293374 100644 --- a/html.c +++ b/html.c @@ -18,12 +18,13 @@ struct html { char *path2; char *href; size_t hreflen; - const char *format; - unsigned long long thresholds[MAXCOLOUR-1]; + const char *format, *rootpage; + unsigned long long thresholds[MAXCOLOUR]; + char *titletexts[MAXCOLOUR+1]; time_t now; }; -static void vhtprintf(struct html *ctx, char *fmt, va_list ap) +static void vhtprintf(struct html *ctx, const char *fmt, va_list ap) { va_list ap2; int size, size2; @@ -49,7 +50,7 @@ static void vhtprintf(struct html *ctx, char *fmt, va_list ap) ctx->buflen += size; } -static void htprintf(struct html *ctx, char *fmt, ...) +static void htprintf(struct html *ctx, const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -171,14 +172,25 @@ static void get_indices(const void *t, char *path, path[pathlen-1] = c2; } -static unsigned long long fetch_size(const void *t, char *path, +static unsigned long long fetch_size(const void *t, + unsigned long xi1, unsigned long xi2, unsigned long long atime) { - unsigned long xi1, xi2; - - get_indices(t, path, &xi1, &xi2); - - return index_query(t, xi2, atime) - index_query(t, xi1, atime); + if (xi2 - xi1 == 1) { + /* + * We are querying an individual file, so we should not + * depend on the index entries either side of the node, + * since they almost certainly don't both exist. Instead, + * just look up the file's size and atime in the main trie. + */ + const struct trie_file *f = trie_getfile(t, xi1); + if (f->atime < atime) + return f->size; + else + return 0; + } else { + return index_query(t, xi2, atime) - index_query(t, xi1, atime); + } } static void htescape(struct html *ctx, const char *s, int n, int italics) @@ -213,7 +225,6 @@ static void begin_colour_bar(struct html *ctx) static void add_to_colour_bar(struct html *ctx, int colour, int pixels) { int r, g, b; - char buf[80]; if (colour >= 0 && colour < 256) /* red -> yellow fade */ r = 255, g = colour, b = 0; @@ -222,26 +233,12 @@ static void add_to_colour_bar(struct html *ctx, int colour, int pixels) else /* background grey */ r = g = b = 240; - if (colour < 0) { - /* no title text here */ - } else if (colour == 0) { - strcpy(buf, "< "); - round_and_format_age(ctx, ctx->thresholds[0], buf+5, 0); - } else if (colour == MAXCOLOUR) { - strcpy(buf, "> "); - round_and_format_age(ctx, ctx->thresholds[MAXCOLOUR-1], buf+5, 0); - } else { - unsigned long long midrange = - (ctx->thresholds[colour] + ctx->thresholds[colour+1]) / 2; - round_and_format_age(ctx, midrange, buf, 0); - } - if (pixels > 0) { htprintf(ctx, "
");
htescape(ctx, vec->name, strlen(vec->name), 1);
+ if (vec->literal)
+ htprintf(ctx, "
");
if (doing_href)
htprintf(ctx, "");
}
htprintf(ctx, "\nKey to colour coding (mouse over for more detail):\n"); htprintf(ctx, "
totalsize = fetch_size(t, path, ULLONG_MAX); + get_indices(t, path, &xi1, &xi2); + ctx->totalsize = fetch_size(t, xi1, xi2, ULLONG_MAX); /* * Generate a report line for the whole subdirectory. @@ -523,7 +596,7 @@ char *html_query(const void *t, unsigned long index, vecsize = 64; vecs = snewn(vecsize, struct vector *); nvecs = 1; - vecs[0] = make_vector(ctx, path, 0, NULL); + vecs[0] = make_vector(ctx, path, 0, 1, NULL, 0); print_heading(ctx, "Overall"); write_report_line(ctx, vecs[0]); @@ -544,14 +617,15 @@ char *html_query(const void *t, unsigned long index, trie_getpath(t, xi1, path2); get_indices(t, ctx->path2, &xj1, &xj2); xi1 = xj2; - if (xj2 - xj1 <= 1) + if (!cfg->showfiles && xj2 - xj1 <= 1) continue; /* skip individual files */ if (nvecs >= vecsize) { vecsize = nvecs * 3 / 2 + 64; vecs = sresize(vecs, vecsize, struct vector *); } assert(strlen(path2) > pathlen); - vecs[nvecs] = make_vector(ctx, path2, 1, path2 + subdirpos); + vecs[nvecs] = make_vector(ctx, path2, downlink && (xj2 - xj1 > 1), 0, + path2 + subdirpos, 1); for (i = 0; i <= MAXCOLOUR; i++) vecs[0]->sizes[i] -= vecs[nvecs]->sizes[i]; nvecs++; @@ -583,3 +657,68 @@ char *html_query(const void *t, unsigned long index, return ctx->buf; } + +int html_dump(const void *t, unsigned long index, unsigned long endindex, + int maxdepth, const struct html_config *cfg, + const char *pathprefix) +{ + /* + * Determine the filename for this file. + */ + assert(cfg->format != NULL); + int prefixlen = strlen(pathprefix); + int fnmax = strlen(pathprefix) + strlen(cfg->format) + 100; + char filename[fnmax]; + strcpy(filename, pathprefix); + make_filename(filename + prefixlen, fnmax - prefixlen, + cfg->format, cfg->rootpage, index); + + /* + * Create the HTML itself. Don't write out downlinks from our + * deepest level. + */ + char *html = html_query(t, index, cfg, maxdepth != 0); + + /* + * Write it out. + */ + FILE *fp = fopen(filename, "w"); + if (!fp) { + fprintf(stderr, "%s: %s: open: %s\n", PNAME, + filename, strerror(errno)); + return 1; + } + if (fputs(html, fp) < 0) { + fprintf(stderr, "%s: %s: write: %s\n", PNAME, + filename, strerror(errno)); + fclose(fp); + return 1; + } + if (fclose(fp) < 0) { + fprintf(stderr, "%s: %s: fclose: %s\n", PNAME, + filename, strerror(errno)); + return 1; + } + + /* + * Recurse. + */ + if (maxdepth != 0) { + unsigned long subindex, subendindex; + int newdepth = (maxdepth > 0 ? maxdepth - 1 : maxdepth); + char path[1+trie_maxpathlen(t)]; + + index++; + while (index < endindex) { + trie_getpath(t, index, path); + get_indices(t, path, &subindex, &subendindex); + index = subendindex; + if (subendindex - subindex > 1) { + if (html_dump(t, subindex, subendindex, newdepth, + cfg, pathprefix)) + return 1; + } + } + } + return 0; +}