Ruby  2.5.0dev(2017-10-22revision60238)
file.c
Go to the documentation of this file.
1 #if defined(__MINGW32__)
2 /* before stdio.h in ruby/define.h */
3 # define MINGW_HAS_SECURE_API 1
4 #endif
5 #include "ruby/ruby.h"
6 #include "ruby/encoding.h"
7 #include "internal.h"
8 #include <winbase.h>
9 #include <wchar.h>
10 #include <shlwapi.h>
11 #include "win32/file.h"
12 
13 #ifndef INVALID_FILE_ATTRIBUTES
14 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
15 #endif
16 
17 /* cache 'encoding name' => 'code page' into a hash */
18 static struct code_page_table {
19  USHORT *table;
20  unsigned int count;
21 } rb_code_page;
22 
23 #define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
24 #define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
25 
26 /* MultiByteToWideChar() doesn't work with code page 51932 */
27 #define INVALID_CODE_PAGE 51932
28 #define PATH_BUFFER_SIZE MAX_PATH * 2
29 
30 #define insecure_obj_p(obj, level) ((level) > 0 && OBJ_TAINTED(obj))
31 
32 /* defined in win32/win32.c */
33 #define system_code_page rb_w32_filecp
34 #define mbstr_to_wstr rb_w32_mbstr_to_wstr
35 #define wstr_to_mbstr rb_w32_wstr_to_mbstr
36 
37 static inline void
38 replace_wchar(wchar_t *s, int find, int replace)
39 {
40  while (*s != 0) {
41  if (*s == find)
42  *s = replace;
43  s++;
44  }
45 }
46 
47 /* Remove trailing invalid ':$DATA' of the path. */
48 static inline size_t
49 remove_invalid_alternative_data(wchar_t *wfullpath, size_t size)
50 {
51  static const wchar_t prime[] = L":$DATA";
52  enum { prime_len = (sizeof(prime) / sizeof(wchar_t)) -1 };
53 
54  if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
55  return size;
56 
57  /* alias of stream */
58  /* get rid of a bug of x64 VC++ */
59  if (wfullpath[size - (prime_len + 1)] == ':') {
60  /* remove trailing '::$DATA' */
61  size -= prime_len + 1; /* prime */
62  wfullpath[size] = L'\0';
63  }
64  else {
65  /* remove trailing ':$DATA' of paths like '/aa:a:$DATA' */
66  wchar_t *pos = wfullpath + size - (prime_len + 1);
67  while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
68  if (*pos == L':') {
69  size -= prime_len; /* alternative */
70  wfullpath[size] = L'\0';
71  break;
72  }
73  pos--;
74  }
75  }
76  return size;
77 }
78 
79 void rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
80 
81 static int
82 code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
83 {
84  const char *n = (const char *)name;
85  if (strncmp("CP", n, 2) == 0) {
86  int code_page = atoi(n + 2);
87  if (code_page != 0) {
88  struct code_page_table *cp = (struct code_page_table *)arg;
89  unsigned int count = cp->count;
90  USHORT *table = cp->table;
91  if (count <= idx) {
92  unsigned int i = count;
93  count = (((idx + 4) & ~31) | 28);
94  table = realloc(table, count * sizeof(*table));
95  if (!table) return ST_CONTINUE;
96  cp->count = count;
97  cp->table = table;
98  while (i < count) table[i++] = INVALID_CODE_PAGE;
99  }
100  table[idx] = (USHORT)code_page;
101  }
102  }
103  return ST_CONTINUE;
104 }
105 
106 /*
107  Return code page number of the encoding.
108  Cache code page into a hash for performance since finding the code page in
109  Encoding#names is slow.
110 */
111 static UINT
112 code_page(rb_encoding *enc)
113 {
114  int enc_idx;
115 
116  if (!enc)
117  return system_code_page();
118 
119  enc_idx = rb_enc_to_index(enc);
120 
121  /* map US-ASCII and ASCII-8bit as code page 1252 (us-ascii) */
122  if (enc_idx == rb_usascii_encindex() || enc_idx == rb_ascii8bit_encindex()) {
123  return 1252;
124  }
125  if (enc_idx == rb_utf8_encindex()) {
126  return CP_UTF8;
127  }
128 
129  if (0 <= enc_idx && (unsigned int)enc_idx < rb_code_page.count)
130  return rb_code_page.table[enc_idx];
131 
132  return INVALID_CODE_PAGE;
133 }
134 
135 #define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
136 
137 /*
138  Replace the last part of the path to long name.
139  We try to avoid to call FindFirstFileW() since it takes long time.
140 */
141 static inline size_t
142 replace_to_long_name(wchar_t **wfullpath, size_t size, size_t buffer_size)
143 {
144  WIN32_FIND_DATAW find_data;
145  HANDLE find_handle;
146 
147  /*
148  Skip long name conversion if the path is already long name.
149  Short name is 8.3 format.
150  http://en.wikipedia.org/wiki/8.3_filename
151  This check can be skipped for directory components that have file
152  extensions longer than 3 characters, or total lengths longer than
153  12 characters.
154  http://msdn.microsoft.com/en-us/library/windows/desktop/aa364980(v=vs.85).aspx
155  */
156  size_t const max_short_name_size = 8 + 1 + 3;
157  size_t const max_extension_size = 3;
158  size_t path_len = 1, extension_len = 0;
159  wchar_t *pos = *wfullpath;
160 
161  if (size == 3 && pos[1] == L':' && pos[2] == L'\\' && pos[3] == L'\0') {
162  /* root path doesn't need short name expansion */
163  return size;
164  }
165 
166  /* skip long name conversion if path contains wildcard characters */
167  if (wcspbrk(pos, L"*?")) {
168  return size;
169  }
170 
171  pos = *wfullpath + size - 1;
172  while (!IS_DIR_SEPARATOR_P(*pos) && pos != *wfullpath) {
173  if (!extension_len && *pos == L'.') {
174  extension_len = path_len - 1;
175  }
176  if (path_len > max_short_name_size || extension_len > max_extension_size) {
177  return size;
178  }
179  path_len++;
180  pos--;
181  }
182 
183  find_handle = FindFirstFileW(*wfullpath, &find_data);
184  if (find_handle != INVALID_HANDLE_VALUE) {
185  size_t trail_pos = pos - *wfullpath + IS_DIR_SEPARATOR_P(*pos);
186  size_t file_len = wcslen(find_data.cFileName);
187  size_t oldsize = size;
188 
189  FindClose(find_handle);
190  size = trail_pos + file_len;
191  if (size > (buffer_size ? buffer_size-1 : oldsize)) {
192  wchar_t *buf = ALLOC_N(wchar_t, (size + 1));
193  wcsncpy(buf, *wfullpath, trail_pos);
194  if (!buffer_size)
195  xfree(*wfullpath);
196  *wfullpath = buf;
197  }
198  wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
199  }
200  return size;
201 }
202 
203 static inline size_t
204 user_length_in_path(const wchar_t *wuser, size_t len)
205 {
206  size_t i;
207 
208  for (i = 0; i < len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
209  ;
210 
211  return i;
212 }
213 
214 static VALUE
215 append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc)
216 {
217  long olen, nlen = (long)len;
218 
219  if (cp != INVALID_CODE_PAGE) {
220  if (len == -1) len = lstrlenW(ws);
221  nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL);
222  olen = RSTRING_LEN(dst);
223  rb_str_modify_expand(dst, nlen);
224  WideCharToMultiByte(cp, 0, ws, len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
225  rb_enc_associate(dst, enc);
226  rb_str_set_len(dst, olen + nlen);
227  }
228  else {
229  const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
230  char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (int)len, &nlen);
231  rb_econv_t *ec = rb_econv_open("UTF-8", rb_enc_name(enc), replaceflags);
232  dst = rb_econv_append(ec, utf8str, nlen, dst, replaceflags);
233  rb_econv_close(ec);
234  free(utf8str);
235  }
236  return dst;
237 }
238 
239 VALUE
241 {
242  WCHAR *dir = rb_w32_home_dir();
243  if (!dir) {
244  rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
245  }
246  append_wstr(result, dir, -1,
248  xfree(dir);
249  return result;
250 }
251 
252 VALUE
253 rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
254 {
255  size_t size = 0, whome_len = 0;
256  size_t buffer_len = 0;
257  long wpath_len = 0, wdir_len = 0;
258  char *fullpath = NULL;
259  wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
260  wchar_t *wdir = NULL, *wdir_pos = NULL;
261  wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
262  UINT path_cp, cp;
263  VALUE path = fname, dir = dname;
264  wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
265  wchar_t path_drive = L'\0', dir_drive = L'\0';
266  int ignore_dir = 0;
267  rb_encoding *path_encoding;
268  int tainted = 0;
269 
270  /* tainted if path is tainted */
271  tainted = OBJ_TAINTED(path);
272 
273  /* get path encoding */
274  if (NIL_P(dir)) {
275  path_encoding = rb_enc_get(path);
276  }
277  else {
278  path_encoding = rb_enc_check(path, dir);
279  }
280 
281  cp = path_cp = code_page(path_encoding);
282 
283  /* workaround invalid codepage */
284  if (path_cp == INVALID_CODE_PAGE) {
285  cp = CP_UTF8;
286  if (!NIL_P(path)) {
287  path = fix_string_encoding(path, path_encoding);
288  }
289  }
290 
291  /* convert char * to wchar_t */
292  if (!NIL_P(path)) {
293  const long path_len = RSTRING_LEN(path);
294 #if SIZEOF_INT < SIZEOF_LONG
295  if ((long)(int)path_len != path_len) {
296  rb_raise(rb_eRangeError, "path (%ld bytes) is too long",
297  path_len);
298  }
299 #endif
300  wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
301  wpath_pos = wpath;
302  }
303 
304  /* determine if we need the user's home directory */
305  /* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
306  if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L'~' &&
307  (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
308  /* tainted if expanding '~' */
309  tainted = 1;
310 
311  whome = rb_w32_home_dir();
312  if (whome == NULL) {
313  free(wpath);
314  rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
315  }
316  whome_len = wcslen(whome);
317 
318  if (PathIsRelativeW(whome) && !(whome_len >= 2 && IS_DIR_UNC_P(whome))) {
319  free(wpath);
320  xfree(whome);
321  rb_raise(rb_eArgError, "non-absolute home");
322  }
323 
324  if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
325  /* use filesystem encoding if expanding home dir */
326  path_encoding = rb_filesystem_encoding();
327  cp = path_cp = system_code_page();
328  }
329 
330  /* ignores dir since we are expanding home */
331  ignore_dir = 1;
332 
333  /* exclude ~ from the result */
334  wpath_pos++;
335  wpath_len--;
336 
337  /* exclude separator if present */
338  if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
339  wpath_pos++;
340  wpath_len--;
341  }
342  }
343  else if (wpath_len >= 2 && wpath_pos[1] == L':') {
344  if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
345  /* ignore dir since path contains a drive letter and a root slash */
346  ignore_dir = 1;
347  }
348  else {
349  /* determine if we ignore dir or not later */
350  path_drive = wpath_pos[0];
351  wpath_pos += 2;
352  wpath_len -= 2;
353  }
354  }
355  else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
356  result = rb_str_new_cstr("can't find user ");
357  result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
358  path_cp, path_encoding);
359 
360  if (wpath)
361  free(wpath);
362 
364  }
365 
366  /* convert dir */
367  if (!ignore_dir && !NIL_P(dir)) {
368  /* fix string encoding */
369  if (path_cp == INVALID_CODE_PAGE) {
370  dir = fix_string_encoding(dir, path_encoding);
371  }
372 
373  /* convert char * to wchar_t */
374  if (!NIL_P(dir)) {
375  const long dir_len = RSTRING_LEN(dir);
376 #if SIZEOF_INT < SIZEOF_LONG
377  if ((long)(int)dir_len != dir_len) {
378  if (wpath) free(wpath);
379  rb_raise(rb_eRangeError, "base directory (%ld bytes) is too long",
380  dir_len);
381  }
382 #endif
383  wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
384  wdir_pos = wdir;
385  }
386 
387  if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
388  (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
389  /* tainted if expanding '~' */
390  tainted = 1;
391 
392  whome = rb_w32_home_dir();
393  if (whome == NULL) {
394  free(wpath);
395  free(wdir);
396  rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
397  }
398  whome_len = wcslen(whome);
399 
400  if (PathIsRelativeW(whome) && !(whome_len >= 2 && IS_DIR_UNC_P(whome))) {
401  free(wpath);
402  free(wdir);
403  xfree(whome);
404  rb_raise(rb_eArgError, "non-absolute home");
405  }
406 
407  /* exclude ~ from the result */
408  wdir_pos++;
409  wdir_len--;
410 
411  /* exclude separator if present */
412  if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
413  wdir_pos++;
414  wdir_len--;
415  }
416  }
417  else if (wdir_len >= 2 && wdir[1] == L':') {
418  dir_drive = wdir[0];
419  if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
420  wdir_len = 2;
421  }
422  }
423  else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
424  /* UNC path */
425  if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
426  /* cut the UNC path tail to '//host/share' */
427  long separators = 0;
428  long pos = 2;
429  while (pos < wdir_len && separators < 2) {
430  if (IS_DIR_SEPARATOR_P(wdir[pos])) {
431  separators++;
432  }
433  pos++;
434  }
435  if (separators == 2)
436  wdir_len = pos - 1;
437  }
438  }
439  else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L'~') {
440  result = rb_str_new_cstr("can't find user ");
441  result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
442  path_cp, path_encoding);
443  if (wpath)
444  free(wpath);
445 
446  if (wdir)
447  free(wdir);
448 
450  }
451  }
452 
453  /* determine if we ignore dir or not */
454  if (!ignore_dir && path_drive && dir_drive) {
455  if (towupper(path_drive) != towupper(dir_drive)) {
456  /* ignore dir since path drive is different from dir drive */
457  ignore_dir = 1;
458  wdir_len = 0;
459  dir_drive = 0;
460  }
461  }
462 
463  if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
464  /* ignore dir since path has UNC root */
465  ignore_dir = 1;
466  wdir_len = 0;
467  }
468  else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
469  !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
470  /* ignore dir since path has root slash and dir doesn't have drive or UNC root */
471  ignore_dir = 1;
472  wdir_len = 0;
473  }
474 
475  buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
476 
477  buffer = buffer_pos = ALLOC_N(wchar_t, (buffer_len + 1));
478 
479  /* add home */
480  if (whome_len) {
481  wcsncpy(buffer_pos, whome, whome_len);
482  buffer_pos += whome_len;
483  }
484 
485  /* Add separator if required */
486  if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
487  buffer_pos[0] = L'\\';
488  buffer_pos++;
489  }
490  else if (!dir_drive && path_drive) {
491  *buffer_pos++ = path_drive;
492  *buffer_pos++ = L':';
493  }
494 
495  if (wdir_len) {
496  /* tainted if dir is used and dir is tainted */
497  if (!tainted && OBJ_TAINTED(dir))
498  tainted = 1;
499 
500  wcsncpy(buffer_pos, wdir_pos, wdir_len);
501  buffer_pos += wdir_len;
502  }
503 
504  /* add separator if required */
505  if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
506  buffer_pos[0] = L'\\';
507  buffer_pos++;
508  }
509 
510  /* now deal with path */
511  if (wpath_len) {
512  wcsncpy(buffer_pos, wpath_pos, wpath_len);
513  buffer_pos += wpath_len;
514  }
515 
516  /* GetFullPathNameW requires at least "." to determine current directory */
517  if (wpath_len == 0) {
518  buffer_pos[0] = L'.';
519  buffer_pos++;
520  }
521 
522  /* Ensure buffer is NULL terminated */
523  buffer_pos[0] = L'\0';
524 
525  /* tainted if path is relative */
526  if (!tainted && PathIsRelativeW(buffer) && !(buffer_len >= 2 && IS_DIR_UNC_P(buffer)))
527  tainted = 1;
528 
529  /* FIXME: Make this more robust */
530  /* Determine require buffer size */
531  size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
532  if (size > PATH_BUFFER_SIZE) {
533  /* allocate more memory than alloted originally by PATH_BUFFER_SIZE */
534  wfullpath = ALLOC_N(wchar_t, size);
535  size = GetFullPathNameW(buffer, size, wfullpath, NULL);
536  }
537  else {
538  wfullpath = wfullpath_buffer;
539  }
540 
541  /* Remove any trailing slashes */
542  if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
543  wfullpath[size - 2] != L':' &&
544  !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
545  size -= 1;
546  wfullpath[size] = L'\0';
547  }
548 
549  /* Remove any trailing dot */
550  if (wfullpath[size - 1] == L'.') {
551  size -= 1;
552  wfullpath[size] = L'\0';
553  }
554 
555  /* removes trailing invalid ':$DATA' */
556  size = remove_invalid_alternative_data(wfullpath, size);
557 
558  /* Replace the trailing path to long name */
559  if (long_name) {
560  size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
561  size = replace_to_long_name(&wfullpath, size, bufsize);
562  }
563 
564  /* sanitize backslashes with forwardslashes */
565  replace_wchar(wfullpath, L'\\', L'/');
566 
567  /* convert to VALUE and set the path encoding */
568  rb_str_set_len(result, 0);
569  result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
570 
571  /* makes the result object tainted if expanding tainted strings or returning modified path */
572  if (tainted)
573  OBJ_TAINT(result);
574 
575  /* TODO: better cleanup */
576  if (buffer)
577  xfree(buffer);
578 
579  if (wpath)
580  free(wpath);
581 
582  if (wdir)
583  free(wdir);
584 
585  if (whome)
586  xfree(whome);
587 
588  if (wfullpath != wfullpath_buffer)
589  xfree(wfullpath);
590 
591  if (fullpath)
592  xfree(fullpath);
593 
594  rb_enc_associate(result, path_encoding);
595  return result;
596 }
597 
598 VALUE
599 rb_readlink(VALUE path, rb_encoding *resultenc)
600 {
601  DWORD len;
602  VALUE wtmp = 0, wpathbuf, str;
603  rb_w32_reparse_buffer_t rbuf, *rp = &rbuf;
604  WCHAR *wpath, *wbuf;
605  rb_encoding *enc;
606  UINT cp, path_cp;
607  int e;
608 
609  FilePathValue(path);
610  enc = rb_enc_get(path);
611  cp = path_cp = code_page(enc);
612  if (cp == INVALID_CODE_PAGE) {
613  path = fix_string_encoding(path, enc);
614  cp = CP_UTF8;
615  }
616  len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), NULL, 0);
617  wpath = ALLOCV_N(WCHAR, wpathbuf, len+1);
618  MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), wpath, len);
619  wpath[len] = L'\0';
620  e = rb_w32_read_reparse_point(wpath, rp, sizeof(rbuf), &wbuf, &len);
621  if (e == ERROR_MORE_DATA) {
622  size_t size = rb_w32_reparse_buffer_size(len + 1);
623  rp = ALLOCV(wtmp, size);
624  e = rb_w32_read_reparse_point(wpath, rp, size, &wbuf, &len);
625  }
626  ALLOCV_END(wpathbuf);
627  if (e) {
628  ALLOCV_END(wtmp);
629  if (e != -1)
631  else /* not symlink; maybe volume mount point */
632  rb_syserr_fail_path(EINVAL, path);
633  }
634  enc = resultenc;
635  path_cp = code_page(enc);
636  len = lstrlenW(wbuf);
637  str = append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, path_cp, enc);
638  ALLOCV_END(wtmp);
639  return str;
640 }
641 
642 int
643 rb_file_load_ok(const char *path)
644 {
645  DWORD attr;
646  int ret = 1;
647  long len;
648  wchar_t* wpath;
649 
650  wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
651  if (!wpath) return 0;
652 
653  attr = GetFileAttributesW(wpath);
654  if (attr == INVALID_FILE_ATTRIBUTES ||
655  (attr & FILE_ATTRIBUTE_DIRECTORY)) {
656  ret = 0;
657  }
658  else {
659  HANDLE h = CreateFileW(wpath, GENERIC_READ,
660  FILE_SHARE_READ | FILE_SHARE_WRITE,
661  NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
662  if (h != INVALID_HANDLE_VALUE) {
663  CloseHandle(h);
664  }
665  else {
666  ret = 0;
667  }
668  }
669  free(wpath);
670  return ret;
671 }
672 
673 int
674 rb_freopen(VALUE fname, const char *mode, FILE *file)
675 {
676  WCHAR *wname, wmode[4];
677  VALUE wtmp;
678  char *name;
679  long len;
680  int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
681  if (n > numberof(wmode)) return EINVAL;
682  MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
683  RSTRING_GETMEM(fname, name, len);
684  n = rb_long2int(len);
685  len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
686  wname = ALLOCV_N(WCHAR, wtmp, len + 1);
687  len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
688  wname[len] = L'\0';
689  RB_GC_GUARD(fname);
690 #if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
691  e = _wfreopen(wname, wmode, file) ? 0 : errno;
692 #else
693  {
694  FILE *newfp = 0;
695  e = _wfreopen_s(&newfp, wname, wmode, file);
696  }
697 #endif
698  ALLOCV_END(wtmp);
699  return e;
700 }
701 
702 void
704 {
705  if (rb_code_page.count) return;
706  rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
707 }
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Definition: encoding.c:879
rb_econv_t * rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags)
Definition: transcode.c:1063
int count
Definition: encoding.c:56
#define FilePathValue(v)
Definition: ruby.h:594
#define wstr_to_mbstr
Definition: file.c:35
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2284
Definition: st.h:99
int rb_w32_read_reparse_point(const WCHAR *path, rb_w32_reparse_buffer_t *rp, size_t bufsize, WCHAR **result, DWORD *len)
Definition: win32.c:5006
void rb_econv_close(rb_econv_t *ec)
Definition: transcode.c:1698
#define rb_long2int(n)
Definition: ruby.h:319
int rb_usascii_encindex(void)
Definition: encoding.c:1344
void rb_str_set_len(VALUE, long)
Definition: string.c:2627
#define PATH_BUFFER_SIZE
Definition: file.c:28
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Definition: ruby.h:984
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:854
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:848
#define mbstr_to_wstr
Definition: file.c:34
#define RB_GC_GUARD(v)
Definition: ruby.h:552
VALUE rb_readlink(VALUE path, rb_encoding *resultenc)
Definition: file.c:599
#define IS_DIR_SEPARATOR_P(c)
Definition: file.c:23
void Init_w32_codepage(void)
Definition: file.c:703
int rb_w32_map_errno(DWORD)
Definition: win32.c:273
VALUE rb_eArgError
Definition: error.c:802
WCHAR * rb_w32_home_dir(void)
Definition: win32.c:540
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
int rb_enc_to_index(rb_encoding *enc)
Definition: encoding.c:126
VALUE rb_eRangeError
Definition: error.c:805
#define ALLOC_N(type, n)
Definition: ruby.h:1587
IUnknown DWORD
Definition: win32ole.c:32
#define ECONV_INVALID_REPLACE
Definition: encoding.h:388
#define system_code_page
Definition: file.c:33
int rb_ascii8bit_encindex(void)
Definition: encoding.c:1314
#define NIL_P(v)
Definition: ruby.h:451
void rb_enc_foreach_name(int(*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
Definition: encoding.c:1964
#define ALLOCV_N(type, v, n)
Definition: ruby.h:1657
#define realloc
Definition: ripper.c:359
#define ALLOCV_END(v)
Definition: ruby.h:1658
#define numberof(array)
Definition: etc.c:618
#define RSTRING_LEN(str)
Definition: ruby.h:971
int errno
#define fix_string_encoding(str, encoding)
Definition: file.c:135
#define rb_enc_name(enc)
Definition: encoding.h:171
void rb_str_modify_expand(VALUE, long)
Definition: string.c:2054
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:615
unsigned long VALUE
Definition: ruby.h:85
#define OBJ_TAINTED(x)
Definition: ruby.h:1296
int rb_utf8_encindex(void)
Definition: encoding.c:1329
VALUE rb_str_new_cstr(const char *)
Definition: string.c:771
#define rb_w32_reparse_buffer_size(n)
Definition: file.h:33
register unsigned int len
Definition: zonetab.h:51
#define RSTRING_PTR(str)
Definition: ruby.h:975
#define ECONV_UNDEF_REPLACE
Definition: encoding.h:390
UINT rb_w32_filecp(void)
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:860
int size
Definition: encoding.c:57
int rb_freopen(VALUE fname, const char *mode, FILE *file)
Definition: file.c:674
#define ALLOCV(v, n)
Definition: ruby.h:1656
VALUE rb_default_home_dir(VALUE result)
Definition: file.c:3327
rb_encoding * rb_filesystem_encoding(void)
Definition: encoding.c:1385
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:759
const char * name
Definition: nkf.c:208
int rb_file_load_ok(const char *path)
Definition: file.c:5852
#define INVALID_CODE_PAGE
Definition: file.c:27
#define rb_syserr_fail_path(err, path)
Definition: internal.h:1217
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags)
Definition: transcode.c:1809
void void xfree(void *)
int rb_enc_str_asciionly_p(VALUE)
Definition: string.c:641
#define NULL
Definition: _sdbm.c:102
#define INVALID_FILE_ATTRIBUTES
Definition: file.c:14
#define OBJ_TAINT(x)
Definition: ruby.h:1298
free(psz)
#define IS_DIR_UNC_P(c)
Definition: file.c:24
VALUE rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
Definition: file.c:3397