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++)
240 long nlen = (long)
len;
242 if (cp != INVALID_CODE_PAGE) {
243 if (
len == -1)
len = lstrlenW(ws);
244 nlen = WideCharToMultiByte(cp, 0, ws,
len, NULL, 0, NULL, NULL);
245 olen = RSTRING_LEN(dst);
247 WideCharToMultiByte(cp, 0, ws,
len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
248 rb_enc_associate(dst, enc);
253 char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (
int)
len, &nlen);
263rb_default_home_dir(
VALUE result)
265 WCHAR *dir = rb_w32_home_dir();
267 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
269 append_wstr(result, dir, -1,
270 CP_UTF8, rb_utf8_encoding());
277rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
283 size_t size = 0, whome_len = 0;
284 size_t buffer_len = 0;
285 long wpath_len = 0, wdir_len = 0;
286 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
287 wchar_t *wdir = NULL, *wdir_pos = NULL;
288 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
290 VALUE path = fname, dir = dname;
291 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
292 wchar_t path_drive = L
'\0', dir_drive = L
'\0';
298 path_encoding = rb_enc_get(path);
301 path_encoding = rb_enc_check(path, dir);
304 cp = path_cp = code_page(path_encoding);
307 if (path_cp == INVALID_CODE_PAGE) {
310 path = fix_string_encoding(path, path_encoding);
316 const rb_len_t path_len = RSTRING_LEN(path);
317#if SIZEOF_INT < SIZEOF_RB_LEN_T
318 if ((
rb_len_t)(
int)path_len != path_len) {
323 wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
329 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L
'~' &&
330 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
331 whome = rb_w32_home_dir();
334 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
336 whome_len = wcslen(whome);
338 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
341 rb_raise(rb_eArgError,
"non-absolute home");
346 path_encoding = rb_filesystem_encoding();
347 cp = path_cp = code_page(path_encoding);
358 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
363 else if (wpath_len >= 2 && wpath_pos[1] == L
':') {
364 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
370 path_drive = wpath_pos[0];
375 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L
'~') {
377 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
378 path_cp, path_encoding);
386 if (!ignore_dir && !
NIL_P(dir)) {
388 if (path_cp == INVALID_CODE_PAGE) {
389 dir = fix_string_encoding(dir, path_encoding);
394 const rb_len_t dir_len = RSTRING_LEN(dir);
395#if SIZEOF_INT < SIZEOF_RB_LEN_T
396 if ((
rb_len_t)(
int)dir_len != dir_len) {
398 rb_raise(
rb_eRangeError,
"base directory (%"PRIdLEN
" bytes) is too long",
402 wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
406 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L
'~' &&
407 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
408 whome = rb_w32_home_dir();
412 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
414 whome_len = wcslen(whome);
416 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
420 rb_raise(rb_eArgError,
"non-absolute home");
428 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
433 else if (wdir_len >= 2 && wdir[1] == L
':') {
435 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
439 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
441 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
445 while (pos < wdir_len && separators < 2) {
446 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
455 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L
'~') {
457 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
458 path_cp, path_encoding);
467 if (!ignore_dir && path_drive && dir_drive) {
468 if (towupper(path_drive) != towupper(dir_drive)) {
476 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
481 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
482 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
488 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
490 buffer = buffer_pos =
ALLOC_N(
wchar_t, (buffer_len + 1));
494 wcsncpy(buffer_pos, whome, whome_len);
495 buffer_pos += whome_len;
499 if (whome_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
500 buffer_pos[0] = L
'\\';
503 else if (!dir_drive && path_drive) {
504 *buffer_pos++ = path_drive;
505 *buffer_pos++ = L
':';
509 wcsncpy(buffer_pos, wdir_pos, wdir_len);
510 buffer_pos += wdir_len;
514 if (wdir_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
515 buffer_pos[0] = L
'\\';
521 wcsncpy(buffer_pos, wpath_pos, wpath_len);
522 buffer_pos += wpath_len;
526 if (wpath_len == 0) {
527 buffer_pos[0] = L
'.';
532 buffer_pos[0] = L
'\0';
536 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
537 if (size > PATH_BUFFER_SIZE) {
539 wfullpath =
ALLOC_N(
wchar_t, size);
540 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
543 wfullpath = wfullpath_buffer;
547 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
548 wfullpath[size - 2] != L
':' &&
549 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
551 wfullpath[size] = L
'\0';
555 if (wfullpath[size - 1] == L
'.') {
557 wfullpath[size] = L
'\0';
561 size = remove_invalid_alternative_data(wfullpath, size);
565 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
566 size = replace_to_long_name(&wfullpath, size, bufsize);
570 replace_wchar(wfullpath, L
'\\', L
'/');
574 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
582 if (wfullpath != wfullpath_buffer)
585 rb_enc_associate(result, path_encoding);
590rb_file_load_ok(
const char *path)
597 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &
len);
598 if (!wpath)
return 0;
600 attr = GetFileAttributesW(wpath);
601 if (attr == INVALID_FILE_ATTRIBUTES ||
602 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
606 HANDLE h = CreateFileW(wpath, GENERIC_READ,
607 FILE_SHARE_READ | FILE_SHARE_WRITE,
608 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
609 if (h != INVALID_HANDLE_VALUE) {
621rb_freopen(
VALUE fname,
const char *mode,
FILE *file)
623 WCHAR *wname, wmode[4];
627 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
628 if (n > numberof(wmode))
return EINVAL;
629 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
632 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
634 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname,
len);
639 e = _wfreopen_s(&newfp, wname, wmode, file);
646Init_w32_codepage(
void)
648 if (rb_code_page.count)
return;
649 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.
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.
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_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_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_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.