Ruby: win32/file.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

file.c
Go to the documentation of this file.
1#if defined(__MINGW32__)
2/* before stdio.h in ruby/define.h */
3# define MINGW_HAS_SECURE_API 1
4#endif
5#include "ruby/ruby.h"
6#include "ruby/encoding.h"
7#include "internal.h"
8#include "internal/error.h"
9#include <winbase.h>
10#include <wchar.h>
11#include <shlwapi.h>
12#include "win32/file.h"
13
14#ifndef INVALID_FILE_ATTRIBUTES
15# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
16#endif
17
18/* cache 'encoding name' => 'code page' into a hash */
19static struct code_page_table {
20 USHORT *table;
21 unsigned int count;
22} rb_code_page;
23
24#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
25#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
26static int
27IS_ABSOLUTE_PATH_P(const WCHAR *path, size_t len)
28{
29 if (len < 2) return FALSE;
30 if (ISALPHA(path[0]))
31 return len > 2 && path[1] == L':' && IS_DIR_SEPARATOR_P(path[2]);
32 else
33 return IS_DIR_UNC_P(path);
34}
35
36/* MultiByteToWideChar() doesn't work with code page 51932 */
37#define INVALID_CODE_PAGE 51932
38#define PATH_BUFFER_SIZE MAX_PATH * 2
39
40#define insecure_obj_p(obj, level) ((level) > 0 && OBJ_TAINTED(obj))
41
42/* defined in win32/win32.c */
43#define system_code_page rb_w32_filecp
44#define mbstr_to_wstr rb_w32_mbstr_to_wstr
45#define wstr_to_mbstr rb_w32_wstr_to_mbstr
46
47static inline void
48replace_wchar(wchar_t *s, int find, int replace)
49{
50 while (*s != 0) {
51 if (*s == find)
52 *s = replace;
53 s++;
54 }
55}
56
57/* Remove trailing invalid ':$DATA' of the path. */
58static inline size_t
59remove_invalid_alternative_data(wchar_t *wfullpath, size_t size)
60{
61 static const wchar_t prime[] = L":$DATA";
62 enum { prime_len = (sizeof(prime) / sizeof(wchar_t)) -1 };
63
64 if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
65 return size;
66
67 /* alias of stream */
68 /* get rid of a bug of x64 VC++ */
69 if (wfullpath[size - (prime_len + 1)] == ':') {
70 /* remove trailing '::$DATA' */
71 size -= prime_len + 1; /* prime */
72 wfullpath[size] = L'\0';
73 }
74 else {
75 /* remove trailing ':$DATA' of paths like '/aa:a:$DATA' */
76 wchar_t *pos = wfullpath + size - (prime_len + 1);
77 while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
78 if (*pos == L':') {
79 size -= prime_len; /* alternative */
80 wfullpath[size] = L'\0';
81 break;
82 }
83 pos--;
84 }
85 }
86 return size;
87}
88
89void rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
90
91static int
92code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
93{
94 const char *n = (const char *)name;
95 if (strncmp("CP", n, 2) == 0) {
96 int code_page = atoi(n + 2);
97 if (code_page != 0) {
98 struct code_page_table *cp = (struct code_page_table *)arg;
99 unsigned int count = cp->count;
100 USHORT *table = cp->table;
101 if (count <= idx) {
102 unsigned int i = count;
103 count = (((idx + 4) & ~31) | 28);
104 table = realloc(table, count * sizeof(*table));
105 if (!table) return ST_CONTINUE;
106 cp->count = count;
107 cp->table = table;
108 while (i < count) table[i++] = INVALID_CODE_PAGE;
109 }
110 table[idx] = (USHORT)code_page;
111 }
112 }
113 return ST_CONTINUE;
114}
115
116/*
117 Return code page number of the encoding.
118 Cache code page into a hash for performance since finding the code page in
119 Encoding#names is slow.
120*/
121static UINT
122code_page(rb_encoding *enc)
123{
124 int enc_idx;
125
126 if (!enc)
127 return system_code_page();
128
129 enc_idx = rb_enc_to_index(enc);
130
131 /* map US-ASCII and ASCII-8bit as code page 1252 (us-ascii) */
132 if (enc_idx == rb_usascii_encindex() || enc_idx == rb_ascii8bit_encindex()) {
133 return 1252;
134 }
135 if (enc_idx == rb_utf8_encindex()) {
136 return CP_UTF8;
137 }
138
139 if (0 <= enc_idx && (unsigned int)enc_idx < rb_code_page.count)
140 return rb_code_page.table[enc_idx];
141
142 return INVALID_CODE_PAGE;
143}
144
145#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
146
147/*
148 Replace the last part of the path to long name.
149 We try to avoid to call FindFirstFileW() since it takes long time.
150*/
151static inline size_t
152replace_to_long_name(wchar_t **wfullpath, size_t size, size_t buffer_size)
153{
154 WIN32_FIND_DATAW find_data;
155 HANDLE find_handle;
156
157 /*
158 Skip long name conversion if the path is already long name.
159 Short name is 8.3 format.
160 https://en.wikipedia.org/wiki/8.3_filename
161 This check can be skipped for directory components that have file
162 extensions longer than 3 characters, or total lengths longer than
163 12 characters.
164 http://msdn.microsoft.com/en-us/library/windows/desktop/aa364980(v=vs.85).aspx
165 */
166 size_t const max_short_name_size = 8 + 1 + 3;
167 size_t const max_extension_size = 3;
168 size_t path_len = 1, extension_len = 0;
169 wchar_t *pos = *wfullpath;
170
171 if (size == 3 && pos[1] == L':' && pos[2] == L'\\' && pos[3] == L'\0') {
172 /* root path doesn't need short name expansion */
173 return size;
174 }
175
176 /* skip long name conversion if path contains wildcard characters */
177 if (wcspbrk(pos, L"*?")) {
178 return size;
179 }
180
181 pos = *wfullpath + size - 1;
182 while (!IS_DIR_SEPARATOR_P(*pos) && pos != *wfullpath) {
183 if (!extension_len && *pos == L'.') {
184 extension_len = path_len - 1;
185 }
186 if (path_len > max_short_name_size || extension_len > max_extension_size) {
187 return size;
188 }
189 path_len++;
190 pos--;
191 }
192
193 if ((pos >= *wfullpath + 2) &&
194 (*wfullpath)[0] == L'\\' && (*wfullpath)[1] == L'\\') {
195 /* UNC path: no short file name, and needs Network Share
196 * Management functions instead of FindFirstFile. */
197 if (pos == *wfullpath + 2) {
198 /* //host only */
199 return size;
200 }
201 if (!wmemchr(*wfullpath + 2, L'\\', pos - *wfullpath - 2)) {
202 /* //host/share only */
203 return size;
204 }
205 }
206
207 find_handle = FindFirstFileW(*wfullpath, &find_data);
208 if (find_handle != INVALID_HANDLE_VALUE) {
209 size_t trail_pos = pos - *wfullpath + IS_DIR_SEPARATOR_P(*pos);
210 size_t file_len = wcslen(find_data.cFileName);
211 size_t oldsize = size;
212
213 FindClose(find_handle);
214 size = trail_pos + file_len;
215 if (size > (buffer_size ? buffer_size-1 : oldsize)) {
216 wchar_t *buf = ALLOC_N(wchar_t, (size + 1));
217 wcsncpy(buf, *wfullpath, trail_pos);
218 if (!buffer_size)
219 xfree(*wfullpath);
220 *wfullpath = buf;
221 }
222 wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
223 }
224 return size;
225}
226
227static inline size_t
228user_length_in_path(const wchar_t *wuser, size_t len)
229{
230 size_t i;
231
232 for (i = 0; i < len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
233 ;
234
235 return i;
236}
237
238static VALUE
239append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc)
240{
241 long olen, nlen = (long)len;
242
243 if (cp != INVALID_CODE_PAGE) {
244 if (len == -1) len = lstrlenW(ws);
245 nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL);
246 olen = RSTRING_LEN(dst);
247 rb_str_modify_expand(dst, nlen);
248 WideCharToMultiByte(cp, 0, ws, len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
249 rb_enc_associate(dst, enc);
250 rb_str_set_len(dst, olen + nlen);
251 }
252 else {
253 const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
254 char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (int)len, &nlen);
255 rb_econv_t *ec = rb_econv_open("UTF-8", rb_enc_name(enc), replaceflags);
256 dst = rb_econv_append(ec, utf8str, nlen, dst, replaceflags);
257 rb_econv_close(ec);
258 free(utf8str);
259 }
260 return dst;
261}
262
263 VALUE
264rb_default_home_dir(VALUE result)
265{
266 WCHAR *dir = rb_w32_home_dir();
267 if (!dir) {
268 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
269 }
270 append_wstr(result, dir, -1,
271 rb_w32_filecp(), rb_filesystem_encoding());
272 xfree(dir);
273 return result;
274}
275
276 VALUE
277rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
278{
279 size_t size = 0, whome_len = 0;
280 size_t buffer_len = 0;
281 long wpath_len = 0, wdir_len = 0;
282 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
283 wchar_t *wdir = NULL, *wdir_pos = NULL;
284 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
285 UINT path_cp, cp;
286 VALUE path = fname, dir = dname;
287 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
288 wchar_t path_drive = L'\0', dir_drive = L'\0';
289 int ignore_dir = 0;
290 rb_encoding *path_encoding;
291 int tainted = 0;
292
293 /* tainted if path is tainted */
294 tainted = OBJ_TAINTED(path);
295
296 /* get path encoding */
297 if (NIL_P(dir)) {
298 path_encoding = rb_enc_get(path);
299 }
300 else {
301 path_encoding = rb_enc_check(path, dir);
302 }
303
304 cp = path_cp = code_page(path_encoding);
305
306 /* workaround invalid codepage */
307 if (path_cp == INVALID_CODE_PAGE) {
308 cp = CP_UTF8;
309 if (!NIL_P(path)) {
310 path = fix_string_encoding(path, path_encoding);
311 }
312 }
313
314 /* convert char * to wchar_t */
315 if (!NIL_P(path)) {
316 const long path_len = RSTRING_LEN(path);
317#if SIZEOF_INT < SIZEOF_LONG
318 if ((long)(int)path_len != path_len) {
319 rb_raise(rb_eRangeError, "path (%ld bytes) is too long",
320 path_len);
321 }
322#endif
323 wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
324 wpath_pos = wpath;
325 }
326
327 /* determine if we need the user's home directory */
328 /* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
329 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L'~' &&
330 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
331 /* tainted if expanding '~' */
332 tainted = 1;
333
334 whome = rb_w32_home_dir();
335 if (whome == NULL) {
336 free(wpath);
337 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
338 }
339 whome_len = wcslen(whome);
340
341 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
342 free(wpath);
343 xfree(whome);
344 rb_raise(rb_eArgError, "non-absolute home");
345 }
346
347 if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
348 /* use filesystem encoding if expanding home dir */
349 path_encoding = rb_filesystem_encoding();
350 cp = path_cp = code_page(path_encoding);
351 }
352
353 /* ignores dir since we are expanding home */
354 ignore_dir = 1;
355
356 /* exclude ~ from the result */
357 wpath_pos++;
358 wpath_len--;
359
360 /* exclude separator if present */
361 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
362 wpath_pos++;
363 wpath_len--;
364 }
365 }
366 else if (wpath_len >= 2 && wpath_pos[1] == L':') {
367 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
368 /* ignore dir since path contains a drive letter and a root slash */
369 ignore_dir = 1;
370 }
371 else {
372 /* determine if we ignore dir or not later */
373 path_drive = wpath_pos[0];
374 wpath_pos += 2;
375 wpath_len -= 2;
376 }
377 }
378 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
379 result = rb_str_new_cstr("can't find user ");
380 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
381 path_cp, path_encoding);
382
383 if (wpath)
384 free(wpath);
385
386 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
387 }
388
389 /* convert dir */
390 if (!ignore_dir && !NIL_P(dir)) {
391 /* fix string encoding */
392 if (path_cp == INVALID_CODE_PAGE) {
393 dir = fix_string_encoding(dir, path_encoding);
394 }
395
396 /* convert char * to wchar_t */
397 if (!NIL_P(dir)) {
398 const long dir_len = RSTRING_LEN(dir);
399#if SIZEOF_INT < SIZEOF_LONG
400 if ((long)(int)dir_len != dir_len) {
401 if (wpath) free(wpath);
402 rb_raise(rb_eRangeError, "base directory (%ld bytes) is too long",
403 dir_len);
404 }
405#endif
406 wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
407 wdir_pos = wdir;
408 }
409
410 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
411 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
412 /* tainted if expanding '~' */
413 tainted = 1;
414
415 whome = rb_w32_home_dir();
416 if (whome == NULL) {
417 free(wpath);
418 free(wdir);
419 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
420 }
421 whome_len = wcslen(whome);
422
423 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
424 free(wpath);
425 free(wdir);
426 xfree(whome);
427 rb_raise(rb_eArgError, "non-absolute home");
428 }
429
430 /* exclude ~ from the result */
431 wdir_pos++;
432 wdir_len--;
433
434 /* exclude separator if present */
435 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
436 wdir_pos++;
437 wdir_len--;
438 }
439 }
440 else if (wdir_len >= 2 && wdir[1] == L':') {
441 dir_drive = wdir[0];
442 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
443 wdir_len = 2;
444 }
445 }
446 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
447 /* UNC path */
448 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
449 /* cut the UNC path tail to '//host/share' */
450 long separators = 0;
451 long pos = 2;
452 while (pos < wdir_len && separators < 2) {
453 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
454 separators++;
455 }
456 pos++;
457 }
458 if (separators == 2)
459 wdir_len = pos - 1;
460 }
461 }
462 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L'~') {
463 result = rb_str_new_cstr("can't find user ");
464 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
465 path_cp, path_encoding);
466 if (wpath)
467 free(wpath);
468
469 if (wdir)
470 free(wdir);
471
472 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
473 }
474 }
475
476 /* determine if we ignore dir or not */
477 if (!ignore_dir && path_drive && dir_drive) {
478 if (towupper(path_drive) != towupper(dir_drive)) {
479 /* ignore dir since path drive is different from dir drive */
480 ignore_dir = 1;
481 wdir_len = 0;
482 dir_drive = 0;
483 }
484 }
485
486 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
487 /* ignore dir since path has UNC root */
488 ignore_dir = 1;
489 wdir_len = 0;
490 }
491 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
492 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
493 /* ignore dir since path has root slash and dir doesn't have drive or UNC root */
494 ignore_dir = 1;
495 wdir_len = 0;
496 }
497
498 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
499
500 buffer = buffer_pos = ALLOC_N(wchar_t, (buffer_len + 1));
501
502 /* add home */
503 if (whome_len) {
504 wcsncpy(buffer_pos, whome, whome_len);
505 buffer_pos += whome_len;
506 }
507
508 /* Add separator if required */
509 if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
510 buffer_pos[0] = L'\\';
511 buffer_pos++;
512 }
513 else if (!dir_drive && path_drive) {
514 *buffer_pos++ = path_drive;
515 *buffer_pos++ = L':';
516 }
517
518 if (wdir_len) {
519 /* tainted if dir is used and dir is tainted */
520 if (!tainted && OBJ_TAINTED(dir))
521 tainted = 1;
522
523 wcsncpy(buffer_pos, wdir_pos, wdir_len);
524 buffer_pos += wdir_len;
525 }
526
527 /* add separator if required */
528 if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
529 buffer_pos[0] = L'\\';
530 buffer_pos++;
531 }
532
533 /* now deal with path */
534 if (wpath_len) {
535 wcsncpy(buffer_pos, wpath_pos, wpath_len);
536 buffer_pos += wpath_len;
537 }
538
539 /* GetFullPathNameW requires at least "." to determine current directory */
540 if (wpath_len == 0) {
541 buffer_pos[0] = L'.';
542 buffer_pos++;
543 }
544
545 /* Ensure buffer is NULL terminated */
546 buffer_pos[0] = L'\0';
547
548 /* tainted if path is relative */
549 if (!tainted && !IS_ABSOLUTE_PATH_P(buffer, buffer_len))
550 tainted = 1;
551
552 /* FIXME: Make this more robust */
553 /* Determine require buffer size */
554 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
555 if (size > PATH_BUFFER_SIZE) {
556 /* allocate more memory than allotted originally by PATH_BUFFER_SIZE */
557 wfullpath = ALLOC_N(wchar_t, size);
558 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
559 }
560 else {
561 wfullpath = wfullpath_buffer;
562 }
563
564 /* Remove any trailing slashes */
565 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
566 wfullpath[size - 2] != L':' &&
567 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
568 size -= 1;
569 wfullpath[size] = L'\0';
570 }
571
572 /* Remove any trailing dot */
573 if (wfullpath[size - 1] == L'.') {
574 size -= 1;
575 wfullpath[size] = L'\0';
576 }
577
578 /* removes trailing invalid ':$DATA' */
579 size = remove_invalid_alternative_data(wfullpath, size);
580
581 /* Replace the trailing path to long name */
582 if (long_name) {
583 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
584 size = replace_to_long_name(&wfullpath, size, bufsize);
585 }
586
587 /* sanitize backslashes with forwardslashes */
588 replace_wchar(wfullpath, L'\\', L'/');
589
590 /* convert to VALUE and set the path encoding */
591 rb_str_set_len(result, 0);
592 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
593
594 /* makes the result object tainted if expanding tainted strings or returning modified path */
595 if (tainted)
596 OBJ_TAINT(result);
597
598 /* TODO: better cleanup */
599 if (buffer)
600 xfree(buffer);
601
602 if (wpath)
603 free(wpath);
604
605 if (wdir)
606 free(wdir);
607
608 if (whome)
609 xfree(whome);
610
611 if (wfullpath != wfullpath_buffer)
612 xfree(wfullpath);
613
614 rb_enc_associate(result, path_encoding);
615 return result;
616}
617
618 VALUE
619rb_readlink(VALUE path, rb_encoding *resultenc)
620{
621 DWORD len;
622 VALUE wtmp = 0, wpathbuf, str;
623 rb_w32_reparse_buffer_t rbuf, *rp = &rbuf;
624 WCHAR *wpath, *wbuf;
625 rb_encoding *enc;
626 UINT cp, path_cp;
627 int e;
628
629 FilePathValue(path);
630 enc = rb_enc_get(path);
631 cp = path_cp = code_page(enc);
632 if (cp == INVALID_CODE_PAGE) {
633 path = fix_string_encoding(path, enc);
634 cp = CP_UTF8;
635 }
636 len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), NULL, 0);
637 wpath = ALLOCV_N(WCHAR, wpathbuf, len+1);
638 MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), wpath, len);
639 wpath[len] = L'\0';
640 e = rb_w32_read_reparse_point(wpath, rp, sizeof(rbuf), &wbuf, &len);
641 if (e == ERROR_MORE_DATA) {
642 size_t size = rb_w32_reparse_buffer_size(len + 1);
643 rp = ALLOCV(wtmp, size);
644 e = rb_w32_read_reparse_point(wpath, rp, size, &wbuf, &len);
645 }
646 ALLOCV_END(wpathbuf);
647 if (e) {
648 ALLOCV_END(wtmp);
649 if (e != -1)
650 rb_syserr_fail_path(rb_w32_map_errno(e), path);
651 else /* not symlink; maybe volume mount point */
652 rb_syserr_fail_path(EINVAL, path);
653 }
654 enc = resultenc;
655 path_cp = code_page(enc);
656 len = lstrlenW(wbuf);
657 str = append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, path_cp, enc);
658 ALLOCV_END(wtmp);
659 return str;
660}
661
662int
663rb_file_load_ok(const char *path)
664{
665 DWORD attr;
666 int ret = 1;
667 long len;
668 wchar_t* wpath;
669
670 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
671 if (!wpath) return 0;
672
673 attr = GetFileAttributesW(wpath);
674 if (attr == INVALID_FILE_ATTRIBUTES ||
675 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
676 ret = 0;
677 }
678 else {
679 HANDLE h = CreateFileW(wpath, GENERIC_READ,
680 FILE_SHARE_READ | FILE_SHARE_WRITE,
681 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
682 if (h != INVALID_HANDLE_VALUE) {
683 CloseHandle(h);
684 }
685 else {
686 ret = 0;
687 }
688 }
689 free(wpath);
690 return ret;
691}
692
693int
694rb_freopen(VALUE fname, const char *mode, FILE *file)
695{
696 WCHAR *wname, wmode[4];
697 VALUE wtmp;
698 char *name;
699 long len;
700 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
701 if (n > numberof(wmode)) return EINVAL;
702 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
703 RSTRING_GETMEM(fname, name, len);
704 n = rb_long2int(len);
705 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
706 wname = ALLOCV_N(WCHAR, wtmp, len + 1);
707 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
708 wname[len] = L'\0';
709 RB_GC_GUARD(fname);
710#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
711 e = _wfreopen(wname, wmode, file) ? 0 : errno;
712#else
713 {
714 FILE *newfp = 0;
715 e = _wfreopen_s(&newfp, wname, wmode, file);
716 }
717#endif
718 ALLOCV_END(wtmp);
719 return e;
720}
721
722void
723Init_w32_codepage(void)
724{
725 if (rb_code_page.count) return;
726 rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
727}
L
#define L(x)
Definition: asm.h:125
ISALPHA
#define ISALPHA
Definition: ctype.h:42
free
#define free(x)
Definition: dln.c:52
rb_ascii8bit_encindex
#define rb_ascii8bit_encindex()
Definition: encindex.h:57
rb_usascii_encindex
#define rb_usascii_encindex()
Definition: encindex.h:59
rb_utf8_encindex
#define rb_utf8_encindex()
Definition: encindex.h:58
rb_enc_associate
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:1064
rb_filesystem_encoding
rb_encoding * rb_filesystem_encoding(void)
Definition: encoding.c:1602
rb_enc_get
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:1070
rb_enc_to_index
int rb_enc_to_index(rb_encoding *enc)
Definition: encoding.c:197
rb_enc_check
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Definition: encoding.c:1089
len
uint8_t len
Definition: escape.c:17
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
numberof
#define numberof(array)
Definition: etc.c:649
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
OBJ_TAINT
#define OBJ_TAINT
Definition: fl_type.h:140
OBJ_TAINTED
#define OBJ_TAINTED
Definition: fl_type.h:142
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:712
rb_eRangeError
VALUE rb_eRangeError
Definition: error.c:1061
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:1107
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1058
encoding.h
rb_econv_open
rb_econv_t * rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags)
Definition: transcode.c:1061
ECONV_UNDEF_REPLACE
#define ECONV_UNDEF_REPLACE
Definition: encoding.h:387
rb_enc_str_new
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:857
rb_enc_name
#define rb_enc_name(enc)
Definition: encoding.h:168
ECONV_INVALID_REPLACE
#define ECONV_INVALID_REPLACE
Definition: encoding.h:385
rb_enc_str_asciionly_p
int rb_enc_str_asciionly_p(VALUE)
Definition: string.c:739
rb_econv_close
void rb_econv_close(rb_econv_t *ec)
Definition: transcode.c:1694
rb_econv_append
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags)
Definition: transcode.c:1805
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:2842
rb_str_modify_expand
void rb_str_modify_expand(VALUE, long)
Definition: string.c:2270
rb_str_new_cstr
#define rb_str_new_cstr(str)
Definition: string.h:219
rb_syserr_fail_path
#define rb_syserr_fail_path(err, path)
Definition: error.h:38
internal.h
rp
#define rp(obj)
Definition: internal.h:95
size
voidpf void uLong size
Definition: ioapi.h:138
long
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
mode
const char int mode
Definition: ioapi.h:137
buf
voidpf void * buf
Definition: ioapi.h:138
INVALID_HANDLE_VALUE
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
rb_long2int
#define rb_long2int
Definition: long.h:62
ALLOCV
#define ALLOCV
Definition: memory.h:138
ALLOC_N
#define ALLOC_N
Definition: memory.h:133
RB_GC_GUARD
#define RB_GC_GUARD(v)
Definition: memory.h:91
ALLOCV_N
#define ALLOCV_N
Definition: memory.h:139
ALLOCV_END
#define ALLOCV_END
Definition: memory.h:140
name
const char * name
Definition: nkf.c:208
count
int count
Definition: nkf.c:5055
FALSE
#define FALSE
Definition: nkf.h:174
NULL
#define NULL
Definition: regenc.h:69
RSTRING_GETMEM
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Definition: rstring.h:211
ruby.h
FilePathValue
#define FilePathValue(v)
Definition: ruby.h:61
NIL_P
#define NIL_P
Definition: special_consts.h:46
realloc
#define realloc
Definition: st.c:172
ST_CONTINUE
@ ST_CONTINUE
Definition: st.h:99
st_data_t
unsigned long st_data_t
Definition: st.h:22
__sFILE
Definition: vsnprintf.c:169
OnigEncodingTypeST
Definition: onigmo.h:160
file
Definition: gzappend.c:170
rb_econv_t
Definition: transcode.c:120
rb_w32_reparse_buffer_t
Definition: file.h:10
VALUE
unsigned long VALUE
Definition: value.h:38
system_code_page
#define system_code_page
Definition: file.c:43
wstr_to_mbstr
#define wstr_to_mbstr
Definition: file.c:45
rb_file_load_ok
int rb_file_load_ok(const char *path)
Definition: file.c:663
rb_freopen
int rb_freopen(VALUE fname, const char *mode, FILE *file)
Definition: file.c:694
INVALID_CODE_PAGE
#define INVALID_CODE_PAGE
Definition: file.c:37
INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES
Definition: file.c:15
IS_DIR_SEPARATOR_P
#define IS_DIR_SEPARATOR_P(c)
Definition: file.c:24
mbstr_to_wstr
#define mbstr_to_wstr
Definition: file.c:44
IS_DIR_UNC_P
#define IS_DIR_UNC_P(c)
Definition: file.c:25
rb_file_expand_path_internal
VALUE rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
Definition: file.c:277
Init_w32_codepage
void Init_w32_codepage(void)
Definition: file.c:723
PATH_BUFFER_SIZE
#define PATH_BUFFER_SIZE
Definition: file.c:38
rb_readlink
VALUE rb_readlink(VALUE path, rb_encoding *resultenc)
Definition: file.c:619
fix_string_encoding
#define fix_string_encoding(str, encoding)
Definition: file.c:145
rb_enc_foreach_name
void rb_enc_foreach_name(int(*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
Definition: encoding.c:2208
rb_default_home_dir
VALUE rb_default_home_dir(VALUE result)
Definition: file.c:264
file.h
rb_w32_filecp
UINT rb_w32_filecp(void)
rb_w32_home_dir
WCHAR * rb_w32_home_dir(void)
Definition: win32.c:547
rb_w32_reparse_buffer_size
#define rb_w32_reparse_buffer_size(n)
Definition: file.h:33
rb_w32_read_reparse_point
int rb_w32_read_reparse_point(const WCHAR *path, rb_w32_reparse_buffer_t *rp, size_t bufsize, WCHAR **result, DWORD *len)
Definition: win32.c:5068
rb_w32_map_errno
int rb_w32_map_errno(DWORD)
Definition: win32.c:280
DWORD
IUnknown DWORD
Definition: win32ole.c:33
xfree
#define xfree
Definition: xmalloc.h:49