1#if defined(__MINGW32__)
3# define MINGW_HAS_SECURE_API 1
8#include "internal/error.h"
9#include "internal/file.h"
13#include "win32/file.h"
15#ifndef INVALID_FILE_ATTRIBUTES
16# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
25#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
26#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
28IS_ABSOLUTE_PATH_P(
const WCHAR *path,
size_t len)
30 if (
len < 2)
return FALSE;
32 return len > 2 && path[1] == L
':' && IS_DIR_SEPARATOR_P(path[2]);
34 return IS_DIR_UNC_P(path);
38#define INVALID_CODE_PAGE 51932
39#define PATH_BUFFER_SIZE MAX_PATH * 2
42#define system_code_page rb_w32_filecp
43#define mbstr_to_wstr rb_w32_mbstr_to_wstr
44#define wstr_to_mbstr rb_w32_wstr_to_mbstr
47replace_wchar(
wchar_t *s,
int find,
int replace)
58remove_invalid_alternative_data(
wchar_t *wfullpath,
size_t size)
60 static const wchar_t prime[] = L
":$DATA";
61 enum { prime_len = (
sizeof(prime) /
sizeof(wchar_t)) -1 };
63 if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
68 if (wfullpath[size - (prime_len + 1)] ==
':') {
70 size -= prime_len + 1;
71 wfullpath[size] = L
'\0';
75 wchar_t *pos = wfullpath + size - (prime_len + 1);
76 while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
79 wfullpath[size] = L
'\0';
88void rb_enc_foreach_name(
int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
91code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
93 const char *n = (
const char *)name;
94 if (strncmp(
"CP", n, 2) == 0) {
95 int code_page = atoi(n + 2);
98 unsigned int count = cp->count;
99 USHORT *table = cp->table;
101 unsigned int i = count;
102 count = (((idx + 4) & ~31) | 28);
103 table = realloc(table, count *
sizeof(*table));
104 if (!table)
return ST_CONTINUE;
107 while (i < count) table[i++] = INVALID_CODE_PAGE;
109 table[idx] = (USHORT)code_page;
121code_page(rb_encoding *enc)
126 return system_code_page();
128 enc_idx = rb_enc_to_index(enc);
138 if (0 <= enc_idx && (
unsigned int)enc_idx < rb_code_page.count)
139 return rb_code_page.table[enc_idx];
141 return INVALID_CODE_PAGE;
144#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
151replace_to_long_name(
wchar_t **wfullpath,
size_t size,
size_t buffer_size)
153 WIN32_FIND_DATAW find_data;
165 size_t const max_short_name_size = 8 + 1 + 3;
166 size_t const max_extension_size = 3;
167 size_t path_len = 1, extension_len = 0;
168 wchar_t *pos = *wfullpath;
170 if (size == 3 && pos[1] == L
':' && pos[2] == L
'\\' && pos[3] == L
'\0') {
176 if (wcspbrk(pos, L
"*?")) {
180 pos = *wfullpath + size - 1;
181 while (!IS_DIR_SEPARATOR_P(*pos) && pos != *wfullpath) {
182 if (!extension_len && *pos == L
'.') {
183 extension_len = path_len - 1;
185 if (path_len > max_short_name_size || extension_len > max_extension_size) {
192 if ((pos >= *wfullpath + 2) &&
193 (*wfullpath)[0] == L
'\\' && (*wfullpath)[1] == L
'\\') {
196 if (pos == *wfullpath + 2) {
200 if (!wmemchr(*wfullpath + 2, L
'\\', pos - *wfullpath - 2)) {
206 find_handle = FindFirstFileW(*wfullpath, &find_data);
207 if (find_handle != INVALID_HANDLE_VALUE) {
208 size_t trail_pos = pos - *wfullpath + IS_DIR_SEPARATOR_P(*pos);
209 size_t file_len = wcslen(find_data.cFileName);
210 size_t oldsize = size;
212 FindClose(find_handle);
213 size = trail_pos + file_len;
214 if (size > (buffer_size ? buffer_size-1 : oldsize)) {
215 wchar_t *buf =
ALLOC_N(
wchar_t, (size + 1));
216 wcsncpy(buf, *wfullpath, trail_pos);
221 wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
227user_length_in_path(
const wchar_t *wuser,
size_t len)
231 for (i = 0; i <
len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
238append_wstr(
VALUE dst,
const WCHAR *ws, ssize_t
len, UINT cp, rb_encoding *enc)
241 long nlen = (long)
len;
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);
248 WideCharToMultiByte(cp, 0, ws,
len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
249 rb_enc_associate(dst, enc);
254 char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (
int)
len, &nlen);
264rb_default_home_dir(
VALUE result)
266 WCHAR *dir = rb_w32_home_dir();
268 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
270 append_wstr(result, dir, -1,
279rb_w32_getcwdofdrv(
int drv)
281 WCHAR drive[3], *wcwd;
285 drive[0] = (WCHAR)drv;
288 len = GetFullPathNameW(drive, 0, NULL, NULL);
292 if (GetFullPathNameW(drive,
len, wcwd, NULL) <
len) {
293 replace_wchar(wcwd, L
'\\', L
'/');
302 RSTRING_PTR(cwd)[0] = (char)drv;
303 RSTRING_PTR(cwd)[1] =
':';
313get_full_path_name(
const wchar_t *path,
wchar_t **wfullpath,
wchar_t *wfullpath_buffer)
315 size_t size = GetFullPathNameW(path, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
316 *wfullpath = wfullpath_buffer;
317 if (size > PATH_BUFFER_SIZE) {
319 size_t bufsize = size;
320 wchar_t *buf =
ALLOC_N(
wchar_t, bufsize);
321 size = GetFullPathNameW(path, bufsize, buf, NULL);
322 if (size == 0 || size >= bufsize) {
337expand_path_generic(
const wchar_t *path,
wchar_t **wfullpath)
340 char *utf8 = wstr_to_mbstr(CP_UTF8, path, -1, &
len);
351 n = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(result),
len, NULL, 0);
353 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(result),
len, buf, n);
361rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
367 size_t size = 0, whome_len = 0;
368 size_t buffer_len = 0;
369 long wpath_len = 0, wdir_len = 0;
370 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
371 wchar_t *wdir = NULL, *wdir_pos = NULL;
372 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
374 VALUE path = fname, dir = dname;
375 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
376 wchar_t path_drive = L
'\0', dir_drive = L
'\0';
378 rb_encoding *path_encoding;
382 path_encoding = rb_enc_get(path);
385 path_encoding = rb_enc_check(path, dir);
388 cp = path_cp = code_page(path_encoding);
391 if (path_cp == INVALID_CODE_PAGE) {
394 path = fix_string_encoding(path, path_encoding);
400 const rb_len_t path_len = RSTRING_LEN(path);
401#if SIZEOF_INT < SIZEOF_RB_LEN_T
402 if ((
rb_len_t)(
int)path_len != path_len) {
407 wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
413 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L
'~' &&
414 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
415 whome = rb_w32_home_dir();
418 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
420 whome_len = wcslen(whome);
422 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
425 rb_raise(rb_eArgError,
"non-absolute home");
431 cp = path_cp = code_page(path_encoding);
442 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
447 else if (wpath_len >= 2 && wpath_pos[1] == L
':') {
448 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
454 path_drive = wpath_pos[0];
459 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L
'~') {
461 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
462 path_cp, path_encoding);
470 if (!ignore_dir && !
NIL_P(dir)) {
472 if (path_cp == INVALID_CODE_PAGE) {
473 dir = fix_string_encoding(dir, path_encoding);
478 const rb_len_t dir_len = RSTRING_LEN(dir);
479#if SIZEOF_INT < SIZEOF_RB_LEN_T
480 if ((
rb_len_t)(
int)dir_len != dir_len) {
482 rb_raise(
rb_eRangeError,
"base directory (%"PRIdLEN
" bytes) is too long",
486 wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
490 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L
'~' &&
491 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
492 whome = rb_w32_home_dir();
496 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
498 whome_len = wcslen(whome);
500 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
504 rb_raise(rb_eArgError,
"non-absolute home");
512 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
517 else if (wdir_len >= 2 && wdir[1] == L
':') {
519 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
523 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
525 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
529 while (pos < wdir_len && separators < 2) {
530 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
539 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L
'~') {
541 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
542 path_cp, path_encoding);
551 if (!ignore_dir && path_drive && dir_drive) {
552 if (towupper(path_drive) != towupper(dir_drive)) {
560 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
565 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
566 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
572 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
574 buffer = buffer_pos =
ALLOC_N(
wchar_t, (buffer_len + 1));
578 wcsncpy(buffer_pos, whome, whome_len);
579 buffer_pos += whome_len;
583 if (whome_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
584 buffer_pos[0] = L
'\\';
587 else if (!dir_drive && path_drive) {
588 *buffer_pos++ = path_drive;
589 *buffer_pos++ = L
':';
593 wcsncpy(buffer_pos, wdir_pos, wdir_len);
594 buffer_pos += wdir_len;
598 if (wdir_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
599 buffer_pos[0] = L
'\\';
605 wcsncpy(buffer_pos, wpath_pos, wpath_len);
606 buffer_pos += wpath_len;
610 if (wpath_len == 0) {
611 buffer_pos[0] = L
'.';
616 buffer_pos[0] = L
'\0';
618 size = get_full_path_name(buffer, &wfullpath, wfullpath_buffer);
625 size = expand_path_generic(buffer, &wfullpath);
630 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
631 wfullpath[size - 2] != L
':' &&
632 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
634 wfullpath[size] = L
'\0';
638 if (wfullpath[size - 1] == L
'.') {
640 wfullpath[size] = L
'\0';
644 size = remove_invalid_alternative_data(wfullpath, size);
648 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
649 size = replace_to_long_name(&wfullpath, size, bufsize);
653 replace_wchar(wfullpath, L
'\\', L
'/');
657 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
665 if (wfullpath != wfullpath_buffer)
668 rb_enc_associate(result, path_encoding);
673rb_file_load_ok(
const char *path)
680 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &
len);
681 if (!wpath)
return 0;
683 attr = GetFileAttributesW(wpath);
684 if (attr == INVALID_FILE_ATTRIBUTES ||
685 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
689 HANDLE h = CreateFileW(wpath, GENERIC_READ,
690 FILE_SHARE_READ | FILE_SHARE_WRITE,
691 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
692 if (h != INVALID_HANDLE_VALUE) {
704rb_freopen(
VALUE fname,
const char *mode, FILE *file)
706 WCHAR *wname, wmode[4];
710 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
711 if (n > numberof(wmode))
return EINVAL;
712 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
715 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
717 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname,
len);
722 e = _wfreopen_s(&newfp, wname, wmode, file);
729Init_w32_codepage(
void)
731 if (rb_code_page.count)
return;
732 rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
#define ISALPHA
@old{rb_isalpha}
#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 Qnil
Old name of RUBY_Qnil.
#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_exc_raise(VALUE mesg)
Raises an exception in the current thread.
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.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
int rb_utf8_encindex(void)
Identical to rb_utf8_encoding(), except it returns the encoding's index instead of the encoding itsel...
int rb_ascii8bit_encindex(void)
Identical to rb_ascii8bit_encoding(), except it returns the encoding's index instead of the encoding ...
int rb_usascii_encindex(void)
Identical to rb_usascii_encoding(), except it returns the encoding's index instead of the encoding it...
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.
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, rb_len_t bytesize, VALUE dst, int flags)
Converts the passed C's pointer according to the passed converter, then append the conversion result ...
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
void rb_memerror(void)
Triggers out-of-memory error.
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
#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_len2int
Just another name of rb_len2int_inline.
#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.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t VALUE
Type that represents a Ruby object.