Commit 8025dc7a authored by Jondy Zhao's avatar Jondy Zhao

Implement function: netuse.mapDrive

parent e14b3d1a
...@@ -131,10 +131,57 @@ netuse_user_info(PyObject *self, PyObject *args) ...@@ -131,10 +131,57 @@ netuse_user_info(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
static char
get_free_drive_letter()
{
DWORD bitmasks = GetLogicalDrives();
char ch = 'A';
while (bitmasks) {
if ((bitmasks & 1L) == 0)
return ch;
++ ch;
bitmasks >>= 1;
}
return 0;
}
static PyObject * static PyObject *
netuse_map_drive(PyObject *self, PyObject *args) netuse_map_drive(PyObject *self, PyObject *args)
{ {
PyErr_SetString(PyExc_RuntimeError, "Not Implemented"); DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;
char *remote = NULL;
char drive[] = { 0, ':', 0 };
char *user = NULL;
char *password = NULL;
if (! PyArg_ParseTuple(args, "s", &remote)) {
return NULL;
}
drive[0] = get_free_drive_letter();
if (!drive[0]) {
PyErr_SetString(PyExc_RuntimeError,
"Add net drive faild: no available drive letter."
);
return NULL;
}
memset(&nr, 0, sizeof (NETRESOURCE));
nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = drive;
nr.lpRemoteName = remote;
nr.lpProvider = NULL;
dwFlags = CONNECT_UPDATE_PROFILE;
dwRetVal = WNetAddConnection2(&nr, password, user, dwFlags);
if (dwRetVal == NO_ERROR)
return PyString_FromString(drive);
PyErr_Format(PyExc_RuntimeError,
"WNetAddConnection2 failed with error: %lu\n",
dwRetVal
);
return NULL; return NULL;
} }
/* /*
...@@ -198,7 +245,7 @@ netuse_usage_report(PyObject *self, PyObject *args) ...@@ -198,7 +245,7 @@ netuse_usage_report(PyObject *self, PyObject *args)
char drivename[] = { 'A', ':', 0 }; char drivename[] = { 'A', ':', 0 };
ULARGE_INTEGER lFreeBytesAvailable; ULARGE_INTEGER lFreeBytesAvailable;
ULARGE_INTEGER lTotalNumberOfBytes; ULARGE_INTEGER lTotalNumberOfBytes;
ULARGE_INTEGER lTotalNumberOfFreeBytes; /* ULARGE_INTEGER lTotalNumberOfFreeBytes; */
char szRemoteName[MAX_PATH]; char szRemoteName[MAX_PATH];
DWORD dwResult, cchBuff = MAX_PATH; DWORD dwResult, cchBuff = MAX_PATH;
...@@ -331,8 +378,9 @@ static PyMethodDef NetUseMethods[] = { ...@@ -331,8 +378,9 @@ static PyMethodDef NetUseMethods[] = {
netuse_map_drive, netuse_map_drive,
METH_VARARGS, METH_VARARGS,
( (
"mapDrive()\n\n" "mapDrive(sharefolder)\n\n"
"Create mapped drive from server shared folder\n" "Create mapped drive from shared folder, it uses the default user\n"
"name. (provided by the user context for the process.) \n"
) )
}, },
{ {
......
...@@ -22,6 +22,10 @@ class NetUseTests(unittest.TestCase): ...@@ -22,6 +22,10 @@ class NetUseTests(unittest.TestCase):
self.assertEquals(len(r), 3) self.assertEquals(len(r), 3)
self.assertEquals(r, ('JONDY', 'JONDY', 'Administrator')) self.assertEquals(r, ('JONDY', 'JONDY', 'Administrator'))
def test_map_drive(self):
r = real_netuse.mapDrive(r'\\server\path')
self.assertEquals(r, 'X:')
def test_usage_report(self): def test_usage_report(self):
r = real_netuse.usageReport() r = real_netuse.usageReport()
self.assertEquals(len(r), 0) self.assertEquals(len(r), 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment