sys-filesystem 0.3.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,35 @@
1
+ == 0.3.2 - 29-Dec-2009
2
+ * Source has been moved to github.
3
+ * Added the 'gem' task and removed build logic from the gemspec.
4
+ * Updated the install task.
5
+ * Minor correction to the manifest.
6
+ * Removed some junk build files that were inadvertently included in
7
+ the last gem.
8
+
9
+ == 0.3.1 - 5-Aug-2009
10
+ * Now compatible with Ruby 1.9.x.
11
+ * Changed license to Artistic 2.0
12
+ * Updated the gemspec, including the explicit addition of a license and
13
+ test-unit as a development dependency, as well as an updated description.
14
+
15
+ == 0.3.0 - 26-Feb-2009
16
+ * Added support for OS X and FreeBSD thanks to an awesome patch by Nobuyoshi
17
+ Miyokawa.
18
+ * Added the Filesystem.mount_point method that takes a file and returns
19
+ the mount point it's sitting on.
20
+
21
+ == 0.2.0 - 30-Dec-2008
22
+ * Added the Filesystem.mounts method for iterating over mount or volume
23
+ information.
24
+
25
+ == 0.1.1 - 28-Mar-2007
26
+ * Bug fix for BSD flavors. Thanks go to Jeremy Kemper and Ole Christian
27
+ Rynning for the spot.
28
+ * Bug fix for OS X (along the same lines as the BSD fix). Thanks go to
29
+ Aurelian Dehay for the spot.
30
+ * Some Rdoc improvements for the C extension.
31
+ * Tweaks to the gemspec.
32
+ * Added synopsis to the README.
33
+
34
+ == 0.1.0 - 17-Nov-2006
35
+ * Initial release. Alpha. Code is stable, but API is not.
data/MANIFEST ADDED
@@ -0,0 +1,13 @@
1
+ * MANIFEST
2
+ * CHANGES
3
+ * Rakefile
4
+ * README
5
+ * sys-fileystem.gemspec
6
+ * examples/example_stat.rb
7
+ * doc/sys-filesystem.txt
8
+ * ext/extconf.rb
9
+ * ext/sys/filesystem.c
10
+ * lib/sys/filesystem.rb
11
+ * test/test_sys_filesystem.rb
12
+ * test/test_sys_filesystem_unix
13
+ * test/test_sys_filesystem_windows
data/README ADDED
@@ -0,0 +1,82 @@
1
+ = Description
2
+ A Ruby interface for getting file system information.
3
+
4
+ = Prerequisites
5
+ === MS Windows
6
+ * windows-pr, 0.9.8 or later.
7
+
8
+ = Installation
9
+ rake test (optional)
10
+ rake install (non-gem) OR rake install_gem (gem)
11
+
12
+ = Synopsis
13
+ require 'sys/filesystem'
14
+ include Sys
15
+
16
+ # Display information about a particular filesystem.
17
+ p Filesystem.stat('/')
18
+
19
+ # Sample output
20
+
21
+ #<Sys::Filesystem::Stat:0x517440
22
+ @base_type = "ufs",
23
+ @flags = 4,
24
+ @files_available = 3817457,
25
+ @block_size = 8192,
26
+ @blocks_available = 19957633,
27
+ @blocks = 34349612,
28
+ @name_max = 255,
29
+ @path = "/",
30
+ @filesystem_id = 35651592,
31
+ @files = 4135040,
32
+ @fragment_size = 1024,
33
+ @files_free = 3817457,
34
+ @blocks_free = 20301129
35
+ >
36
+
37
+ # Describe all mount points on the system
38
+ Filesystem.mounts{ |mount| p mount }
39
+
40
+ # Find the mount point of any particular file
41
+ puts Filesystem.mount_point('/home/djberge/some_file.txt') => '/home'
42
+
43
+ = Notes
44
+ === MS Windows
45
+ This is a pure Ruby implementation using the windows-pr library, which in
46
+ turn wraps native Windows functions.
47
+ === UNIX
48
+ This is a C extension that wraps statvfs, etc.
49
+
50
+ = Sample code
51
+ Run 'rake example' if you want to see a basic sample run. The actual code
52
+ is 'example_stat.rb' in the 'examples' directory. Modify it as you see fit.
53
+
54
+ = Known Bugs
55
+ None that I'm aware of. Please report bugs on the project page at
56
+ http://www.rubyforge.org/projects/sysutils.
57
+
58
+ = Future Plans
59
+ Suggestions welcome.
60
+
61
+ = Acknowledgements
62
+ Mike Hall, for ideas and code that I borrowed from his 'filesystem'
63
+ library.
64
+
65
+ Park Heesob, for implementation and API ideas for the MS Windows version.
66
+
67
+ Nobuyoshi Miyokawa, for adding FreeBSD and OS X support.
68
+
69
+ = License
70
+ Artistic 2.0
71
+
72
+ = Copyright
73
+ (C) 2003-2009 Daniel J. Berger
74
+ All Rights Reserved
75
+
76
+ = Warranty
77
+ This library is provided "as is" and without any express or
78
+ implied warranties, including, without limitation, the implied
79
+ warranties of merchantability and fitness for a particular purpose.
80
+
81
+ = Author
82
+ Daniel J. Berger
data/Rakefile ADDED
@@ -0,0 +1,95 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ include Config
5
+
6
+ desc "Clean the build files for the sys-filesystem source for UNIX systems"
7
+ task :clean do |task|
8
+ Dir.chdir('examples') do
9
+ FileUtils.rm_rf('sys') if File.exists?('sys')
10
+ end
11
+
12
+ unless Config::CONFIG['host_os'] =~ /mswin32|mingw|cygwin|windows|dos/i
13
+ file = 'sys/filesystem.' + CONFIG['DLEXT']
14
+ Dir.chdir('ext') do
15
+ sh 'make distclean' rescue nil
16
+ rm file if File.exists?(file)
17
+ rm_rf 'conftest.dSYM' if File.exists?('conftest.dSYM') # OS X weirdness
18
+ end
19
+ end
20
+ end
21
+
22
+ desc "Build the sys-filesystem library on UNIX systems (but don't install it)"
23
+ task :build => [:clean] do
24
+ unless Config::CONFIG['host_os'] =~ /mswin32|mingw|cygwin|windows|dos/i
25
+ file = 'filesystem.' + CONFIG['DLEXT']
26
+ Dir.chdir('ext') do
27
+ ruby 'extconf.rb'
28
+ sh 'make'
29
+ mv file, 'sys'
30
+ end
31
+ end
32
+ end
33
+
34
+ desc "Install the sys-filesystem library"
35
+ task :install do
36
+ unless Config::CONFIG['host_os'] =~ /mswin32|mingw|cygwin|windows|dos/i
37
+ install_dir = File.join(CONFIG['sitelibdir'], 'sys')
38
+ Dir.mkdir(install_dir) unless File.exists?(install_dir)
39
+ FileUtils.cp('lib/sys/filesystem.rb', install_dir, :verbose => true)
40
+ else
41
+ task :install => :build
42
+ Dir.chdir('ext') do
43
+ sh 'make install'
44
+ end
45
+ end
46
+ end
47
+
48
+ desc "Run the test suite"
49
+ Rake::TestTask.new("test") do |t|
50
+ unless Config::CONFIG['host_os'] =~ /mswin32|mingw|cygwin|windows|dos/i
51
+ task :test => :build
52
+ t.libs << 'ext'
53
+ t.libs.delete('lib')
54
+ end
55
+
56
+ t.warning = true
57
+ t.verbose = true
58
+ t.test_files = FileList['test/test_sys_filesystem.rb']
59
+ end
60
+
61
+ task :test do
62
+ Rake.application[:clean].execute
63
+ end
64
+
65
+ desc "Run the example program"
66
+ task :example => [:build] do |t|
67
+ Dir.chdir('examples') do
68
+ Dir.mkdir('sys') unless File.exists?('sys')
69
+ end
70
+
71
+ FileUtils.cp('ext/sys/filesystem.' + Config::CONFIG['DLEXT'], 'examples/sys')
72
+
73
+ Dir.chdir('examples') do
74
+ ruby 'example_stat.rb'
75
+ end
76
+ end
77
+
78
+ desc "Build a gem"
79
+ task :gem => [:clean] do |t|
80
+ spec = eval(IO.read('sys-filesystem.gemspec'))
81
+
82
+ if Config::CONFIG['host_os'] =~ /mswin32|mingw|cygwin|windows|dos/i
83
+ spec.required_ruby_version = '>= 1.8.2'
84
+ spec.files -= Dir['ext/**/*']
85
+ spec.platform = Gem::Platform::CURRENT
86
+ spec.add_dependency('windows-pr', '>= 1.0.5')
87
+ else
88
+ spec.required_ruby_version = '>= 1.8.0'
89
+ spec.extensions = ['ext/extconf.rb']
90
+ spec.files -= Dir['lib/**/*']
91
+ spec.extra_rdoc_files << 'ext/sys/filesystem.c'
92
+ end
93
+
94
+ Gem::Builder.new(spec).build
95
+ end
@@ -0,0 +1,24 @@
1
+ ######################################################################
2
+ # example_stat.rb
3
+ #
4
+ # Example program that demonstrates the FileSystem.stat method.
5
+ # Use the 'rake example' task to run this program.
6
+ ######################################################################
7
+ require 'sys/filesystem'
8
+ include Sys
9
+
10
+ p Filesystem::VERSION
11
+
12
+ stat = Filesystem.stat("/")
13
+ puts "Path: " + stat.path
14
+ puts "Block size: " + stat.block_size.to_s
15
+ puts "Fragment size: " + stat.fragment_size.to_s
16
+ puts "Blocks free: " + stat.blocks_free.to_s
17
+ puts "Blocks available: " + stat.blocks_available.to_s
18
+ puts "Files/Inodes: " + stat.files.to_s
19
+ puts "Files/Inodes free: " + stat.files_free.to_s
20
+ puts "Files/Inodes available: " + stat.files_available.to_s
21
+ puts "File system id: " + stat.filesystem_id.to_s
22
+ puts "Base type: " + stat.base_type if stat.base_type
23
+ puts "Flags: " + stat.flags.to_s
24
+ puts "Name max: " + stat.name_max.to_s
@@ -0,0 +1,408 @@
1
+ require 'windows/error'
2
+ require 'windows/path'
3
+ require 'windows/filesystem'
4
+ require 'windows/volume'
5
+ require 'windows/handle'
6
+ require 'windows/limits'
7
+ require 'socket'
8
+ require 'win32ole'
9
+ require 'date'
10
+ require 'time'
11
+
12
+ # The Sys module serves as a namespace only.
13
+ module Sys
14
+
15
+ # The Filesystem class encapsulates information about your filesystem.
16
+ class Filesystem
17
+ include Windows::Error
18
+ include Windows::Handle
19
+ include Windows::Limits
20
+
21
+ extend Windows::Error
22
+ extend Windows::FileSystem
23
+ extend Windows::Volume
24
+ extend Windows::Path
25
+
26
+ # Error typically raised if any of the Sys::Filesystem methods fail.
27
+ class Error < StandardError; end
28
+
29
+ CASE_SENSITIVE_SEARCH = 0x00000001
30
+ CASE_PRESERVED_NAMES = 0x00000002
31
+ UNICODE_ON_DISK = 0x00000004
32
+ PERSISTENT_ACLS = 0x00000008
33
+ FILE_COMPRESSION = 0x00000010
34
+ VOLUME_QUOTAS = 0x00000020
35
+ SUPPORTS_SPARSE_FILES = 0x00000040
36
+ SUPPORTS_REPARSE_POINTS = 0x00000080
37
+ SUPPORTS_REMOTE_STORAGE = 0x00000100
38
+ VOLUME_IS_COMPRESSED = 0x00008000
39
+ SUPPORTS_OBJECT_IDS = 0x00010000
40
+ SUPPORTS_ENCRYPTION = 0x00020000
41
+ NAMED_STREAMS = 0x00040000
42
+ READ_ONLY_VOLUME = 0x00080000
43
+
44
+ # The version of the sys-filesystem library.
45
+ VERSION = '0.3.2'
46
+
47
+ class Mount
48
+ # The name of the volume. This is the device mapping.
49
+ attr_reader :name
50
+
51
+ # The last time the volume was mounted. For MS Windows this equates
52
+ # to your system's boot time.
53
+ attr_reader :mount_time
54
+
55
+ # The type of mount, e.g. NTFS, UDF, etc.
56
+ attr_reader :mount_type
57
+
58
+ # The volume mount point, e.g. 'C:\'
59
+ attr_reader :mount_point
60
+
61
+ # Various comma separated options that reflect the volume's features
62
+ attr_reader :options
63
+
64
+ # Always nil on MS Windows. Provided for interface compatibility only.
65
+ attr_reader :pass_number
66
+
67
+ # Always nil on MS Windows. Provided for interface compatibility only.
68
+ attr_reader :frequency
69
+
70
+ alias fsname name
71
+ alias dir mount_point
72
+ alias opts options
73
+ alias passno pass_number
74
+ alias freq frequency
75
+ end
76
+
77
+ class Stat
78
+ # The path of the file system.
79
+ attr_reader :path
80
+
81
+ # The file system block size. MS Windows typically defaults to 4096.
82
+ attr_reader :block_size
83
+
84
+ # Fragment size. Meaningless at the moment.
85
+ attr_reader :fragment_size
86
+
87
+ # The total number of blocks available (used or unused) on the file
88
+ # system.
89
+ attr_reader :blocks
90
+
91
+ # The total number of unused blocks.
92
+ attr_reader :blocks_free
93
+
94
+ # The total number of unused blocks available to unprivileged
95
+ # processes. Identical to +blocks+ at the moment.
96
+ attr_reader :blocks_available
97
+
98
+ # Total number of files/inodes that can be created on the file system.
99
+ # This attribute is always nil on MS Windows.
100
+ attr_reader :files
101
+
102
+ # Total number of free files/inodes that can be created on the file
103
+ # system. This attribute is always nil on MS Windows.
104
+ attr_reader :files_free
105
+
106
+ # Total number of available files/inodes for unprivileged processes
107
+ # that can be created on the file system. This attribute is always
108
+ # nil on MS Windows.
109
+ attr_reader :files_available
110
+
111
+ # The file system volume id.
112
+ attr_reader :filesystem_id
113
+
114
+ # A bit mask of file system flags.
115
+ attr_reader :flags
116
+
117
+ # The maximum length of a file name permitted on the file system.
118
+ attr_reader :name_max
119
+
120
+ # The file system type, e.g. NTFS, FAT, etc.
121
+ attr_reader :base_type
122
+
123
+ alias inodes files
124
+ alias inodes_free files_free
125
+ alias inodes_available files_available
126
+ end
127
+
128
+ # Yields a Filesystem::Mount object for each volume on your system in
129
+ # block form. Returns an array of Filesystem::Mount objects in non-block
130
+ # form.
131
+ #
132
+ # Example:
133
+ #
134
+ # Sys::Filesystem.mounts{ |mount|
135
+ # p mt.name # => \\Device\\HarddiskVolume1
136
+ # p mt.mount_point # => C:\
137
+ # p mt.mount_time # => Thu Dec 18 20:12:08 -0700 2008
138
+ # p mt.mount_type # => NTFS
139
+ # p mt.options # => casepres,casesens,ro,unicode
140
+ # p mt.pass_number # => nil
141
+ # p mt.dump_freq # => nil
142
+ # }
143
+ #
144
+ # This method is a bit of a fudge for MS Windows in the name of interface
145
+ # compatibility because this method deals with volumes, not actual mount
146
+ # points. But, I believe it provides the sort of information many users
147
+ # want at a glance.
148
+ #
149
+ # The possible values for the +options+ and their meanings are as follows:
150
+ #
151
+ # casepres => The filesystem preserves the case of file names when it places a name on disk.
152
+ # casesens => The filesystem supports case-sensitive file names.
153
+ # compression => The filesystem supports file-based compression.
154
+ # namedstreams => The filesystem supports named streams.
155
+ # pacls => The filesystem preserves and enforces access control lists.
156
+ # ro => The filesystem is read-only.
157
+ # encryption => The filesystem supports the Encrypted File System (EFS).
158
+ # objids => The filesystem supports object identifiers.
159
+ # rpoints => The filesystem supports reparse points.
160
+ # sparse => The filesystem supports sparse files.
161
+ # unicode => The filesystem supports Unicode in file names as they appear on disk.
162
+ # compressed => The filesystem is compressed.
163
+ #
164
+ def self.mounts
165
+ buffer = 0.chr * MAXPATH
166
+ length = GetLogicalDriveStrings(buffer.size, buffer)
167
+
168
+ if length == 0
169
+ raise Error, get_last_error
170
+ end
171
+
172
+ mounts = block_given? ? nil : []
173
+
174
+ # Try again if it fails
175
+ if length > buffer.size
176
+ buffer = 0.chr * length
177
+ if GetLogicalDriveStrings(buffer.size, buffer) == 0
178
+ raise Error, get_last_error
179
+ end
180
+ end
181
+
182
+ boot_time = get_boot_time
183
+
184
+ drives = buffer.strip.split("\0")
185
+
186
+ drives.each{ |drive|
187
+ mount = Mount.new
188
+ volume = 0.chr * MAXPATH
189
+ fsname = 0.chr * MAXPATH
190
+
191
+ mount.instance_variable_set(:@mount_point, drive)
192
+ mount.instance_variable_set(:@mount_time, boot_time)
193
+
194
+ volume_serial_number = [0].pack('L')
195
+ max_component_length = [0].pack('L')
196
+ filesystem_flags = [0].pack('L')
197
+
198
+ bool = GetVolumeInformation(
199
+ drive,
200
+ volume,
201
+ volume.size,
202
+ volume_serial_number,
203
+ max_component_length,
204
+ filesystem_flags,
205
+ fsname,
206
+ fsname.size
207
+ )
208
+
209
+ # Skip unmounted floppies or cd-roms
210
+ unless bool
211
+ errnum = GetLastError()
212
+ if errnum == ERROR_NOT_READY
213
+ next
214
+ else
215
+ raise Error, get_last_error(errnum)
216
+ end
217
+ end
218
+
219
+ filesystem_flags = filesystem_flags.unpack('L')[0]
220
+
221
+ name = 0.chr * MAXPATH
222
+
223
+ if QueryDosDevice(drive[0,2], name, name.size) == 0
224
+ raise Error, get_last_error
225
+ end
226
+
227
+ mount.instance_variable_set(:@name, name.strip)
228
+ mount.instance_variable_set(:@mount_type, fsname.strip)
229
+ mount.instance_variable_set(:@options, get_options(filesystem_flags))
230
+
231
+ if block_given?
232
+ yield mount
233
+ else
234
+ mounts << mount
235
+ end
236
+ }
237
+
238
+ mounts # Nil if the block form was used.
239
+ end
240
+
241
+ # Returns the mount point for the given +file+. For MS Windows this
242
+ # means the root of the path.
243
+ #
244
+ # Example:
245
+ #
246
+ # File.mount_point("C:\\Documents and Settings") # => "C:\\'
247
+ #
248
+ def self.mount_point(file)
249
+ file = file.tr(File::SEPARATOR, File::ALT_SEPARATOR)
250
+ PathStripToRoot(file)
251
+ file[/^[^\0]*/]
252
+ end
253
+
254
+ # Returns a Filesystem::Stat object that contains information about the
255
+ # +path+ file system.
256
+ #
257
+ # Examples:
258
+ #
259
+ # File.stat("C:\\")
260
+ # File.stat("C:\\Documents and Settings\\some_user")
261
+ #
262
+ def self.stat(path)
263
+ bytes_avail = [0].pack('Q')
264
+ bytes_free = [0].pack('Q')
265
+ total_bytes = [0].pack('Q')
266
+
267
+ unless GetDiskFreeSpaceEx(path, bytes_avail, total_bytes, bytes_free)
268
+ raise Error, get_last_error
269
+ end
270
+
271
+ bytes_avail = bytes_avail.unpack('Q').first
272
+ bytes_free = bytes_free.unpack('Q').first
273
+ total_bytes = total_bytes.unpack('Q').first
274
+
275
+ sectors = [0].pack('Q')
276
+ bytes = [0].pack('Q')
277
+ free = [0].pack('Q')
278
+ total = [0].pack('Q')
279
+
280
+ unless GetDiskFreeSpace(path, sectors, bytes, free, total)
281
+ raise Error, get_last_error
282
+ end
283
+
284
+ sectors = sectors.unpack('Q').first
285
+ bytes = bytes.unpack('Q').first
286
+ free = free.unpack('Q').first
287
+ total = total.unpack('Q').first
288
+
289
+ block_size = sectors * bytes
290
+ blocks_avail = total_bytes / block_size
291
+ blocks_free = bytes_free / block_size
292
+
293
+ vol_name = 0.chr * 260
294
+ base_type = 0.chr * 260
295
+ vol_serial = [0].pack('L')
296
+ name_max = [0].pack('L')
297
+ flags = [0].pack('L')
298
+
299
+ bool = GetVolumeInformation(
300
+ path,
301
+ vol_name,
302
+ vol_name.size,
303
+ vol_serial,
304
+ name_max,
305
+ flags,
306
+ base_type,
307
+ base_type.size
308
+ )
309
+
310
+ unless bool
311
+ raise Error, get_last_error
312
+ end
313
+
314
+ vol_serial = vol_serial.unpack('L').first
315
+ name_max = name_max.unpack('L').first
316
+ flags = flags.unpack('L').first
317
+ base_type = base_type[/^[^\0]*/]
318
+
319
+ stat_obj = Stat.new
320
+ stat_obj.instance_variable_set(:@path, path)
321
+ stat_obj.instance_variable_set(:@block_size, block_size)
322
+ stat_obj.instance_variable_set(:@blocks, blocks_avail)
323
+ stat_obj.instance_variable_set(:@blocks_available, blocks_avail)
324
+ stat_obj.instance_variable_set(:@blocks_free, blocks_free)
325
+ stat_obj.instance_variable_set(:@name_max, name_max)
326
+ stat_obj.instance_variable_set(:@base_type, base_type)
327
+ stat_obj.instance_variable_set(:@flags, flags)
328
+ stat_obj.instance_variable_set(:@filesystem_id, vol_serial)
329
+
330
+ stat_obj.freeze # Read-only object
331
+ end
332
+
333
+ private
334
+
335
+ # This method is used to get the boot time of the system, which is used
336
+ # for the mount_time attribute within the File.mounts method.
337
+ #
338
+ def self.get_boot_time
339
+ host = Socket.gethostname
340
+ cs = "winmgmts://#{host}/root/cimv2"
341
+ begin
342
+ wmi = WIN32OLE.connect(cs)
343
+ rescue WIN32OLERuntimeError => e
344
+ raise Error, e
345
+ else
346
+ query = 'select LastBootupTime from Win32_OperatingSystem'
347
+ results = wmi.ExecQuery(query)
348
+ results.each{ |ole|
349
+ time_array = Time.parse(ole.LastBootupTime.split('.').first)
350
+ return Time.mktime(*time_array)
351
+ }
352
+ end
353
+ end
354
+
355
+ # Private method that converts filesystem flags into a comma separated
356
+ # list of strings. The presentation is meant as a rough analogue to the
357
+ # way options are presented for Unix filesystems.
358
+ #
359
+ def self.get_options(flags)
360
+ str = ""
361
+ str << " casepres" if CASE_PRESERVED_NAMES & flags > 0
362
+ str << " casesens" if CASE_SENSITIVE_SEARCH & flags > 0
363
+ str << " compression" if FILE_COMPRESSION & flags > 0
364
+ str << " namedstreams" if NAMED_STREAMS & flags > 0
365
+ str << " pacls" if PERSISTENT_ACLS & flags > 0
366
+ str << " ro" if READ_ONLY_VOLUME & flags > 0
367
+ str << " encryption" if SUPPORTS_ENCRYPTION & flags > 0
368
+ str << " objids" if SUPPORTS_OBJECT_IDS & flags > 0
369
+ str << " rpoints" if SUPPORTS_REPARSE_POINTS & flags > 0
370
+ str << " sparse" if SUPPORTS_SPARSE_FILES & flags > 0
371
+ str << " unicode" if UNICODE_ON_DISK & flags > 0
372
+ str << " compressed" if VOLUME_IS_COMPRESSED & flags > 0
373
+
374
+ str.tr!(' ', ',')
375
+ str = str[1..-1] # Ignore the first comma
376
+ str
377
+ end
378
+
379
+ end
380
+ end
381
+
382
+ # Some convenient methods for converting bytes to kb, mb, and gb.
383
+ #
384
+ class Fixnum
385
+ # call-seq:
386
+ # <tt>fix</tt>.to_kb
387
+ #
388
+ # Returns +fix+ in terms of kilobytes.
389
+ def to_kb
390
+ self / 1024
391
+ end
392
+
393
+ # call-seq:
394
+ # <tt>fix</tt>.to_mb
395
+ #
396
+ # Returns +fix+ in terms of megabytes.
397
+ def to_mb
398
+ self / 1048576
399
+ end
400
+
401
+ # call-seq:
402
+ # <tt>fix</tt>.to_gb
403
+ #
404
+ # Returns +fix+ in terms of gigabytes.
405
+ def to_gb
406
+ self / 1073741824
407
+ end
408
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+
3
+ spec = Gem::Specification.new do |gem|
4
+ gem.name = 'sys-filesystem'
5
+ gem.version = '0.3.2'
6
+ gem.author = 'Daniel J. Berger'
7
+ gem.email = 'djberg96@gmail.com'
8
+ gem.homepage = 'http://www.rubyforge.org/projects/sysutils'
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.summary = 'A Ruby interface for getting file system information.'
11
+ gem.test_file = 'test/test_sys_filesystem.rb'
12
+ gem.has_rdoc = true
13
+ gem.files = Dir['**/*'].reject{ |f| f.include?('git') }
14
+ gem.license = 'Artistic 2.0'
15
+
16
+ gem.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
17
+ gem.rubyforge_project = 'sysutils'
18
+
19
+ gem.add_development_dependency('test-unit', '>= 2.0.3')
20
+
21
+ gem.description = <<-EOF
22
+ The sys-filesystem library provides an interface for gathering filesystem
23
+ information, such as disk space and mount point data.
24
+ EOF
25
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.dirname(File.expand_path(__FILE__))
2
+
3
+ require 'rbconfig'
4
+
5
+ if Config::CONFIG['host_os'] =~ /mswin32|mingw|cygwin|windows|dos/i
6
+ require 'test_sys_filesystem_windows'
7
+ else
8
+ require 'test_sys_filesystem_unix'
9
+ end
@@ -0,0 +1,248 @@
1
+ ####################################################################
2
+ # test_sys_filesystem_unix.rb
3
+ #
4
+ # Test case for the Sys::Filesystem.stat method and related stuff.
5
+ # This test suite should be run via the 'rake test' task.
6
+ ####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'sys/filesystem'
12
+ include Sys
13
+
14
+ class TC_Sys_Filesystem_Unix < Test::Unit::TestCase
15
+ def self.startup
16
+ @@solaris = Config::CONFIG['host_os'] =~ /solaris/i
17
+ @@linux = Config::CONFIG['host_os'] =~ /linux/i
18
+ @@freebsd = Config::CONFIG['host_os'] =~ /freebsd/i
19
+ @@darwin = Config::CONFIG['host_os'] =~ /darwin/i
20
+ end
21
+
22
+ def setup
23
+ @dir = "/"
24
+ @stat = Filesystem.stat(@dir)
25
+ @mnt = Filesystem.mounts[0]
26
+ @size = 58720256
27
+ @array = []
28
+ end
29
+
30
+ def test_version
31
+ assert_equal('0.3.2', Filesystem::VERSION)
32
+ end
33
+
34
+ def test_stat_path
35
+ assert_respond_to(@stat, :path)
36
+ assert_equal("/", @stat.path)
37
+ end
38
+
39
+ def test_stat_block_size
40
+ assert_respond_to(@stat, :block_size)
41
+ assert_kind_of(Fixnum, @stat.block_size)
42
+ end
43
+
44
+ def test_stat_fragment_size
45
+ assert_respond_to(@stat, :fragment_size)
46
+ assert_kind_of(Fixnum, @stat.fragment_size)
47
+ end
48
+
49
+ def test_stat_blocks
50
+ assert_respond_to(@stat, :blocks)
51
+ assert_kind_of(Fixnum, @stat.blocks)
52
+ end
53
+
54
+ def test_stat_blocks_free
55
+ assert_respond_to(@stat, :blocks_free)
56
+ assert_kind_of(Fixnum, @stat.blocks_free)
57
+ end
58
+
59
+ def test_stat_blocks_available
60
+ assert_respond_to(@stat, :blocks_available)
61
+ assert_kind_of(Fixnum, @stat.blocks_available)
62
+ end
63
+
64
+ def test_stat_files
65
+ assert_respond_to(@stat, :files)
66
+ assert_kind_of(Fixnum, @stat.files)
67
+ end
68
+
69
+ def test_inodes_alias
70
+ assert_respond_to(@stat, :inodes)
71
+ assert_true(@stat.method(:inodes) == @stat.method(:files))
72
+ end
73
+
74
+ def test_stat_files_free
75
+ assert_respond_to(@stat, :files_free)
76
+ assert_kind_of(Fixnum, @stat.files_free)
77
+ end
78
+
79
+ def test_stat_inodes_free_alias
80
+ assert_respond_to(@stat, :inodes_free)
81
+ assert_true(@stat.method(:inodes_free) == @stat.method(:files_free))
82
+ end
83
+
84
+ def test_stat_files_available
85
+ assert_respond_to(@stat, :files_available)
86
+ assert_kind_of(Fixnum, @stat.files_available)
87
+ end
88
+
89
+ def test_stat_inodes_available_alias
90
+ assert_respond_to(@stat, :inodes_available)
91
+ assert_true(@stat.method(:inodes_available) == @stat.method(:files_available))
92
+ end
93
+
94
+ def test_stat_filesystem_id
95
+ assert_respond_to(@stat, :filesystem_id)
96
+ assert_kind_of(Integer, @stat.filesystem_id)
97
+ end
98
+
99
+ def test_stat_flags
100
+ assert_respond_to(@stat, :flags)
101
+ assert_kind_of(Fixnum, @stat.flags)
102
+ end
103
+
104
+ def test_stat_name_max
105
+ assert_respond_to(@stat, :name_max)
106
+ assert_kind_of(Fixnum, @stat.name_max)
107
+ end
108
+
109
+ def test_stat_base_type
110
+ omit_unless(@@solaris, "base_type test skipped except on Solaris")
111
+
112
+ assert_respond_to(@stat, :base_type)
113
+ assert_kind_of(String, @stat.base_type)
114
+ end
115
+
116
+ def test_stat_constants
117
+ assert_not_nil(Filesystem::Stat::RDONLY)
118
+ assert_not_nil(Filesystem::Stat::NOSUID)
119
+
120
+ omit_unless(@@solaris, "NOTRUNC test skipped except on Solaris")
121
+
122
+ assert_not_nil(Filesystem::Stat::NOTRUNC)
123
+ end
124
+
125
+ def test_stat_expected_errors
126
+ assert_raises(ArgumentError){ Filesystem.stat }
127
+ end
128
+
129
+ def test_fixnum_methods_basic
130
+ assert_respond_to(@size, :to_kb)
131
+ assert_respond_to(@size, :to_mb)
132
+ assert_respond_to(@size, :to_gb)
133
+ end
134
+
135
+ def test_to_kb
136
+ assert_equal(57344, @size.to_kb)
137
+ end
138
+
139
+ def test_to_mb
140
+ assert_equal(56, @size.to_mb)
141
+ end
142
+
143
+ def test_to_gb
144
+ assert_equal(0, @size.to_gb)
145
+ end
146
+
147
+ # Filesystem::Mount tests
148
+
149
+ def test_mounts_with_no_block
150
+ assert_nothing_raised{ @array = Filesystem.mounts }
151
+ assert_kind_of(Filesystem::Mount, @array[0])
152
+ end
153
+
154
+ def test_mounts_with_block
155
+ assert_nothing_raised{ Filesystem.mounts{ |m| @array << m } }
156
+ assert_kind_of(Filesystem::Mount, @array[0])
157
+ end
158
+
159
+ def test_mounts_high_iteration
160
+ assert_nothing_raised{ 1000.times{ @array = Filesystem.mounts } }
161
+ end
162
+
163
+ def test_mount_name
164
+ assert_respond_to(@mnt, :name)
165
+ assert_kind_of(String, @mnt.name)
166
+ end
167
+
168
+ def test_fsname_alias
169
+ assert_respond_to(@mnt, :fsname)
170
+ assert_true(@mnt.method(:fsname) == @mnt.method(:name))
171
+ end
172
+
173
+ def test_mount_point
174
+ assert_respond_to(@mnt, :mount_point)
175
+ assert_kind_of(String, @mnt.mount_point)
176
+ end
177
+
178
+ def test_dir_alias
179
+ assert_respond_to(@mnt, :dir)
180
+ assert_true(@mnt.method(:dir) == @mnt.method(:mount_point))
181
+ end
182
+
183
+ def test_mount_type
184
+ assert_respond_to(@mnt, :mount_type)
185
+ assert_kind_of(String, @mnt.mount_type)
186
+ end
187
+
188
+ def test_mount_options
189
+ assert_respond_to(@mnt, :options)
190
+ assert_kind_of(String, @mnt.options)
191
+ end
192
+
193
+ def test_opts_alias
194
+ assert_respond_to(@mnt, :opts)
195
+ assert_true(@mnt.method(:opts) == @mnt.method(:options))
196
+ end
197
+
198
+ def test_mount_time
199
+ assert_respond_to(@mnt, :mount_time)
200
+
201
+ if @@solaris
202
+ assert_kind_of(Time, @mnt.mount_time)
203
+ else
204
+ assert_nil(@mnt.mount_time)
205
+ end
206
+ end
207
+
208
+ def test_mount_dump_frequency
209
+ omit_if(@@solaris || @@freebsd || @@darwin, 'dump_frequency test skipped on this platform')
210
+ assert_respond_to(@mnt, :dump_frequency)
211
+ assert_kind_of(Fixnum, @mnt.dump_frequency)
212
+ end
213
+
214
+ def test_freq_alias
215
+ assert_respond_to(@mnt, :freq)
216
+ assert_true(@mnt.method(:freq) == @mnt.method(:dump_frequency))
217
+ end
218
+
219
+ def test_mount_pass_number
220
+ omit_if(@@solaris || @@freebsd || @@darwin, 'pass_number test skipped on this platform')
221
+ assert_respond_to(@mnt, :pass_number)
222
+ assert_kind_of(Fixnum, @mnt.pass_number)
223
+ end
224
+
225
+ def test_passno_alias
226
+ assert_respond_to(@mnt, :passno)
227
+ assert_true(@mnt.method(:passno) == @mnt.method(:pass_number))
228
+ end
229
+
230
+ def test_mount_point_singleton
231
+ assert_respond_to(Filesystem, :mount_point)
232
+ assert_nothing_raised{ Filesystem.mount_point(Dir.pwd) }
233
+ assert_kind_of(String, Filesystem.mount_point(Dir.pwd))
234
+ end
235
+
236
+ def teardown
237
+ @dir = nil
238
+ @stat = nil
239
+ @array = nil
240
+ end
241
+
242
+ def self.shutdown
243
+ @@solaris = nil
244
+ @@linux = nil
245
+ @@freebsd = nil
246
+ @@darwin = nil
247
+ end
248
+ end
@@ -0,0 +1,198 @@
1
+ ####################################################################
2
+ # test_sys_filesystem_windows.rb
3
+ #
4
+ # Test case for the Sys::Filesystem.stat method and related stuff.
5
+ # This should be run via the 'rake test' task.
6
+ ####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'sys/filesystem'
12
+ require 'rbconfig'
13
+ include Sys
14
+
15
+ 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('0.3.2', 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
122
+
123
+ # Filesystem.mounts
124
+
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
198
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sys-filesystem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: x86-mingw32
6
+ authors:
7
+ - Daniel J. Berger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-30 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: test-unit
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.0.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: windows-pr
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.5
34
+ version:
35
+ description: " The sys-filesystem library provides an interface for gathering filesystem\n information, such as disk space and mount point data.\n"
36
+ email: djberg96@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - CHANGES
43
+ - README
44
+ - MANIFEST
45
+ files:
46
+ - CHANGES
47
+ - examples/example_stat.rb
48
+ - lib/sys/filesystem.rb
49
+ - MANIFEST
50
+ - Rakefile
51
+ - README
52
+ - sys-filesystem.gemspec
53
+ - test/test_sys_filesystem.rb
54
+ - test/test_sys_filesystem_unix.rb
55
+ - test/test_sys_filesystem_windows.rb
56
+ has_rdoc: true
57
+ homepage: http://www.rubyforge.org/projects/sysutils
58
+ licenses:
59
+ - Artistic 2.0
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.2
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project: sysutils
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: A Ruby interface for getting file system information.
84
+ test_files:
85
+ - test/test_sys_filesystem.rb