sys-filesystem 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ module Sys
2
+ class Filesystem
3
+ module Constants
4
+ MAXPATH = 260
5
+
6
+ CASE_SENSITIVE_SEARCH = 0x00000001
7
+ CASE_PRESERVED_NAMES = 0x00000002
8
+ UNICODE_ON_DISK = 0x00000004
9
+ PERSISTENT_ACLS = 0x00000008
10
+ FILE_COMPRESSION = 0x00000010
11
+ VOLUME_QUOTAS = 0x00000020
12
+ SUPPORTS_SPARSE_FILES = 0x00000040
13
+ SUPPORTS_REPARSE_POINTS = 0x00000080
14
+ SUPPORTS_REMOTE_STORAGE = 0x00000100
15
+ VOLUME_IS_COMPRESSED = 0x00008000
16
+ SUPPORTS_OBJECT_IDS = 0x00010000
17
+ SUPPORTS_ENCRYPTION = 0x00020000
18
+ NAMED_STREAMS = 0x00040000
19
+ READ_ONLY_VOLUME = 0x00080000
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ require 'ffi'
2
+
3
+ module Sys
4
+ class Filesystem
5
+ module Functions
6
+ extend FFI::Library
7
+ ffi_lib :kernel32
8
+
9
+ # Make FFI functions private
10
+ module FFI::Library
11
+ def attach_pfunc(*args)
12
+ attach_function(*args)
13
+ private args[0]
14
+ end
15
+ end
16
+
17
+ attach_pfunc :GetDiskFreeSpaceW, [:buffer_in, :pointer, :pointer, :pointer, :pointer], :bool
18
+ attach_pfunc :GetDiskFreeSpaceExW, [:buffer_in, :pointer, :pointer, :pointer], :bool
19
+ attach_pfunc :GetLogicalDriveStringsA, [:ulong, :pointer], :ulong
20
+
21
+ attach_pfunc :GetVolumeInformationA,
22
+ [:buffer_in, :pointer, :ulong, :pointer, :pointer, :pointer, :pointer, :ulong],
23
+ :bool
24
+
25
+ attach_pfunc :GetVolumeInformationW,
26
+ [:buffer_in, :pointer, :ulong, :pointer, :pointer, :pointer, :pointer, :ulong],
27
+ :bool
28
+
29
+ attach_pfunc :QueryDosDeviceA, [:buffer_in, :buffer_out, :ulong], :ulong
30
+
31
+ ffi_lib :shlwapi
32
+
33
+ attach_pfunc :PathStripToRootW, [:pointer], :bool
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ class String
2
+ # Convenience method for converting strings to UTF-16LE for wide character
3
+ # functions that require it.
4
+ def wincode
5
+ (self.tr(File::SEPARATOR, File::ALT_SEPARATOR) + 0.chr).encode('UTF-16LE')
6
+ end
7
+ end
File without changes
@@ -2,11 +2,10 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'sys-filesystem'
5
- spec.version = '1.0.0'
5
+ spec.version = '1.1.0'
6
6
  spec.author = 'Daniel J. Berger'
7
7
  spec.email = 'djberg96@gmail.com'
8
- spec.homepage = 'http://www.rubyforge.org/projects/sysutils'
9
- spec.platform = Gem::Platform::RUBY
8
+ spec.homepage = 'https://github.com/djberg96/sys-filesystem'
10
9
  spec.summary = 'A Ruby interface for getting file system information.'
11
10
  spec.test_file = 'test/test_sys_filesystem.rb'
12
11
  spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
@@ -15,7 +14,8 @@ Gem::Specification.new do |spec|
15
14
  spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
16
15
  spec.rubyforge_project = 'sysutils'
17
16
 
18
- spec.add_development_dependency('test-unit', '>= 2.1.1')
17
+ spec.add_dependency('ffi')
18
+ spec.add_development_dependency('test-unit', '>= 2.5.0')
19
19
 
20
20
  spec.description = <<-EOF
21
21
  The sys-filesystem library provides an interface for gathering filesystem
@@ -4,10 +4,7 @@
4
4
  # Test case for the Sys::Filesystem.stat method and related stuff.
5
5
  # This test suite should be run via the 'rake test' task.
6
6
  ####################################################################
7
- require 'rubygems'
8
- gem 'test-unit'
9
-
10
- require 'test/unit'
7
+ require 'test-unit'
11
8
  require 'sys/filesystem'
12
9
  include Sys
13
10
 
@@ -28,7 +25,7 @@ class TC_Sys_Filesystem_Unix < Test::Unit::TestCase
28
25
  end
29
26
 
30
27
  def test_version
31
- assert_equal('1.0.0', Filesystem::VERSION)
28
+ assert_equal('1.1.0', Filesystem::VERSION)
32
29
  end
33
30
 
34
31
  def test_stat_path
@@ -241,7 +238,6 @@ class TC_Sys_Filesystem_Unix < Test::Unit::TestCase
241
238
  end
242
239
 
243
240
  def test_ffi_functions_are_private
244
- methods = Filesystem.methods(false).map{ |e| e.to_s }
245
241
  assert_false(Filesystem.methods.include?('statvfs'))
246
242
  assert_false(Filesystem.methods.include?('strerror'))
247
243
  end
@@ -4,195 +4,219 @@
4
4
  # Test case for the Sys::Filesystem.stat method and related stuff.
5
5
  # This should be run via the 'rake test' task.
6
6
  ####################################################################
7
- require 'rubygems'
8
- gem 'test-unit'
9
-
10
- require 'test/unit'
7
+ require 'test-unit'
11
8
  require 'sys/filesystem'
12
9
  require 'rbconfig'
13
10
  include Sys
14
11
 
15
12
  class TC_Sys_Filesystem_Windows < Test::Unit::TestCase
16
- def setup
17
- @dir = '/'
18
- @stat = Filesystem.stat(@dir)
19
- @mount = Filesystem.mounts[0]
20
- @size = 58720256
21
- @array = []
22
- end
23
-
24
- def test_version
25
- assert_equal('1.0.0', Filesystem::VERSION)
26
- end
27
-
28
- def test_stat_path
29
- assert_respond_to(@stat, :path)
30
- assert_equal("/", @stat.path)
31
- end
32
-
33
- def test_stat_block_size
34
- assert_respond_to(@stat, :block_size)
35
- assert_kind_of(Fixnum, @stat.block_size)
36
- end
37
-
38
- def test_stat_fragment_size
39
- assert_respond_to(@stat, :fragment_size)
40
- assert_nil(@stat.fragment_size)
41
- end
42
-
43
- def test_stat_blocks
44
- assert_respond_to(@stat, :blocks)
45
- assert_kind_of(Fixnum, @stat.blocks)
46
- end
47
-
48
- def test_stat_blocks_free
49
- assert_respond_to(@stat, :blocks_free)
50
- assert_kind_of(Fixnum, @stat.blocks_free)
51
- end
52
-
53
- def test_stat_blocks_available
54
- assert_respond_to(@stat, :blocks_available)
55
- assert_kind_of(Fixnum, @stat.blocks_available)
56
- end
57
-
58
- def test_stat_files
59
- assert_respond_to(@stat, :files)
60
- assert_respond_to(@stat, :inodes) # Alias
61
- assert_nil(@stat.files)
62
- end
63
-
64
- def test_stat_files_free
65
- assert_respond_to(@stat, :files_free)
66
- assert_respond_to(@stat, :inodes_free) # Alias
67
- assert_nil(@stat.files_free)
68
- end
69
-
70
- def test_stat_files_available
71
- assert_respond_to(@stat, :files_available)
72
- assert_respond_to(@stat, :inodes_available) # Alias
73
- assert_nil(@stat.files_available)
74
- end
75
-
76
- def test_stat_filesystem_id
77
- assert_respond_to(@stat, :filesystem_id)
78
- assert_kind_of(Integer, @stat.filesystem_id)
79
- end
80
-
81
- def test_stat_flags
82
- assert_respond_to(@stat, :flags)
83
- assert_kind_of(Fixnum, @stat.flags)
84
- end
85
-
86
- def test_stat_name_max
87
- assert_respond_to(@stat, :name_max)
88
- assert_kind_of(Fixnum, @stat.name_max)
89
- end
90
-
91
- def test_stat_base_type
92
- assert_respond_to(@stat, :base_type)
93
- assert_kind_of(String, @stat.base_type)
94
- end
95
-
96
- def test_mount_point_singleton
97
- assert_respond_to(Filesystem, :mount_point)
98
- assert_nothing_raised{ Filesystem.mount_point(Dir.pwd) }
99
- assert_kind_of(String, Filesystem.mount_point(Dir.pwd))
100
- end
101
-
102
- def test_constants
103
- assert_not_nil(Filesystem::CASE_SENSITIVE_SEARCH)
104
- assert_not_nil(Filesystem::CASE_PRESERVED_NAMES)
105
- assert_not_nil(Filesystem::UNICODE_ON_DISK)
106
- assert_not_nil(Filesystem::PERSISTENT_ACLS)
107
- assert_not_nil(Filesystem::FILE_COMPRESSION)
108
- assert_not_nil(Filesystem::VOLUME_QUOTAS)
109
- assert_not_nil(Filesystem::SUPPORTS_SPARSE_FILES)
110
- assert_not_nil(Filesystem::SUPPORTS_REPARSE_POINTS)
111
- assert_not_nil(Filesystem::SUPPORTS_REMOTE_STORAGE)
112
- assert_not_nil(Filesystem::VOLUME_IS_COMPRESSED)
113
- assert_not_nil(Filesystem::SUPPORTS_OBJECT_IDS)
114
- assert_not_nil(Filesystem::SUPPORTS_ENCRYPTION)
115
- assert_not_nil(Filesystem::NAMED_STREAMS)
116
- assert_not_nil(Filesystem::READ_ONLY_VOLUME)
117
- end
118
-
119
- def test_stat_expected_errors
120
- assert_raises(ArgumentError){ Filesystem.stat }
121
- end
13
+ def setup
14
+ @dir = '/'
15
+ @stat = Filesystem.stat(@dir)
16
+ @mount = Filesystem.mounts[0]
17
+ @size = 58720256
18
+ @array = []
19
+ end
20
+
21
+ test "version number is set to the expected value" do
22
+ assert_equal('1.1.0', Filesystem::VERSION)
23
+ end
24
+
25
+ test "stat path works as expected" do
26
+ assert_respond_to(@stat, :path)
27
+ assert_equal("/", @stat.path)
28
+ end
29
+
30
+ test "stat block_size works as expected" do
31
+ assert_respond_to(@stat, :block_size)
32
+ assert_kind_of(Fixnum, @stat.block_size)
33
+ end
34
+
35
+ test "stat fragment_size works as expected" do
36
+ assert_respond_to(@stat, :fragment_size)
37
+ assert_nil(@stat.fragment_size)
38
+ end
39
+
40
+ test "stat blocks works as expected" do
41
+ assert_respond_to(@stat, :blocks)
42
+ assert_kind_of(Fixnum, @stat.blocks)
43
+ end
44
+
45
+ test "stat blocks_free works as expected" do
46
+ assert_respond_to(@stat, :blocks_free)
47
+ assert_kind_of(Fixnum, @stat.blocks_free)
48
+ end
49
+
50
+ test "stat blocks_available works as expected" do
51
+ assert_respond_to(@stat, :blocks_available)
52
+ assert_kind_of(Fixnum, @stat.blocks_available)
53
+ end
54
+
55
+ test "stat files works as expected" do
56
+ assert_respond_to(@stat, :files)
57
+ assert_nil(@stat.files)
58
+ end
59
+
60
+ test "stat inodes is an alias for files" do
61
+ assert_alias_method(@stat, :inodes, :files)
62
+ end
63
+
64
+ test "stat files_free works as expected" do
65
+ assert_respond_to(@stat, :files_free)
66
+ assert_nil(@stat.files_free)
67
+ end
68
+
69
+ test "stat inodes_free is an alias for files_free" do
70
+ assert_respond_to(@stat, :inodes_free)
71
+ end
72
+
73
+ test "stat files available works as expected" do
74
+ assert_respond_to(@stat, :files_available)
75
+ assert_nil(@stat.files_available)
76
+ end
77
+
78
+ test "stat inodes_available is an alias for files_available" do
79
+ assert_alias_method(@stat, :inodes_available, :files_available)
80
+ end
81
+
82
+ test "stat filesystem_id works as expected" do
83
+ assert_respond_to(@stat, :filesystem_id)
84
+ assert_kind_of(Integer, @stat.filesystem_id)
85
+ end
86
+
87
+ test "stat flags works as expected" do
88
+ assert_respond_to(@stat, :flags)
89
+ assert_kind_of(Fixnum, @stat.flags)
90
+ end
91
+
92
+ test "stat name_max works as expected" do
93
+ assert_respond_to(@stat, :name_max)
94
+ assert_kind_of(Fixnum, @stat.name_max)
95
+ end
96
+
97
+ test "stat base_type works as expected" do
98
+ assert_respond_to(@stat, :base_type)
99
+ assert_kind_of(String, @stat.base_type)
100
+ end
101
+
102
+ test "mount_point singleton method basic functionality" do
103
+ assert_respond_to(Filesystem, :mount_point)
104
+ assert_nothing_raised{ Filesystem.mount_point(Dir.pwd) }
105
+ assert_kind_of(String, Filesystem.mount_point(Dir.pwd))
106
+ end
107
+
108
+ test "mount_point singleton method returns expected value" do
109
+ assert_equal("C:\\", Filesystem.mount_point("C:\\Users\\foo"))
110
+ assert_equal("\\\\foo\\bar", Filesystem.mount_point("//foo/bar/baz"))
111
+ end
112
+
113
+ test "filesystem constants are defined" do
114
+ assert_not_nil(Filesystem::CASE_SENSITIVE_SEARCH)
115
+ assert_not_nil(Filesystem::CASE_PRESERVED_NAMES)
116
+ assert_not_nil(Filesystem::UNICODE_ON_DISK)
117
+ assert_not_nil(Filesystem::PERSISTENT_ACLS)
118
+ assert_not_nil(Filesystem::FILE_COMPRESSION)
119
+ assert_not_nil(Filesystem::VOLUME_QUOTAS)
120
+ assert_not_nil(Filesystem::SUPPORTS_SPARSE_FILES)
121
+ assert_not_nil(Filesystem::SUPPORTS_REPARSE_POINTS)
122
+ assert_not_nil(Filesystem::SUPPORTS_REMOTE_STORAGE)
123
+ assert_not_nil(Filesystem::VOLUME_IS_COMPRESSED)
124
+ assert_not_nil(Filesystem::SUPPORTS_OBJECT_IDS)
125
+ assert_not_nil(Filesystem::SUPPORTS_ENCRYPTION)
126
+ assert_not_nil(Filesystem::NAMED_STREAMS)
127
+ assert_not_nil(Filesystem::READ_ONLY_VOLUME)
128
+ end
129
+
130
+ test "stat singleton method requires a single argument" do
131
+ assert_raise(ArgumentError){ Filesystem.stat }
132
+ assert_raise(ArgumentError){ Filesystem.stat(Dir.pwd, Dir.pwd) }
133
+ end
122
134
 
123
135
  # Filesystem.mounts
124
136
 
125
- def test_mounts_constructor_basic
126
- assert_respond_to(Filesystem, :mounts)
127
- assert_nothing_raised{ Filesystem.mounts }
128
- assert_nothing_raised{ Filesystem.mounts{} }
129
- end
130
-
131
- def test_mounts
132
- assert_kind_of(Array, Filesystem.mounts)
133
- assert_kind_of(Filesystem::Mount, Filesystem.mounts[0])
134
- end
135
-
136
- def test_mounts_block_form
137
- assert_nil(Filesystem.mounts{})
138
- assert_nothing_raised{ Filesystem.mounts{ |mt| @array << mt }}
139
- assert_kind_of(Filesystem::Mount, @array[0])
140
- end
141
-
142
- def test_mount_name
143
- assert_respond_to(@mount, :name)
144
- assert_kind_of(String, @mount.name)
145
- end
146
-
147
- def test_mount_time
148
- assert_respond_to(@mount, :mount_time)
149
- assert_kind_of(Time, @mount.mount_time)
150
- end
151
-
152
- def test_mount_type
153
- assert_respond_to(@mount, :mount_type)
154
- assert_kind_of(String, @mount.mount_type)
155
- end
156
-
157
- def test_mount_point
158
- assert_respond_to(@mount, :mount_point)
159
- assert_kind_of(String, @mount.mount_point)
160
- end
161
-
162
- def test_mount_options
163
- assert_respond_to(@mount, :options)
164
- assert_kind_of(String, @mount.options)
165
- end
166
-
167
- def test_pass_number
168
- assert_respond_to(@mount, :pass_number)
169
- assert_nil(@mount.pass_number)
170
- end
171
-
172
- def test_frequency
173
- assert_respond_to(@mount, :frequency)
174
- assert_nil(@mount.frequency)
175
- end
176
-
177
- def test_mounts_expected_errors
178
- assert_raise(ArgumentError){ Filesystem.mounts("C:\\") }
179
- end
180
-
181
- def test_fixnum_methods
182
- assert_respond_to(@size, :to_kb)
183
- assert_respond_to(@size, :to_mb)
184
- assert_respond_to(@size, :to_gb)
185
-
186
- assert_equal(57344, @size.to_kb)
187
- assert_equal(56, @size.to_mb)
188
- assert_equal(0, @size.to_gb)
189
- end
190
-
191
- def teardown
192
- @array = nil
193
- @dir = nil
194
- @stat = nil
195
- @size = nil
196
- @mount = nil
197
- end
137
+ test "mounts singleton method basic functionality" do
138
+ assert_respond_to(Filesystem, :mounts)
139
+ assert_nothing_raised{ Filesystem.mounts }
140
+ assert_nothing_raised{ Filesystem.mounts{} }
141
+ end
142
+
143
+ test "mounts singleton method returns the expected value" do
144
+ assert_kind_of(Array, Filesystem.mounts)
145
+ assert_kind_of(Filesystem::Mount, Filesystem.mounts[0])
146
+ end
147
+
148
+ test "mounts singleton method works as expected when a block is provided" do
149
+ assert_nil(Filesystem.mounts{})
150
+ assert_nothing_raised{ Filesystem.mounts{ |mt| @array << mt }}
151
+ assert_kind_of(Filesystem::Mount, @array[0])
152
+ end
153
+
154
+ test "mount name works as expected" do
155
+ assert_respond_to(@mount, :name)
156
+ assert_kind_of(String, @mount.name)
157
+ end
158
+
159
+ test "mount time works as expected" do
160
+ assert_respond_to(@mount, :mount_time)
161
+ assert_kind_of(Time, @mount.mount_time)
162
+ end
163
+
164
+ test "mount type works as expected" do
165
+ assert_respond_to(@mount, :mount_type)
166
+ assert_kind_of(String, @mount.mount_type)
167
+ end
168
+
169
+ test "mount point works as expected" do
170
+ assert_respond_to(@mount, :mount_point)
171
+ assert_kind_of(String, @mount.mount_point)
172
+ end
173
+
174
+ test "mount options works as expected" do
175
+ assert_respond_to(@mount, :options)
176
+ assert_kind_of(String, @mount.options)
177
+ end
178
+
179
+ test "mount pass_number works as expected" do
180
+ assert_respond_to(@mount, :pass_number)
181
+ assert_nil(@mount.pass_number)
182
+ end
183
+
184
+ test "mount frequency works as expected" do
185
+ assert_respond_to(@mount, :frequency)
186
+ assert_nil(@mount.frequency)
187
+ end
188
+
189
+ test "mounts singleton method does not accept any arguments" do
190
+ assert_raise(ArgumentError){ Filesystem.mounts("C:\\") }
191
+ end
192
+
193
+ test "custom Fixnum#to_kb method works as expected" do
194
+ assert_respond_to(@size, :to_kb)
195
+ assert_equal(57344, @size.to_kb)
196
+ end
197
+
198
+ test "custom Fixnum#to_mb method works as expected" do
199
+ assert_respond_to(@size, :to_mb)
200
+ assert_equal(56, @size.to_mb)
201
+ end
202
+
203
+ test "custom Fixnum#to_gb method works as expected" do
204
+ assert_respond_to(@size, :to_gb)
205
+ assert_equal(0, @size.to_gb)
206
+ end
207
+
208
+ # FFI
209
+
210
+ test "internal ffi functions are not public" do
211
+ assert_false(Filesystem.methods.include?(:GetVolumeInformationA))
212
+ assert_false(Filesystem.instance_methods.include?(:GetVolumeInformationA))
213
+ end
214
+
215
+ def teardown
216
+ @array = nil
217
+ @dir = nil
218
+ @stat = nil
219
+ @size = nil
220
+ @mount = nil
221
+ end
198
222
  end