From 4c82356ad323db57c861f5422ba0b34c96cf2db0 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Thu, 31 Mar 2016 23:05:26 +0000 Subject: Add disk_space() to llvm::fs Summary: Adapted from Boost::filesystem. (This is a reapply by reverting commit r265062 and fixing the WinAPI part) Differential Revision: http://reviews.llvm.org/D18467 From: Mehdi Amini llvm-svn: 265068 --- llvm/lib/Support/Unix/Path.inc | 32 +++++++++++++++++++++++++++++++- llvm/lib/Support/Windows/Path.inc | 13 +++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Support') diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc index a80e0ddb8a1..f79c4636e5a 100644 --- a/llvm/lib/Support/Unix/Path.inc +++ b/llvm/lib/Support/Unix/Path.inc @@ -60,6 +60,24 @@ # define PATH_MAX 4096 #endif +#include +#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__ANDROID__) +#include +#define STATVFS statvfs +#define STATVFS_F_FRSIZE(vfs) vfs.f_frsize +#else +#ifdef __OpenBSD__ +#include +#elif defined(__ANDROID__) +#include +#else +#include +#endif +#define STATVFS statfs +#define STATVFS_F_FRSIZE(vfs) static_cast(vfs.f_bsize) +#endif + + using namespace llvm; namespace llvm { @@ -70,7 +88,7 @@ namespace fs { defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__) static int test_dir(char ret[PATH_MAX], const char *dir, const char *bin) -{ +{ struct stat sb; char fullpath[PATH_MAX]; @@ -190,6 +208,18 @@ UniqueID file_status::getUniqueID() const { return UniqueID(fs_st_dev, fs_st_ino); } +ErrorOr disk_space(const Twine Path) { + struct STATVFS Vfs; + if (::STATVFS(Path.str().c_str(), &Vfs)) + return std::error_code(errno, std::generic_category()); + auto FrSize = STATVFS_F_FRSIZE(Vfs); + space_info SpaceInfo; + SpaceInfo.capacity = static_cast(Vfs.f_blocks) * FrSize; + SpaceInfo.free = static_cast(Vfs.f_bfree) * FrSize; + SpaceInfo.available = static_cast(Vfs.f_bavail) * FrSize; + return SpaceInfo; +} + std::error_code current_path(SmallVectorImpl &result) { result.clear(); diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc index 98fd7b0034a..72604c0e406 100644 --- a/llvm/lib/Support/Windows/Path.inc +++ b/llvm/lib/Support/Windows/Path.inc @@ -151,6 +151,19 @@ UniqueID file_status::getUniqueID() const { return UniqueID(VolumeSerialNumber, FileID); } +ErrorOr disk_space(const Twine Path) { + PULARGE_INTEGER Avail, Total, Free; + if (!::GetDiskFreeSpaceExA(Path.str().c_str(), &Avail, &Total, &Free)) + return mapWindowsError(::GetLastError()); + space_info SpaceInfo; + SpaceInfo.capacity = + (static_cast(Total.HighPart) << 32) + Total.LowPart; + SpaceInfo.Free = (static_cast(Free.HighPart) << 32) + Free.LowPart; + SpaceInfo.available = + (static_cast(Avail.HighPart) << 32) + Avail.LowPart; + return SpaceInfo; +} + TimeValue file_status::getLastAccessedTime() const { ULARGE_INTEGER UI; UI.LowPart = LastAccessedTimeLow; -- cgit v1.2.3