2015-07-03 08:30:21 +03:00
|
|
|
#if defined(__MINGW32__)
|
|
|
|
/* before stdio.h in ruby/define.h */
|
|
|
|
# define MINGW_HAS_SECURE_API 1
|
|
|
|
#endif
|
2012-02-28 19:51:25 +04:00
|
|
|
#include "ruby/ruby.h"
|
2012-08-24 07:44:56 +04:00
|
|
|
#include "ruby/encoding.h"
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
|
|
|
#include "internal.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/error.h"
|
2012-02-28 19:51:25 +04:00
|
|
|
#include <winbase.h>
|
2020-05-26 08:19:20 +03:00
|
|
|
#include <wchar.h>
|
2012-08-24 07:44:56 +04:00
|
|
|
#include <shlwapi.h>
|
2015-08-25 08:11:19 +03:00
|
|
|
#include "win32/file.h"
|
2012-02-28 19:51:25 +04:00
|
|
|
|
2012-04-18 09:50:45 +04:00
|
|
|
#ifndef INVALID_FILE_ATTRIBUTES
|
|
|
|
# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
|
|
|
|
#endif
|
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
/* cache 'encoding name' => 'code page' into a hash */
|
2013-10-19 14:59:06 +04:00
|
|
|
static struct code_page_table {
|
|
|
|
USHORT *table;
|
|
|
|
unsigned int count;
|
|
|
|
} rb_code_page;
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
|
|
|
|
#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
|
2018-03-28 10:01:48 +03:00
|
|
|
static int
|
|
|
|
IS_ABSOLUTE_PATH_P(const WCHAR *path, size_t len)
|
|
|
|
{
|
|
|
|
if (len < 2) return FALSE;
|
|
|
|
if (ISALPHA(path[0]))
|
|
|
|
return len > 2 && path[1] == L':' && IS_DIR_SEPARATOR_P(path[2]);
|
|
|
|
else
|
|
|
|
return IS_DIR_UNC_P(path);
|
|
|
|
}
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
/* MultiByteToWideChar() doesn't work with code page 51932 */
|
|
|
|
#define INVALID_CODE_PAGE 51932
|
|
|
|
#define PATH_BUFFER_SIZE MAX_PATH * 2
|
|
|
|
|
2014-08-09 14:10:11 +04:00
|
|
|
/* defined in win32/win32.c */
|
|
|
|
#define system_code_page rb_w32_filecp
|
|
|
|
#define mbstr_to_wstr rb_w32_mbstr_to_wstr
|
|
|
|
#define wstr_to_mbstr rb_w32_wstr_to_mbstr
|
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
static inline void
|
|
|
|
replace_wchar(wchar_t *s, int find, int replace)
|
|
|
|
{
|
|
|
|
while (*s != 0) {
|
|
|
|
if (*s == find)
|
|
|
|
*s = replace;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove trailing invalid ':$DATA' of the path. */
|
|
|
|
static inline size_t
|
2012-09-05 09:29:41 +04:00
|
|
|
remove_invalid_alternative_data(wchar_t *wfullpath, size_t size)
|
|
|
|
{
|
2012-08-24 07:44:56 +04:00
|
|
|
static const wchar_t prime[] = L":$DATA";
|
|
|
|
enum { prime_len = (sizeof(prime) / sizeof(wchar_t)) -1 };
|
|
|
|
|
|
|
|
if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
/* alias of stream */
|
|
|
|
/* get rid of a bug of x64 VC++ */
|
|
|
|
if (wfullpath[size - (prime_len + 1)] == ':') {
|
|
|
|
/* remove trailing '::$DATA' */
|
|
|
|
size -= prime_len + 1; /* prime */
|
|
|
|
wfullpath[size] = L'\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* remove trailing ':$DATA' of paths like '/aa:a:$DATA' */
|
|
|
|
wchar_t *pos = wfullpath + size - (prime_len + 1);
|
|
|
|
while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
|
|
|
|
if (*pos == L':') {
|
|
|
|
size -= prime_len; /* alternative */
|
|
|
|
wfullpath[size] = L'\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-10-07 11:20:59 +04:00
|
|
|
void rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
|
|
|
|
|
|
|
|
static int
|
|
|
|
code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
|
|
|
|
{
|
|
|
|
const char *n = (const char *)name;
|
|
|
|
if (strncmp("CP", n, 2) == 0) {
|
|
|
|
int code_page = atoi(n + 2);
|
|
|
|
if (code_page != 0) {
|
2013-10-19 14:59:06 +04:00
|
|
|
struct code_page_table *cp = (struct code_page_table *)arg;
|
|
|
|
unsigned int count = cp->count;
|
|
|
|
USHORT *table = cp->table;
|
|
|
|
if (count <= idx) {
|
|
|
|
unsigned int i = count;
|
2014-12-26 08:48:12 +03:00
|
|
|
count = (((idx + 4) & ~31) | 28);
|
|
|
|
table = realloc(table, count * sizeof(*table));
|
|
|
|
if (!table) return ST_CONTINUE;
|
|
|
|
cp->count = count;
|
|
|
|
cp->table = table;
|
2013-10-19 14:59:06 +04:00
|
|
|
while (i < count) table[i++] = INVALID_CODE_PAGE;
|
|
|
|
}
|
|
|
|
table[idx] = (USHORT)code_page;
|
2013-10-07 11:20:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
/*
|
|
|
|
Return code page number of the encoding.
|
|
|
|
Cache code page into a hash for performance since finding the code page in
|
|
|
|
Encoding#names is slow.
|
|
|
|
*/
|
|
|
|
static UINT
|
|
|
|
code_page(rb_encoding *enc)
|
|
|
|
{
|
2013-10-07 10:50:18 +04:00
|
|
|
int enc_idx;
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
if (!enc)
|
|
|
|
return system_code_page();
|
|
|
|
|
2013-10-07 10:50:18 +04:00
|
|
|
enc_idx = rb_enc_to_index(enc);
|
|
|
|
|
2013-10-07 10:09:01 +04:00
|
|
|
/* map US-ASCII and ASCII-8bit as code page 1252 (us-ascii) */
|
2013-10-07 10:50:18 +04:00
|
|
|
if (enc_idx == rb_usascii_encindex() || enc_idx == rb_ascii8bit_encindex()) {
|
2013-10-07 10:09:01 +04:00
|
|
|
return 1252;
|
|
|
|
}
|
2014-08-09 15:50:13 +04:00
|
|
|
if (enc_idx == rb_utf8_encindex()) {
|
|
|
|
return CP_UTF8;
|
|
|
|
}
|
2013-10-07 10:09:01 +04:00
|
|
|
|
2013-10-19 14:59:06 +04:00
|
|
|
if (0 <= enc_idx && (unsigned int)enc_idx < rb_code_page.count)
|
|
|
|
return rb_code_page.table[enc_idx];
|
2012-08-24 07:44:56 +04:00
|
|
|
|
2013-10-07 11:20:59 +04:00
|
|
|
return INVALID_CODE_PAGE;
|
2012-08-24 07:44:56 +04:00
|
|
|
}
|
|
|
|
|
2013-07-26 12:49:31 +04:00
|
|
|
#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
Replace the last part of the path to long name.
|
|
|
|
We try to avoid to call FindFirstFileW() since it takes long time.
|
|
|
|
*/
|
|
|
|
static inline size_t
|
2016-04-28 09:54:05 +03:00
|
|
|
replace_to_long_name(wchar_t **wfullpath, size_t size, size_t buffer_size)
|
2012-09-05 09:29:41 +04:00
|
|
|
{
|
2012-08-24 07:44:56 +04:00
|
|
|
WIN32_FIND_DATAW find_data;
|
|
|
|
HANDLE find_handle;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Skip long name conversion if the path is already long name.
|
|
|
|
Short name is 8.3 format.
|
2020-07-28 13:51:07 +03:00
|
|
|
https://en.wikipedia.org/wiki/8.3_filename
|
2012-08-24 07:44:56 +04:00
|
|
|
This check can be skipped for directory components that have file
|
|
|
|
extensions longer than 3 characters, or total lengths longer than
|
|
|
|
12 characters.
|
2021-02-19 22:10:21 +03:00
|
|
|
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364980(v=vs.85).aspx
|
2012-08-24 07:44:56 +04:00
|
|
|
*/
|
|
|
|
size_t const max_short_name_size = 8 + 1 + 3;
|
|
|
|
size_t const max_extension_size = 3;
|
|
|
|
size_t path_len = 1, extension_len = 0;
|
|
|
|
wchar_t *pos = *wfullpath;
|
|
|
|
|
|
|
|
if (size == 3 && pos[1] == L':' && pos[2] == L'\\' && pos[3] == L'\0') {
|
|
|
|
/* root path doesn't need short name expansion */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-11-17 16:46:17 +04:00
|
|
|
/* skip long name conversion if path contains wildcard characters */
|
2012-11-17 21:53:21 +04:00
|
|
|
if (wcspbrk(pos, L"*?")) {
|
2012-11-17 02:43:22 +04:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
pos = *wfullpath + size - 1;
|
|
|
|
while (!IS_DIR_SEPARATOR_P(*pos) && pos != *wfullpath) {
|
|
|
|
if (!extension_len && *pos == L'.') {
|
|
|
|
extension_len = path_len - 1;
|
|
|
|
}
|
|
|
|
if (path_len > max_short_name_size || extension_len > max_extension_size) {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
path_len++;
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
|
2019-03-04 11:13:25 +03:00
|
|
|
if ((pos >= *wfullpath + 2) &&
|
|
|
|
(*wfullpath)[0] == L'\\' && (*wfullpath)[1] == L'\\') {
|
|
|
|
/* UNC path: no short file name, and needs Network Share
|
|
|
|
* Management functions instead of FindFirstFile. */
|
|
|
|
if (pos == *wfullpath + 2) {
|
|
|
|
/* //host only */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
if (!wmemchr(*wfullpath + 2, L'\\', pos - *wfullpath - 2)) {
|
|
|
|
/* //host/share only */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
find_handle = FindFirstFileW(*wfullpath, &find_data);
|
|
|
|
if (find_handle != INVALID_HANDLE_VALUE) {
|
2016-04-28 09:54:03 +03:00
|
|
|
size_t trail_pos = pos - *wfullpath + IS_DIR_SEPARATOR_P(*pos);
|
2012-08-24 07:44:56 +04:00
|
|
|
size_t file_len = wcslen(find_data.cFileName);
|
2016-04-28 09:54:05 +03:00
|
|
|
size_t oldsize = size;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
FindClose(find_handle);
|
2016-04-28 09:54:03 +03:00
|
|
|
size = trail_pos + file_len;
|
2016-04-28 09:54:05 +03:00
|
|
|
if (size > (buffer_size ? buffer_size-1 : oldsize)) {
|
2016-08-25 05:57:41 +03:00
|
|
|
wchar_t *buf = ALLOC_N(wchar_t, (size + 1));
|
2016-04-28 09:54:03 +03:00
|
|
|
wcsncpy(buf, *wfullpath, trail_pos);
|
2016-04-28 09:54:05 +03:00
|
|
|
if (!buffer_size)
|
2012-08-24 07:44:56 +04:00
|
|
|
xfree(*wfullpath);
|
|
|
|
*wfullpath = buf;
|
|
|
|
}
|
2016-04-28 09:54:03 +03:00
|
|
|
wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
|
2012-08-24 07:44:56 +04:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-08-09 15:50:13 +04:00
|
|
|
static inline size_t
|
2014-09-29 17:54:50 +04:00
|
|
|
user_length_in_path(const wchar_t *wuser, size_t len)
|
2013-03-14 07:56:09 +04:00
|
|
|
{
|
2014-09-29 17:54:50 +04:00
|
|
|
size_t i;
|
2013-03-14 07:56:09 +04:00
|
|
|
|
2014-09-29 17:54:50 +04:00
|
|
|
for (i = 0; i < len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
|
|
|
|
;
|
2013-03-14 07:56:09 +04:00
|
|
|
|
2014-09-29 17:54:50 +04:00
|
|
|
return i;
|
2014-08-09 15:50:13 +04:00
|
|
|
}
|
2013-03-14 07:56:09 +04:00
|
|
|
|
2014-08-09 15:50:13 +04:00
|
|
|
static VALUE
|
2016-08-24 18:42:33 +03:00
|
|
|
append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc)
|
2014-08-09 15:50:13 +04:00
|
|
|
{
|
|
|
|
long olen, nlen = (long)len;
|
|
|
|
|
2016-08-24 18:42:33 +03:00
|
|
|
if (cp != INVALID_CODE_PAGE) {
|
2016-08-24 18:39:52 +03:00
|
|
|
if (len == -1) len = lstrlenW(ws);
|
2014-08-09 15:50:13 +04:00
|
|
|
nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL);
|
|
|
|
olen = RSTRING_LEN(dst);
|
|
|
|
rb_str_modify_expand(dst, nlen);
|
|
|
|
WideCharToMultiByte(cp, 0, ws, len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
|
2016-08-24 18:42:33 +03:00
|
|
|
rb_enc_associate(dst, enc);
|
2014-09-29 17:54:50 +04:00
|
|
|
rb_str_set_len(dst, olen + nlen);
|
2013-03-14 07:56:09 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-08-09 15:50:13 +04:00
|
|
|
const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
|
|
|
|
char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (int)len, &nlen);
|
2016-08-24 18:42:33 +03:00
|
|
|
rb_econv_t *ec = rb_econv_open("UTF-8", rb_enc_name(enc), replaceflags);
|
2014-08-09 15:50:13 +04:00
|
|
|
dst = rb_econv_append(ec, utf8str, nlen, dst, replaceflags);
|
|
|
|
rb_econv_close(ec);
|
|
|
|
free(utf8str);
|
|
|
|
}
|
|
|
|
return dst;
|
2013-03-14 07:56:09 +04:00
|
|
|
}
|
|
|
|
|
2016-11-26 14:37:01 +03:00
|
|
|
VALUE
|
|
|
|
rb_default_home_dir(VALUE result)
|
|
|
|
{
|
2016-12-07 05:03:40 +03:00
|
|
|
WCHAR *dir = rb_w32_home_dir();
|
2016-11-26 14:37:01 +03:00
|
|
|
if (!dir) {
|
|
|
|
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
|
|
|
|
}
|
|
|
|
append_wstr(result, dir, -1,
|
2022-12-18 23:05:22 +03:00
|
|
|
CP_UTF8, rb_utf8_encoding());
|
|
|
|
|
2016-11-26 14:37:01 +03:00
|
|
|
xfree(dir);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
VALUE
|
|
|
|
rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
|
|
|
|
{
|
2014-08-09 14:10:11 +04:00
|
|
|
size_t size = 0, whome_len = 0;
|
2012-08-24 07:44:56 +04:00
|
|
|
size_t buffer_len = 0;
|
2014-08-09 14:10:11 +04:00
|
|
|
long wpath_len = 0, wdir_len = 0;
|
2013-03-10 18:39:09 +04:00
|
|
|
wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
|
|
|
|
wchar_t *wdir = NULL, *wdir_pos = NULL;
|
2012-08-24 07:44:56 +04:00
|
|
|
wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
|
|
|
|
UINT path_cp, cp;
|
|
|
|
VALUE path = fname, dir = dname;
|
|
|
|
wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
|
|
|
|
wchar_t path_drive = L'\0', dir_drive = L'\0';
|
|
|
|
int ignore_dir = 0;
|
|
|
|
rb_encoding *path_encoding;
|
|
|
|
|
|
|
|
/* get path encoding */
|
|
|
|
if (NIL_P(dir)) {
|
|
|
|
path_encoding = rb_enc_get(path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
path_encoding = rb_enc_check(path, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = path_cp = code_page(path_encoding);
|
|
|
|
|
|
|
|
/* workaround invalid codepage */
|
|
|
|
if (path_cp == INVALID_CODE_PAGE) {
|
|
|
|
cp = CP_UTF8;
|
|
|
|
if (!NIL_P(path)) {
|
|
|
|
path = fix_string_encoding(path, path_encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert char * to wchar_t */
|
2013-07-26 08:00:28 +04:00
|
|
|
if (!NIL_P(path)) {
|
2015-03-22 07:54:35 +03:00
|
|
|
const long path_len = RSTRING_LEN(path);
|
|
|
|
#if SIZEOF_INT < SIZEOF_LONG
|
|
|
|
if ((long)(int)path_len != path_len) {
|
|
|
|
rb_raise(rb_eRangeError, "path (%ld bytes) is too long",
|
|
|
|
path_len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
|
2013-07-26 08:00:28 +04:00
|
|
|
wpath_pos = wpath;
|
|
|
|
}
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
/* determine if we need the user's home directory */
|
|
|
|
/* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
|
2012-09-05 09:29:44 +04:00
|
|
|
if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L'~' &&
|
|
|
|
(wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
|
2016-11-26 14:37:01 +03:00
|
|
|
whome = rb_w32_home_dir();
|
2012-08-24 07:44:56 +04:00
|
|
|
if (whome == NULL) {
|
2016-04-10 10:30:42 +03:00
|
|
|
free(wpath);
|
2012-08-24 07:44:56 +04:00
|
|
|
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
|
|
|
|
}
|
|
|
|
whome_len = wcslen(whome);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2018-03-28 10:01:48 +03:00
|
|
|
if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
|
2016-04-10 10:30:42 +03:00
|
|
|
free(wpath);
|
2013-10-17 09:21:38 +04:00
|
|
|
xfree(whome);
|
2012-08-24 07:44:56 +04:00
|
|
|
rb_raise(rb_eArgError, "non-absolute home");
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-10-19 14:59:06 +04:00
|
|
|
if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
|
|
|
|
/* use filesystem encoding if expanding home dir */
|
|
|
|
path_encoding = rb_filesystem_encoding();
|
2020-12-19 20:16:31 +03:00
|
|
|
cp = path_cp = code_page(path_encoding);
|
2013-10-19 14:59:06 +04:00
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-05-19 07:10:21 +04:00
|
|
|
/* ignores dir since we are expanding home */
|
2012-08-24 07:44:56 +04:00
|
|
|
ignore_dir = 1;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
/* exclude ~ from the result */
|
|
|
|
wpath_pos++;
|
|
|
|
wpath_len--;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
/* exclude separator if present */
|
|
|
|
if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
|
|
|
|
wpath_pos++;
|
|
|
|
wpath_len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wpath_len >= 2 && wpath_pos[1] == L':') {
|
|
|
|
if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
|
|
|
|
/* ignore dir since path contains a drive letter and a root slash */
|
|
|
|
ignore_dir = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* determine if we ignore dir or not later */
|
|
|
|
path_drive = wpath_pos[0];
|
2015-02-17 04:53:32 +03:00
|
|
|
wpath_pos += 2;
|
|
|
|
wpath_len -= 2;
|
2012-08-24 07:44:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
|
2014-08-09 15:50:13 +04:00
|
|
|
result = rb_str_new_cstr("can't find user ");
|
2014-09-29 17:54:50 +04:00
|
|
|
result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
|
2016-08-24 18:42:33 +03:00
|
|
|
path_cp, path_encoding);
|
2012-08-24 07:44:56 +04:00
|
|
|
|
2023-06-29 23:31:35 +03:00
|
|
|
free(wpath);
|
2012-08-24 07:44:56 +04:00
|
|
|
|
2014-08-09 15:50:13 +04:00
|
|
|
rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
|
2012-08-24 07:44:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* convert dir */
|
|
|
|
if (!ignore_dir && !NIL_P(dir)) {
|
|
|
|
/* fix string encoding */
|
|
|
|
if (path_cp == INVALID_CODE_PAGE) {
|
|
|
|
dir = fix_string_encoding(dir, path_encoding);
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
/* convert char * to wchar_t */
|
2013-07-26 08:00:28 +04:00
|
|
|
if (!NIL_P(dir)) {
|
2015-03-22 07:54:35 +03:00
|
|
|
const long dir_len = RSTRING_LEN(dir);
|
|
|
|
#if SIZEOF_INT < SIZEOF_LONG
|
|
|
|
if ((long)(int)dir_len != dir_len) {
|
2023-06-29 23:31:35 +03:00
|
|
|
free(wpath);
|
2015-03-22 07:54:35 +03:00
|
|
|
rb_raise(rb_eRangeError, "base directory (%ld bytes) is too long",
|
|
|
|
dir_len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
|
2013-07-26 08:00:28 +04:00
|
|
|
wdir_pos = wdir;
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-03-10 18:39:09 +04:00
|
|
|
if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
|
|
|
|
(wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
|
2016-11-26 14:37:01 +03:00
|
|
|
whome = rb_w32_home_dir();
|
2013-03-10 18:39:09 +04:00
|
|
|
if (whome == NULL) {
|
2014-08-09 14:10:11 +04:00
|
|
|
free(wpath);
|
|
|
|
free(wdir);
|
2013-03-10 18:39:09 +04:00
|
|
|
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
|
|
|
|
}
|
|
|
|
whome_len = wcslen(whome);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2018-03-28 10:01:48 +03:00
|
|
|
if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
|
2014-08-09 14:10:11 +04:00
|
|
|
free(wpath);
|
|
|
|
free(wdir);
|
2013-10-17 09:21:38 +04:00
|
|
|
xfree(whome);
|
2013-03-10 18:39:09 +04:00
|
|
|
rb_raise(rb_eArgError, "non-absolute home");
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-03-10 18:39:09 +04:00
|
|
|
/* exclude ~ from the result */
|
|
|
|
wdir_pos++;
|
|
|
|
wdir_len--;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-03-10 18:39:09 +04:00
|
|
|
/* exclude separator if present */
|
|
|
|
if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
|
|
|
|
wdir_pos++;
|
|
|
|
wdir_len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wdir_len >= 2 && wdir[1] == L':') {
|
2012-08-24 07:44:56 +04:00
|
|
|
dir_drive = wdir[0];
|
|
|
|
if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
|
|
|
|
wdir_len = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
|
|
|
|
/* UNC path */
|
|
|
|
if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
|
|
|
|
/* cut the UNC path tail to '//host/share' */
|
2014-08-09 14:10:11 +04:00
|
|
|
long separators = 0;
|
|
|
|
long pos = 2;
|
2012-08-24 07:44:56 +04:00
|
|
|
while (pos < wdir_len && separators < 2) {
|
|
|
|
if (IS_DIR_SEPARATOR_P(wdir[pos])) {
|
|
|
|
separators++;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (separators == 2)
|
|
|
|
wdir_len = pos - 1;
|
|
|
|
}
|
|
|
|
}
|
2013-03-14 07:56:09 +04:00
|
|
|
else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L'~') {
|
2014-08-09 15:50:13 +04:00
|
|
|
result = rb_str_new_cstr("can't find user ");
|
2014-09-29 17:54:50 +04:00
|
|
|
result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
|
2016-08-24 18:42:33 +03:00
|
|
|
path_cp, path_encoding);
|
2023-06-29 23:31:35 +03:00
|
|
|
free(wpath);
|
|
|
|
free(wdir);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2014-08-09 15:50:13 +04:00
|
|
|
rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
|
2013-03-14 07:56:09 +04:00
|
|
|
}
|
2012-08-24 07:44:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* determine if we ignore dir or not */
|
|
|
|
if (!ignore_dir && path_drive && dir_drive) {
|
2015-02-17 04:53:32 +03:00
|
|
|
if (towupper(path_drive) != towupper(dir_drive)) {
|
2012-08-24 07:44:56 +04:00
|
|
|
/* ignore dir since path drive is different from dir drive */
|
|
|
|
ignore_dir = 1;
|
|
|
|
wdir_len = 0;
|
2015-02-18 19:58:13 +03:00
|
|
|
dir_drive = 0;
|
2012-08-24 07:44:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
|
|
|
|
/* ignore dir since path has UNC root */
|
|
|
|
ignore_dir = 1;
|
|
|
|
wdir_len = 0;
|
|
|
|
}
|
|
|
|
else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
|
2012-09-05 09:29:41 +04:00
|
|
|
!dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
|
2012-08-24 07:44:56 +04:00
|
|
|
/* ignore dir since path has root slash and dir doesn't have drive or UNC root */
|
|
|
|
ignore_dir = 1;
|
|
|
|
wdir_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
|
|
|
|
|
2016-08-25 05:57:41 +03:00
|
|
|
buffer = buffer_pos = ALLOC_N(wchar_t, (buffer_len + 1));
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
/* add home */
|
|
|
|
if (whome_len) {
|
|
|
|
wcsncpy(buffer_pos, whome, whome_len);
|
|
|
|
buffer_pos += whome_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add separator if required */
|
|
|
|
if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
|
|
|
|
buffer_pos[0] = L'\\';
|
|
|
|
buffer_pos++;
|
|
|
|
}
|
2015-02-17 04:53:32 +03:00
|
|
|
else if (!dir_drive && path_drive) {
|
|
|
|
*buffer_pos++ = path_drive;
|
|
|
|
*buffer_pos++ = L':';
|
|
|
|
}
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
if (wdir_len) {
|
2013-03-10 18:39:09 +04:00
|
|
|
wcsncpy(buffer_pos, wdir_pos, wdir_len);
|
2012-08-24 07:44:56 +04:00
|
|
|
buffer_pos += wdir_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add separator if required */
|
|
|
|
if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
|
|
|
|
buffer_pos[0] = L'\\';
|
|
|
|
buffer_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now deal with path */
|
|
|
|
if (wpath_len) {
|
|
|
|
wcsncpy(buffer_pos, wpath_pos, wpath_len);
|
|
|
|
buffer_pos += wpath_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GetFullPathNameW requires at least "." to determine current directory */
|
|
|
|
if (wpath_len == 0) {
|
|
|
|
buffer_pos[0] = L'.';
|
|
|
|
buffer_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure buffer is NULL terminated */
|
|
|
|
buffer_pos[0] = L'\0';
|
|
|
|
|
|
|
|
/* FIXME: Make this more robust */
|
|
|
|
/* Determine require buffer size */
|
|
|
|
size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
|
|
|
|
if (size > PATH_BUFFER_SIZE) {
|
2019-12-20 03:19:39 +03:00
|
|
|
/* allocate more memory than allotted originally by PATH_BUFFER_SIZE */
|
2016-08-25 05:57:41 +03:00
|
|
|
wfullpath = ALLOC_N(wchar_t, size);
|
2012-08-24 07:44:56 +04:00
|
|
|
size = GetFullPathNameW(buffer, size, wfullpath, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wfullpath = wfullpath_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove any trailing slashes */
|
|
|
|
if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
|
|
|
|
wfullpath[size - 2] != L':' &&
|
|
|
|
!(size == 2 && IS_DIR_UNC_P(wfullpath))) {
|
|
|
|
size -= 1;
|
|
|
|
wfullpath[size] = L'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove any trailing dot */
|
|
|
|
if (wfullpath[size - 1] == L'.') {
|
|
|
|
size -= 1;
|
|
|
|
wfullpath[size] = L'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* removes trailing invalid ':$DATA' */
|
|
|
|
size = remove_invalid_alternative_data(wfullpath, size);
|
|
|
|
|
|
|
|
/* Replace the trailing path to long name */
|
2016-04-28 09:54:05 +03:00
|
|
|
if (long_name) {
|
|
|
|
size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
|
|
|
|
size = replace_to_long_name(&wfullpath, size, bufsize);
|
|
|
|
}
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
/* sanitize backslashes with forwardslashes */
|
|
|
|
replace_wchar(wfullpath, L'\\', L'/');
|
|
|
|
|
|
|
|
/* convert to VALUE and set the path encoding */
|
2014-08-09 15:50:13 +04:00
|
|
|
rb_str_set_len(result, 0);
|
2016-08-24 18:42:33 +03:00
|
|
|
result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
|
2012-08-24 07:44:56 +04:00
|
|
|
|
|
|
|
/* TODO: better cleanup */
|
2023-06-29 23:31:35 +03:00
|
|
|
xfree(buffer);
|
|
|
|
free(wpath);
|
|
|
|
free(wdir);
|
|
|
|
xfree(whome);
|
2012-08-24 07:44:56 +04:00
|
|
|
|
2014-12-26 08:51:36 +03:00
|
|
|
if (wfullpath != wfullpath_buffer)
|
2012-08-24 07:44:56 +04:00
|
|
|
xfree(wfullpath);
|
|
|
|
|
2014-08-09 15:50:13 +04:00
|
|
|
rb_enc_associate(result, path_encoding);
|
2012-08-24 07:44:56 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-28 19:51:25 +04:00
|
|
|
int
|
Revert "reuse open(2) from rb_file_load_ok on POSIX-like system"
This reverts commit 35136e1e9c232ad7a03407b992b2e86b6df43f63.
test-spec has been failing since this revision.
.github/workflows/compilers.yml:82
https://github.com/ruby/ruby/actions/runs/4276884159/jobs/7445299562
```
env:
# Minimal flags to pass the check.
default_cc: 'gcc-11 -fcf-protection -Wa,--generate-missing-build-notes=yes'
optflags: '-O2'
LDFLAGS: '-Wl,-z,now'
# FIXME: Drop skipping options
# https://bugs.ruby-lang.org/issues/18061
# https://sourceware.org/annobin/annobin.html/Test-pie.html
TEST_ANNOCHECK_OPTS: "--skip-pie --skip-gaps"
```
Failure:
```
1)
An exception occurred during: Kernel#require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:317
Kernel#require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'
2)
An exception occurred during: Kernel#require ($LOADED_FEATURES) stores an absolute path
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:330
Kernel#require ($LOADED_FEATURES) stores an absolute path ERROR
LeakError: Closed file descriptor: 8
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'
3)
An exception occurred during: Kernel#require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:535
Kernel#require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded ERROR
LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'
4)
An exception occurred during: Kernel#require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:551
Kernel#require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded ERROR
LeakError: Leaked file descriptor: 9 : #<File:../code/load_fixture.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'
5)
An exception occurred during: Kernel#require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:563
Kernel#require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required ERROR
LeakError: Closed file descriptor: 8
Closed file descriptor: 9
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'
6)
An exception occurred during: Kernel.require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:317
Kernel.require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'
7)
An exception occurred during: Kernel.require ($LOADED_FEATURES) stores an absolute path
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:330
Kernel.require ($LOADED_FEATURES) stores an absolute path ERROR
LeakError: Closed file descriptor: 8
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'
8)
An exception occurred during: Kernel.require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:535
Kernel.require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded ERROR
LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'
9)
An exception occurred during: Kernel.require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:551
Kernel.require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded ERROR
LeakError: Leaked file descriptor: 9 : #<File:../code/load_fixture.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'
10)
An exception occurred during: Kernel.require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required
/__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:563
Kernel.require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required ERROR
LeakError: Closed file descriptor: 8
Closed file descriptor: 9
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'
11)
An exception occurred during: Kernel#require_relative with a relative path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:197
Kernel#require_relative with a relative path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:4:in `<top (required)>'
12)
An exception occurred during: Kernel#require_relative with a relative path ($LOADED_FEATURES) stores an absolute path
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:205
Kernel#require_relative with a relative path ($LOADED_FEATURES) stores an absolute path ERROR
LeakError: Closed file descriptor: 8
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:4:in `<top (required)>'
13)
An exception occurred during: Kernel#require_relative with an absolute path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:399
Kernel#require_relative with an absolute path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:277:in `<top (required)>'
14)
An exception occurred during: Kernel#require_relative with an absolute path ($LOAD_FEATURES) stores an absolute path
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:407
Kernel#require_relative with an absolute path ($LOAD_FEATURES) stores an absolute path ERROR
LeakError: Closed file descriptor: 8
/__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:277:in `<top (required)>'
```
2023-02-27 20:24:45 +03:00
|
|
|
rb_file_load_ok(const char *path)
|
2012-02-28 19:51:25 +04:00
|
|
|
{
|
2013-07-26 08:04:23 +04:00
|
|
|
DWORD attr;
|
2012-02-29 08:07:08 +04:00
|
|
|
int ret = 1;
|
2014-08-09 14:10:11 +04:00
|
|
|
long len;
|
2013-07-26 08:04:23 +04:00
|
|
|
wchar_t* wpath;
|
|
|
|
|
2014-08-09 14:10:11 +04:00
|
|
|
wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
|
|
|
|
if (!wpath) return 0;
|
2013-07-26 08:04:23 +04:00
|
|
|
|
|
|
|
attr = GetFileAttributesW(wpath);
|
2012-02-29 08:07:08 +04:00
|
|
|
if (attr == INVALID_FILE_ATTRIBUTES ||
|
2013-07-26 08:04:23 +04:00
|
|
|
(attr & FILE_ATTRIBUTE_DIRECTORY)) {
|
2012-02-28 19:51:25 +04:00
|
|
|
ret = 0;
|
2012-02-29 08:07:08 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-10-16 09:54:38 +03:00
|
|
|
HANDLE h = CreateFileW(wpath, GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
2012-02-28 19:51:25 +04:00
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
2012-02-29 08:07:08 +04:00
|
|
|
CloseHandle(h);
|
2012-02-28 19:51:25 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-02-29 08:07:08 +04:00
|
|
|
ret = 0;
|
2012-02-28 19:51:25 +04:00
|
|
|
}
|
2012-02-29 08:07:08 +04:00
|
|
|
}
|
2014-08-09 14:10:11 +04:00
|
|
|
free(wpath);
|
2012-02-29 08:07:08 +04:00
|
|
|
return ret;
|
2012-02-28 19:51:25 +04:00
|
|
|
}
|
2012-08-24 07:44:56 +04:00
|
|
|
|
2015-06-30 11:28:28 +03:00
|
|
|
int
|
|
|
|
rb_freopen(VALUE fname, const char *mode, FILE *file)
|
|
|
|
{
|
|
|
|
WCHAR *wname, wmode[4];
|
2015-08-02 08:15:02 +03:00
|
|
|
VALUE wtmp;
|
|
|
|
char *name;
|
2015-06-30 12:38:51 +03:00
|
|
|
long len;
|
2015-06-30 11:28:28 +03:00
|
|
|
int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
|
|
|
|
if (n > numberof(wmode)) return EINVAL;
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
|
2015-08-02 08:15:02 +03:00
|
|
|
RSTRING_GETMEM(fname, name, len);
|
|
|
|
n = rb_long2int(len);
|
|
|
|
len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
|
|
|
|
wname = ALLOCV_N(WCHAR, wtmp, len + 1);
|
|
|
|
len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
|
|
|
|
wname[len] = L'\0';
|
2015-06-30 11:28:28 +03:00
|
|
|
RB_GC_GUARD(fname);
|
|
|
|
#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
|
|
|
|
e = _wfreopen(wname, wmode, file) ? 0 : errno;
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
FILE *newfp = 0;
|
|
|
|
e = _wfreopen_s(&newfp, wname, wmode, file);
|
|
|
|
}
|
|
|
|
#endif
|
2015-08-02 08:15:02 +03:00
|
|
|
ALLOCV_END(wtmp);
|
2015-06-30 11:28:28 +03:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2012-08-24 07:44:56 +04:00
|
|
|
void
|
2013-10-19 14:55:39 +04:00
|
|
|
Init_w32_codepage(void)
|
2012-08-24 07:44:56 +04:00
|
|
|
{
|
2013-10-19 14:59:06 +04:00
|
|
|
if (rb_code_page.count) return;
|
|
|
|
rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
|
2012-08-24 07:44:56 +04:00
|
|
|
}
|