win32-file 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,27 @@
1
- require 'ffi'
2
-
3
- module Windows
4
- module File
5
- module Constants
6
- FILE_ATTRIBUTE_NORMAL = 0x00000080
7
- FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400
8
- FILE_FLAG_BACKUP_SEMANTICS = 0x02000000
9
- FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000
10
- FILE_SHARE_READ = 1
11
-
12
- FILE_TYPE_UNKNOWN = 0x0000
13
- FILE_TYPE_CHAR = 0x0002
14
- NO_ERROR = 0
15
-
16
- GENERIC_READ = 0x80000000
17
- INVALID_HANDLE_VALUE = (1 << FFI::Platform::ADDRESS_SIZE) - 1
18
- INVALID_FILE_ATTRIBUTES = (1 << FFI::Platform::ADDRESS_SIZE) - 1
19
- IO_REPARSE_TAG_SYMLINK = 0xA000000C
20
- OPEN_EXISTING = 3
21
-
22
- DRIVE_REMOVABLE = 2
23
- DRIVE_CDROM = 5
24
- DRIVE_RAMDISK = 6
25
- end
26
- end
27
- end
1
+ require 'ffi'
2
+
3
+ module Windows
4
+ module File
5
+ module Constants
6
+ FILE_ATTRIBUTE_NORMAL = 0x00000080
7
+ FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400
8
+ FILE_FLAG_BACKUP_SEMANTICS = 0x02000000
9
+ FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000
10
+ FILE_SHARE_READ = 1
11
+
12
+ FILE_TYPE_UNKNOWN = 0x0000
13
+ FILE_TYPE_CHAR = 0x0002
14
+ NO_ERROR = 0
15
+
16
+ GENERIC_READ = 0x80000000
17
+ INVALID_HANDLE_VALUE = (1 << FFI::Platform::ADDRESS_SIZE) - 1
18
+ INVALID_FILE_ATTRIBUTES = (1 << FFI::Platform::ADDRESS_SIZE) - 1
19
+ IO_REPARSE_TAG_SYMLINK = 0xA000000C
20
+ OPEN_EXISTING = 3
21
+
22
+ DRIVE_REMOVABLE = 2
23
+ DRIVE_CDROM = 5
24
+ DRIVE_RAMDISK = 6
25
+ end
26
+ end
27
+ end
@@ -1,49 +1,49 @@
1
- module Windows
2
- module File
3
- module Functions
4
- extend FFI::Library
5
- ffi_lib :kernel32
6
-
7
- typedef :ulong, :dword
8
- typedef :uintptr_t, :handle
9
- typedef :pointer, :ptr
10
-
11
- def self.attach_pfunc(*args)
12
- attach_function(*args)
13
- private args[0]
14
- end
15
-
16
- attach_pfunc :CloseHandle, [:handle], :bool
17
- attach_pfunc :CreateFileW, [:buffer_in, :dword, :dword, :pointer, :dword, :dword, :handle], :handle
18
- attach_pfunc :CreateSymbolicLinkW, [:buffer_in, :buffer_in, :dword], :bool
19
- attach_pfunc :FindFirstFileW, [:buffer_in, :pointer], :handle
20
- attach_pfunc :GetDiskFreeSpaceW, [:buffer_in, :pointer, :pointer, :pointer, :pointer], :bool
21
- attach_pfunc :GetDriveTypeW, [:buffer_in], :uint
22
- attach_pfunc :GetFileType, [:handle], :dword
23
- attach_pfunc :GetFileAttributesW, [:buffer_in], :dword
24
- attach_pfunc :GetFinalPathNameByHandleW, [:handle, :buffer_out, :dword, :dword], :dword
25
- attach_pfunc :GetShortPathNameW, [:buffer_in, :buffer_out, :dword], :dword
26
- attach_pfunc :GetLongPathNameW, [:buffer_in, :buffer_out, :dword], :dword
27
- attach_pfunc :QueryDosDeviceA, [:string, :buffer_out, :dword], :dword
28
-
29
- ffi_lib :shlwapi
30
-
31
- attach_pfunc :PathFindExtensionW, [:buffer_in], :pointer
32
- attach_pfunc :PathIsRootW, [:buffer_in], :bool
33
- attach_pfunc :PathStripPathW, [:pointer], :void
34
- attach_pfunc :PathRemoveBackslashW, [:buffer_in], :string
35
- attach_pfunc :PathRemoveFileSpecW, [:pointer], :bool
36
- attach_pfunc :PathRemoveExtensionW, [:buffer_in], :void
37
- attach_pfunc :PathStripToRootW, [:buffer_in], :bool
38
- end
39
- end
40
- end
41
-
42
- class String
43
- # Read a wide character string up until the first double null, and delete
44
- # any remaining null characters.
45
- def wstrip
46
- self.force_encoding('UTF-16LE').encode('UTF-8',:invalid=>:replace,:undef=>:replace).
47
- split("\x00")[0].encode(Encoding.default_external)
48
- end
49
- end
1
+ module Windows
2
+ module File
3
+ module Functions
4
+ extend FFI::Library
5
+ ffi_lib :kernel32
6
+
7
+ typedef :ulong, :dword
8
+ typedef :uintptr_t, :handle
9
+ typedef :pointer, :ptr
10
+
11
+ def self.attach_pfunc(*args)
12
+ attach_function(*args)
13
+ private args[0]
14
+ end
15
+
16
+ attach_pfunc :CloseHandle, [:handle], :bool
17
+ attach_pfunc :CreateFileW, [:buffer_in, :dword, :dword, :pointer, :dword, :dword, :handle], :handle
18
+ attach_pfunc :CreateSymbolicLinkW, [:buffer_in, :buffer_in, :dword], :bool
19
+ attach_pfunc :FindFirstFileW, [:buffer_in, :pointer], :handle
20
+ attach_pfunc :GetDiskFreeSpaceW, [:buffer_in, :pointer, :pointer, :pointer, :pointer], :bool
21
+ attach_pfunc :GetDriveTypeW, [:buffer_in], :uint
22
+ attach_pfunc :GetFileType, [:handle], :dword
23
+ attach_pfunc :GetFileAttributesW, [:buffer_in], :dword
24
+ attach_pfunc :GetFinalPathNameByHandleW, [:handle, :buffer_out, :dword, :dword], :dword
25
+ attach_pfunc :GetShortPathNameW, [:buffer_in, :buffer_out, :dword], :dword
26
+ attach_pfunc :GetLongPathNameW, [:buffer_in, :buffer_out, :dword], :dword
27
+ attach_pfunc :QueryDosDeviceA, [:string, :buffer_out, :dword], :dword
28
+
29
+ ffi_lib :shlwapi
30
+
31
+ attach_pfunc :PathFindExtensionW, [:buffer_in], :pointer
32
+ attach_pfunc :PathIsRootW, [:buffer_in], :bool
33
+ attach_pfunc :PathStripPathW, [:pointer], :void
34
+ attach_pfunc :PathRemoveBackslashW, [:buffer_in], :string
35
+ attach_pfunc :PathRemoveFileSpecW, [:pointer], :bool
36
+ attach_pfunc :PathRemoveExtensionW, [:buffer_in], :void
37
+ attach_pfunc :PathStripToRootW, [:buffer_in], :bool
38
+ end
39
+ end
40
+ end
41
+
42
+ class String
43
+ # Read a wide character string up until the first double null, and delete
44
+ # any remaining null characters.
45
+ def wstrip
46
+ self.force_encoding('UTF-16LE').encode('UTF-8',:invalid=>:replace,:undef=>:replace).
47
+ split("\x00")[0].encode(Encoding.default_external)
48
+ end
49
+ end
@@ -1,24 +1,24 @@
1
- module Windows
2
- module File
3
- module Structs
4
- class FILETIME < FFI::Struct
5
- layout(:dwLowDateTime, :ulong, :dwHighDateTime, :ulong)
6
- end
7
-
8
- class WIN32_FIND_DATA < FFI::Struct
9
- layout(
10
- :dwFileAttributes, :ulong,
11
- :ftCreationTime, FILETIME,
12
- :ftLastAccessTime, FILETIME,
13
- :ftLastWriteTime, FILETIME,
14
- :nFileSizeHigh, :ulong,
15
- :nFileSizeLow, :ulong,
16
- :dwReserved0, :ulong,
17
- :dwReserved1, :ulong,
18
- :cFileName, [:uint8, 260*2],
19
- :cAlternateFileName, [:uint8, 14*2]
20
- )
21
- end
22
- end
23
- end
24
- end
1
+ module Windows
2
+ module File
3
+ module Structs
4
+ class FILETIME < FFI::Struct
5
+ layout(:dwLowDateTime, :ulong, :dwHighDateTime, :ulong)
6
+ end
7
+
8
+ class WIN32_FIND_DATA < FFI::Struct
9
+ layout(
10
+ :dwFileAttributes, :ulong,
11
+ :ftCreationTime, FILETIME,
12
+ :ftLastAccessTime, FILETIME,
13
+ :ftLastWriteTime, FILETIME,
14
+ :nFileSizeHigh, :ulong,
15
+ :nFileSizeLow, :ulong,
16
+ :dwReserved0, :ulong,
17
+ :dwReserved1, :ulong,
18
+ :cFileName, [:uint8, 260*2],
19
+ :cAlternateFileName, [:uint8, 14*2]
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,141 +1,141 @@
1
- #############################################################################
2
- # test_win32_file_link.rb
3
- #
4
- # Test case for the link related methods of win32-file. You should run this
5
- # test via the 'rake test' or 'rake test:link' task.
6
- #############################################################################
7
- require 'test-unit'
8
- require 'win32/security'
9
- require 'win32/file'
10
-
11
- class TC_Win32_File_Link < Test::Unit::TestCase
12
- def self.startup
13
- Dir.chdir(File.expand_path(File.dirname(__FILE__)))
14
- @@file = File.join(Dir.pwd, 'link_test.txt')
15
- File.open(@@file, 'w'){ |fh| fh.puts "This is a link test." }
16
- end
17
-
18
- def setup
19
- @link = "this_is_a_symlink"
20
- @file = "link_test.txt"
21
- @admin = Win32::Security.elevated_security?
22
- end
23
-
24
- test "symlink basic functionality" do
25
- assert_respond_to(File, :symlink)
26
- end
27
-
28
- test "symlink to a file works as expected" do
29
- omit_unless(@admin)
30
- assert_nothing_raised{ File.symlink(@@file, @link) }
31
- assert_true(File.exist?(@link))
32
- assert_true(File.symlink?(@link))
33
- end
34
-
35
- test "symlink method returns zero" do
36
- omit_unless(@admin)
37
- assert_equal(0, File.symlink(@@file, @link))
38
- end
39
-
40
- test "symlink requires two arguments" do
41
- assert_raise(ArgumentError){ File.symlink }
42
- assert_raise(ArgumentError){ File.symlink(@link) }
43
- assert_raise(ArgumentError){ File.symlink(@@file, @link, @link) }
44
- end
45
-
46
- test "symlink fails if link already exists" do
47
- omit_unless(@admin)
48
- File.symlink(@@file, @link)
49
- assert_raise(SystemCallError){ File.symlink(@@file, @link) }
50
- end
51
-
52
- test "symlink does not fail if target does not exist" do
53
- omit_unless(@admin)
54
- assert_nothing_raised{ File.symlink('bogus.txt', @link) }
55
- end
56
-
57
- test "symlink? basic functionality" do
58
- assert_respond_to(File, :symlink?)
59
- assert_boolean(File.symlink?(@@file))
60
- end
61
-
62
- test "symlink? returns expected result" do
63
- omit_unless(@admin)
64
- File.symlink(@@file, @link)
65
- assert_true(File.symlink?(@link))
66
- assert_false(File.symlink?(@file))
67
- end
68
-
69
- test "symlink? requires one argument only" do
70
- assert_raise(ArgumentError){ File.symlink? }
71
- assert_raise(ArgumentError){ File.symlink?(@@file, @@file) }
72
- end
73
-
74
- test "readlink basic functionality" do
75
- assert_respond_to(File, :readlink)
76
- end
77
-
78
- test "readlink returns the expected value when reading a symlink" do
79
- omit_unless(@admin)
80
- File.symlink(@file, @link)
81
- expected = File.expand_path(@file).tr("/", "\\")
82
- assert_equal(expected, File.readlink(@link))
83
- end
84
-
85
- test "readlink raises an error when reading a regular file" do
86
- assert_raise(Errno::EINVAL){ File.readlink(@@file) }
87
- end
88
-
89
- test "readlink requires one argument only" do
90
- assert_raise(ArgumentError){ File.readlink }
91
- assert_raise(ArgumentError){ File.readlink(@link, @link) }
92
- end
93
-
94
- test "readlink raises an error if the file is not found" do
95
- assert_raise(Errno::ENOENT){ File.readlink('bogus.txt') }
96
- end
97
-
98
- test "realdirpath basic functionality" do
99
- assert_respond_to(File, :realdirpath)
100
- end
101
-
102
- test "realdirpath returns the expected value for a regular file" do
103
- assert_equal(Dir.pwd, File.realdirpath(Dir.pwd))
104
- assert_equal(@@file, File.realdirpath(@@file).tr("/", "\\"))
105
- end
106
-
107
- test "realdirpath returns the expected value for a symlink" do
108
- omit_unless(@admin)
109
- File.symlink(@@file, @link)
110
- expected = File.expand_path(@file).tr("/", "\\")
111
- assert_equal(expected, File.realdirpath(@link))
112
- end
113
-
114
- test "realpath basic functionality" do
115
- assert_respond_to(File, :realpath)
116
- end
117
-
118
- test "realpath returns the expected value for a regular file" do
119
- assert_equal(Dir.pwd, File.realpath(Dir.pwd))
120
- assert_equal(@@file, File.realpath(@@file).tr("/", "\\"))
121
- end
122
-
123
- test "realpath returns the expected value for a symlink" do
124
- omit_unless(@admin)
125
- File.symlink(@@file, @link)
126
- expected = File.expand_path(@file).tr("/", "\\")
127
- assert_equal(expected, File.realpath(@link))
128
- end
129
-
130
- def teardown
131
- File.delete(@link) rescue nil # Necessary for dangling links
132
- @link = nil
133
- @admin = nil
134
- @file = nil
135
- end
136
-
137
- def self.shutdown
138
- File.delete(@@file) if File.exist?(@@file)
139
- @@file = nil
140
- end
141
- end
1
+ #############################################################################
2
+ # test_win32_file_link.rb
3
+ #
4
+ # Test case for the link related methods of win32-file. You should run this
5
+ # test via the 'rake test' or 'rake test:link' task.
6
+ #############################################################################
7
+ require 'test-unit'
8
+ require 'win32/security'
9
+ require 'win32/file'
10
+
11
+ class TC_Win32_File_Link < Test::Unit::TestCase
12
+ def self.startup
13
+ Dir.chdir(File.expand_path(File.dirname(__FILE__)))
14
+ @@file = File.join(Dir.pwd, 'link_test.txt')
15
+ File.open(@@file, 'w'){ |fh| fh.puts "This is a link test." }
16
+ end
17
+
18
+ def setup
19
+ @link = "this_is_a_symlink"
20
+ @file = "link_test.txt"
21
+ @admin = Win32::Security.elevated_security?
22
+ end
23
+
24
+ test "symlink basic functionality" do
25
+ assert_respond_to(File, :symlink)
26
+ end
27
+
28
+ test "symlink to a file works as expected" do
29
+ omit_unless(@admin)
30
+ assert_nothing_raised{ File.symlink(@@file, @link) }
31
+ assert_true(File.exist?(@link))
32
+ assert_true(File.symlink?(@link))
33
+ end
34
+
35
+ test "symlink method returns zero" do
36
+ omit_unless(@admin)
37
+ assert_equal(0, File.symlink(@@file, @link))
38
+ end
39
+
40
+ test "symlink requires two arguments" do
41
+ assert_raise(ArgumentError){ File.symlink }
42
+ assert_raise(ArgumentError){ File.symlink(@link) }
43
+ assert_raise(ArgumentError){ File.symlink(@@file, @link, @link) }
44
+ end
45
+
46
+ test "symlink fails if link already exists" do
47
+ omit_unless(@admin)
48
+ File.symlink(@@file, @link)
49
+ assert_raise(SystemCallError){ File.symlink(@@file, @link) }
50
+ end
51
+
52
+ test "symlink does not fail if target does not exist" do
53
+ omit_unless(@admin)
54
+ assert_nothing_raised{ File.symlink('bogus.txt', @link) }
55
+ end
56
+
57
+ test "symlink? basic functionality" do
58
+ assert_respond_to(File, :symlink?)
59
+ assert_boolean(File.symlink?(@@file))
60
+ end
61
+
62
+ test "symlink? returns expected result" do
63
+ omit_unless(@admin)
64
+ File.symlink(@@file, @link)
65
+ assert_true(File.symlink?(@link))
66
+ assert_false(File.symlink?(@file))
67
+ end
68
+
69
+ test "symlink? requires one argument only" do
70
+ assert_raise(ArgumentError){ File.symlink? }
71
+ assert_raise(ArgumentError){ File.symlink?(@@file, @@file) }
72
+ end
73
+
74
+ test "readlink basic functionality" do
75
+ assert_respond_to(File, :readlink)
76
+ end
77
+
78
+ test "readlink returns the expected value when reading a symlink" do
79
+ omit_unless(@admin)
80
+ File.symlink(@file, @link)
81
+ expected = File.expand_path(@file).tr("/", "\\")
82
+ assert_equal(expected, File.readlink(@link))
83
+ end
84
+
85
+ test "readlink raises an error when reading a regular file" do
86
+ assert_raise(Errno::EINVAL){ File.readlink(@@file) }
87
+ end
88
+
89
+ test "readlink requires one argument only" do
90
+ assert_raise(ArgumentError){ File.readlink }
91
+ assert_raise(ArgumentError){ File.readlink(@link, @link) }
92
+ end
93
+
94
+ test "readlink raises an error if the file is not found" do
95
+ assert_raise(Errno::ENOENT){ File.readlink('bogus.txt') }
96
+ end
97
+
98
+ test "realdirpath basic functionality" do
99
+ assert_respond_to(File, :realdirpath)
100
+ end
101
+
102
+ test "realdirpath returns the expected value for a regular file" do
103
+ assert_equal(Dir.pwd, File.realdirpath(Dir.pwd))
104
+ assert_equal(@@file, File.realdirpath(@@file).tr("/", "\\"))
105
+ end
106
+
107
+ test "realdirpath returns the expected value for a symlink" do
108
+ omit_unless(@admin)
109
+ File.symlink(@@file, @link)
110
+ expected = File.expand_path(@file).tr("/", "\\")
111
+ assert_equal(expected, File.realdirpath(@link))
112
+ end
113
+
114
+ test "realpath basic functionality" do
115
+ assert_respond_to(File, :realpath)
116
+ end
117
+
118
+ test "realpath returns the expected value for a regular file" do
119
+ assert_equal(Dir.pwd, File.realpath(Dir.pwd))
120
+ assert_equal(@@file, File.realpath(@@file).tr("/", "\\"))
121
+ end
122
+
123
+ test "realpath returns the expected value for a symlink" do
124
+ omit_unless(@admin)
125
+ File.symlink(@@file, @link)
126
+ expected = File.expand_path(@file).tr("/", "\\")
127
+ assert_equal(expected, File.realpath(@link))
128
+ end
129
+
130
+ def teardown
131
+ File.delete(@link) rescue nil # Necessary for dangling links
132
+ @link = nil
133
+ @admin = nil
134
+ @file = nil
135
+ end
136
+
137
+ def self.shutdown
138
+ File.delete(@@file) if File.exist?(@@file)
139
+ @@file = nil
140
+ end
141
+ end