[PATCH 1/4] Add a default size argument to STV_FileSize

Martin Blix Grydeland martin at varnish-software.com
Mon Oct 6 17:53:12 CEST 2014


---
 bin/varnishd/storage/stevedore_utils.c        | 29 ++++++++++++++++++---------
 bin/varnishd/storage/storage.h                |  4 ++--
 bin/varnishd/storage/storage_file.c           | 11 +++++-----
 bin/varnishd/storage/storage_persistent_mgt.c |  2 +-
 4 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/bin/varnishd/storage/stevedore_utils.c b/bin/varnishd/storage/stevedore_utils.c
index 540f911..6f55b69 100644
--- a/bin/varnishd/storage/stevedore_utils.c
+++ b/bin/varnishd/storage/stevedore_utils.c
@@ -168,13 +168,21 @@ stv_fssize(int fd, unsigned *bs)
  * Decide file size.
  *
  * If the sizespecification is empty and the file exists with non-zero
- * size, use that, otherwise, interpret the specification.
+ * size
+ *	use that
+ * else if the sizespecification is non-empty
+ *	interpret the specification
+ * else if the default specification is non-empty
+ *	interpret the default specification
+ * else
+ *	bail with ARGV_ERR
  *
  * Handle off_t sizes and pointer width limitations.
  */
 
 uintmax_t
-STV_FileSize(int fd, const char *size, unsigned *granularity, const char *ctx)
+STV_FileSize(int fd, const char *size, const char *defsize,
+    unsigned *granularity, const char *ctx)
 {
 	uintmax_t l, fssize;
 	unsigned bs;
@@ -190,27 +198,30 @@ STV_FileSize(int fd, const char *size, unsigned *granularity, const char *ctx)
 	fssize = stv_fssize(fd, &bs);
 	XXXAZ(bs % *granularity);
 
-	if ((size == NULL || *size == '\0') && st.st_size != 0) {
+	l = 0;
+	if (size != NULL && *size == '\0')
+		size = NULL;
+	if (size == NULL && st.st_size != 0) {
 		/*
 		 * We have no size specification, but an existing file,
 		 * use its existing size.
 		 */
 		l = st.st_size;
-	} else {
-		AN(size);
-		q = VNUM_2bytes(size, &l, 0);
+	} else if (size == NULL)
+		size = defsize;
 
+	if (size != NULL) {
+		q = VNUM_2bytes(size, &l, 0);
 		if (q != NULL)
 			ARGV_ERR("(%s) size \"%s\": %s\n", ctx, size, q);
-
 		if (l < 1024*1024)
 			ARGV_ERR("(%s) size \"%s\": too small, "
 			    "did you forget to specify M or G?\n", ctx, size);
-
 		if (l > fssize)
 			ARGV_ERR("(%s) size \"%s\": larger than file system\n",
 			    ctx, size);
-	}
+	} else if (l == 0)
+		ARGV_ERR("(%s) no size specified\n", ctx);
 
 	/*
 	 * This trickery wouldn't be necessary if X/Open would
diff --git a/bin/varnishd/storage/storage.h b/bin/varnishd/storage/storage.h
index 0d162ca..06264ae 100644
--- a/bin/varnishd/storage/storage.h
+++ b/bin/varnishd/storage/storage.h
@@ -194,8 +194,8 @@ extern struct stevedore *stv_transient;
 
 /*--------------------------------------------------------------------*/
 int STV_GetFile(const char *fn, int *fdp, const char **fnp, const char *ctx);
-uintmax_t STV_FileSize(int fd, const char *size, unsigned *granularity,
-    const char *ctx);
+uintmax_t STV_FileSize(int fd, const char *size, const char *defsize,
+    unsigned *granularity, const char *ctx);
 struct object *STV_MkObject(struct stevedore *, struct objcore *, void *ptr);
 
 struct lru *LRU_Alloc(void);
diff --git a/bin/varnishd/storage/storage_file.c b/bin/varnishd/storage/storage_file.c
index 863ad25..db634f2 100644
--- a/bin/varnishd/storage/storage_file.c
+++ b/bin/varnishd/storage/storage_file.c
@@ -98,19 +98,20 @@ struct smf_sc {
 
 /*--------------------------------------------------------------------*/
 
+static const char default_size[] = "100M";
+static const char default_filename[] = ".";
+
 static void
 smf_initfile(struct smf_sc *sc, const char *size)
 {
-	sc->filesize = STV_FileSize(sc->fd, size, &sc->pagesize, "-sfile");
+	sc->filesize = STV_FileSize(sc->fd, size, default_size, &sc->pagesize,
+	    "-sfile");
 
 	AZ(ftruncate(sc->fd, (off_t)sc->filesize));
 
 	/* XXX: force block allocation here or in open ? */
 }
 
-static const char default_size[] = "100M";
-static const char default_filename[] = ".";
-
 static void
 smf_init(struct stevedore *parent, int ac, char * const *av)
 {
@@ -122,7 +123,7 @@ smf_init(struct stevedore *parent, int ac, char * const *av)
 	AZ(av[ac]);
 
 	fn = default_filename;
-	size = default_size;
+	size = NULL;
 	page_size = getpagesize();
 
 	if (ac > 3)
diff --git a/bin/varnishd/storage/storage_persistent_mgt.c b/bin/varnishd/storage/storage_persistent_mgt.c
index 33f41e0..e3a41b0 100644
--- a/bin/varnishd/storage/storage_persistent_mgt.c
+++ b/bin/varnishd/storage/storage_persistent_mgt.c
@@ -168,7 +168,7 @@ smp_mgt_init(struct stevedore *parent, int ac, char * const *av)
 
 	sc->align = sizeof(void*) * 2;
 	sc->granularity = getpagesize();
-	sc->mediasize = STV_FileSize(sc->fd, av[1], &sc->granularity,
+	sc->mediasize = STV_FileSize(sc->fd, av[1], NULL, &sc->granularity,
 	    "-spersistent");
 
 	AZ(ftruncate(sc->fd, sc->mediasize));
-- 
2.1.1




More information about the varnish-dev mailing list