[4.0] d98c415 VFIL_fsinfo function to retrieve file system information

Martin Blix Grydeland martin at varnish-software.com
Tue Feb 24 11:53:12 CET 2015


commit d98c415dfc02df0002f9462c8311fc9eb9d117b3
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Tue Oct 7 13:27:57 2014 +0200

    VFIL_fsinfo function to retrieve file system information

diff --git a/include/vfil.h b/include/vfil.h
index 533dfd8..32f2c78 100644
--- a/include/vfil.h
+++ b/include/vfil.h
@@ -34,3 +34,4 @@ int VFIL_tmpfile(char *);
 char *VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz);
 char *VFIL_readfd(int fd, ssize_t *sz);
 int VFIL_nonblocking(int fd);
+int VFIL_fsinfo(int fd, unsigned *pbs, uintmax_t *size, uintmax_t *space);
diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
index 920a06f..b3743f2 100644
--- a/lib/libvarnish/vfil.c
+++ b/lib/libvarnish/vfil.c
@@ -38,6 +38,18 @@
 #include <stdlib.h>
 #include <time.h>
 #include <unistd.h>
+#include <stdint.h>
+#include <sys/types.h>
+#ifdef HAVE_SYS_MOUNT_H
+#  include <sys/param.h>
+#  include <sys/mount.h>
+#endif
+#ifdef HAVE_SYS_STATVFS_H
+#  include <sys/statvfs.h>
+#endif
+#ifdef HAVE_SYS_VFS_H
+#  include <sys/vfs.h>
+#endif
 
 #include "vas.h"
 #include "vdef.h"
@@ -136,3 +148,42 @@ VFIL_nonblocking(int fd)
 	assert(i != -1);
 	return (i);
 }
+
+/*
+ * Get file system information from an fd
+ * Returns block size, total size and space available in the passed pointers
+ * Returns 0 on success, or -1 on failure with errno set
+ */
+int
+VFIL_fsinfo(int fd, unsigned *pbs, uintmax_t *psize, uintmax_t *pspace)
+{
+	unsigned bs;
+	uintmax_t size, space;
+#if defined(HAVE_SYS_STATVFS_H)
+	struct statvfs fsst;
+
+	if (fstatvfs(fd, &fsst))
+		return (-1);
+	bs = fsst.f_frsize;
+	size = fsst.f_blocks * fsst.f_frsize;
+	space = fsst.f_bavail * fsst.f_frsize;
+#elif defined(HAVE_SYS_MOUNT_H) || defined(HAVE_SYS_VFS_H)
+	struct statfs fsst;
+
+	if (fstatfs(fd, &fsst))
+		return (-1);
+	bs = fsst.f_bsize;
+	size = fsst.f_blocks * fsst.f_bsize;
+	space = fsst.f_bavail * fsst.f_bsize;
+#else
+#error no struct statfs / struct statvfs
+#endif
+
+	if (pbs)
+		*pbs = bs;
+	if (psize)
+		*psize = size;
+	if (pspace)
+		*pspace = space;
+	return (0);
+}



More information about the varnish-commit mailing list