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]))
27 IS_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
46 replace_wchar(
wchar_t *s,
int find,
int replace)
57 remove_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';
87 void rb_enc_foreach_name(
int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
90 code_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())
150 replace_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);
226 user_length_in_path(
const wchar_t *wuser,
size_t len)
230 for (i = 0; i < len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
237 append_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);
262 rb_default_home_dir(
VALUE result)
264 WCHAR *dir = rb_w32_home_dir();
268 append_wstr(result, dir, -1,
276 rb_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';
299 cp = path_cp = code_page(path_encoding);
302 if (path_cp == INVALID_CODE_PAGE) {
305 path = fix_string_encoding(path, path_encoding);
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();
331 whome_len = wcslen(whome);
333 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
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);
382 if (!ignore_dir && !
NIL_P(dir)) {
384 if (path_cp == INVALID_CODE_PAGE) {
385 dir = fix_string_encoding(dir, path_encoding);
391 #if SIZEOF_INT < SIZEOF_LONG
392 if ((
long)(
int)dir_len != dir_len) {
393 if (wpath) free(wpath);
398 wdir = mbstr_to_wstr(cp,
RSTRING_PTR(dir), dir_len, &wdir_len);
402 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L
'~' &&
403 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
404 whome = rb_w32_home_dir();
410 whome_len = wcslen(whome);
412 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
424 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
429 else if (wdir_len >= 2 && wdir[1] == L
':') {
431 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
435 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
437 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
441 while (pos < wdir_len && separators < 2) {
442 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
451 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L
'~') {
453 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
454 path_cp, path_encoding);
466 if (!ignore_dir && path_drive && dir_drive) {
467 if (towupper(path_drive) != towupper(dir_drive)) {
475 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
480 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
481 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
487 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
489 buffer = buffer_pos =
ALLOC_N(
wchar_t, (buffer_len + 1));
493 wcsncpy(buffer_pos, whome, whome_len);
494 buffer_pos += whome_len;
498 if (whome_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
499 buffer_pos[0] = L
'\\';
502 else if (!dir_drive && path_drive) {
503 *buffer_pos++ = path_drive;
504 *buffer_pos++ = L
':';
508 wcsncpy(buffer_pos, wdir_pos, wdir_len);
509 buffer_pos += wdir_len;
513 if (wdir_len && wcsrchr(L
"\\/:", buffer_pos[-1]) == NULL) {
514 buffer_pos[0] = L
'\\';
520 wcsncpy(buffer_pos, wpath_pos, wpath_len);
521 buffer_pos += wpath_len;
525 if (wpath_len == 0) {
526 buffer_pos[0] = L
'.';
531 buffer_pos[0] = L
'\0';
535 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
536 if (size > PATH_BUFFER_SIZE) {
538 wfullpath =
ALLOC_N(
wchar_t, size);
539 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
542 wfullpath = wfullpath_buffer;
546 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
547 wfullpath[size - 2] != L
':' &&
548 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
550 wfullpath[size] = L
'\0';
554 if (wfullpath[size - 1] == L
'.') {
556 wfullpath[size] = L
'\0';
560 size = remove_invalid_alternative_data(wfullpath, size);
564 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
565 size = replace_to_long_name(&wfullpath, size, bufsize);
569 replace_wchar(wfullpath, L
'\\', L
'/');
573 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
588 if (wfullpath != wfullpath_buffer)
599 VALUE wtmp = 0, wpathbuf, str;
608 cp = path_cp = code_page(enc);
609 if (cp == INVALID_CODE_PAGE) {
610 path = fix_string_encoding(path, enc);
614 wpath =
ALLOCV_N(WCHAR, wpathbuf, len+1);
617 e = rb_w32_read_reparse_point(wpath, rp,
sizeof(rbuf), &wbuf, &len);
618 if (e == ERROR_MORE_DATA) {
619 size_t size = rb_w32_reparse_buffer_size(len + 1);
621 e = rb_w32_read_reparse_point(wpath, rp, size, &wbuf, &len);
627 rb_syserr_fail_path(rb_w32_map_errno(e), path);
629 rb_syserr_fail_path(EINVAL, path);
632 path_cp = code_page(enc);
633 len = lstrlenW(wbuf);
634 str = append_wstr(
rb_enc_str_new(0, 0, enc), wbuf, len, path_cp, enc);
640 rb_file_load_ok(
const char *path)
647 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
648 if (!wpath)
return 0;
650 attr = GetFileAttributesW(wpath);
651 if (attr == INVALID_FILE_ATTRIBUTES ||
652 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
656 HANDLE h = CreateFileW(wpath, GENERIC_READ,
657 FILE_SHARE_READ | FILE_SHARE_WRITE,
658 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
659 if (h != INVALID_HANDLE_VALUE) {
671 rb_freopen(
VALUE fname,
const char *mode,
FILE *file)
673 WCHAR *wname, wmode[4];
677 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
678 if (n > numberof(wmode))
return EINVAL;
679 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
682 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
683 wname =
ALLOCV_N(WCHAR, wtmp, len + 1);
684 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
687 #if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
688 e = _wfreopen(wname, wmode, file) ? 0 : errno;
692 e = _wfreopen_s(&newfp, wname, wmode, file);
700 Init_w32_codepage(
void)
702 if (rb_code_page.count)
return;
703 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_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.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Identical to rb_enc_associate(), except it takes an encoding itself instead of its index.
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_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.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
int rb_usascii_encindex(void)
Identical to rb_usascii_encoding(), except it returns the encoding's index instead of the encoding it...
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
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".
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 ...
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_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.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#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.
#define FilePathValue(v)
Ensures that the parameter object is a path.
uintptr_t VALUE
Type that represents a Ruby object.