2012-03-22 21:09:54 +00:00
|
|
|
/* disk free space checking, C mini-library
|
|
|
|
*
|
|
|
|
* Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
*
|
|
|
|
* Licensed under the GNU GPL version 3 or higher.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Include appropriate headers for the OS, and define what will be used to
|
|
|
|
* check the free space. */
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/mount.h>
|
2012-03-23 02:48:19 +00:00
|
|
|
/* In newer OSX versions, statfs64 is deprecated, in favor of statfs,
|
2012-03-23 16:05:05 +00:00
|
|
|
* which is 64 bit only with a build option -- but statfs64 still works,
|
2012-03-23 02:48:19 +00:00
|
|
|
* and this keeps older OSX also supported. */
|
2012-03-22 21:09:54 +00:00
|
|
|
# define STATCALL statfs64
|
2012-03-23 16:05:05 +00:00
|
|
|
# define STATSTRUCT statfs64
|
2012-03-22 21:09:54 +00:00
|
|
|
#else
|
2012-03-22 21:30:35 +00:00
|
|
|
#if defined (__FreeBSD__)
|
2012-03-22 21:09:54 +00:00
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/mount.h>
|
2012-03-22 21:30:35 +00:00
|
|
|
# define STATCALL statfs /* statfs64 not yet tested on a real FreeBSD machine */
|
2012-03-23 16:05:05 +00:00
|
|
|
# define STATSTRUCT statfs
|
2012-03-22 21:30:35 +00:00
|
|
|
#else
|
|
|
|
#if defined (__FreeBSD_kernel__) /* Debian kFreeBSD */
|
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/mount.h>
|
|
|
|
# define STATCALL statfs64
|
2012-03-23 16:05:05 +00:00
|
|
|
# define STATSTRUCT statfs
|
2012-04-12 19:48:59 +00:00
|
|
|
# warning free space checking code temporarily disabled due to build failure
|
|
|
|
# define UNKNOWN
|
2012-03-22 21:09:54 +00:00
|
|
|
#else
|
|
|
|
#if defined (__linux__)
|
2012-03-23 02:48:19 +00:00
|
|
|
/* This is a POSIX standard, so might also work elsewhere. */
|
2012-03-22 21:09:54 +00:00
|
|
|
# include <sys/statvfs.h>
|
|
|
|
# define STATCALL statvfs
|
2012-03-23 16:05:05 +00:00
|
|
|
# define STATSTRUCT statvfs
|
2012-03-22 21:09:54 +00:00
|
|
|
#else
|
|
|
|
# warning free space checking code not available for this OS
|
|
|
|
# define UNKNOWN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-03-22 21:30:35 +00:00
|
|
|
#endif
|
2012-03-22 21:09:54 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2012-03-22 21:30:35 +00:00
|
|
|
#include <stdio.h>
|
2012-03-22 21:09:54 +00:00
|
|
|
|
|
|
|
/* Checks the amount of disk that is available to regular (non-root) users.
|
|
|
|
* (If there's an error, or this is not supported,
|
|
|
|
* returns 0 and sets errno to nonzero.)
|
|
|
|
*/
|
|
|
|
unsigned long long int diskfree(const char *path) {
|
|
|
|
#ifdef UNKNOWN
|
|
|
|
errno = 1;
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
unsigned long long int available, blocksize;
|
|
|
|
struct STATSTRUCT buf;
|
|
|
|
|
2012-04-18 17:23:33 +00:00
|
|
|
if (STATCALL(path, &buf) != 0)
|
2012-03-22 21:09:54 +00:00
|
|
|
return 0; /* errno is set */
|
2012-04-18 17:23:33 +00:00
|
|
|
else
|
|
|
|
errno = 0;
|
2012-03-22 21:09:54 +00:00
|
|
|
|
|
|
|
available = buf.f_bavail;
|
|
|
|
blocksize = buf.f_bsize;
|
|
|
|
return available * blocksize;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
main () {
|
|
|
|
printf("%lli\n", diskfree("."));
|
|
|
|
}
|
|
|
|
*/
|