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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <Python.h>
#include <windows.h>
#include <tchar.h>
#include "../../arch/all/init.h"
#ifndef _ARRAYSIZE
#define _ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
static char *psutil_get_drive_type(int type) {
switch (type) {
case DRIVE_FIXED:
return "fixed";
case DRIVE_CDROM:
return "cdrom";
case DRIVE_REMOVABLE:
return "removable";
case DRIVE_UNKNOWN:
return "unknown";
case DRIVE_NO_ROOT_DIR:
return "unmounted";
case DRIVE_REMOTE:
return "remote";
case DRIVE_RAMDISK:
return "ramdisk";
default:
return "?";
}
}
/*
* Return path's disk total and free as a Python tuple.
*/
PyObject *
psutil_disk_usage(PyObject *self, PyObject *args) {
BOOL retval;
ULARGE_INTEGER _, total, free;
PyObject *py_path;
wchar_t *path;
if (!PyArg_ParseTuple(args, "U", &py_path)) {
return NULL;
}
path = PyUnicode_AsWideCharString(py_path, NULL);
if (path == NULL) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Py_END_ALLOW_THREADS
PyMem_Free(path);
if (retval == 0)
return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, py_path);
return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
}
/*
* Return a Python dict of tuples for disk I/O information. This may
* require running "diskperf -y" command first.
*/
PyObject *
psutil_disk_io_counters(PyObject *self, PyObject *args) {
DISK_PERFORMANCE diskPerformance;
DWORD dwSize;
HANDLE hDevice = NULL;
char szDevice[MAX_PATH];
char szDeviceDisplay[MAX_PATH];
int devNum;
int i;
DWORD ioctrlSize;
BOOL ret;
PyObject *py_retdict = PyDict_New();
PyObject *py_tuple = NULL;
if (py_retdict == NULL)
return NULL;
// Apparently there's no way to figure out how many times we have
// to iterate in order to find valid drives.
// Let's assume 32, which is higher than 26, the number of letters
// in the alphabet (from A:\ to Z:\).
for (devNum = 0; devNum <= 32; ++devNum) {
py_tuple = NULL;
sprintf_s(szDevice, MAX_PATH, "\\\\.\\PhysicalDrive%d", devNum);
hDevice = CreateFile(szDevice, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
continue;
// DeviceIoControl() sucks!
i = 0;
ioctrlSize = sizeof(diskPerformance);
while (1) {
i += 1;
ret = DeviceIoControl(
hDevice, IOCTL_DISK_PERFORMANCE, NULL, 0, &diskPerformance,
ioctrlSize, &dwSize, NULL);
if (ret != 0)
break; // OK!
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Retry with a bigger buffer (+ limit for retries).
if (i <= 1024) {
ioctrlSize *= 2;
continue;
}
}
else if (GetLastError() == ERROR_INVALID_FUNCTION) {
// This happens on AppVeyor:
// https://ci.appveyor.com/project/giampaolo/psutil/build/
// 1364/job/ascpdi271b06jle3
// Assume it means we're dealing with some exotic disk
// and go on.
psutil_debug("DeviceIoControl -> ERROR_INVALID_FUNCTION; "
"ignore PhysicalDrive%i", devNum);
goto next;
}
else if (GetLastError() == ERROR_NOT_SUPPORTED) {
// Again, let's assume we're dealing with some exotic disk.
psutil_debug("DeviceIoControl -> ERROR_NOT_SUPPORTED; "
"ignore PhysicalDrive%i", devNum);
goto next;
}
// XXX: it seems we should also catch ERROR_INVALID_PARAMETER:
// https://sites.ualberta.ca/dept/aict/uts/software/openbsd/
// ports/4.1/i386/openafs/w-openafs-1.4.14-transarc/
// openafs-1.4.14/src/usd/usd_nt.c
// XXX: we can also bump into ERROR_MORE_DATA in which case
// (quoting doc) we're supposed to retry with a bigger buffer
// and specify a new "starting point", whatever it means.
PyErr_SetFromWindowsErr(0);
goto error;
}
sprintf_s(szDeviceDisplay, MAX_PATH, "PhysicalDrive%i", devNum);
py_tuple = Py_BuildValue(
"(IILLKK)",
diskPerformance.ReadCount,
diskPerformance.WriteCount,
diskPerformance.BytesRead,
diskPerformance.BytesWritten,
// convert to ms:
// https://github.com/giampaolo/psutil/issues/1012
(unsigned long long)
(diskPerformance.ReadTime.QuadPart) / 10000000,
(unsigned long long)
(diskPerformance.WriteTime.QuadPart) / 10000000);
if (!py_tuple)
goto error;
if (PyDict_SetItemString(py_retdict, szDeviceDisplay, py_tuple))
goto error;
Py_CLEAR(py_tuple);
next:
CloseHandle(hDevice);
}
return py_retdict;
error:
Py_XDECREF(py_tuple);
Py_DECREF(py_retdict);
if (hDevice != NULL)
CloseHandle(hDevice);
return NULL;
}
/*
* Return disk partitions as a list of tuples such as
* (drive_letter, drive_letter, type, "")
*/
PyObject *
psutil_disk_partitions(PyObject *self, PyObject *args) {
DWORD num_bytes;
char drive_strings[255];
char *drive_letter = drive_strings;
char mp_buf[MAX_PATH];
char mp_path[MAX_PATH];
int all;
int type;
int ret;
unsigned int old_mode = 0;
char opts[50];
HANDLE mp_h;
BOOL mp_flag= TRUE;
LPTSTR fs_type[MAX_PATH + 1] = { 0 };
DWORD pflags = 0;
DWORD lpMaximumComponentLength = 0; // max file name
PyObject *py_all;
PyObject *py_retlist = PyList_New(0);
PyObject *py_tuple = NULL;
if (py_retlist == NULL) {
return NULL;
}
// avoid to visualize a message box in case something goes wrong
// see https://github.com/giampaolo/psutil/issues/264
old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
if (! PyArg_ParseTuple(args, "O", &py_all))
goto error;
all = PyObject_IsTrue(py_all);
Py_BEGIN_ALLOW_THREADS
num_bytes = GetLogicalDriveStrings(254, drive_letter);
Py_END_ALLOW_THREADS
if (num_bytes == 0) {
PyErr_SetFromWindowsErr(0);
goto error;
}
while (*drive_letter != 0) {
py_tuple = NULL;
opts[0] = 0;
fs_type[0] = 0;
Py_BEGIN_ALLOW_THREADS
type = GetDriveType(drive_letter);
Py_END_ALLOW_THREADS
// by default we only show hard drives and cd-roms
if (all == 0) {
if ((type == DRIVE_UNKNOWN) ||
(type == DRIVE_NO_ROOT_DIR) ||
(type == DRIVE_REMOTE) ||
(type == DRIVE_RAMDISK)) {
goto next;
}
// floppy disk: skip it by default as it introduces a
// considerable slowdown.
if ((type == DRIVE_REMOVABLE) &&
(strcmp(drive_letter, "A:\\") == 0)) {
goto next;
}
}
ret = GetVolumeInformation(
(LPCTSTR)drive_letter,
NULL,
_ARRAYSIZE(drive_letter),
NULL,
&lpMaximumComponentLength,
&pflags,
(LPTSTR)fs_type,
_ARRAYSIZE(fs_type));
if (ret == 0) {
// We might get here in case of a floppy hard drive, in
// which case the error is (21, "device not ready").
// Let's pretend it didn't happen as we already have
// the drive name and type ('removable').
strcat_s(opts, _countof(opts), "");
SetLastError(0);
}
else {
if (pflags & FILE_READ_ONLY_VOLUME)
strcat_s(opts, _countof(opts), "ro");
else
strcat_s(opts, _countof(opts), "rw");
if (pflags & FILE_VOLUME_IS_COMPRESSED)
strcat_s(opts, _countof(opts), ",compressed");
if (pflags & FILE_READ_ONLY_VOLUME)
strcat_s(opts, _countof(opts), ",readonly");
// Check for mount points on this volume and add/get info
// (checks first to know if we can even have mount points)
if (pflags & FILE_SUPPORTS_REPARSE_POINTS) {
mp_h = FindFirstVolumeMountPoint(
drive_letter, mp_buf, MAX_PATH);
if (mp_h != INVALID_HANDLE_VALUE) {
mp_flag = TRUE;
while (mp_flag) {
// Append full mount path with drive letter
strcpy_s(mp_path, _countof(mp_path), drive_letter);
strcat_s(mp_path, _countof(mp_path), mp_buf);
py_tuple = Py_BuildValue(
"(ssss)",
drive_letter,
mp_path,
fs_type, // typically "NTFS"
opts
);
if (!py_tuple ||
PyList_Append(py_retlist, py_tuple) == -1) {
FindVolumeMountPointClose(mp_h);
goto error;
}
Py_CLEAR(py_tuple);
// Continue looking for more mount points
mp_flag = FindNextVolumeMountPoint(
mp_h, mp_buf, MAX_PATH);
}
FindVolumeMountPointClose(mp_h);
}
}
}
if (strlen(opts) > 0)
strcat_s(opts, _countof(opts), ",");
strcat_s(opts, _countof(opts), psutil_get_drive_type(type));
py_tuple = Py_BuildValue(
"(ssss)",
drive_letter,
drive_letter,
fs_type, // either FAT, FAT32, NTFS, HPFS, CDFS, UDF or NWFS
opts
);
if (!py_tuple)
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
Py_CLEAR(py_tuple);
goto next;
next:
drive_letter = strchr(drive_letter, 0) + 1;
}
SetErrorMode(old_mode);
return py_retlist;
error:
SetErrorMode(old_mode);
Py_XDECREF(py_tuple);
Py_DECREF(py_retlist);
return NULL;
}
/*
Accept a filename's drive in native format like "\Device\HarddiskVolume1\"
and return the corresponding drive letter (e.g. "C:\\").
If no match is found return an empty string.
*/
PyObject *
psutil_QueryDosDevice(PyObject *self, PyObject *args) {
LPCTSTR lpDevicePath;
TCHAR d = TEXT('A');
TCHAR szBuff[5];
if (!PyArg_ParseTuple(args, "s", &lpDevicePath))
return NULL;
while (d <= TEXT('Z')) {
TCHAR szDeviceName[3] = {d, TEXT(':'), TEXT('\0')};
TCHAR szTarget[512] = {0};
if (QueryDosDevice(szDeviceName, szTarget, 511) != 0) {
if (_tcscmp(lpDevicePath, szTarget) == 0) {
_stprintf_s(szBuff, _countof(szBuff), TEXT("%c:"), d);
return Py_BuildValue("s", szBuff);
}
}
d++;
}
return Py_BuildValue("s", "");
}
|