-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpaths.cpp
More file actions
123 lines (107 loc) · 3.05 KB
/
paths.cpp
File metadata and controls
123 lines (107 loc) · 3.05 KB
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
#include <Python.h>
#include <windows.h>
#include <shlwapi.h>
#include <string>
#include "helpers.h"
#pragma comment(lib, "Shlwapi.lib")
extern "C" {
PyObject *
package_get_root(PyObject *, PyObject *, PyObject *)
{
// Assume current process is running in the package root
wchar_t buffer[MAX_PATH];
DWORD cch = GetModuleFileName(NULL, buffer, MAX_PATH);
if (!cch) {
PyErr_SetFromWindowsErr(GetLastError());
return NULL;
}
while (cch > 0 && buffer[--cch] != L'\\') { }
return PyUnicode_FromWideChar(buffer, cch);
}
PyObject *
file_url_to_path(PyObject *, PyObject *args, PyObject *kwargs)
{
static const char * keywords[] = {"url", NULL};
wchar_t *url = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:file_url_to_path", keywords,
as_utf16, &url)) {
return NULL;
}
PyObject *r = NULL;
wchar_t path[32768];
DWORD path_len = 32767;
HRESULT hr = PathCreateFromUrlW(url, path, &path_len, 0);
if (SUCCEEDED(hr)) {
r = PyUnicode_FromWideChar(path, -1);
} else {
PyErr_SetFromWindowsErr(hr);
}
PyMem_Free(url);
return r;
}
PyObject *
file_lock_for_delete(PyObject *, PyObject *args, PyObject *kwargs)
{
static const char * keywords[] = {"path", NULL};
wchar_t *path = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:file_lock_for_delete", keywords,
as_utf16, &path)) {
return NULL;
}
HANDLE h = CreateFileW(path, FILE_GENERIC_WRITE | DELETE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (h == INVALID_HANDLE_VALUE) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
return PyLong_FromNativeBytes(&h, sizeof(h), -1);
}
PyObject *
file_unlock_for_delete(PyObject *, PyObject *args, PyObject *kwargs)
{
static const char * keywords[] = {"handle", NULL};
PyObject *handle = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:file_unlock_for_delete", keywords,
&handle)) {
return NULL;
}
HANDLE h;
if (PyLong_AsNativeBytes(handle, &h, sizeof(h), -1) < 0) {
return NULL;
}
CloseHandle(h);
return Py_GetConstant(Py_CONSTANT_NONE);
}
PyObject *
file_locked_delete(PyObject *, PyObject *args, PyObject *kwargs)
{
static const char * keywords[] = {"handle", NULL};
PyObject *handle = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:file_locked_delete", keywords,
&handle)) {
return NULL;
}
HANDLE h;
if (PyLong_AsNativeBytes(handle, &h, sizeof(h), -1) < 0) {
return NULL;
}
DWORD cch = 0;
std::wstring buf;
cch = GetFinalPathNameByHandleW(h, NULL, 0, FILE_NAME_OPENED);
if (cch) {
buf.resize(cch);
cch = GetFinalPathNameByHandleW(h, buf.data(), cch, FILE_NAME_OPENED);
}
if (!cch) {
PyErr_SetFromWindowsErr(0);
CloseHandle(h);
return NULL;
}
CloseHandle(h);
if (!DeleteFileW(buf.c_str())) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
return Py_GetConstant(Py_CONSTANT_NONE);
}
}