runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
SystemHelper.cpp
Go to the documentation of this file.
1 #include <SystemHelper.h>
2 
3 #ifdef HAVE_WINDOWS
4 #include <Windows.h>
5 #endif
6 #ifdef HAVE_LINUX
7 #include <sys/statvfs.h>
8 #endif
9 
11 
12 }
13 
15 
16 }
17 
18 DiskStatus SystemHelper::getDiskStatus(const std::string & path) const {
19  DiskStatus out = {0,0};
20 #ifdef HAVE_WINDOWS
21  ULARGE_INTEGER freeBytesAvailable, totalNumberOfBytes, totalFreeBytes;
22  /* for conversion macros see: http://msdn.microsoft.com/en-us/library/87zae4a3%28VS.71%29.aspx
23  * CA2CT should be const ascii tring to const tchar string
24  */
25 #ifdef _UNICODE
26  int wchars_num = MultiByteToWideChar( CP_UTF8 , 0 , path.c_str() , -1, NULL , 0 );
27  wchar_t* wstr = new wchar_t[wchars_num];
28  MultiByteToWideChar( CP_UTF8 , 0 , path.c_str() , -1, wstr , wchars_num );
29  if(GetDiskFreeSpaceEx(wstr, &freeBytesAvailable, &totalNumberOfBytes, &totalFreeBytes)) {
30  out.totalSize = totalNumberOfBytes.QuadPart;
31  out.freeSize = freeBytesAvailable.QuadPart;
32  }
33  delete wstr;
34 #else
35  if(GetDiskFreeSpaceEx(path.c_str(), &freeBytesAvailable, &totalNumberOfBytes, &totalFreeBytes)) {
36  out.totalSize = totalNumberOfBytes.QuadPart;
37  out.freeSize = freeBytesAvailable.QuadPart;
38  }
39 #endif
40 #endif
41 #ifdef HAVE_LINUX
42  struct statvfs fiData;
43  if(statvfs(path.c_str(), &fiData) >= 0) {
44  out.totalSize = fiData.f_blocks;
45  out.totalSize *= fiData.f_frsize;
46  out.freeSize = fiData.f_bavail;
47  out.freeSize *= fiData.f_frsize;
48  }
49 #endif
50  return out;
51 }
52 
53 double SystemHelper::getDiskFreePercent(const std::string & path) const {
54  DiskStatus status = getDiskStatus(path);
55  if(status.totalSize == 0) {
56  return 0;
57  }
58  return ((double)(status.freeSize)) / ((double)(status.totalSize));
59 }
unsigned long long freeSize
Definition: SystemHelper.h:8
unsigned long long totalSize
Definition: SystemHelper.h:7
DiskStatus getDiskStatus(const std::string &path) const
Get status of the disk behind the given path.
double getDiskFreePercent(const std::string &path) const
Get free fraction of the disk behind the given path.