1#if defined(__MINGW32__)
3# define MINGW_HAS_SECURE_API 1
8#include "internal/error.h"
12#include "win32/file.h"
14#ifndef INVALID_FILE_ATTRIBUTES
15# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
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]))
27IS_ABSOLUTE_PATH_P(
const WCHAR *path,
size_t len)
29 if (len < 2)
return FALSE;
31 return len > 2 && path[1] == L
':' && IS_DIR_SEPARATOR_P(path[2]);
33 return IS_DIR_UNC_P(path);
37#define INVALID_CODE_PAGE 51932
38#define PATH_BUFFER_SIZE MAX_PATH * 2
41#define system_code_page rb_w32_filecp
42#define mbstr_to_wstr rb_w32_mbstr_to_wstr
43#define wstr_to_mbstr rb_w32_wstr_to_mbstr
46replace_wchar(
wchar_t *s,
int find,
int replace)
57remove_invalid_alternative_data(
wchar_t *wfullpath,
size_t size)
59 static const wchar_t prime[] = L
":$DATA";
60 enum { prime_len = (
sizeof(prime) /
sizeof(
wchar_t)) -1 };
62 if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
67 if (wfullpath[size - (prime_len + 1)] ==
':') {
69 size -= prime_len + 1;
70 wfullpath[size] = L
'\0';
74 wchar_t *pos = wfullpath + size - (prime_len + 1);
75 while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
78 wfullpath[size] = L
'\0';
87void rb_enc_foreach_name(
int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
90code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
92 const char *n = (
const char *)name;
93 if (strncmp(
"CP", n, 2) == 0) {
94 int code_page = atoi(n + 2);
97 unsigned int count = cp->count;
98 USHORT *table = cp->table;
100 unsigned int i = count;
101 count = (((idx + 4) & ~31) | 28);
102 table = realloc(table, count *
sizeof(*table));
103 if (!table)
return ST_CONTINUE;
106 while (i < count) table[i++] = INVALID_CODE_PAGE;
108 table[idx] = (USHORT)code_page;
125 return system_code_page();
137 if (0 <= enc_idx && (
unsigned int)enc_idx < rb_code_page.count)
138 return rb_code_page.table[enc_idx];
140 return INVALID_CODE_PAGE;
143#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
150replace_to_long_name(
wchar_t **wfullpath,
size_t size,
size_t buffer_size)
152 WIN32_FIND_DATAW find_data;
164 size_t const max_short_name_size = 8 + 1 + 3;
165 size_t const max_extension_size = 3;
166 size_t path_len = 1, extension_len = 0;
167 wchar_t *pos = *wfullpath;
169 if (size == 3 && pos[1] == L
':' && pos[2] == L
'\\' && pos[3] == L
'\0') {
175 if (wcspbrk(pos, L
"*?")) {
179 pos = *wfullpath + size - 1;
180 while (!IS_DIR_SEPARATOR_P(*pos) && pos != *wfullpath) {
181 if (!extension_len && *pos == L
'.') {
182 extension_len = path_len - 1;
184 if (path_len > max_short_name_size || extension_len > max_extension_size) {
191 if ((pos >= *wfullpath + 2) &&
192 (*wfullpath)[0] == L
'\\' && (*wfullpath)[1] == L
'\\') {
195 if (pos == *wfullpath + 2) {
199 if (!wmemchr(*wfullpath + 2, L
'\\', pos - *wfullpath - 2)) {
205 find_handle = FindFirstFileW(*wfullpath, &find_data);
206 if (find_handle != INVALID_HANDLE_VALUE) {
207 size_t trail_pos = pos - *wfullpath + IS_DIR_SEPARATOR_P(*pos);
208 size_t file_len = wcslen(find_data.cFileName);
209 size_t oldsize = size;
211 FindClose(find_handle);
212 size = trail_pos + file_len;
213 if (size > (buffer_size ? buffer_size-1 : oldsize)) {
214 wchar_t *buf =
ALLOC_N(
wchar_t, (size + 1));
215 wcsncpy(buf, *wfullpath, trail_pos);
220 wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
226user_length_in_path(
const wchar_t *wuser,
size_t len)
230 for (i = 0; i < len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
237append_wstr(VALUE dst,
const WCHAR *ws, ssize_t len, UINT cp,
rb_encoding *enc)
239 long olen, nlen = (long)len;
241 if (cp != INVALID_CODE_PAGE) {
242 if (len == -1) len = lstrlenW(ws);
243 nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL);
246 WideCharToMultiByte(cp, 0, ws, len,
RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
252 char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (
int)len, &nlen);
262rb_default_home_dir(VALUE result)
264 WCHAR *dir = rb_w32_home_dir();
266 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding `~'");
268 append_wstr(result, dir, -1,
275rb_file_expand_path_internal(VALUE fname, VALUE dname,
int abs_mode,
int long_name, VALUE result)
277 size_t size = 0, whome_len = 0;
278 size_t buffer_len = 0;
279 long wpath_len = 0, wdir_len = 0;
280 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
281 wchar_t *wdir = NULL, *wdir_pos = NULL;
282 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
284 VALUE path = fname, dir = dname;
285 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
286 wchar_t path_drive = L
'\0', dir_drive = L
'\0';
298 cp = path_cp = code_page(path_encoding);
301 if (path_cp == INVALID_CODE_PAGE) {
304 path = fix_string_encoding(path, path_encoding);
311#if SIZEOF_INT < SIZEOF_LONG
312 if ((
long)(
int)path_len != path_len) {
313 rb_raise(rb_eRangeError,
"path (%ld bytes) is too long",
317 wpath = mbstr_to_wstr(cp,
RSTRING_PTR(path), path_len, &wpath_len);
323 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L
'~' &&
324 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
325 whome = rb_w32_home_dir();
328 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding `~'");
330 whome_len = wcslen(whome);
332 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
335 rb_raise(rb_eArgError,
"non-absolute home");
341 cp = path_cp = code_page(path_encoding);
352 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
357 else if (wpath_len >= 2 && wpath_pos[1] == L
':') {
358 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
364 path_drive = wpath_pos[0];
369 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L
'~') {
371 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
372 path_cp, path_encoding);
381 if (!ignore_dir && !
NIL_P(dir)) {
383 if (path_cp == INVALID_CODE_PAGE) {
384 dir = fix_string_encoding(dir, path_encoding);
390#if SIZEOF_INT < SIZEOF_LONG
391 if ((
long)(
int)dir_len != dir_len) {
392 if (wpath) free(wpath);
393 rb_raise(rb_eRangeError,
"base directory (%ld bytes) is too long",
397 wdir = mbstr_to_wstr(cp,
RSTRING_PTR(dir), dir_len, &wdir_len);
401 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L
'~' &&
402 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
403 whome = rb_w32_home_dir();
407 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding `~'");
409 whome_len = wcslen(whome);
411 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
415 rb_raise(rb_eArgError,
"non-absolute home");
423 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
428 else if (wdir_len >= 2 && wdir[1] == L
':') {
430 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
434 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
436 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
440 while (pos < wdir_len && separators < 2) {
441 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
450 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L
'~') {
452 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
453 path_cp, path_encoding);
465 if (!ignore_dir && path_drive && dir_drive) {
466 if (towupper(path_drive) != towupper(dir_drive)) {
474 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
479 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
480 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
486 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
488 buffer = buffer_pos =
ALLOC_N(
wchar_t, (buffer_len + 1));
492 wcsncpy(buffer_pos, whome, whome_len);
493 buffer_pos += whome_len;
497 if (whome_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
498 buffer_pos[0] = L
'\\';
501 else if (!dir_drive && path_drive) {
502 *buffer_pos++ = path_drive;
503 *buffer_pos++ = L
':';
507 wcsncpy(buffer_pos, wdir_pos, wdir_len);
508 buffer_pos += wdir_len;
512 if (wdir_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
513 buffer_pos[0] = L
'\\';
519 wcsncpy(buffer_pos, wpath_pos, wpath_len);
520 buffer_pos += wpath_len;
524 if (wpath_len == 0) {
525 buffer_pos[0] = L
'.';
530 buffer_pos[0] = L
'\0';
534 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
535 if (size > PATH_BUFFER_SIZE) {
537 wfullpath =
ALLOC_N(
wchar_t, size);
538 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
541 wfullpath = wfullpath_buffer;
545 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
546 wfullpath[size - 2] != L
':' &&
547 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
549 wfullpath[size] = L
'\0';
553 if (wfullpath[size - 1] == L
'.') {
555 wfullpath[size] = L
'\0';
559 size = remove_invalid_alternative_data(wfullpath, size);
563 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
564 size = replace_to_long_name(&wfullpath, size, bufsize);
568 replace_wchar(wfullpath, L
'\\', L
'/');
572 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
587 if (wfullpath != wfullpath_buffer)
598 VALUE wtmp = 0, wpathbuf, str;
607 cp = path_cp = code_page(enc);
608 if (cp == INVALID_CODE_PAGE) {
609 path = fix_string_encoding(path, enc);
613 wpath =
ALLOCV_N(WCHAR, wpathbuf, len+1);
616 e = rb_w32_read_reparse_point(wpath, rp,
sizeof(rbuf), &wbuf, &len);
617 if (e == ERROR_MORE_DATA) {
618 size_t size = rb_w32_reparse_buffer_size(len + 1);
620 e = rb_w32_read_reparse_point(wpath, rp, size, &wbuf, &len);
626 rb_syserr_fail_path(rb_w32_map_errno(e), path);
628 rb_syserr_fail_path(EINVAL, path);
631 path_cp = code_page(enc);
632 len = lstrlenW(wbuf);
633 str = append_wstr(
rb_enc_str_new(0, 0, enc), wbuf, len, path_cp, enc);
639rb_file_load_ok(
const char *path)
646 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
647 if (!wpath)
return 0;
649 attr = GetFileAttributesW(wpath);
650 if (attr == INVALID_FILE_ATTRIBUTES ||
651 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
655 HANDLE h = CreateFileW(wpath, GENERIC_READ,
656 FILE_SHARE_READ | FILE_SHARE_WRITE,
657 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
658 if (h != INVALID_HANDLE_VALUE) {
670rb_freopen(VALUE fname,
const char *mode,
FILE *file)
672 WCHAR *wname, wmode[4];
676 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
677 if (n > numberof(wmode))
return EINVAL;
678 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
681 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
682 wname =
ALLOCV_N(WCHAR, wtmp, len + 1);
683 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
686#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
687 e = _wfreopen(wname, wmode, file) ? 0 : errno;
691 e = _wfreopen_s(&newfp, wname, wmode, file);
699Init_w32_codepage(
void)
701 if (rb_code_page.count)
return;
702 rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
#define ALLOCV
Old name of RB_ALLOCV.
#define xfree
Old name of ruby_xfree.
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define ISALPHA
Old name of rb_isalpha.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Identical to rb_enc_associate(), except it takes an encoding itself instead of its index.
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
int rb_utf8_encindex(void)
Identical to rb_utf8_encoding(), except it returns the encoding's index instead of the encoding itsel...
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
int rb_ascii8bit_encindex(void)
Identical to rb_ascii8bit_encoding(), except it returns the encoding's index instead of the encoding ...
int rb_enc_to_index(rb_encoding *enc)
Queries the index of the encoding.
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Identical to rb_enc_compatible(), except it raises an exception instead of returning NULL.
int rb_usascii_encindex(void)
Identical to rb_usascii_encoding(), except it returns the encoding's index instead of the encoding it...
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it additionally takes an encoding.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
rb_econv_t * rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags)
Creates a new instance of struct rb_econv_t.
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags)
Converts the passed C's pointer according to the passed converter, then append the conversion result ...
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#define rb_long2int
Just another name of rb_long2int_inline.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define FilePathValue(v)
Ensures that the parameter object is a path.