diff options
author | Nick Mathewson <nickm@torproject.org> | 2015-08-10 15:14:49 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2015-08-10 15:14:49 -0400 |
commit | 81e0fd8360d66a8e9901a50ca2245ffedc89fc3c (patch) | |
tree | 13369a46a8956de6da618a51d053e9d63b0fdfad /src/common/compat.c | |
parent | 8afbc154f794835633559f81a89232f1bccc1d8e (diff) | |
parent | 50049df0d4b9ba3654749cfbb111c72e07d54bc5 (diff) | |
download | tor-81e0fd8360d66a8e9901a50ca2245ffedc89fc3c.tar.gz tor-81e0fd8360d66a8e9901a50ca2245ffedc89fc3c.zip |
Merge remote-tracking branch 'public/feature16734'
Diffstat (limited to 'src/common/compat.c')
-rw-r--r-- | src/common/compat.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index 28b8344852..10c43416bf 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -68,6 +68,9 @@ #ifdef HAVE_CRT_EXTERNS_H #include <crt_externs.h> #endif +#ifdef HAVE_SYS_STATVFS_H +#include <sys/statvfs.h> +#endif #ifdef _WIN32 #include <conio.h> @@ -3382,3 +3385,42 @@ tor_getpass(const char *prompt, char *output, size_t buflen) #endif } +/** Return the amount of free disk space we have permission to use, in + * bytes. Return -1 if the amount of free space can't be determined. */ +int64_t +tor_get_avail_disk_space(const char *path) +{ +#ifdef HAVE_STATVFS + struct statvfs st; + int r; + memset(&st, 0, sizeof(st)); + + r = statvfs(path, &st); + if (r < 0) + return -1; + + int64_t result = st.f_bavail; + if (st.f_frsize) { + result *= st.f_frsize; + } else if (st.f_bsize) { + result *= st.f_bsize; + } else { + return -1; + } + + return result; +#elif defined(_WIN32) + ULARGE_INTEGER freeBytesAvail; + BOOL ok; + + ok = GetDiskFreeSpaceEx(path, &freeBytesAvail, NULL, NULL); + if (!ok) { + return -1; + } + return (int64_t)freeBytesAvail; +#else + (void)path; + errno = ENOSYS; + return -1; +#endif +} |