@@ -41,21 +41,50 @@ uuidcreate(PyObject* obj, PyObject*args)
41
41
42
42
}
43
43
44
+ /* Helper for converting file names from UTF-8 to wchat_t*. */
45
+ static wchar_t *
46
+ utf8_to_wchar (const char * s , int * err )
47
+ {
48
+ PyObject * obj = PyUnicode_FromString (s );
49
+ if (obj == NULL ) {
50
+ if (PyErr_ExceptionMatches (PyExc_MemoryError )) {
51
+ * err = ENOMEM ;
52
+ }
53
+ else {
54
+ * err = EINVAL ;
55
+ }
56
+ PyErr_Clear ();
57
+ return NULL ;
58
+ }
59
+ wchar_t * ws = PyUnicode_AsWideCharString (obj , NULL );
60
+ if (ws == NULL ) {
61
+ * err = ENOMEM ;
62
+ PyErr_Clear ();
63
+ }
64
+ Py_DECREF (obj );
65
+ return ws ;
66
+ }
67
+
44
68
/* FCI callback functions */
45
69
46
70
static FNFCIALLOC (cb_alloc )
47
71
{
48
- return malloc (cb );
72
+ return PyMem_RawMalloc (cb );
49
73
}
50
74
51
75
static FNFCIFREE (cb_free )
52
76
{
53
- free (memory );
77
+ PyMem_RawFree (memory );
54
78
}
55
79
56
80
static FNFCIOPEN (cb_open )
57
81
{
58
- int result = _open (pszFile , oflag | O_NOINHERIT , pmode );
82
+ wchar_t * ws = utf8_to_wchar (pszFile , err );
83
+ if (ws == NULL ) {
84
+ return -1 ;
85
+ }
86
+ int result = _wopen (ws , oflag | O_NOINHERIT , pmode );
87
+ PyMem_Free (ws );
59
88
if (result == -1 )
60
89
* err = errno ;
61
90
return result ;
@@ -95,7 +124,12 @@ static FNFCISEEK(cb_seek)
95
124
96
125
static FNFCIDELETE (cb_delete )
97
126
{
98
- int result = remove (pszFile );
127
+ wchar_t * ws = utf8_to_wchar (pszFile , err );
128
+ if (ws == NULL ) {
129
+ return -1 ;
130
+ }
131
+ int result = _wremove (ws );
132
+ PyMem_Free (ws );
99
133
if (result != 0 )
100
134
* err = errno ;
101
135
return result ;
@@ -159,15 +193,22 @@ static FNFCIGETOPENINFO(cb_getopeninfo)
159
193
FILETIME filetime ;
160
194
HANDLE handle ;
161
195
196
+ wchar_t * ws = utf8_to_wchar (pszName , err );
197
+ if (ws == NULL ) {
198
+ return -1 ;
199
+ }
200
+
162
201
/* Need Win32 handle to get time stamps */
57AE
163
- handle = CreateFile ( pszName , GENERIC_READ , FILE_SHARE_READ , NULL ,
202
+ handle = CreateFileW ( ws , GENERIC_READ , FILE_SHARE_READ , NULL ,
164
203
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , NULL );
165
- if (handle == INVALID_HANDLE_VALUE )
204
+ if (handle == INVALID_HANDLE_VALUE ) {
205
+ PyMem_Free (ws );
166
206
return -1 ;
207
+ }
167
208
168
- if (GetFileInformationByHandle (handle , & bhfi ) == FALSE)
169
- {
209
+ if (GetFileInformationByHandle (handle , & bhfi ) == FALSE) {
170
210
CloseHandle (handle );
211
+ PyMem_Free (ws );
171
212
return -1 ;
172
213
}
173
214
@@ -179,7 +220,9 @@ static FNFCIGETOPENINFO(cb_getopeninfo)
179
220
180
221
CloseHandle (handle );
181
222
182
- return _open (pszName , _O_RDONLY | _O_BINARY | O_NOINHERIT );
223
+ int result = _wopen (ws , _O_RDONLY | _O_BINARY | O_NOINHERIT );
224
+ PyMem_Free (ws );
225
+ return result ;
183
226
}
184
227
185
228
static PyObject * fcicreate (PyObject * obj , PyObject * args )
@@ -212,7 +255,7 @@ static PyObject* fcicreate(PyObject* obj, PyObject* args)
212
255
ccab .setID = 0 ;
213
256
ccab .szDisk [0 ] = '\0' ;
214
257
215
- for (i = 0 , p = cabname ; * p ; p = CharNext ( p ) )
258
+ for (i = 0 , p = cabname ; * p ; p ++ )
216
259
if (* p == '\\' || * p == '/' )
217
260
i = p - cabname + 1 ;
218
261
0 commit comments