2012-03-22 21:09:54 +00:00
|
|
|
/* disk free space checking, C mini-library
|
|
|
|
*
|
2014-01-23 02:19:52 +00:00
|
|
|
* Copyright 2012, 2014 Joey Hess <joey@kitenet.net>
|
2012-03-22 21:09:54 +00:00
|
|
|
*
|
2014-05-10 14:01:27 +00:00
|
|
|
* License: BSD-2-clause
|
2012-03-22 21:09:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Include appropriate headers for the OS, and define what will be used to
|
|
|
|
* check the free space. */
|
|
|
|
#if defined(__APPLE__)
|
2014-12-31 16:20:27 +00:00
|
|
|
# define _DARWIN_FEATURE_64_BIT_INODE 1
|
2012-03-22 21:09:54 +00:00
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/mount.h>
|
2014-12-31 16:20:27 +00:00
|
|
|
# define STATCALL statfs
|
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
|
2013-02-27 06:39:22 +00:00
|
|
|
#if defined __ANDROID__
|
2013-02-10 19:48:38 +00:00
|
|
|
# warning free space checking code not available for Android
|
|
|
|
# define UNKNOWN
|
|
|
|
#else
|
2012-06-17 22:10:57 +00:00
|
|
|
#if defined (__linux__) || defined (__FreeBSD_kernel__)
|
|
|
|
/* Linux or Debian kFreeBSD */
|
|
|
|
/* This is a POSIX standard, so might also work elsewhere too. */
|
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
|
2013-02-10 19:48:38 +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
|
|
|
|
2014-01-23 02:19:52 +00:00
|
|
|
unsigned long long int get(const char *path, int req) {
|
2012-03-22 21:09:54 +00:00
|
|
|
#ifdef UNKNOWN
|
|
|
|
errno = 1;
|
|
|
|
return 0;
|
|
|
|
#else
|
2014-01-23 02:19:52 +00:00
|
|
|
unsigned long long int v, blocksize;
|
2012-03-22 21:09:54 +00:00
|
|
|
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
|
|
|
|
2014-01-23 02:19:52 +00:00
|
|
|
switch (req) {
|
|
|
|
case 0:
|
|
|
|
v = buf.f_blocks;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
v = buf.f_bavail;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-22 21:09:54 +00:00
|
|
|
blocksize = buf.f_bsize;
|
2014-01-23 02:19:52 +00:00
|
|
|
return v * blocksize;
|
2012-03-22 21:09:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-23 02:19:52 +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) {
|
|
|
|
return get(path, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gets the total size of the disk. */
|
|
|
|
unsigned long long int disksize(const char *path) {
|
|
|
|
return get(path, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-22 21:09:54 +00:00
|
|
|
/*
|
|
|
|
main () {
|
|
|
|
printf("%lli\n", diskfree("."));
|
|
|
|
}
|
|
|
|
*/
|