/
dirops.c
356 lines (299 loc) · 10.7 KB
/
dirops.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "moar.h"
#ifndef _WIN32
#include <dirent.h>
#endif
#ifdef _WIN32
# define IS_SLASH(c) ((c) == L'\\' || (c) == L'/')
#else
# define IS_SLASH(c) ((c) == '/')
#endif
#ifdef _WIN32
static wchar_t * UTF8ToUnicode(char *str)
{
const int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
wchar_t * const result = (wchar_t *)MVM_malloc(len * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, str, -1, result, len);
return result;
}
static char * UnicodeToUTF8(const wchar_t *str)
{
const int len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
char * const result = (char *)MVM_malloc(len * sizeof(char));
WideCharToMultiByte(CP_UTF8, 0, str, -1, result, len, NULL, NULL);
return result;
}
static int mkdir_p(MVMThreadContext *tc, wchar_t *pathname, MVMint64 mode) {
wchar_t *p = pathname, ch;
#else
static int mkdir_p(MVMThreadContext *tc, char *pathname, MVMint64 mode) {
char *p = pathname, ch;
uv_fs_t req;
#endif
int created = 0;
for (;; ++p)
if (!*p || IS_SLASH(*p)) {
ch = *p;
*p = '\0';
#ifdef _WIN32
if (!PathFileExistsW(pathname)) {
if (CreateDirectoryW(pathname, NULL)) {
created = 1;
}
#else
if (uv_fs_stat(tc->loop, &req, pathname, NULL) <= 0) {
if (mkdir(pathname, mode) != -1) {
created = 1;
}
#endif
}
if (!(*p = ch)) break;
}
if (!created) return -1;
return 0;
}
/* Create a directory recursively. */
void MVM_dir_mkdir(MVMThreadContext *tc, MVMString *path, MVMint64 mode) {
char * const pathname = MVM_string_utf8_c8_encode_C_string(tc, path);
#ifdef _WIN32
/* Must using UTF8ToUnicode for supporting CJK Windows file name. */
wchar_t *wpathname = UTF8ToUnicode(pathname);
int str_len = wcslen(wpathname);
MVM_free(pathname);
if (str_len > MAX_PATH) {
wchar_t abs_dirname[4096]; /* 4096 should be enough for absolute path */
wchar_t *lpp_part;
/* You cannot use the "\\?\" prefix with a relative path,
* relative paths are always limited to a total of MAX_PATH characters.
* see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx */
if (!GetFullPathNameW(wpathname, 4096, abs_dirname, &lpp_part)) {
MVM_free(wpathname);
MVM_exception_throw_adhoc(tc, "Directory path is wrong: %d", GetLastError());
}
MVM_free(wpathname);
str_len = wcslen(abs_dirname);
wpathname = (wchar_t *)MVM_malloc((str_len + 4) * sizeof(wchar_t));
wcscpy(wpathname, L"\\\\?\\");
wcscat(wpathname, abs_dirname);
}
if (mkdir_p(tc, wpathname, mode) == -1) {
DWORD error = GetLastError();
if (error != ERROR_ALREADY_EXISTS) {
MVM_free(wpathname);
MVM_exception_throw_adhoc(tc, "Failed to mkdir: %d", error);
}
}
MVM_free(wpathname);
#else
if (mkdir_p(tc, pathname, mode) == -1 && errno != EEXIST) {
int mkdir_error = errno;
MVM_free(pathname);
MVM_exception_throw_adhoc(tc, "Failed to mkdir: %d", mkdir_error);
}
MVM_free(pathname);
#endif
}
/* Remove a directory recursively. */
void MVM_dir_rmdir(MVMThreadContext *tc, MVMString *path) {
char * const pathname = MVM_string_utf8_c8_encode_C_string(tc, path);
uv_fs_t req;
if(uv_fs_rmdir(tc->loop, &req, pathname, NULL) < 0 ) {
MVM_free(pathname);
MVM_exception_throw_adhoc(tc, "Failed to rmdir: %s", uv_strerror(req.result));
}
MVM_free(pathname);
}
/* Get the current working directory. */
MVMString * MVM_dir_cwd(MVMThreadContext *tc) {
#ifdef _WIN32
char path[MAX_PATH];
size_t max_path = MAX_PATH;
int r;
#else
char path[PATH_MAX];
size_t max_path = PATH_MAX;
int r;
#endif
if ((r = uv_cwd(path, (size_t *)&max_path)) < 0) {
MVM_exception_throw_adhoc(tc, "chdir failed: %s", uv_strerror(r));
}
return MVM_string_utf8_c8_decode(tc, tc->instance->VMString, path, strlen(path));
}
/* Change directory. */
void MVM_dir_chdir(MVMThreadContext *tc, MVMString *dir) {
char * const dirstring = MVM_string_utf8_c8_encode_C_string(tc, dir);
if (uv_chdir((const char *)dirstring) != 0) {
int chdir_error = errno;
MVM_free(dirstring);
MVM_exception_throw_adhoc(tc, "chdir failed: %s", uv_strerror(chdir_error));
}
MVM_free(dirstring);
}
/* Structure to keep track of directory iteration state. */
typedef struct {
#ifdef _WIN32
wchar_t *dir_name;
HANDLE dir_handle;
#else
DIR *dir_handle;
#endif
MVMuint8 encoding;
} MVMIODirIter;
/* Sets the encoding used for reading the directory listing. */
static void set_encoding(MVMThreadContext *tc, MVMOSHandle *h, MVMint64 encoding) {
MVMIODirIter *data = (MVMIODirIter *)h->body.data;
data->encoding = encoding;
}
/* Frees data associated with the directory handle. */
static void gc_free(MVMThreadContext *tc, MVMObject *h, void *d) {
MVMIODirIter *data = (MVMIODirIter *)d;
if (data) {
#ifdef _WIN32
if (data->dir_name)
MVM_free(data->dir_name);
if (data->dir_handle)
FindClose(data->dir_handle);
#else
if (data->dir_handle)
closedir(data->dir_handle);
#endif
MVM_free(data);
}
}
/* Ops table for directory iterator; it all works off special ops, so almost
* no entries. */
static const MVMIOEncodable encodable = { set_encoding };
static const MVMIOOps op_table = {
NULL,
&encodable,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
gc_free
};
/* Open a filehandle, returning a handle. */
MVMObject * MVM_dir_open(MVMThreadContext *tc, MVMString *dirname) {
MVMOSHandle * const result = (MVMOSHandle *)MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTIO);
MVMIODirIter * const data = MVM_calloc(1, sizeof(MVMIODirIter));
#ifdef _WIN32
char *name;
int str_len;
wchar_t *wname;
wchar_t *dir_name;
name = MVM_string_utf8_c8_encode_C_string(tc, dirname);
wname = UTF8ToUnicode(name);
MVM_free(name);
str_len = wcslen(wname);
if (str_len > MAX_PATH - 2) { // the length of later appended '\*' is 2
wchar_t abs_dirname[4096]; /* 4096 should be enough for absolute path */
wchar_t *lpp_part;
/* You cannot use the "\\?\" prefix with a relative path,
* relative paths are always limited to a total of MAX_PATH characters.
* see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx */
if (!GetFullPathNameW(wname, 4096, abs_dirname, &lpp_part)) {
MVM_free(wname);
MVM_exception_throw_adhoc(tc, "Directory path is wrong: %d", GetLastError());
}
MVM_free(wname);
str_len = wcslen(abs_dirname);
dir_name = (wchar_t *)MVM_malloc((str_len + 7) * sizeof(wchar_t));
wcscpy(dir_name, L"\\\\?\\");
wcscat(dir_name, abs_dirname);
} else {
dir_name = (wchar_t *)MVM_malloc((str_len + 3) * sizeof(wchar_t));
wcscpy(dir_name, wname);
MVM_free(wname);
}
wcscat(dir_name, L"\\*"); /* Three characters are for the "\*" plus NULL appended.
* see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200%28v=vs.85%29.aspx */
data->dir_name = dir_name;
data->dir_handle = INVALID_HANDLE_VALUE;
#else
char * const dir_name = MVM_string_utf8_c8_encode_C_string(tc, dirname);
DIR * const dir_handle = opendir(dir_name);
int opendir_error = errno;
MVM_free(dir_name);
if (!dir_handle)
MVM_exception_throw_adhoc(tc, "Failed to open dir: %d", opendir_error);
data->dir_handle = dir_handle;
#endif
data->encoding = MVM_encoding_type_utf8_c8;
result->body.ops = &op_table;
result->body.data = data;
return (MVMObject *)result;
}
/* Casts to a handle, checking it's a directory handle along the way. */
static MVMOSHandle * get_dirhandle(MVMThreadContext *tc, MVMObject *oshandle, const char *msg) {
MVMOSHandle *handle = (MVMOSHandle *)oshandle;
if (REPR(oshandle)->ID != MVM_REPR_ID_MVMOSHandle)
MVM_exception_throw_adhoc(tc, "%s requires an object with REPR MVMOSHandle", msg);
if (handle->body.ops != &op_table)
MVM_exception_throw_adhoc(tc, "%s got incorrect kind of handle", msg);
return handle;
}
/* Reads a directory entry from a directory. */
MVMString * MVM_dir_read(MVMThreadContext *tc, MVMObject *oshandle) {
MVMOSHandle *handle = get_dirhandle(tc, oshandle, "readdir");
MVMIODirIter *data = (MVMIODirIter *)handle->body.data;
#ifdef _WIN32
MVMString *result;
TCHAR dir[MAX_PATH];
WIN32_FIND_DATAW ffd;
char *dir_str;
if (data->dir_handle == INVALID_HANDLE_VALUE) {
HANDLE hFind = FindFirstFileW(data->dir_name, &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
MVM_exception_throw_adhoc(tc, "read from dirhandle failed: %d", GetLastError());
}
data->dir_handle = hFind;
dir_str = UnicodeToUTF8(ffd.cFileName);
result = MVM_string_utf8_c8_decode(tc, tc->instance->VMString, dir_str, strlen(dir_str));
MVM_free(dir_str);
return result;
}
else if (FindNextFileW(data->dir_handle, &ffd) != 0) {
dir_str = UnicodeToUTF8(ffd.cFileName);
result = MVM_string_decode(tc, tc->instance->VMString, dir_str, strlen(dir_str),
data->encoding);
MVM_free(dir_str);
return result;
} else {
return tc->instance->str_consts.empty;
}
#else
struct dirent entry;
struct dirent *result;
int ret;
ret = readdir_r(data->dir_handle, &entry, &result);
if (ret == 0) {
if (result == NULL)
return tc->instance->str_consts.empty;
return MVM_string_decode(tc, tc->instance->VMString, entry.d_name, strlen(entry.d_name), data->encoding);
}
MVM_exception_throw_adhoc(tc, "Failed to read dirhandle: %d", errno);
#endif
}
void MVM_dir_close(MVMThreadContext *tc, MVMObject *oshandle) {
MVMOSHandle *handle = get_dirhandle(tc, oshandle, "readdir");
MVMIODirIter *data = (MVMIODirIter *)handle->body.data;
#ifdef _WIN32
if (data->dir_name) {
MVM_free(data->dir_name);
data->dir_name = NULL;
}
if (!FindClose(data->dir_handle))
MVM_exception_throw_adhoc(tc, "Failed to close dirhandle: %d", GetLastError());
data->dir_handle = NULL;
#else
if (closedir(data->dir_handle) == -1)
MVM_exception_throw_adhoc(tc, "Failed to close dirhandle: %d", errno);
data->dir_handle = NULL;
#endif
}