[master] cc35e776b Revert previous commit, it breaks sunos

Poul-Henning Kamp phk at FreeBSD.org
Tue Apr 30 22:09:06 UTC 2024


commit cc35e776b816bb99ff14f6c567842bc1950cf0ad
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Apr 30 20:36:08 2024 +0000

    Revert previous commit, it breaks sunos

diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
index 4df3999d0..8d1f3ab60 100644
--- a/lib/libvarnish/vfil.c
+++ b/lib/libvarnish/vfil.c
@@ -230,6 +230,11 @@ VFIL_allocate(int fd, uintmax_t size, int insist)
 {
 	struct stat st;
 	uintmax_t fsspace;
+	size_t l;
+	ssize_t l2, l3;
+	char *buf;
+	ssize_t bufsiz;
+	int retval = 0;
 
 	if (ftruncate(fd, size))
 		return (-1);
@@ -243,10 +248,6 @@ VFIL_allocate(int fd, uintmax_t size, int insist)
 		errno = ENOSPC;
 		return (-1);
 	}
-	if (insist) {
-		/* This falls back to writing zeros, as we want */
-		return (posix_fallocate(fd, 0, size) ? -1 : 0);
-	}
 #if defined(__linux__) && defined(HAVE_FALLOCATE)
 	{
 		/* fallocate will for some filesystems (e.g. xfs) not take
@@ -266,7 +267,31 @@ VFIL_allocate(int fd, uintmax_t size, int insist)
 		}
 	}
 #endif
-	return (0);
+	if (!insist)
+		return (0);
+
+	/* Write size zero bytes to make sure the entire file is allocated
+	   in the file system */
+	if (size > 65536)
+		bufsiz = 64 * 1024;
+	else
+		bufsiz = size;
+	buf = calloc(1, bufsiz);
+	AN(buf);
+	assert(lseek(fd, 0, SEEK_SET) == 0);
+	for (l = 0; l < size; l += l2) {
+		l2 = bufsiz;
+		if (l + l2 > size)
+			l2 = size - l;
+		l3 = write(fd, buf, l2);
+		if (l3 != l2) {
+			retval = -1;
+			break;
+		}
+	}
+	assert(lseek(fd, 0, SEEK_SET) == 0);
+	free(buf);
+	return (retval);
 }
 
 struct vfil_dir {


More information about the varnish-commit mailing list