Add decimal to file sizes

pull/62/head
Anders Trier 2016-09-22 12:00:59 +02:00 committed by Adrian Perez
parent f2f0cdc9de
commit 1e438a694d
1 changed files with 20 additions and 36 deletions

View File

@ -190,6 +190,10 @@ static ngx_conf_enum_t ngx_http_fancyindex_sort_criteria[] = {
*/ */
#define ngx_sizeof_ssz(_s) (sizeof(_s) - 1) #define ngx_sizeof_ssz(_s) (sizeof(_s) - 1)
/**
* Compute the length of a statically allocated array
*/
#define DIM(x) (sizeof(x)/sizeof(*(x)))
/** /**
* Copy a static zero-terminated string. Useful to output template * Copy a static zero-terminated string. Useful to output template
@ -552,16 +556,20 @@ make_content_buf(
off_t length; off_t length;
size_t len, root, copy, allocated; size_t len, root, copy, allocated;
u_char *filename, *last, scale; int64_t multiplier;
u_char *filename, *last;
ngx_tm_t tm; ngx_tm_t tm;
ngx_array_t entries; ngx_array_t entries;
ngx_time_t *tp; ngx_time_t *tp;
ngx_uint_t i; ngx_uint_t i, j;
ngx_int_t size;
ngx_str_t path; ngx_str_t path;
ngx_dir_t dir; ngx_dir_t dir;
ngx_buf_t *b; ngx_buf_t *b;
static const char *sizes[] = { "EiB", "PiB", "TiB", "GiB", "MiB", "KiB", "B" };
static const int64_t exbibyte = 1024LL * 1024LL * 1024LL *
1024LL * 1024LL * 1024LL;
/* /*
* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_FANCYINDEX_PREALLOCATE * NGX_DIR_MASK_LEN is lesser than NGX_HTTP_FANCYINDEX_PREALLOCATE
*/ */
@ -982,41 +990,17 @@ make_content_buf(
*b->last++ = '-'; *b->last++ = '-';
} else { } else {
length = entry[i].size; length = entry[i].size;
multiplier = exbibyte;
if (length > 1024 * 1024 * 1024 - 1) { for (j = 0; j < DIM(sizes) - 1 && length < multiplier; j++)
size = (ngx_int_t) (length / (1024 * 1024 * 1024)); multiplier /= 1024;
if ((length % (1024 * 1024 * 1024))
> (1024 * 1024 * 1024 / 2 - 1))
{
size++;
}
scale = 'G';
} else if (length > 1024 * 1024 - 1) { /* If we are showing the filesize in bytes, do not show a decimal */
size = (ngx_int_t) (length / (1024 * 1024)); if (j == DIM(sizes) - 1)
if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) { b->last = ngx_sprintf(b->last, "%d %s", length, sizes[j]);
size++; else
} b->last = ngx_sprintf(b->last, "%.1f %s",
scale = 'M'; (float) length / multiplier, sizes[j]);
} else if (length > 9999) {
size = (ngx_int_t) (length / 1024);
if (length % 1024 > 511) {
size++;
}
scale = 'K';
} else {
size = (ngx_int_t) length;
scale = '\0';
}
if (scale) {
b->last = ngx_sprintf(b->last, "%6i%c", size, scale);
} else {
b->last = ngx_sprintf(b->last, " %6i", size);
}
} }
} }