[master] 0bd8445 Fix up the binary file reading VSL cursor

Martin Blix Grydeland martin at varnish-cache.org
Thu Jun 13 12:41:23 CEST 2013


commit 0bd8445a93cb7abd2e26d09836f7225e9675686e
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Wed May 22 10:53:29 2013 +0200

    Fix up the binary file reading VSL cursor

diff --git a/include/vapi/vsl_int.h b/include/vapi/vsl_int.h
index 8765b84..419af70 100644
--- a/include/vapi/vsl_int.h
+++ b/include/vapi/vsl_int.h
@@ -80,6 +80,7 @@ struct VSL_head {
 
 #define VSL_LENMASK		0xffff
 #define VSL_WORDS(len)		(((len) + 3) / 4)
+#define VSL_BYTES(words)	((words) * 4)
 #define VSL_END(ptr, len)	((ptr) + 2 + VSL_WORDS(len))
 #define VSL_NEXT(ptr)		VSL_END(ptr, VSL_LEN(ptr))
 #define VSL_LEN(ptr)		((ptr)[0] & VSL_LENMASK)
diff --git a/lib/libvarnishapi/vsl_api.h b/lib/libvarnishapi/vsl_api.h
index b2f9fb7..05987a6 100644
--- a/lib/libvarnishapi/vsl_api.h
+++ b/lib/libvarnishapi/vsl_api.h
@@ -33,7 +33,7 @@
 #include "vqueue.h"
 #include "vapi/vsm.h"
 
-#define VSL_FILE_HEAD			"VSL"
+#define VSL_FILE_ID			"VSL"
 
 struct vslc_shmptr {
 	uint32_t	*ptr;
diff --git a/lib/libvarnishapi/vsl_cursor.c b/lib/libvarnishapi/vsl_cursor.c
index 9bee281..443a176 100644
--- a/lib/libvarnishapi/vsl_cursor.c
+++ b/lib/libvarnishapi/vsl_cursor.c
@@ -325,28 +325,30 @@ vslc_file_next(void *cursor)
 
 	do {
 		c->c.c.rec.ptr = NULL;
-		assert(c->buflen >= 2 * 4);
-		i = vslc_file_readn(c->fd, c->buf, 2 * 4);
+		assert(c->buflen >= VSL_BYTES(2));
+		i = vslc_file_readn(c->fd, c->buf, VSL_BYTES(2));
 		if (i < 0)
 			return (-4);	/* I/O error */
 		if (i == 0)
 			return (-1);	/* EOF */
-		assert(i == 2 * 4);
-		l = (2 + VSL_WORDS(VSL_LEN(c->buf))) * 4;
+		assert(i == VSL_BYTES(2));
+		l = VSL_BYTES(2 + VSL_WORDS(VSL_LEN(c->buf)));
 		if (c->buflen < l) {
 			c->buf = realloc(c->buf, 2 * l);
 			AN(c->buf);
 			c->buflen = 2 * l;
 		}
-		i = vslc_file_readn(c->fd, c->buf + 2, l - 2 * 4);
-		if (i < 0)
-			return (-4);	/* I/O error */
-		if (i == 0)
-			return (-1);	/* EOF */
-		assert(i == l - 2 * 4);
+		if (l > VSL_BYTES(2)) {
+			i = vslc_file_readn(c->fd, c->buf + 2,
+			    l - VSL_BYTES(2));
+			if (i < 0)
+				return (-4);	/* I/O error */
+			if (i == 0)
+				return (-1);	/* EOF */
+			assert(i == l - VSL_BYTES(2));
+		}
 		c->c.c.rec.ptr = c->buf;
-	} while (c->c.c.rec.ptr != NULL &&
-	    VSL_TAG(c->c.c.rec.ptr) == SLT__Batch);
+	} while (VSL_TAG(c->c.c.rec.ptr) == SLT__Batch);
 	return (1);
 }
 
@@ -371,18 +373,19 @@ VSL_CursorFile(struct VSL_data *vsl, const char *name)
 {
 	struct vslc_file *c;
 	int fd;
-	char buf[4];
+	char buf[] = VSL_FILE_ID;
 	ssize_t i;
 
-	if (!strcmp(name, "-")) {
+	if (!strcmp(name, "-"))
+		fd = STDIN_FILENO;
+	else {
 		fd = open(name, O_RDONLY);
 		if (fd < 0) {
 			vsl_diag(vsl, "Could not open %s: %s\n", name,
 			    strerror(errno));
 			return (NULL);
 		}
-	} else
-		fd = STDIN_FILENO;
+	}
 
 	i = vslc_file_readn(fd, buf, sizeof buf);
 	if (i <= 0) {
@@ -393,7 +396,7 @@ VSL_CursorFile(struct VSL_data *vsl, const char *name)
 		return (NULL);
 	}
 	assert(i == sizeof buf);
-	if (memcmp(buf, VSL_FILE_HEAD, sizeof buf)) {
+	if (memcmp(buf, VSL_FILE_ID, sizeof buf)) {
 		if (fd > STDIN_FILENO)
 			(void)close(fd);
 		vsl_diag(vsl, "Not a VSL file: %s\n", name);



More information about the varnish-commit mailing list