[PATCH] D18467: Add disk_space() to llvm::fs

Mehdi AMINI via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 24 17:17:21 PDT 2016


joker.eph created this revision.
joker.eph added reviewers: bruno, silvas.
joker.eph added a subscriber: llvm-commits.
Herald added subscribers: srhines, danalbert, tberghammer.

Adapted from Boost::filesystem.

http://reviews.llvm.org/D18467

Files:
  include/llvm/Support/FileSystem.h
  lib/Support/Unix/Path.inc
  lib/Support/Windows/Path.inc

Index: lib/Support/Windows/Path.inc
===================================================================
--- lib/Support/Windows/Path.inc
+++ lib/Support/Windows/Path.inc
@@ -151,6 +151,19 @@
   return UniqueID(VolumeSerialNumber, FileID);
 }
 
+std::error_code disk_space(const Twine Path, space_info &SpaceInfo) {
+  PULARGE_INTEGER avail, total, free;
+  if (!::GetDiskFreeSpaceExW(Path.str().c_str(), &avail, &total, &free))
+      return mapWindowsError(::GetLastError());
+  SpaceInfo.capacity = (static_cast<uint64_t>(total.HighPart) << 32)
+    + total.LowPart;
+  SpaceInfo.free = (static_cast<uint64_t>(free.HighPart) << 32)
+    + free.LowPart;
+  SpaceInfo.available = (static_cast<uint64_t>(avail.HighPart) << 32)
+    + avail.LowPart;
+  return std::error_code();
+}
+
 TimeValue file_status::getLastAccessedTime() const {
   ULARGE_INTEGER UI;
   UI.LowPart = LastAccessedTimeLow;
Index: lib/Support/Unix/Path.inc
===================================================================
--- lib/Support/Unix/Path.inc
+++ lib/Support/Unix/Path.inc
@@ -60,6 +60,24 @@
 # define PATH_MAX 4096
 #endif
 
+#include <sys/types.h>
+#if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__ANDROID__)
+#  include <sys/statvfs.h>
+#  define STATVFS statvfs
+#  define STATVFS_F_FRSIZE(vfs) vfs.f_frsize
+#else
+# ifdef __OpenBSD__
+#  include <sys/param.h>
+# elif defined(__ANDROID__)
+#  include <sys/vfs.h>
+# else
+#  include <sys/mount.h>
+# endif
+# define STATVFS statfs
+# define STATVFS_F_FRSIZE(vfs) static_cast<uint64_t>(vfs.f_bsize)
+#endif
+
+
 using namespace llvm;
 
 namespace llvm {
@@ -190,6 +208,18 @@
   return UniqueID(fs_st_dev, fs_st_ino);
 }
 
+std::error_code disk_space(const Twine Path, space_info &SpaceInfo) {
+  struct STATVFS Vfs;
+  if(::STATVFS(Path.str().c_str(), &Vfs))
+    return std::error_code(errno, std::generic_category());
+  SpaceInfo.capacity =
+      static_cast<uint64_t>(Vfs.f_blocks)* STATVFS_F_FRSIZE(Vfs);
+  SpaceInfo.free = static_cast<uint64_t>(Vfs.f_bfree)* STATVFS_F_FRSIZE(Vfs);
+  SpaceInfo.available =
+      static_cast<uint64_t>(Vfs.f_bavail)* STATVFS_F_FRSIZE(Vfs);
+  return std::error_code();
+}
+
 std::error_code current_path(SmallVectorImpl<char> &result) {
   result.clear();
 
Index: include/llvm/Support/FileSystem.h
===================================================================
--- include/llvm/Support/FileSystem.h
+++ include/llvm/Support/FileSystem.h
@@ -647,6 +647,15 @@
 
 std::error_code getUniqueID(const Twine Path, UniqueID &Result);
 
+/// @brief Get disk space usage information.
+///
+/// @param Path Input path.
+/// @param SpaceInfo Set to the capacity, free, and available space on the
+///        device \a Path is on.
+/// @results errc::success if result has been successfully set, otherwise a
+///          platform specific error_code.
+std::error_code disk_space(const Twine Path, space_info &SpaceInfo);
+
 /// This class represents a memory mapped file. It is based on
 /// boost::iostreams::mapped_file.
 class mapped_file_region {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18467.51621.patch
Type: text/x-patch
Size: 3039 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160325/dd60ed1d/attachment.bin>


More information about the llvm-commits mailing list