Clean up implementation for getting mounted volumes on macOS

This commit is contained in:
Kovid Goyal 2017-06-15 09:29:11 +05:30
parent e4ae318b8f
commit 649847f1b2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -19,7 +19,7 @@
* >>> usbobserver.get_devices() * >>> usbobserver.get_devices()
*/ */
#define _DARWIN_USE_64_BIT_INODE
#include <Python.h> #include <Python.h>
#include <stdio.h> #include <stdio.h>
@ -274,40 +274,41 @@ usbobserver_get_usb_drives(PyObject *self, PyObject *args) {
return ans; return ans;
} }
typedef struct statfs fsstat;
static PyObject* static PyObject*
usbobserver_get_mounted_filesystems(PyObject *self, PyObject *args) { usbobserver_get_mounted_filesystems(PyObject *self, PyObject *args) {
struct statfs *buf, t; fsstat *buf = NULL;
int num, i; int num, i;
PyObject *ans, *key, *val; PyObject *ans = NULL, *val;
num = getfsstat(NULL, 0, MNT_NOWAIT); num = getfsstat(NULL, 0, MNT_NOWAIT);
if (num == -1) { if (num == -1) {
PyErr_SetString(PyExc_RuntimeError, "Initial call to getfsstat failed"); PyErr_SetFromErrno(PyExc_OSError);
return NULL; return NULL;
} }
ans = PyDict_New(); num += 10; // In case the number of volumes has increased
if (ans == NULL) return PyErr_NoMemory(); buf = PyMem_New(fsstat, num);
if (buf == NULL) return PyErr_NoMemory();
buf = (struct statfs*)calloc(num, sizeof(struct statfs)); num = getfsstat(buf, num*sizeof(fsstat), MNT_NOWAIT);
if (buf == NULL) return PyErr_NoMemory();
num = getfsstat(buf, num*sizeof(struct statfs), MNT_NOWAIT);
if (num == -1) { if (num == -1) {
PyErr_SetString(PyExc_RuntimeError, "Call to getfsstat failed"); PyErr_SetFromErrno(PyExc_OSError);
return NULL; goto end;
} }
ans = PyDict_New();
if (ans == NULL) { goto end; }
for (i = 0 ; i < num; i++) { for (i = 0 ; i < num; i++) {
t = buf[i]; val = PyBytes_FromString(buf[i].f_mntonname);
key = PyBytes_FromString(t.f_mntfromname); if (!val) { NUKE(ans); goto end; }
val = PyBytes_FromString(t.f_mntonname); if (PyDict_SetItemString(ans, buf[i].f_mntfromname, val) != 0) { NUKE(ans); NUKE(val); goto end; }
if (key != NULL && val != NULL) { NUKE(val);
PyDict_SetItem(ans, key, val);
}
NUKE(key); NUKE(val);
} }
free(buf); end:
PyMem_Del(buf);
return ans; return ans;