[pywin32-checkins] pywin32/win32/src win32file.i,1.32,1.33
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: <mha...@us...> - 2003-10-29 02:08:12
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1:/tmp/cvs-serv31738 Modified Files: win32file.i Log Message: New security functions from Roger, and a reorg of the code that fetches the function pointers. Index: win32file.i =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/win32file.i,v retrieving revision 1.32 retrieving revision 1.33 diff -C2 -d -r1.32 -r1.33 *** win32file.i 1 Sep 2003 08:11:08 -0000 1.32 --- win32file.i 29 Oct 2003 02:06:14 -0000 1.33 *************** *** 2338,2375 **** static BOOL (WINAPI *pfnDeleteVolumeMountPointW)(LPCWSTR) = NULL; static BOOL (WINAPI *pfnCreateHardLinkW)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES ) = NULL; - #define VOLUME_POINTERS_NON_NULL \ - (pfnGetVolumeNameForVolumeMountPointW != NULL && \ - pfnSetVolumeMountPointW != NULL && \ - pfnDeleteVolumeMountPointW != NULL && \ - pfnCreateHardLinkW != NULL) - - static BOOL _CheckVolumePfns() - { - // Do a LoadLibrary for the function pointers, as these are - // win2k specific. - if (!VOLUME_POINTERS_NON_NULL) { - - HMODULE hMod = GetModuleHandle("kernel32.dll"); - if (hMod==0) { - PyWin_SetAPIError("GetModuleHandle", E_HANDLE); - return FALSE; - } - - FARPROC fp = GetProcAddress(hMod, "GetVolumeNameForVolumeMountPointW"); - if (fp) pfnGetVolumeNameForVolumeMountPointW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR, DWORD))(fp); - - fp = GetProcAddress(hMod, "SetVolumeMountPointW"); - if (fp) pfnSetVolumeMountPointW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR))(fp); - - fp = GetProcAddress(hMod, "DeleteVolumeMountPointW"); - if (fp) pfnDeleteVolumeMountPointW = (BOOL (WINAPI *)(LPCWSTR))(fp); - - fp = GetProcAddress(hMod, "CreateHardLinkW"); - if (fp) pfnCreateHardLinkW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES))(fp); - - } - return VOLUME_POINTERS_NON_NULL; - } // @pyswig <o PyUnicode>|SetVolumeMountPoint|Mounts the specified volume at the specified volume mount point. --- 2338,2345 ---- static BOOL (WINAPI *pfnDeleteVolumeMountPointW)(LPCWSTR) = NULL; static BOOL (WINAPI *pfnCreateHardLinkW)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES ) = NULL; + static BOOL (WINAPI *pfnEncryptFile)(WCHAR *)=NULL; + static BOOL (WINAPI *pfnDecryptFile)(WCHAR *)=NULL; + static BOOL (WINAPI *pfnEncryptionDisable)(WCHAR *, BOOL)=NULL; // @pyswig <o PyUnicode>|SetVolumeMountPoint|Mounts the specified volume at the specified volume mount point. *************** *** 2391,2397 **** WCHAR *mount_point = NULL; WCHAR volume_name[50]; - if (!_CheckVolumePfns()) - return NULL; if (!PyArg_ParseTuple(args,"OO", &mount_point_obj, &volume_obj)) return NULL; --- 2361,2367 ---- WCHAR *mount_point = NULL; WCHAR volume_name[50]; + if ((pfnSetVolumeMountPointW==NULL)||(pfnGetVolumeNameForVolumeMountPointW==NULL)) + return PyErr_Format(PyExc_NotImplementedError,"SetVolumeMountPoint not supported by this version of Windows"); if (!PyArg_ParseTuple(args,"OO", &mount_point_obj, &volume_obj)) return NULL; *************** *** 2425,2429 **** } - // @pyswig |DeleteVolumeMountPoint|Unmounts the volume from the specified volume mount point. static PyObject* --- 2395,2398 ---- *************** *** 2441,2446 **** WCHAR *mount_point = NULL; ! if (!_CheckVolumePfns()) ! return NULL; if (!PyArg_ParseTuple(args,"O", &mount_point_obj)) --- 2410,2415 ---- WCHAR *mount_point = NULL; ! if (pfnDeleteVolumeMountPointW==NULL) ! return PyErr_Format(PyExc_NotImplementedError,"DeleteVolumeMountPoint not supported by this version of Windows"); if (!PyArg_ParseTuple(args,"O", &mount_point_obj)) *************** *** 2452,2456 **** } - assert(pfnDeleteVolumeMountPointW); if (!(*pfnDeleteVolumeMountPointW)(mount_point)){ PyWin_SetAPIError("DeleteVolumeMountPoint"); --- 2421,2424 ---- *************** *** 2470,2474 **** { // @comm An NTFS hard link is similar to a POSIX hard link. ! // <nl>This function reates a second directory entry for an existing file, can be different name in // same directory or any name in a different directory. // Both file paths must be on the same NTFS volume.<nl>To remove the link, simply delete --- 2438,2442 ---- { // @comm An NTFS hard link is similar to a POSIX hard link. ! // <nl>This function creates a second directory entry for an existing file, can be different name in // same directory or any name in a different directory. // Both file paths must be on the same NTFS volume.<nl>To remove the link, simply delete *************** *** 2491,2497 **** SECURITY_ATTRIBUTES *sa; ! if (!_CheckVolumePfns()) ! return NULL; ! if (!PyArg_ParseTuple(args,"OO|O", &new_file_obj, &existing_file_obj, &sa_obj)) return NULL; --- 2459,2464 ---- SECURITY_ATTRIBUTES *sa; ! if (pfnCreateHardLinkW==NULL) ! return PyErr_Format(PyExc_NotImplementedError,"CreateHardLink not supported by this version of Windows"); if (!PyArg_ParseTuple(args,"OO|O", &new_file_obj, &existing_file_obj, &sa_obj)) return NULL; *************** *** 2512,2516 **** } - assert(pfnCreateHardLinkW); if (!((*pfnCreateHardLinkW)(new_file, existing_file, sa))){ PyWin_SetAPIError("CreateHardLink"); --- 2479,2482 ---- *************** *** 2525,2533 **** --- 2491,2638 ---- } + // @pyswig <o PyUnicode>|GetVolumeNameForVolumeMountPoint|Returns unique volume name (Win2k or later) + static PyObject* + py_GetVolumeNameForVolumeMountPoint(PyObject *self, PyObject *args) + { + // @pyparm string|mountPoint||Volume mount point or root drive - trailing backslash required + PyObject *ret=NULL; + PyObject *obvolume_name = NULL, *obmount_point = NULL; + + WCHAR *mount_point = NULL; + WCHAR volume_name[50]; + if (pfnGetVolumeNameForVolumeMountPointW==NULL) + return PyErr_Format(PyExc_NotImplementedError,"GetVolumeNameForVolumeMountPoint not supported by this version of Windows"); + + if (!PyArg_ParseTuple(args,"O", &obmount_point)) + return NULL; + + if (!PyWinObject_AsWCHAR(obmount_point, &mount_point, false)){ + PyErr_SetString(PyExc_TypeError,"Mount point must be string or unicode"); + goto cleanup; + } + + assert(pfnGetVolumeNameForVolumeMountPointW); + if (!(*pfnGetVolumeNameForVolumeMountPointW)(mount_point, volume_name, sizeof(volume_name)/sizeof(volume_name[0]))) + PyWin_SetAPIError("GetVolumeNameForVolumeMountPoint"); + else + ret=PyWinObject_FromWCHAR(volume_name); + cleanup: + PyWinObject_FreeWCHAR(mount_point); + return ret; + } + + // @pyswig |EncryptFile|Encrypts specified file (requires Win2k or higher and NTFS) + static PyObject* + py_EncryptFile(PyObject *self, PyObject *args) + { + // @pyparm string/unicode|filename||File to encrypt + + PyObject *ret=NULL, *obfname=NULL; + WCHAR *fname = NULL; + if (pfnEncryptFile==NULL) + return PyErr_Format(PyExc_NotImplementedError,"EncryptFile not supported by this version of Windows"); + if (!PyArg_ParseTuple(args,"O", &obfname)) + return NULL; + if (!PyWinObject_AsWCHAR(obfname, &fname, FALSE)) + return NULL; + if (!(*pfnEncryptFile)(fname)) + PyWin_SetAPIError("EncryptFile"); + else + ret=Py_None; + PyWinObject_FreeWCHAR(fname); + Py_XINCREF(ret); + return ret; + } + + // @pyswig |DecryptFile|Decrypts specified file (requires Win2k or higher and NTFS) + static PyObject* + py_DecryptFile(PyObject *self, PyObject *args) + { + // @pyparm string/unicode|filename||File to decrypt + PyObject *ret=NULL, *obfname=NULL; + WCHAR *fname = NULL; + if (pfnDecryptFile==NULL) + return PyErr_Format(PyExc_NotImplementedError,"DecryptFile not supported by this version of Windows"); + if (!PyArg_ParseTuple(args,"O", &obfname)) + return NULL; + if (!PyWinObject_AsWCHAR(obfname, &fname, FALSE)) + return NULL; + if (!(*pfnDecryptFile)(fname)) + PyWin_SetAPIError("DecryptFile"); + else + ret=Py_None; + PyWinObject_FreeWCHAR(fname); + Py_XINCREF(ret); + return ret; + } + + // @pyswig |EncryptionDisable|Enables/disables encryption for a directory (requires Win2k or higher and NTFS) + static PyObject* + py_EncryptionDisable(PyObject *self, PyObject *args) + { + // @pyparm string/unicode|DirName||Directory to enable or disable + // @pyparm boolean|Disable||Set to False to enable encryption + PyObject *ret=NULL, *obfname=NULL; + WCHAR *fname = NULL; + BOOL Disable; + if (pfnEncryptionDisable==NULL) + return PyErr_Format(PyExc_NotImplementedError,"EncryptionDisable not supported by this version of Windows"); + if (!PyArg_ParseTuple(args,"Oi", &obfname, &Disable)) + return NULL; + if (!PyWinObject_AsWCHAR(obfname, &fname, FALSE)) + return NULL; + if (!(*pfnEncryptionDisable)(fname,Disable)) + PyWin_SetAPIError("EncryptionDisable"); + else + ret=Py_None; + PyWinObject_FreeWCHAR(fname); + Py_XINCREF(ret); + return ret; + } + %} + %native (SetVolumeMountPoint) py_SetVolumeMountPoint; %native (DeleteVolumeMountPoint) py_DeleteVolumeMountPoint; %native (CreateHardLink) py_CreateHardLink; + %native (GetVolumeNameForVolumeMountPoint) py_GetVolumeNameForVolumeMountPoint; + // end of win2k volume mount functions. + %native (EncryptFile) py_EncryptFile; + %native (DecryptFile) py_DecryptFile; + %native (EncryptionDisable) py_EncryptionDisable; + + + %init %{ + HMODULE hmodule; + FARPROC fp; + + hmodule=LoadLibrary("AdvAPI32.dll"); + if (hmodule){ + fp=GetProcAddress(hmodule,"EncryptFileW"); + if (fp) pfnEncryptFile=(BOOL (WINAPI *)(WCHAR *))(fp); + + fp=GetProcAddress(hmodule,"DecryptFileW"); + if (fp) pfnDecryptFile=(BOOL (WINAPI *)(WCHAR *))(fp); + + fp=GetProcAddress(hmodule,"EncryptionDisable"); + if (fp) pfnEncryptionDisable=(BOOL (WINAPI *)(WCHAR *, BOOL))(fp); + } + + hmodule = GetModuleHandle("kernel32.dll"); + if (hmodule){ + fp = GetProcAddress(hmodule, "GetVolumeNameForVolumeMountPointW"); + if (fp) pfnGetVolumeNameForVolumeMountPointW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR, DWORD))(fp); + + fp = GetProcAddress(hmodule, "SetVolumeMountPointW"); + if (fp) pfnSetVolumeMountPointW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR))(fp); + + fp = GetProcAddress(hmodule, "DeleteVolumeMountPointW"); + if (fp) pfnDeleteVolumeMountPointW = (BOOL (WINAPI *)(LPCWSTR))(fp); + + fp = GetProcAddress(hmodule, "CreateHardLinkW"); + if (fp) pfnCreateHardLinkW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES))(fp); + } + %} #define EV_BREAK EV_BREAK // A break was detected on input. |