Skip to content

Commit

Permalink
Add mount points to disk_partitions() in Windows (#775) (#1192)
Browse files Browse the repository at this point in the history
* POC for adding mountpoints to disk_partitions() with all=True

* Refactor check for mount points
- Less code duplication
- Show even with all=False like in Linux

* Goto error and close mp handler
  • Loading branch information
jomann09 authored and giampaolo committed Dec 12, 2017
1 parent e46803b commit 87f8810
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ E: [email protected]
I: 823

N: Jake Omann
E: https://github.com/jhomann
I: 816
E: https://github.com/jomann09
I: 816, 775

N: Jeremy Humble
W: https://github.com/jhumble
Expand Down
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

*XXXX-XX-XX*

**Enhancements**

- 775_: disk_partitions() on Windows return mount points.

**Bug fixes**

- 1193_: pids() may return False on OSX.
Expand Down
39 changes: 39 additions & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,7 @@ static char *psutil_get_drive_type(int type) {
#define _ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
#endif


/*
* Return disk partitions as a list of tuples such as
* (drive_letter, drive_letter, type, "")
Expand All @@ -2498,11 +2499,15 @@ 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[20];
HANDLE mp_h;
BOOL mp_flag= TRUE;
LPTSTR fs_type[MAX_PATH + 1] = { 0 };
DWORD pflags = 0;
PyObject *py_all;
Expand Down Expand Up @@ -2573,6 +2578,40 @@ psutil_disk_partitions(PyObject *self, PyObject *args) {
strcat_s(opts, _countof(opts), "rw");
if (pflags & FILE_VOLUME_IS_COMPRESSED)
strcat_s(opts, _countof(opts), ",compressed");

// 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) {
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_DECREF(py_tuple);

// Continue looking for more mount points
mp_flag = FindNextVolumeMountPoint(mp_h, mp_buf, MAX_PATH);
}
FindVolumeMountPointClose(mp_h);
}

}
}

if (strlen(opts) > 0)
Expand Down

0 comments on commit 87f8810

Please sign in to comment.