Commit 22ad1c95 authored by Martin Blix Grydeland's avatar Martin Blix Grydeland

VFIL_fsinfo function to retrieve file system information

parent 3eeafa8b
......@@ -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);
......@@ -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);
}
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