Project

General

Profile

Bug #6836 ยป improve-file-expand_path-windows.v1.diff

luislavena (Luis Lavena), 08/05/2012 06:55 AM

View differences:

configure.in
1185 1185
		AC_CHECK_FUNCS(cygwin_conv_path)
1186 1186
		AC_LIBOBJ([langinfo])
1187 1187
		],
1188
[mingw*], [	LIBS="-lshell32 -lws2_32 -limagehlp $LIBS"
1188
[mingw*], [	LIBS="-lshell32 -lws2_32 -limagehlp -lshlwapi $LIBS"
1189 1189
		ac_cv_header_a_out_h=no
1190 1190
		ac_cv_header_pwd_h=no
1191 1191
		ac_cv_header_utime_h=no
file.c
2882 2882
    return buf + dirlen;
2883 2883
}
2884 2884

  
2885
static VALUE
2886
file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
2885
#ifndef _WIN32
2886
VALUE
2887
rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, VALUE result)
2887 2888
{
2888 2889
    const char *s, *b, *fend;
2889 2890
    char *buf, *p, *pend, *root;
......
2945 2946
	    /* specified drive, but not full path */
2946 2947
	    int same = 0;
2947 2948
	    if (!NIL_P(dname) && !not_same_drive(dname, s[0])) {
2948
		file_expand_path(dname, Qnil, abs_mode, result);
2949
		rb_file_expand_path_internal(dname, Qnil, abs_mode, result);
2949 2950
		BUFINIT();
2950 2951
		if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) {
2951 2952
		    /* ok, same drive */
......
2969 2970
#endif
2970 2971
    else if (!rb_is_absolute_path(s)) {
2971 2972
	if (!NIL_P(dname)) {
2972
	    file_expand_path(dname, Qnil, abs_mode, result);
2973
	    rb_file_expand_path_internal(dname, Qnil, abs_mode, result);
2973 2974
	    rb_enc_associate(result, rb_enc_check(result, fname));
2974 2975
	    BUFINIT();
2975 2976
	    p = pend;
......
3222 3223
    ENC_CODERANGE_CLEAR(result);
3223 3224
    return result;
3224 3225
}
3226
#endif
3225 3227

  
3226 3228
#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2)
3227 3229

  
......
3232 3234
static VALUE
3233 3235
file_expand_path_1(VALUE fname)
3234 3236
{
3235
    return file_expand_path(fname, Qnil, 0, EXPAND_PATH_BUFFER());
3237
    return rb_file_expand_path_internal(fname, Qnil, 0, EXPAND_PATH_BUFFER());
3236 3238
}
3237 3239

  
3238 3240
VALUE
3239 3241
rb_file_expand_path(VALUE fname, VALUE dname)
3240 3242
{
3241 3243
    check_expand_path_args(fname, dname);
3242
    return file_expand_path(fname, dname, 0, EXPAND_PATH_BUFFER());
3244
    return rb_file_expand_path_internal(fname, dname, 0, EXPAND_PATH_BUFFER());
3243 3245
}
3244 3246

  
3245 3247
/*
......
3276 3278
rb_file_absolute_path(VALUE fname, VALUE dname)
3277 3279
{
3278 3280
    check_expand_path_args(fname, dname);
3279
    return file_expand_path(fname, dname, 1, EXPAND_PATH_BUFFER());
3281
    return rb_file_expand_path_internal(fname, dname, 1, EXPAND_PATH_BUFFER());
3280 3282
}
3281 3283

  
3282 3284
/*
......
5250 5252

  
5251 5253
	    RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
5252 5254
	    if (RSTRING_LEN(str) == 0) continue;
5253
	    file_expand_path(fname, str, 0, tmp);
5255
	    rb_file_expand_path_internal(fname, str, 0, tmp);
5254 5256
	    if (rb_file_load_ok(RSTRING_PTR(tmp))) {
5255 5257
		*filep = copy_path_class(tmp, *filep);
5256 5258
		return (int)(j+1);
......
5309 5311
	    VALUE str = RARRAY_PTR(load_path)[i];
5310 5312
	    RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
5311 5313
	    if (RSTRING_LEN(str) > 0) {
5312
		file_expand_path(path, str, 0, tmp);
5314
		rb_file_expand_path_internal(path, str, 0, tmp);
5313 5315
		f = RSTRING_PTR(tmp);
5314 5316
		if (rb_file_load_ok(f)) goto found;
5315 5317
	    }
......
5544 5546
    rb_define_method(rb_cStat, "setuid?",  rb_stat_suid, 0);
5545 5547
    rb_define_method(rb_cStat, "setgid?",  rb_stat_sgid, 0);
5546 5548
    rb_define_method(rb_cStat, "sticky?",  rb_stat_sticky, 0);
5549

  
5550
#ifdef _WIN32
5551
    rb_w32_init_file();
5552
#endif
5547 5553
}
include/ruby/win32.h
299 299
extern int rb_w32_uaccess(const char *, int);
300 300
extern char rb_w32_fd_is_text(int);
301 301
extern int rb_w32_fstati64(int, struct stati64 *);
302
extern void rb_w32_init_file(void);
302 303

  
303 304
#ifdef __BORLANDC__
304 305
extern off_t _lseeki64(int, off_t, int);
internal.h
102 102
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict);
103 103
void rb_file_const(const char*, VALUE);
104 104
int rb_file_load_ok(const char *);
105
VALUE rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, VALUE result);
105 106
void Init_File(void);
106 107

  
107 108
/* gc.c */
lib/webrick/httpservlet/filehandler.rb
327 327
        end
328 328
      end
329 329

  
330
      if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
331
        require 'dl/import'
332
        require 'dl/types'
333

  
334
        module Kernel32
335
          extend DL::Importer
336
          dlload "kernel32"
337
          include DL::Win32Types
338
          extern "DWORD GetLongPathName(LPCSTR, LPSTR, DWORD)"
339

  
340
          def self.long_path_name(name)
341
            if (len = GetLongPathName(name, nil, 0)).nonzero?
342
              buf = "\0" * len
343
              buf[0...GetLongPathName(name, buf, buf.size)]
344
            else
345
              name
346
            end
347
          end
348
        end
349

  
350
        def long_path_name(name)
351
          Kernel32.long_path_name(name)
352
        end
353
      end
354

  
330 355
      def shift_path_info(req, res, path_info, base=nil)
331 356
        tmp = path_info.shift
332 357
        base = base || tmp
333 358
        req.path_info = path_info.join
334 359
        req.script_name << base
335 360
        res.filename = File.expand_path(res.filename + base)
361
        res.filename = long_path_name(res.filename) if defined?(long_path_name)
336 362
        check_filename(req, res, File.basename(res.filename))
337 363
      end
338 364

  
test/ruby/test_file_exhaustive.rb
14 14

  
15 15
  def setup
16 16
    @dir = Dir.mktmpdir("rubytest-file")
17
    @rootdir = "#{DRIVE}/"
17 18
    File.chown(-1, Process.gid, @dir)
18 19
    @file = make_tmp_filename("file")
19 20
    @zerofile = make_tmp_filename("zerofile")
......
425 426
      assert_equal(expected.force_encoding(cp), File.expand_path(a.dup.force_encoding(cp)), cp)
426 427
    end
427 428

  
429
    path = "\u3042\u3044\u3046\u3048\u304a".encode("EUC-JP")
430
    assert_equal("#{Dir.pwd}/#{path}".encode("CP932"), File.expand_path(path).encode("CP932"))
431

  
432
    path = "\u3042\u3044\u3046\u3048\u304a".encode("CP51932")
433
    assert_equal("#{Dir.pwd}/#{path}", File.expand_path(path))
434

  
428 435
    assert_kind_of(String, File.expand_path("~")) if ENV["HOME"]
429 436
    assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha") }
430 437
    assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha", "/") }
431 438
    begin
432 439
      bug3630 = '[ruby-core:31537]'
433 440
      home = ENV["HOME"]
441
      home_drive = ENV["HOMEDRIVE"]
442
      home_path = ENV["HOMEPATH"]
443
      user_profile = ENV["USERPROFILE"]
434 444
      ENV["HOME"] = nil
445
      ENV["HOMEDRIVE"] = nil
446
      ENV["HOMEPATH"] = nil
447
      ENV["USERPROFILE"] = nil
435 448
      assert_raise(ArgumentError) { File.expand_path("~") }
436 449
      ENV["HOME"] = "~"
437 450
      assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
......
439 452
      assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
440 453
    ensure
441 454
      ENV["HOME"] = home
455
      ENV["HOMEDRIVE"] = home_drive
456
      ENV["HOMEPATH"] = home_path
457
      ENV["USERPROFILE"] = user_profile
442 458
    end
443 459
    assert_incompatible_encoding {|d| File.expand_path(d)}
444 460
  end
445 461

  
462
  def test_resolve_empty_string_current_directory
463
    assert_equal(Dir.pwd, File.expand_path(""))
464
  end
465

  
466
  def test_resolve_dot_current_directory
467
    assert_equal(Dir.pwd, File.expand_path("."))
468
  end
469

  
470
  def test_resolve_file_name_relative_current_directory
471
    assert_equal(File.join(Dir.pwd, "foo"), File.expand_path("foo"))
472
  end
473

  
474
  def test_ignore_nil_dir_string
475
    assert_equal(File.join(Dir.pwd, "foo"), File.expand_path("foo", nil))
476
  end
477

  
478
  def test_resolve_file_name_and_dir_string_relative
479
    assert_equal(File.join(Dir.pwd, "bar", "foo"),
480
      File.expand_path("foo", "bar"))
481
  end
482

  
483
  def test_cleanup_dots_file_name
484
    bug = "[ruby-talk:18512]"
485

  
486
    assert_equal(File.join(Dir.pwd, ".a"), File.expand_path(".a"), bug)
487
    assert_equal(File.join(Dir.pwd, "..a"), File.expand_path("..a"), bug)
488

  
489
    if DRIVE
490
      # cleanup dots only on Windows
491
      assert_equal(File.join(Dir.pwd, "a"), File.expand_path("a."), bug)
492
      skip "FIXME"
493
      assert_equal(File.join(Dir.pwd, "a"), File.expand_path("a.."), bug)
494
    else
495
      assert_equal(File.join(Dir.pwd, "a."), File.expand_path("a."), bug)
496
      assert_equal(File.join(Dir.pwd, "a.."), File.expand_path("a.."), bug)
497
    end
498
  end
499

  
500
  def test_converts_a_pathname_to_an_absolute_pathname_using_a_complete_path
501
    assert_equal(@dir, File.expand_path("", "#{@dir}"))
502
    assert_equal(File.join(@dir, "a"), File.expand_path("a", "#{@dir}"))
503
    assert_equal(File.join(@dir, "a"), File.expand_path("../a", "#{@dir}/xxx"))
504
    assert_equal(@rootdir, File.expand_path(".", "#{@rootdir}"))
505
  end
506

  
507
  def test_ignores_supplied_dir_if_path_contains_a_drive_letter
508
    assert_equal(@rootdir, File.expand_path(@rootdir, "D:/"))
509
  end if DRIVE
510

  
511
  def test_removes_trailing_slashes_from_absolute_path
512
    assert_equal(File.join(@rootdir, "foo"), File.expand_path("#{@rootdir}foo/"))
513
    assert_equal(File.join(@rootdir, "foo.rb"), File.expand_path("#{@rootdir}foo.rb/"))
514
  end
515

  
516
  def test_removes_trailing_spaces_from_absolute_path
517
    assert_equal(File.join(@rootdir, "a"), File.expand_path("#{@rootdir}a "))
518
  end if DRIVE
519

  
520
  def test_converts_a_pathname_which_starts_with_a_slash_using_dir_s_drive
521
    assert_match(%r"\Az:/foo\z"i, File.expand_path('/foo', "z:/bar"))
522
  end if DRIVE
523

  
524
  def test_converts_a_pathname_which_starts_with_a_slash_and_unc_pathname
525
    assert_equal("//foo", File.expand_path('//foo', "//bar"))
526
    assert_equal("//bar/foo", File.expand_path('/foo', "//bar"))
527
    assert_equal("//foo", File.expand_path('//foo', "/bar"))
528
  end if DRIVE
529

  
530
  def test_converts_a_dot_with_unc_dir
531
    assert_equal("//", File.expand_path('.', "//"))
532
  end
533

  
534
  def test_preserves_unc_path_root
535
    assert_equal("//", File.expand_path("//"))
536
    assert_equal("//", File.expand_path("//."))
537
    assert_equal("//", File.expand_path("//.."))
538
  end
539

  
540
  def test_converts_a_pathname_which_starts_with_a_slash_using_host_share
541
    assert_match(%r"\A//host/share/foo\z"i, File.expand_path('/foo', "//host/share/bar"))
542
  end if DRIVE
543

  
544
  def test_converts_a_pathname_which_starts_with_a_slash_using_a_current_drive
545
    assert_match(%r"\A#{DRIVE}/foo\z"i, File.expand_path('/foo'))
546
  end
547

  
548
  def test_returns_tainted_strings_or_not
549
    assert_equal(true, File.expand_path('foo').tainted?)
550
    assert_equal(true, File.expand_path('foo'.taint).tainted?)
551
    assert_equal(true, File.expand_path('/foo'.taint).tainted?)
552
    assert_equal(true, File.expand_path('foo', 'bar').tainted?)
553
    assert_equal(true, File.expand_path('foo', '/bar'.taint).tainted?)
554
    assert_equal(true, File.expand_path('foo'.taint, '/bar').tainted?)
555
    assert_equal(true, File.expand_path('~').tainted?)
556

  
557
    if DRIVE
558
      assert_equal(true, File.expand_path('/foo').tainted?)
559
      assert_equal(false, File.expand_path('//foo').tainted?)
560
      assert_equal(true, File.expand_path('C:/foo'.taint).tainted?)
561
      assert_equal(false, File.expand_path('C:/foo').tainted?)
562
      assert_equal(true, File.expand_path('foo', '/bar').tainted?)
563
      assert_equal(true, File.expand_path('foo', 'C:/bar'.taint).tainted?)
564
      assert_equal(true, File.expand_path('foo'.taint, 'C:/bar').tainted?)
565
      assert_equal(false, File.expand_path('foo', 'C:/bar').tainted?)
566
      assert_equal(false, File.expand_path('C:/foo/../bar').tainted?)
567
      assert_equal(false, File.expand_path('foo', '//bar').tainted?)
568
    else
569
      assert_equal(false, File.expand_path('/foo').tainted?)
570
      assert_equal(false, File.expand_path('foo', '/bar').tainted?)
571
    end
572
  end
573

  
574
  def test_converts_a_pathname_to_an_absolute_pathname_using_home_as_base
575
    old_home = ENV["HOME"]
576
    home = ENV["HOME"] = "#{DRIVE}/UserHome"
577
    assert_equal(home, File.expand_path("~"))
578
    assert_equal(home, File.expand_path("~", "C:/FooBar"))
579
    assert_equal(File.join(home, "a"), File.expand_path("~/a", "C:/FooBar"))
580
  ensure
581
    ENV["HOME"] = old_home
582
  end
583

  
584
  def test_converts_a_pathname_to_an_absolute_pathname_using_unc_home
585
    old_home = ENV["HOME"]
586
    unc_home = ENV["HOME"] = "//UserHome"
587
    assert_equal(unc_home, File.expand_path("~"))
588
  ensure
589
    ENV["HOME"] = old_home
590
  end if DRIVE
591

  
592
  def test_does_not_modify_a_home_string_argument
593
    old_home = ENV["HOME"]
594
    home = ENV["HOME"] = "#{DRIVE}/UserHome"
595
    str = "~/a"
596
    assert_equal("#{home}/a", File.expand_path(str))
597
    assert_equal("~/a", str)
598
  ensure
599
    ENV["HOME"] = old_home
600
  end
601

  
602
  def test_raises_argument_error_for_any_supplied_username
603
    bug = '[ruby-core:39597]'
604
    assert_raise(ArgumentError, bug) { File.expand_path("~anything") }
605
  end if DRIVE
606

  
607
  def test_raises_a_type_error_if_not_passed_a_string_type
608
    assert_raise(TypeError) { File.expand_path(1) }
609
    assert_raise(TypeError) { File.expand_path(nil) }
610
    assert_raise(TypeError) { File.expand_path(true) }
611
  end
612

  
613
  def test_expands_dot_dir
614
    assert_equal("#{DRIVE}/dir", File.expand_path("#{DRIVE}/./dir"))
615
  end
616

  
617
  def test_does_not_modify_the_string_argument
618
    str = "./a/b/../c"
619
    assert_equal("#{Dir.pwd}/a/c", File.expand_path(str, Dir.pwd))
620
    assert_equal("./a/b/../c", str)
621
  end
622

  
623
  def test_returns_a_string_when_passed_a_string_subclass
624
    sub = Class.new(String)
625
    str = sub.new "./a/b/../c"
626
    path = File.expand_path(str, Dir.pwd)
627
    assert_equal("#{Dir.pwd}/a/c", path)
628
    assert_instance_of(String, path)
629
  end
630

  
631
  def test_accepts_objects_that_have_a_to_path_method
632
    klass = Class.new { def to_path; "a/b/c"; end }
633
    obj = klass.new
634
    assert_equal("#{Dir.pwd}/a/b/c", File.expand_path(obj))
635
  end
636

  
446 637
  def test_basename
447 638
    assert_equal(File.basename(@file).sub(/\.test$/, ""), File.basename(@file, ".test"))
448 639
    assert_equal("", s = File.basename(""))
win32/Makefile.sub
226 226
EXTSOLIBS =
227 227
!endif
228 228
!if !defined(LIBS)
229
LIBS = oldnames.lib user32.lib advapi32.lib shell32.lib ws2_32.lib imagehlp.lib $(EXTLIBS)
229
LIBS = oldnames.lib user32.lib advapi32.lib shell32.lib ws2_32.lib imagehlp.lib shlwapi.lib $(EXTLIBS)
230 230
!endif
231 231
!if !defined(MISSING)
232 232
MISSING = acosh.obj cbrt.obj crypt.obj erf.obj ffs.obj langinfo.obj lgamma_r.obj strlcat.obj strlcpy.obj tgamma.obj win32/win32.obj win32/file.obj setproctitle.obj
win32/file.c
1 1
#include "ruby/ruby.h"
2
#include "ruby/encoding.h"
2 3
#include <winbase.h>
4
#include <wchar.h>
5
#include <shlwapi.h>
6

  
7
/* MultiByteToWideChar() doesn't work with code page 51932 */
8
#define INVALID_CODE_PAGE 51932
9

  
10
#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
11
#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
12

  
13
#define malloc xmalloc
14
#define free xfree
3 15

  
4 16
#ifndef INVALID_FILE_ATTRIBUTES
5 17
# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
......
27 39
    }
28 40
    return ret;
29 41
}
42

  
43

  
44
static inline void
45
replace_wchar(wchar_t *s, int find, int replace)
46
{
47
    while (*s != 0) {
48
	if (*s == find)
49
	    *s = replace;
50
	s++;
51
    }
52
}
53

  
54
/*
55
 * Return user's home directory using environment variables combinations.
56
 * Memory allocated by this function should be manually freeded afterwards.
57
 */
58
static wchar_t *
59
home_dir(void)
60
{
61
    wchar_t *buffer = NULL;
62
    size_t buffer_len = 0, len = 0;
63
    size_t home_env = 0;
64

  
65
    /*
66
     * determine User's home directory trying:
67
     * HOME, HOMEDRIVE + HOMEPATH and USERPROFILE environment variables
68
     * TODO: Special Folders - Profile and Personal
69
     */
70

  
71
    /*
72
     * GetEnvironmentVariableW when used with NULL will return the required
73
     * buffer size and its terminating character.
74
     * http://msdn.microsoft.com/en-us/library/windows/desktop/ms683188(v=vs.85).aspx
75
     */
76

  
77
    if (len = GetEnvironmentVariableW(L"HOME", NULL, 0)) {
78
	buffer_len = len;
79
	home_env = 1;
80
    }
81
    else if (len = GetEnvironmentVariableW(L"HOMEDRIVE", NULL, 0)) {
82
	buffer_len = len;
83
	if (len = GetEnvironmentVariableW(L"HOMEPATH", NULL, 0)) {
84
	    buffer_len += len;
85
	    home_env = 2;
86
	}
87
	else {
88
	    buffer_len = 0;
89
	}
90
    }
91
    else if (len = GetEnvironmentVariableW(L"USERPROFILE", NULL, 0)) {
92
	buffer_len = len;
93
	home_env = 3;
94
    }
95

  
96
    /* allocate buffer */
97
    if (home_env)
98
	buffer = (wchar_t *)malloc(buffer_len * sizeof(wchar_t));
99

  
100
    switch (home_env) {
101
	case 1: /* HOME */
102
	    GetEnvironmentVariableW(L"HOME", buffer, buffer_len);
103
	    break;
104
	case 2: /* HOMEDRIVE + HOMEPATH */
105
	    len = GetEnvironmentVariableW(L"HOMEDRIVE", buffer, buffer_len);
106
	    GetEnvironmentVariableW(L"HOMEPATH", buffer + len, buffer_len - len);
107
	    break;
108
	case 3: /* USERPROFILE */
109
	    GetEnvironmentVariableW(L"USERPROFILE", buffer, buffer_len);
110
	    break;
111
	default:
112
	    break;
113
    }
114

  
115
    if (home_env) {
116
	/* sanitize backslashes with forwardslashes */
117
	replace_wchar(buffer, L'\\', L'/');
118
	return buffer;
119
    }
120

  
121
    return NULL;
122
}
123

  
124

  
125
/* Convert the path from char to wchar with specified code page */
126
static inline void
127
path_to_wchar(VALUE path, wchar_t **wpath, wchar_t **wpath_pos, size_t *wpath_len, UINT cp)
128
{
129
    size_t size;
130

  
131
    if (NIL_P(path))
132
	return;
133

  
134
    size = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), -1, NULL, 0);
135
    *wpath = (wchar_t *)malloc(size * sizeof(wchar_t));
136
    if (wpath_pos)
137
	*wpath_pos = *wpath;
138

  
139
    MultiByteToWideChar(cp, 0, RSTRING_PTR(path), -1, *wpath, size);
140
    *wpath_len = size - 1;
141
}
142

  
143
/* Remove trailing invalid ':$DATA' of the path. */
144
static inline size_t
145
remove_invalid_alternative_data(wchar_t *wfullpath, size_t size) {
146
    static const wchar_t prime[] = L":$DATA";
147
    enum {prime_len = (sizeof(prime) / sizeof(wchar_t)) -1};
148

  
149
    if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
150
	return size;
151

  
152
    /* alias of stream */
153
    /* get rid of a bug of x64 VC++ */
154
    if (wfullpath[size - (prime_len + 1)] == ':') {
155
	/* remove trailing '::$DATA' */
156
	size -= prime_len + 1; /* prime */
157
	wfullpath[size] = L'\0';
158
    }
159
    else {
160
	/* remove trailing ':$DATA' of paths like '/aa:a:$DATA' */
161
	wchar_t *pos = wfullpath + size - (prime_len + 1);
162
	while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
163
	    if (*pos == L':') {
164
		size -= prime_len; /* alternative */
165
		wfullpath[size] = L'\0';
166
		break;
167
	    }
168
	    pos--;
169
	}
170
    }
171
    return size;
172
}
173

  
174
/* Return system code page. */
175
static inline UINT
176
system_code_page(void) {
177
    return AreFileApisANSI() ? CP_ACP : CP_OEMCP;
178
}
179

  
180
/* cache 'encoding name' => 'code page' into a hash */
181
static VALUE rb_code_page;
182

  
183

  
184
/*
185
 * Return code page number of the encoding.
186
 * Cache code page into a hash for performance since finding the code page in
187
 * Encoding#names is slow.
188
 */
189
static UINT
190
code_page(rb_encoding *enc)
191
{
192
    VALUE code_page_value, name_key;
193
    VALUE encoding, names_ary = Qundef, name;
194
    char *enc_name;
195
    struct RString fake_str;
196
    ID names;
197
    long i;
198

  
199
    if (!enc)
200
	return system_code_page();
201

  
202
    enc_name = (char *)rb_enc_name(enc);
203

  
204
    fake_str.basic.flags = T_STRING|RSTRING_NOEMBED;
205
    fake_str.basic.klass = rb_cString;
206
    fake_str.as.heap.len = strlen(enc_name);
207
    fake_str.as.heap.ptr = enc_name;
208
    fake_str.as.heap.aux.capa = fake_str.as.heap.len;
209
    name_key = (VALUE)&fake_str;
210
    ENCODING_CODERANGE_SET(name_key, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
211

  
212
    code_page_value = rb_hash_lookup(rb_code_page, name_key);
213
    if (code_page_value != Qnil) {
214
	return (UINT)FIX2INT(code_page_value);
215
    }
216

  
217
    name_key = rb_usascii_str_new2(enc_name);
218

  
219
    encoding = rb_enc_from_encoding(enc);
220
    if (!NIL_P(encoding)) {
221
	CONST_ID(names, "names");
222
	names_ary = rb_funcall(encoding, names, 0);
223
    }
224

  
225
    if (enc == rb_usascii_encoding() || enc == rb_ascii8bit_encoding()) {
226
	UINT code_page = 20127;
227
	rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page));
228
	return code_page;
229
    }
230

  
231
    if (names_ary != Qundef) {
232
	for (i = 0; i < RARRAY_LEN(names_ary); i++) {
233
	    name = RARRAY_PTR(names_ary)[i];
234
	    if (strncmp("CP", RSTRING_PTR(name), 2) == 0) {
235
		int code_page = atoi(RSTRING_PTR(name) + 2);
236
		if (code_page != 0) {
237
		    rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page));
238
		    return (UINT)code_page;
239
		}
240
	    }
241
	}
242
    }
243

  
244
    rb_hash_aset(rb_code_page, name_key, INT2FIX(INVALID_CODE_PAGE));
245
    return INVALID_CODE_PAGE;
246
}
247

  
248
#define PATH_BUFFER_SIZE MAX_PATH * 2
249

  
250
VALUE
251
rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, VALUE result)
252
{
253
    size_t size = 0, wpath_len = 0, wdir_len = 0, whome_len = 0;
254
    size_t buffer_len = 0;
255
    wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL, *wdir = NULL;
256
    wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
257
    UINT path_cp, cp;
258
    VALUE path = fname, dir = dname;
259
    wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
260
    wchar_t path_drive = L'\0', dir_drive = L'\0';
261
    int ignore_dir = 0;
262
    rb_encoding *path_encoding;
263
    int tainted = 0;
264

  
265
    /* tainted if path is tainted */
266
    tainted = OBJ_TAINTED(path);
267

  
268

  
269
    /* get path encoding */
270
    if (NIL_P(dir)) {
271
	path_encoding = rb_enc_get(path);
272
    }
273
    else {
274
	path_encoding = rb_enc_check(path, dir);
275
    }
276
    cp = path_cp = code_page(path_encoding);
277

  
278
    /* convert char * to wchar_t */
279
    if (path_cp == INVALID_CODE_PAGE) {
280
	cp = CP_UTF8;
281
	if (!NIL_P(path)) {
282
	    VALUE tmp = rb_enc_str_new(RSTRING_PTR(path), RSTRING_LEN(path), path_encoding);
283
	    path = rb_str_encode(tmp, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil);
284
	    rb_str_resize(tmp, 0);
285
	}
286
    }
287
    path_to_wchar(path, &wpath, &wpath_pos, &wpath_len, cp);
288

  
289
    /* determine if we need the user's home directory */
290
    /* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
291
    if (abs_mode == 0 && ((wpath_len == 1 && wpath_pos[0] == L'~') ||
292
		(wpath_len >= 2 && wpath_pos[0] == L'~' && IS_DIR_SEPARATOR_P(wpath_pos[1])))) {
293
	/* tainted if expanding '~' */
294
	tainted = 1;
295

  
296
	whome = home_dir();
297
	if (whome == NULL) {
298
	    free(wpath);
299
	    rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
300
	}
301
	whome_len = wcslen(whome);
302

  
303
	if (PathIsRelativeW(whome) && !(whome_len >= 2 && IS_DIR_UNC_P(whome))) {
304
	    free(wpath);
305
	    rb_raise(rb_eArgError, "non-absolute home");
306
	}
307

  
308
	/* ignores dir since we are expading home */
309
	ignore_dir = 1;
310

  
311
	/* exclude ~ from the result */
312
	wpath_pos++;
313
	wpath_len--;
314

  
315
	/* exclude separator if present */
316
	if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
317
	    wpath_pos++;
318
	    wpath_len--;
319
	}
320
    }
321
    else if (wpath_len >= 2 && wpath_pos[1] == L':') {
322
	if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
323
	    /* ignore dir since path contains a drive letter and a root slash */
324
	    ignore_dir = 1;
325
	}
326
	else {
327
	    /* determine if we ignore dir or not later */
328
	    path_drive = wpath_pos[0];
329
	}
330
    }
331
    else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
332
	wchar_t *wuser = wpath_pos + 1;
333
	wchar_t *pos = wuser;
334
	char *user;
335

  
336
	/* tainted if expanding '~' */
337
	tainted = 1;
338

  
339
	while (!IS_DIR_SEPARATOR_P(*pos) && *pos != '\0')
340
	    pos++;
341

  
342
	*pos = '\0';
343

  
344
	size = WideCharToMultiByte(cp, 0, wuser, -1, NULL, 0, NULL, NULL);
345
	user = (char *)malloc(size * sizeof(char));
346
	WideCharToMultiByte(cp, 0, wuser, -1, user, size, NULL, NULL);
347
	/* convert to VALUE and set the path encoding */
348
	if (path_cp == INVALID_CODE_PAGE) {
349
	    VALUE tmp = rb_enc_str_new(user, size - 1, rb_utf8_encoding());
350
	    result = rb_str_encode(tmp, rb_enc_from_encoding(path_encoding), 0, Qnil);
351
	    rb_str_resize(tmp, 0);
352
	}
353
	else {
354
	    result = rb_enc_str_new(user, size - 1, path_encoding);
355
	}
356

  
357
	free(wpath);
358
	if (user)
359
	    free(user);
360

  
361
	rb_raise(rb_eArgError, "can't find user %s", StringValuePtr(result));
362
    }
363

  
364
    /* convert dir */
365
    if (!ignore_dir && !NIL_P(dir)) {
366
	/* convert char * to wchar_t */
367
	if (path_cp == INVALID_CODE_PAGE) {
368
	    VALUE tmp = rb_enc_str_new(RSTRING_PTR(dir), RSTRING_LEN(dir), path_encoding);
369
	    dir = rb_str_encode(tmp, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil);
370
	    rb_str_resize(tmp, 0);
371
	}
372
	path_to_wchar(dir, &wdir, NULL, &wdir_len, cp);
373

  
374
	if (wdir_len >= 2 && wdir[1] == L':') {
375
	    dir_drive = wdir[0];
376
	    if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
377
		wdir_len = 2;
378
	    }
379
	}
380
	else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
381
	    /* UNC path */
382
	    if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
383
		/* cut the UNC path tail to '//host/share' */
384
		size_t separators = 0;
385
		size_t pos = 2;
386
		while (pos < wdir_len && separators < 2) {
387
		    if (IS_DIR_SEPARATOR_P(wdir[pos])) {
388
			separators++;
389
		    }
390
		    pos++;
391
		}
392
		if (separators == 2)
393
		    wdir_len = pos - 1;
394
	    }
395
	}
396
    }
397

  
398
    /* determine if we ignore dir or not */
399
    if (!ignore_dir && path_drive && dir_drive) {
400
	if (towupper(path_drive) == towupper(dir_drive)) {
401
	    /* exclude path drive letter to use dir */
402
	    wpath_pos += 2;
403
	    wpath_len -= 2;
404
	}
405
	else {
406
	    /* ignore dir since path drive is different from dir drive */
407
	    ignore_dir = 1;
408
	    wdir_len = 0;
409
	}
410
    }
411

  
412
    if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
413
	/* ignore dir since path has UNC root */
414
	ignore_dir = 1;
415
	wdir_len = 0;
416
    }
417
    else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
418
	    !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
419
	/* ignore dir since path has root slash and dir doesn't have drive or UNC root */
420
	ignore_dir = 1;
421
	wdir_len = 0;
422
    }
423

  
424
    buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
425

  
426
    buffer = buffer_pos = (wchar_t *)malloc((buffer_len + 1) * sizeof(wchar_t));
427

  
428
    /* add home */
429
    if (whome_len) {
430
	wcsncpy(buffer_pos, whome, whome_len);
431
	buffer_pos += whome_len;
432
    }
433

  
434
    /* Add separator if required */
435
    if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
436
	buffer_pos[0] = L'\\';
437
	buffer_pos++;
438
    }
439

  
440
    if (wdir_len) {
441
	/* tainted if dir is used and dir is tainted */
442
	if (!tainted && OBJ_TAINTED(dir))
443
	    tainted = 1;
444

  
445
	wcsncpy(buffer_pos, wdir, wdir_len);
446
	buffer_pos += wdir_len;
447
    }
448

  
449
    /* add separator if required */
450
    if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
451
	buffer_pos[0] = L'\\';
452
	buffer_pos++;
453
    }
454

  
455
    /* now deal with path */
456
    if (wpath_len) {
457
	wcsncpy(buffer_pos, wpath_pos, wpath_len);
458
	buffer_pos += wpath_len;
459
    }
460

  
461
    /* GetFullPathNameW requires at least "." to determine current directory */
462
    if (wpath_len == 0) {
463
	buffer_pos[0] = L'.';
464
	buffer_pos++;
465
    }
466

  
467
    /* Ensure buffer is NULL terminated */
468
    buffer_pos[0] = L'\0';
469

  
470
    /* tainted if path is relative */
471
    if (!tainted && PathIsRelativeW(buffer) && !(buffer_len >= 2 && IS_DIR_UNC_P(buffer))) {
472
	tainted = 1;
473
    }
474

  
475
    /* Determine require buffer size */
476
    size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
477
    if (size) {
478
	size_t wfullpath_size;
479
	if (size > PATH_BUFFER_SIZE) {
480
	    /* allocate enough memory to contain the response */
481
	    wfullpath = (wchar_t *)malloc(size * sizeof(wchar_t));
482
	    size = GetFullPathNameW(buffer, size, wfullpath, NULL);
483
	}
484
	else {
485
	    wfullpath = wfullpath_buffer;
486
	}
487

  
488

  
489
	/* Remove any trailing slashes */
490
	if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
491
		wfullpath[size - 2] != L':' &&
492
		!(size == 2 && IS_DIR_UNC_P(wfullpath))) {
493
	    size -= 1;
494
	    wfullpath[size] = L'\0';
495
	}
496

  
497
	/* Remove any trailing dot */
498
	if (wfullpath[size - 1] == L'.') {
499
	    size -= 1;
500
	    wfullpath[size] = L'\0';
501
	}
502

  
503
	/* removes trailing invalid ':$DATA' */
504
	size = remove_invalid_alternative_data(wfullpath, size);
505

  
506
	/* sanitize backslashes with forwardslashes */
507
	replace_wchar(wfullpath, L'\\', L'/');
508

  
509
	/* convert to char * */
510
	wfullpath_size = size;
511
	size = WideCharToMultiByte(cp, 0, wfullpath, wfullpath_size, NULL, 0, NULL, NULL);
512
	if (size > (size_t)RSTRING_LEN(result)) {
513
	    rb_str_modify(result);
514
	    rb_str_resize(result, size);
515
	}
516

  
517
	WideCharToMultiByte(cp, 0, wfullpath, wfullpath_size, RSTRING_PTR(result), size, NULL, NULL);
518

  
519
	/* set the String VALUE length and the path encoding */
520
	rb_str_set_len(result, size);
521
	if (path_cp == INVALID_CODE_PAGE) {
522
	    VALUE tmp;
523
	    size_t len;
524

  
525
	    rb_enc_associate(result, rb_utf8_encoding());
526
	    ENC_CODERANGE_CLEAR(result);
527
	    tmp = rb_str_encode(result, rb_enc_from_encoding(path_encoding), 0, Qnil);
528
	    len = RSTRING_LEN(tmp);
529
	    rb_str_modify(result);
530
	    rb_str_resize(result, len);
531
	    memcpy(RSTRING_PTR(result), RSTRING_PTR(tmp), len);
532
	    rb_str_resize(tmp, 0);
533
	}
534
	rb_enc_associate(result, path_encoding);
535
	ENC_CODERANGE_CLEAR(result);
536

  
537
	/* makes the result object tainted if expanding tainted strings or returning modified path */
538
	if (tainted)
539
	    OBJ_TAINT(result);
540
    }
541

  
542
    if (buffer)
543
	free(buffer);
544

  
545
    if (wpath)
546
	free(wpath);
547

  
548
    if (wdir)
549
	free(wdir);
550

  
551
    if (whome)
552
	free(whome);
553

  
554
    if (wfullpath && wfullpath != wfullpath_buffer)
555
	free(wfullpath);
556

  
557
    return result;
558
}
559

  
560

  
561
void
562
rb_w32_init_file(void)
563
{
564
    rb_code_page = rb_hash_new();
565

  
566
    /* prevent GC removing rb_code_page */
567
    rb_gc_register_mark_object(rb_code_page);
568
}