-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdisk_info.c
More file actions
163 lines (143 loc) · 4.64 KB
/
disk_info.c
File metadata and controls
163 lines (143 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*------------------------------------------------------------------------
* disk_info.c
* System disk information
*
* Copyright (c) 2020, EnterpriseDB Corporation. All Rights Reserved.
*
*------------------------------------------------------------------------
*/
#include "postgres.h"
#include "system_stats.h"
#include <windows.h>
#include <wbemidl.h>
void ReadDiskInformation(Tuplestorestate *tupstore, TupleDesc tupdesc)
{
Datum values[Natts_disk_info];
bool nulls[Natts_disk_info];
memset(nulls, 0, sizeof(nulls));
// result code from COM calls from program
HRESULT hres = 0;
IEnumWbemClassObject *results = NULL;
BSTR query = SysAllocString(L"SELECT * FROM Win32_Volume");
// issue a WMI query
results = execute_query(query);
/* list the query results */
if (results != NULL)
{
IWbemClassObject *result = NULL;
ULONG returnedCount = 0;
// enumerate the retrieved objects
while ((hres = results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)) == S_OK)
{
VARIANT query_result;
uint64 total_space, free_space, used_space;
int wstr_length = 0;
char *dst = NULL;
size_t charsConverted = 0;
hres = result->lpVtbl->Get(result, L"DriveLetter", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_disk_drive_letter] = true;
else
{
wstr_length = 0;
charsConverted = 0;
wstr_length = SysStringLen(query_result.bstrVal);
if (wstr_length == 0)
nulls[Anum_disk_drive_letter] = true;
else
{
dst = (char *)malloc(wstr_length + 10);
memset(dst, 0x00, (wstr_length + 10));
wcstombs_s(&charsConverted, dst, wstr_length + 10, query_result.bstrVal, wstr_length);
values[Anum_disk_drive_letter] = CStringGetTextDatum(dst);
free(dst);
}
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"DriveType", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_disk_drive_type] = true;
else {
values[Anum_disk_drive_type] = Int32GetDatum(query_result.intVal);
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"FileSystem", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_disk_file_system] = true;
else
{
wstr_length = 0;
charsConverted = 0;
wstr_length = SysStringLen(query_result.bstrVal);
if (wstr_length == 0)
nulls[Anum_disk_file_system] = true;
else
{
dst = (char *)malloc(wstr_length + 10);
memset(dst, 0x00, (wstr_length + 10));
wcstombs_s(&charsConverted, dst, wstr_length + 10, query_result.bstrVal, wstr_length);
values[Anum_disk_file_system] = CStringGetTextDatum(dst);
free(dst);
}
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"FreeSpace", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_disk_free_space] = true;
else
{
wstr_length = 0;
charsConverted = 0;
wstr_length = SysStringLen(query_result.bstrVal);
if (wstr_length == 0)
nulls[Anum_disk_free_space] = true;
else
{
dst = (char *)malloc(wstr_length + 10);
memset(dst, 0x00, (wstr_length + 10));
wcstombs_s(&charsConverted, dst, wstr_length + 10, query_result.bstrVal, wstr_length);
free_space = strtoll(dst, NULL, 10);
values[Anum_disk_free_space] = UInt64GetDatum(free_space);
free(dst);
}
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"Capacity", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_disk_total_space] = true;
else
{
wstr_length = 0;
charsConverted = 0;
wstr_length = SysStringLen(query_result.bstrVal);
if (wstr_length == 0)
nulls[Anum_disk_total_space] = true;
else
{
dst = (char *)malloc(wstr_length + 10);
memset(dst, 0x00, (wstr_length + 10));
wcstombs_s(&charsConverted, dst, wstr_length + 10, query_result.bstrVal, wstr_length);
total_space = strtoll(dst, NULL, 10);
values[Anum_disk_total_space] = UInt64GetDatum(total_space);
free(dst);
}
VariantClear(&query_result);
}
used_space = (total_space - free_space);
values[Anum_disk_used_space] = UInt64GetDatum(used_space);
nulls[Anum_disk_mount_point] = true;
nulls[Anum_disk_file_system_type] = true;
nulls[Anum_disk_total_inodes] = true;
nulls[Anum_disk_used_inodes] = true;
nulls[Anum_disk_free_inodes] = true;
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
/* release the current result object */
result->lpVtbl->Release(result);
}
/* release results set */
results->lpVtbl->Release(results);
}
else
ereport(DEBUG1, (errmsg("[ReadDiskInformation]: Failed to get query result")));
SysFreeString(query);
}