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();
127 enc_idx = rb_enc_to_index(enc);
130 if (enc_idx == rb_usascii_encindex() || enc_idx == rb_ascii8bit_encindex()) {
133 if (enc_idx == rb_utf8_encindex()) {
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++)
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);
244 olen = RSTRING_LEN(dst);
246 WideCharToMultiByte(cp, 0, ws,
len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
247 rb_enc_associate(dst, enc);
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,
269 CP_UTF8, rb_utf8_encoding());
276rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
278 size_t size = 0, whome_len = 0;
279 size_t buffer_len = 0;
280 long wpath_len = 0, wdir_len = 0;
281 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
282 wchar_t *wdir = NULL, *wdir_pos = NULL;
283 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
285 VALUE path = fname, dir = dname;
286 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
287 wchar_t path_drive = L
'\0', dir_drive = L
'\0';
293 path_encoding = rb_enc_get(path);
296 path_encoding = rb_enc_check(path, dir);
299 cp = path_cp = code_page(path_encoding);
302 if (path_cp == INVALID_CODE_PAGE) {
305 path = fix_string_encoding(path, path_encoding);
311 const long path_len = RSTRING_LEN(path);
312#if SIZEOF_INT < SIZEOF_LONG
313 if ((
long)(
int)path_len != path_len) {
318 wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
324 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L
'~' &&
325 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
326 whome = rb_w32_home_dir();
329 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
331 whome_len = wcslen(whome);
333 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
336 rb_raise(rb_eArgError,
"non-absolute home");
341 path_encoding = rb_filesystem_encoding();
342 cp = path_cp = code_page(path_encoding);
353 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
358 else if (wpath_len >= 2 && wpath_pos[1] == L
':') {
359 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
365 path_drive = wpath_pos[0];
370 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L
'~') {
372 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
373 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);
389 const long dir_len = RSTRING_LEN(dir);
390#if SIZEOF_INT < SIZEOF_LONG
391 if ((
long)(
int)dir_len != dir_len) {
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);
462 if (!ignore_dir && path_drive && dir_drive) {
463 if (towupper(path_drive) != towupper(dir_drive)) {
471 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
476 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
477 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
483 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
485 buffer = buffer_pos =
ALLOC_N(
wchar_t, (buffer_len + 1));
489 wcsncpy(buffer_pos, whome, whome_len);
490 buffer_pos += whome_len;
494 if (whome_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
495 buffer_pos[0] = L
'\\';
498 else if (!dir_drive && path_drive) {
499 *buffer_pos++ = path_drive;
500 *buffer_pos++ = L
':';
504 wcsncpy(buffer_pos, wdir_pos, wdir_len);
505 buffer_pos += wdir_len;
509 if (wdir_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
510 buffer_pos[0] = L
'\\';
516 wcsncpy(buffer_pos, wpath_pos, wpath_len);
517 buffer_pos += wpath_len;
521 if (wpath_len == 0) {
522 buffer_pos[0] = L
'.';
527 buffer_pos[0] = L
'\0';
531 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
532 if (size > PATH_BUFFER_SIZE) {
534 wfullpath =
ALLOC_N(
wchar_t, size);
535 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
538 wfullpath = wfullpath_buffer;
542 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
543 wfullpath[size - 2] != L
':' &&
544 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
546 wfullpath[size] = L
'\0';
550 if (wfullpath[size - 1] == L
'.') {
552 wfullpath[size] = L
'\0';
556 size = remove_invalid_alternative_data(wfullpath, size);
560 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
561 size = replace_to_long_name(&wfullpath, size, bufsize);
565 replace_wchar(wfullpath, L
'\\', L
'/');
569 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
577 if (wfullpath != wfullpath_buffer)
580 rb_enc_associate(result, path_encoding);
585rb_file_load_ok(
const char *path)
592 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &
len);
593 if (!wpath)
return 0;
595 attr = GetFileAttributesW(wpath);
596 if (attr == INVALID_FILE_ATTRIBUTES ||
597 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
601 HANDLE h = CreateFileW(wpath, GENERIC_READ,
602 FILE_SHARE_READ | FILE_SHARE_WRITE,
603 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
604 if (h != INVALID_HANDLE_VALUE) {
616rb_freopen(
VALUE fname,
const char *mode,
FILE *file)
618 WCHAR *wname, wmode[4];
622 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
623 if (n > numberof(wmode))
return EINVAL;
624 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
627 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
629 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname,
len);
632#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
633 e = _wfreopen(wname, wmode, file) ? 0 :
errno;
637 e = _wfreopen_s(&newfp, wname, wmode, file);
645Init_w32_codepage(
void)
647 if (rb_code_page.count)
return;
648 rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
#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.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
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.
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_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
int len
Length of the buffer.
#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.
#define errno
Ractor-aware version of errno.
uintptr_t VALUE
Type that represents a Ruby object.