Commit 5e8287f3 authored by Dag Erling Smørgrav's avatar Dag Erling Smørgrav

Tweak storage file size calculations. Avoid overflow when calculating 80%

of available storage on 32-bit Linux (most fields in struct statfs are long
instead of int64_t as in BSD)

git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@1009 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent efa50946
...@@ -147,23 +147,26 @@ smf_calcsize(struct smf_sc *sc, const char *size, int newfile) ...@@ -147,23 +147,26 @@ smf_calcsize(struct smf_sc *sc, const char *size, int newfile)
} }
} }
/*
* This trickery wouldn't be necessary if X/Open would
* just add OFF_MAX to <limits.h>...
*/
o = l; o = l;
if (o != l || o < 0) { if (o != l || o < 0) {
fprintf(stderr,
"Warning: size reduced to system limit (off_t)\n");
do { do {
l >>= 1; l >>= 1;
o = l; o = l;
} while (o != l || o < 0); } while (o != l || o < 0);
fprintf(stderr, "WARNING: storage file size reduced"
" to %ju due to system limitations\n", l);
} }
if (l < st.st_size) { if (l < st.st_size) {
AZ(ftruncate(sc->fd, l)); AZ(ftruncate(sc->fd, l));
} else if (l - st.st_size > fsst.f_bsize * fsst.f_bavail) { } else if (l - st.st_size > fsst.f_bsize * fsst.f_bavail) {
fprintf(stderr, l = ((uintmax_t)fsst.f_bsize * fsst.f_bavail * 80) / 100;
"Warning: size larger than filesystem free space," fprintf(stderr, "WARNING: storage file size reduced"
" reduced to 80%% of free space.\n"); " to %ju (80%% of available disk space)\n", l);
l = (fsst.f_bsize * fsst.f_bavail * 80) / 100;
} }
} }
...@@ -177,13 +180,13 @@ smf_calcsize(struct smf_sc *sc, const char *size, int newfile) ...@@ -177,13 +180,13 @@ smf_calcsize(struct smf_sc *sc, const char *size, int newfile)
exit (2); exit (2);
} }
if (expl < 3 && sizeof(void *) == 4 && l > (1ULL << 31)) { if (expl < 3 && sizeof(void *) == 4 && l > INT32_MAX) {
fprintf(stderr, fprintf(stderr,
"NB: Limiting size to 2GB on 32 bit architecture to" "NB: Limiting size to 2GB on 32 bit architecture to"
" prevent running out of\naddress space." " prevent running out of\naddress space."
" Specifiy explicit size to override.\n" " Specifiy explicit size to override.\n"
); );
l = 1ULL << 31; l = INT32_MAX;
} }
printf("file %s size %ju bytes (%ju fs-blocks, %ju pages)\n", printf("file %s size %ju bytes (%ju fs-blocks, %ju pages)\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment