COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
DeviceDisk Class Reference

#include <devicedisk.h>

Collaboration diagram for DeviceDisk:

Classes

struct  info
 

Public Member Functions

 DeviceDisk ()
 
vector< infogetInfo ()
 
double getAll (string path)
 
uint64_t getSize (string path)
 
uint64_t getUsed (string path)
 
uint64_t getFree (string path)
 
double getAll ()
 
uint64_t getSize ()
 
uint64_t getUsed ()
 
uint64_t getFree ()
 
double getFreeGiB (string path)
 
double getUsedGiB (string path)
 
double getSizeGiB (string path)
 
double getFreeGiB ()
 
double getUsedGiB ()
 
double getSizeGiB ()
 

Public Attributes

uint64_t Size
 
uint64_t Used
 
uint64_t Free
 
double SizeGiB
 
double UsedGiB
 
double FreeGiB
 
double FreePercent
 

Constructor & Destructor Documentation

DeviceDisk::DeviceDisk ( )
4 {
5 
6 }

Member Function Documentation

vector< DeviceDisk::info > DeviceDisk::getInfo ( )
180 {
181  vector <info> result;
182  info tinfo;
183 #ifdef COSMOS_LINUX_OS
184  string tdata;
185  uint64_t tsize;
186 
187  int32_t iretn = data_execute("lsblk -fbl -o SIZE,MOUNTPOINT", tdata);
188  vector<string> lines;
189  if(iretn<0) {
190  return result;
191  } else {
192  lines = string_split(tdata, "\n");
193  }
194  for (string line : lines)
195  {
196  char tmount[50];
197  if (sscanf(line.c_str(), "%lu %s\n", &tsize, tmount) == 2 && tmount[0] == '/')
198  {
199  tinfo.mount = tmount;
200  tinfo.size = tsize;
201  tinfo.used = getUsed(tinfo.mount);
202  tinfo.free = tinfo.size - tinfo.used;
203  result.push_back(tinfo);
204  }
205  }
206 // FILE *fp = popen("/bin/lsblk -fbl -o SIZE,MOUNTPOINT", "r");
207 // char tdata[100];
208 // uint64_t tsize;
209 // while ((fgets(tdata, 100, fp)) == tdata)
210 // {
211 // char tmount[50];
212 // if (sscanf(tdata, "%lu %s\n", &tsize, tmount) == 2 && tmount[0] == '/')
213 // {
214 // tinfo.mount = tmount;
215 // tinfo.size = tsize;
216 // tinfo.used = getUsed(tinfo.mount);
217 // tinfo.free = tinfo.size - tinfo.used;
218 // result.push_back(tinfo);
219 // }
220 // }
221 #endif
222 #ifdef COSMOS_WIN_OS
223  getAll();
224  tinfo.size = Size;
225  tinfo.free = Free;
226  tinfo.used = Used;
227  tinfo.mount = "c:/";
228  result.push_back(tinfo);
229 #endif
230 #ifdef COSMOS_MAC_OS
231  getAll();
232  tinfo.size = Size;
233  tinfo.mount = "/";
234  result.push_back(tinfo);
235 #endif
236  return result;
237 }
int iretn
Definition: rw_test.cpp:37
uint64_t getUsed()
Definition: devicedisk.cpp:116
int32_t data_execute(string cmd, string &result, string shell)
Definition: datalib.cpp:1947
double getAll()
Definition: devicedisk.cpp:9
uint64_t Size
Definition: devicedisk.h:35
uint64_t Free
Definition: devicedisk.h:37
backing_store_ptr info
Definition: jmemsys.h:181
uint64_t Used
Definition: devicedisk.h:36
vector< string > string_split(string in, string delimeters)
Parse a string.
Definition: stringlib.cpp:47
double DeviceDisk::getAll ( string  path)
21 {
22  Size = 0;
23  Free = 0;
24  Used = 0;
25  FreePercent = 0;
26 
27 #if defined COSMOS_LINUX_OS
28  struct statvfs buf;
29 
30  statvfs(path.c_str(),&buf);
31 
32  // disk size in bytes
33  // must add the casting otherwise there will be problems
34  Size = (uint64_t)buf.f_frsize * (uint64_t)buf.f_blocks;
35  Free = (uint64_t)buf.f_frsize * (uint64_t)buf.f_bfree;
36 
37 #endif
38 
39 #if defined COSMOS_MAC_OS
40  // Open disk
41  uint32_t dev = open("/dev/disk0", O_RDONLY);
42 
43  if (dev == -1) {
44  perror("Failed to open disk");
45  return -1;
46  }
47 
48  uint64_t sector_count = 0;
49  // Query the number of sectors on the disk
50  ioctl(dev, DKIOCGETBLOCKCOUNT, &sector_count);
51 
52  uint32_t sector_size = 0;
53  // Query the size of each sector
54  ioctl(dev, DKIOCGETBLOCKSIZE, &sector_size);
55 
56  uint64_t disk_size = sector_count * sector_size;
57  printf("%ld", disk_size);
58 
59  Size = disk_size;
60 
61  return 0;
62 #endif
63 
64 #if defined COSMOS_WIN_OS
65  uint64_t freeSpace;
66 
67  GetDiskFreeSpaceExA( (LPCSTR) path.c_str(),
68  (PULARGE_INTEGER)&freeSpace,
69  (PULARGE_INTEGER)&Size,
70  (PULARGE_INTEGER)&Free);
71 
72 #endif
73 
74  Used = Size - Free;
75  FreePercent = (double)Free / Used;
76 
77  // convert to GiB
78  SizeGiB = Size/GiB;
79  FreeGiB = Free/GiB;
80  UsedGiB = Used/GiB;
81 
82  return FreePercent;
83 }
static const double GiB
Definition: devicedisk.h:30
double SizeGiB
Definition: devicedisk.h:38
double FreeGiB
Definition: devicedisk.h:40
uint64_t Size
Definition: devicedisk.h:35
uint64_t Free
Definition: devicedisk.h:37
double FreePercent
Definition: devicedisk.h:41
char buf[128]
Definition: rw_test.cpp:40
uint64_t Used
Definition: devicedisk.h:36
double UsedGiB
Definition: devicedisk.h:39
uint64_t DeviceDisk::getSize ( string  path)
98 {
99  getAll(path);
100  return Size;
101 }
double getAll()
Definition: devicedisk.cpp:9
uint64_t Size
Definition: devicedisk.h:35
uint64_t DeviceDisk::getUsed ( string  path)
128 {
129  getAll(path);
130  return (Used);
131 }
double getAll()
Definition: devicedisk.cpp:9
uint64_t Used
Definition: devicedisk.h:36
uint64_t DeviceDisk::getFree ( string  path)
160 {
161  getAll(path);
162 
163  return Free;
164 }
double getAll()
Definition: devicedisk.cpp:9
uint64_t Free
Definition: devicedisk.h:37
double DeviceDisk::getAll ( )
10 {
11 #if defined COSMOS_LINUX_OS
12  return getAll("/");
13 #endif
14 
15 #if defined COSMOS_WIN_OS
16  return getAll("C:");
17 #endif
18 }
double getAll()
Definition: devicedisk.cpp:9
uint64_t DeviceDisk::getSize ( )
86 {
87 #if defined COSMOS_LINUX_OS
88  return getSize("/");
89 #endif
90 
91 #if defined COSMOS_WIN_OS
92  return getSize("C:");
93 #endif
94 }
uint64_t getSize()
Definition: devicedisk.cpp:85
uint64_t DeviceDisk::getUsed ( )
117 {
118 #if defined COSMOS_LINUX_OS
119  return getUsed("/");
120 #endif
121 
122 #if defined COSMOS_WIN_OS
123  return getUsed("C:");
124 #endif
125 }
uint64_t getUsed()
Definition: devicedisk.cpp:116
uint64_t DeviceDisk::getFree ( )
148 {
149 #if defined COSMOS_LINUX_OS
150  return getFree("/");
151 #endif
152 
153 #if defined COSMOS_WIN_OS
154  return getFree("C:");
155 #endif
156 }
uint64_t getFree()
Definition: devicedisk.cpp:147
double DeviceDisk::getFreeGiB ( string  path)
168 {
169  // convert from Byte to GiB
170  return (double)getFree(path)/GiB;
171 }
static const double GiB
Definition: devicedisk.h:30
uint64_t getFree()
Definition: devicedisk.cpp:147
double DeviceDisk::getUsedGiB ( string  path)
135 {
136  // convert from Byte to GiB
137  return (double)getUsed(path)/GiB;
138 }
static const double GiB
Definition: devicedisk.h:30
uint64_t getUsed()
Definition: devicedisk.cpp:116
double DeviceDisk::getSizeGiB ( string  path)
105 {
106  // convert from Byte to GiB
107  return (double)getSize(path)/GiB;
108 }
static const double GiB
Definition: devicedisk.h:30
uint64_t getSize()
Definition: devicedisk.cpp:85
double DeviceDisk::getFreeGiB ( )
174 {
175  // convert from Byte to GiB
176  return (double)getFree()/GiB;
177 }
static const double GiB
Definition: devicedisk.h:30
uint64_t getFree()
Definition: devicedisk.cpp:147
double DeviceDisk::getUsedGiB ( )
141 {
142  // convert from Byte to GiB
143  return (double)getUsed()/GiB;
144 }
static const double GiB
Definition: devicedisk.h:30
uint64_t getUsed()
Definition: devicedisk.cpp:116
double DeviceDisk::getSizeGiB ( )
111 {
112  // convert from Byte to GiB
113  return (double)getSize()/GiB;
114 }
static const double GiB
Definition: devicedisk.h:30
uint64_t getSize()
Definition: devicedisk.cpp:85

Member Data Documentation

uint64_t DeviceDisk::Size
uint64_t DeviceDisk::Used
uint64_t DeviceDisk::Free
double DeviceDisk::SizeGiB
double DeviceDisk::UsedGiB
double DeviceDisk::FreeGiB
double DeviceDisk::FreePercent

The documentation for this class was generated from the following files: