sys-filesystem 1.1.1 → 1.5.1

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.
@@ -1,6 +1,6 @@
1
- require File.join(File.dirname(__FILE__), 'filesystem', 'constants')
2
- require File.join(File.dirname(__FILE__), 'filesystem', 'functions')
3
- require File.join(File.dirname(__FILE__), 'filesystem', 'helper')
1
+ require_relative 'filesystem/constants'
2
+ require_relative 'filesystem/functions'
3
+ require_relative 'filesystem/helper'
4
4
 
5
5
  require 'socket'
6
6
  require 'win32ole'
@@ -9,7 +9,6 @@ require 'time'
9
9
 
10
10
  # The Sys module serves as a namespace only.
11
11
  module Sys
12
-
13
12
  # The Filesystem class encapsulates information about your filesystem.
14
13
  class Filesystem
15
14
  include Sys::Filesystem::Constants
@@ -18,9 +17,9 @@ module Sys
18
17
  # Error typically raised if any of the Sys::Filesystem methods fail.
19
18
  class Error < StandardError; end
20
19
 
21
- # The version of the sys-filesystem library.
22
- VERSION = '1.1.1'
20
+ private_class_method :new
23
21
 
22
+ # Mount objects are returned by the Sys::Filesystem.mounts method.
24
23
  class Mount
25
24
  # The name of the volume. This is the device mapping.
26
25
  attr_reader :name
@@ -51,11 +50,12 @@ module Sys
51
50
  alias freq frequency
52
51
  end
53
52
 
53
+ # Stat objects are returned by the Sys::Filesystem.stat method.
54
54
  class Stat
55
55
  # The path of the file system.
56
56
  attr_reader :path
57
57
 
58
- # The file system block size. MS Windows typically defaults to 4096.
58
+ # The file system block size. MS Windows typically defaults to 4096.
59
59
  attr_reader :block_size
60
60
 
61
61
  # Fragment size. Meaningless at the moment.
@@ -68,8 +68,7 @@ module Sys
68
68
  # The total number of unused blocks.
69
69
  attr_reader :blocks_free
70
70
 
71
- # The total number of unused blocks available to unprivileged
72
- # processes. Identical to +blocks+ at the moment.
71
+ # The total number of unused blocks available to unprivileged processes.
73
72
  attr_reader :blocks_available
74
73
 
75
74
  # Total number of files/inodes that can be created on the file system.
@@ -77,7 +76,7 @@ module Sys
77
76
  attr_reader :files
78
77
 
79
78
  # Total number of free files/inodes that can be created on the file
80
- # system. This attribute is always nil on MS Windows.
79
+ # system. This attribute is always nil on MS Windows.
81
80
  attr_reader :files_free
82
81
 
83
82
  # Total number of available files/inodes for unprivileged processes
@@ -97,9 +96,30 @@ module Sys
97
96
  # The file system type, e.g. NTFS, FAT, etc.
98
97
  attr_reader :base_type
99
98
 
99
+ # The total amount of free space on the partition.
100
+ attr_reader :bytes_free
101
+
102
+ # The amount of free space available to unprivileged processes.
103
+ attr_reader :bytes_available
104
+
100
105
  alias inodes files
101
106
  alias inodes_free files_free
102
107
  alias inodes_available files_available
108
+
109
+ # Returns the total space on the partition.
110
+ def bytes_total
111
+ blocks * block_size
112
+ end
113
+
114
+ # Returns the total amount of used space on the partition.
115
+ def bytes_used
116
+ bytes_total - bytes_free
117
+ end
118
+
119
+ # Returns the percentage of the partition that has been used.
120
+ def percent_used
121
+ 100 - (100.0 * bytes_free.to_f / bytes_total.to_f)
122
+ end
103
123
  end
104
124
 
105
125
  # Yields a Filesystem::Mount object for each volume on your system in
@@ -141,6 +161,8 @@ module Sys
141
161
  #--
142
162
  # I couldn't really find a good reason to use the wide functions for this
143
163
  # method. If you have one, patches welcome.
164
+ #--
165
+ # rubocop:disable Metrics/BlockLength
144
166
  #
145
167
  def self.mounts
146
168
  # First call, get needed buffer size
@@ -164,7 +186,7 @@ module Sys
164
186
 
165
187
  boot_time = get_boot_time
166
188
 
167
- drives.each{ |drive|
189
+ drives.each do |drive|
168
190
  mount = Mount.new
169
191
  volume = FFI::MemoryPointer.new(:char, MAXPATH)
170
192
  fsname = FFI::MemoryPointer.new(:char, MAXPATH)
@@ -177,19 +199,19 @@ module Sys
177
199
  filesystem_flags = FFI::MemoryPointer.new(:ulong)
178
200
 
179
201
  bool = GetVolumeInformationA(
180
- drive,
181
- volume,
182
- volume.size,
183
- volume_serial_number,
184
- max_component_length,
185
- filesystem_flags,
186
- fsname,
187
- fsname.size
202
+ drive,
203
+ volume,
204
+ volume.size,
205
+ volume_serial_number,
206
+ max_component_length,
207
+ filesystem_flags,
208
+ fsname,
209
+ fsname.size
188
210
  )
189
211
 
190
212
  # Skip unmounted floppies or cd-roms, or inaccessible drives
191
213
  unless bool
192
- if [5,21].include?(FFI.errno) # ERROR_NOT_READY or ERROR_ACCESS_DENIED
214
+ if [5, 21].include?(FFI.errno) # ERROR_NOT_READY or ERROR_ACCESS_DENIED
193
215
  next
194
216
  else
195
217
  raise SystemCallError.new('GetVolumeInformation', FFI.errno)
@@ -201,7 +223,7 @@ module Sys
201
223
 
202
224
  name = 0.chr * MAXPATH
203
225
 
204
- if QueryDosDeviceA(drive[0,2], name, name.size) == 0
226
+ if QueryDosDeviceA(drive[0, 2], name, name.size) == 0
205
227
  raise SystemCallError.new('QueryDosDevice', FFI.errno)
206
228
  end
207
229
 
@@ -214,10 +236,11 @@ module Sys
214
236
  else
215
237
  mounts << mount
216
238
  end
217
- }
239
+ end
218
240
 
219
241
  mounts # Nil if the block form was used.
220
242
  end
243
+ # rubocop:enable Metrics/BlockLength
221
244
 
222
245
  # Returns the mount point for the given +file+. For MS Windows this
223
246
  # means the root of the path.
@@ -227,30 +250,48 @@ module Sys
227
250
  # File.mount_point("C:\\Documents and Settings") # => "C:\\'
228
251
  #
229
252
  def self.mount_point(file)
230
- wfile = FFI::MemoryPointer.from_string(file.wincode)
253
+ wfile = FFI::MemoryPointer.from_string(file.to_s.wincode)
231
254
 
232
255
  if PathStripToRootW(wfile)
233
256
  wfile.read_string(wfile.size).split("\000\000").first.tr(0.chr, '')
234
- else
235
- nil
236
257
  end
237
258
  end
238
259
 
239
260
  # Returns a Filesystem::Stat object that contains information about the
240
- # +path+ file system.
261
+ # +path+ file system. On Windows this will default to using the root
262
+ # path for volume information.
241
263
  #
242
264
  # Examples:
243
265
  #
244
- # File.stat("C:\\")
245
- # File.stat("C:\\Documents and Settings\\some_user")
266
+ # # Regular directory
267
+ # Sys::Filesystem.stat("C:\\")
268
+ # Sys::Filesystem.stat("C:\\Documents and Settings\\some_user")
269
+ #
270
+ # # Pathname
271
+ # pathname = Pathname.new("C:\\")
272
+ # Sys::Filesystem.stat(pathname)
273
+ #
274
+ # # Dir
275
+ # dir = Dir.open("C:\\")
276
+ # Sys::Filesystem.stat(dir)
277
+ #
278
+ # Note that on Windows you cannot stat a regular file because
279
+ # Windows won't like it. It must be a directory in some form.
246
280
  #
247
281
  def self.stat(path)
282
+ path = path.path if path.respond_to?(:path) # Dir
283
+ path = path.to_s if path.respond_to?(:to_s) # Pathname
284
+
248
285
  bytes_avail = FFI::MemoryPointer.new(:ulong_long)
249
286
  bytes_free = FFI::MemoryPointer.new(:ulong_long)
250
287
  total_bytes = FFI::MemoryPointer.new(:ulong_long)
251
288
 
252
- wpath = path.wincode
289
+ mpoint = mount_point(path).to_s
290
+ mpoint << '/' unless mpoint.end_with?('/')
291
+
292
+ wpath = path.to_s.wincode
253
293
 
294
+ # We need this call for the 64 bit support
254
295
  unless GetDiskFreeSpaceExW(wpath, bytes_avail, total_bytes, bytes_free)
255
296
  raise SystemCallError.new('GetDiskFreeSpaceEx', FFI.errno)
256
297
  end
@@ -259,65 +300,106 @@ module Sys
259
300
  bytes_free = bytes_free.read_ulong_long
260
301
  total_bytes = total_bytes.read_ulong_long
261
302
 
262
- sectors = FFI::MemoryPointer.new(:ulong_long)
263
- bytes = FFI::MemoryPointer.new(:ulong_long)
264
- free = FFI::MemoryPointer.new(:ulong_long)
265
- total = FFI::MemoryPointer.new(:ulong_long)
303
+ sectors_ptr = FFI::MemoryPointer.new(:ulong_long)
304
+ bytes_ptr = FFI::MemoryPointer.new(:ulong_long)
305
+ free_ptr = FFI::MemoryPointer.new(:ulong_long)
306
+ total_ptr = FFI::MemoryPointer.new(:ulong_long)
266
307
 
267
- unless GetDiskFreeSpaceW(wpath, sectors, bytes, free, total)
308
+ # We need this call for the total/cluster info, which is not in the Ex call.
309
+ unless GetDiskFreeSpaceW(wpath, sectors_ptr, bytes_ptr, free_ptr, total_ptr)
268
310
  raise SystemCallError.new('GetDiskFreeSpace', FFI.errno)
269
311
  end
270
312
 
271
- sectors = sectors.read_ulong_long
272
- bytes = bytes.read_ulong_long
273
- free = free.read_ulong_long
274
- total = total.read_ulong_long
313
+ sectors_per_cluster = sectors_ptr.read_ulong_long
314
+ bytes_per_sector = bytes_ptr.read_ulong_long
315
+
316
+ free_ptr.free
317
+ total_ptr.free
275
318
 
276
- block_size = sectors * bytes
277
- blocks_avail = total_bytes / block_size
319
+ block_size = sectors_per_cluster * bytes_per_sector
320
+ blocks_avail = bytes_avail / block_size
278
321
  blocks_free = bytes_free / block_size
322
+ total_blocks = total_bytes / block_size
279
323
 
280
- vol_name = FFI::MemoryPointer.new(:char, MAXPATH)
281
- base_type = FFI::MemoryPointer.new(:char, MAXPATH)
282
- vol_serial = FFI::MemoryPointer.new(:ulong)
283
- name_max = FFI::MemoryPointer.new(:ulong)
284
- flags = FFI::MemoryPointer.new(:ulong)
324
+ vol_name_ptr = FFI::MemoryPointer.new(:char, MAXPATH)
325
+ base_type_ptr = FFI::MemoryPointer.new(:char, MAXPATH)
326
+ vol_serial_ptr = FFI::MemoryPointer.new(:ulong)
327
+ name_max_ptr = FFI::MemoryPointer.new(:ulong)
328
+ flags_ptr = FFI::MemoryPointer.new(:ulong)
285
329
 
286
330
  bool = GetVolumeInformationW(
287
- wpath,
288
- vol_name,
289
- vol_name.size,
290
- vol_serial,
291
- name_max,
292
- flags,
293
- base_type,
294
- base_type.size
331
+ mpoint.wincode,
332
+ vol_name_ptr,
333
+ vol_name_ptr.size,
334
+ vol_serial_ptr,
335
+ name_max_ptr,
336
+ flags_ptr,
337
+ base_type_ptr,
338
+ base_type_ptr.size
295
339
  )
296
340
 
297
341
  unless bool
298
- raise SystemCallError.new('GetVolumInformation', FFI.errno)
342
+ raise SystemCallError.new('GetVolumeInformation', FFI.errno)
299
343
  end
300
344
 
301
- vol_serial = vol_serial.read_ulong
302
- name_max = name_max.read_ulong
303
- flags = flags.read_ulong
304
- base_type = base_type.read_string(base_type.size).tr(0.chr, '')
345
+ vol_serial = vol_serial_ptr.read_ulong
346
+ name_max = name_max_ptr.read_ulong
347
+ flags = flags_ptr.read_ulong
348
+ base_type = base_type_ptr.read_string(base_type_ptr.size).tr(0.chr, '')
349
+
350
+ # Lets explicitly free our pointers
351
+ vol_name_ptr.free
352
+ vol_serial_ptr.free
353
+ name_max_ptr.free
354
+ flags_ptr.free
355
+ base_type_ptr.free
356
+ sectors_ptr.free
357
+ bytes_ptr.free
305
358
 
306
359
  stat_obj = Stat.new
307
360
  stat_obj.instance_variable_set(:@path, path)
308
361
  stat_obj.instance_variable_set(:@block_size, block_size)
309
- stat_obj.instance_variable_set(:@blocks, blocks_avail)
362
+ stat_obj.instance_variable_set(:@blocks, total_blocks)
310
363
  stat_obj.instance_variable_set(:@blocks_available, blocks_avail)
311
364
  stat_obj.instance_variable_set(:@blocks_free, blocks_free)
312
365
  stat_obj.instance_variable_set(:@name_max, name_max)
313
366
  stat_obj.instance_variable_set(:@base_type, base_type)
314
367
  stat_obj.instance_variable_set(:@flags, flags)
315
368
  stat_obj.instance_variable_set(:@filesystem_id, vol_serial)
369
+ stat_obj.instance_variable_set(:@bytes_free, bytes_free)
370
+ stat_obj.instance_variable_set(:@bytes_available, bytes_avail)
316
371
 
317
372
  stat_obj.freeze # Read-only object
318
373
  end
319
374
 
320
- private
375
+ # Associate a volume with a drive letter or a directory on another volume.
376
+ #
377
+ def self.mount(target, source)
378
+ targetw = target.to_s.wincode
379
+ sourcew = source.to_s.wincode
380
+
381
+ volume_namew = (0.chr * 256).wincode
382
+
383
+ unless GetVolumeNameForVolumeMountPointW(sourcew, volume_namew, volume_namew.size)
384
+ raise SystemCallError.new('GetVolumeNameForVolumeMountPoint', FFI.errno)
385
+ end
386
+
387
+ unless SetVolumeMountPointW(targetw, volume_namew)
388
+ raise SystemCallError.new('SetVolumeMountPoint', FFI.errno)
389
+ end
390
+
391
+ self
392
+ end
393
+
394
+ # Deletes a drive letter or mounted folder.
395
+ #
396
+ def self.umount(mount_point)
397
+ unless DeleteVolumeMountPoint(mount_point)
398
+ raise SystemCallError.new('DeleteVolumeMountPoint', FFI.errno)
399
+ end
400
+
401
+ self
402
+ end
321
403
 
322
404
  # This method is used to get the boot time of the system, which is used
323
405
  # for the mount_time attribute within the File.mounts method.
@@ -331,64 +413,37 @@ module Sys
331
413
  raise Error, e
332
414
  else
333
415
  query = 'select LastBootupTime from Win32_OperatingSystem'
334
- results = wmi.ExecQuery(query)
335
- results.each{ |ole|
336
- time_array = Time.parse(ole.LastBootupTime.split('.').first)
337
- return Time.mktime(*time_array)
338
- }
416
+ ole = wmi.ExecQuery(query).ItemIndex(0)
417
+ time_array = Time.parse(ole.LastBootupTime.split('.').first)
418
+ Time.mktime(*time_array)
339
419
  end
340
420
  end
341
421
 
422
+ private_class_method :get_boot_time
423
+
342
424
  # Private method that converts filesystem flags into a comma separated
343
425
  # list of strings. The presentation is meant as a rough analogue to the
344
426
  # way options are presented for Unix filesystems.
345
427
  #
346
428
  def self.get_options(flags)
347
- str = ""
348
- str << " casepres" if CASE_PRESERVED_NAMES & flags > 0
349
- str << " casesens" if CASE_SENSITIVE_SEARCH & flags > 0
350
- str << " compression" if FILE_COMPRESSION & flags > 0
351
- str << " namedstreams" if NAMED_STREAMS & flags > 0
352
- str << " pacls" if PERSISTENT_ACLS & flags > 0
353
- str << " ro" if READ_ONLY_VOLUME & flags > 0
354
- str << " encryption" if SUPPORTS_ENCRYPTION & flags > 0
355
- str << " objids" if SUPPORTS_OBJECT_IDS & flags > 0
356
- str << " rpoints" if SUPPORTS_REPARSE_POINTS & flags > 0
357
- str << " sparse" if SUPPORTS_SPARSE_FILES & flags > 0
358
- str << " unicode" if UNICODE_ON_DISK & flags > 0
359
- str << " compressed" if VOLUME_IS_COMPRESSED & flags > 0
360
-
361
- str.tr!(' ', ',')
362
- str = str[1..-1] # Ignore the first comma
363
- str
429
+ str = ''
430
+ str << ' casepres' if CASE_PRESERVED_NAMES & flags > 0
431
+ str << ' casesens' if CASE_SENSITIVE_SEARCH & flags > 0
432
+ str << ' compression' if FILE_COMPRESSION & flags > 0
433
+ str << ' namedstreams' if NAMED_STREAMS & flags > 0
434
+ str << ' pacls' if PERSISTENT_ACLS & flags > 0
435
+ str << ' ro' if READ_ONLY_VOLUME & flags > 0
436
+ str << ' encryption' if SUPPORTS_ENCRYPTION & flags > 0
437
+ str << ' objids' if SUPPORTS_OBJECT_IDS & flags > 0
438
+ str << ' rpoints' if SUPPORTS_REPARSE_POINTS & flags > 0
439
+ str << ' sparse' if SUPPORTS_SPARSE_FILES & flags > 0
440
+ str << ' unicode' if UNICODE_ON_DISK & flags > 0
441
+ str << ' compressed' if VOLUME_IS_COMPRESSED & flags > 0
442
+
443
+ str.tr!(' ', ',')
444
+ str[1..-1] # Ignore the first comma
364
445
  end
365
- end
366
- end
367
-
368
- # Some convenient methods for converting bytes to kb, mb, and gb.
369
- #
370
- class Fixnum
371
- # call-seq:
372
- # <tt>fix</tt>.to_kb
373
- #
374
- # Returns +fix+ in terms of kilobytes.
375
- def to_kb
376
- self / 1024
377
- end
378
-
379
- # call-seq:
380
- # <tt>fix</tt>.to_mb
381
- #
382
- # Returns +fix+ in terms of megabytes.
383
- def to_mb
384
- self / 1048576
385
- end
386
446
 
387
- # call-seq:
388
- # <tt>fix</tt>.to_gb
389
- #
390
- # Returns +fix+ in terms of gigabytes.
391
- def to_gb
392
- self / 1073741824
447
+ private_class_method :get_options
393
448
  end
394
449
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sys/filesystem'
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec'
4
+ require 'sys_filesystem_shared'
5
+
6
+ RSpec.configure do |config|
7
+ config.include_context(Sys::Filesystem)
8
+ config.filter_run_excluding(:windows) unless Gem.win_platform?
9
+ config.filter_run_excluding(:unix) if Gem.win_platform?
10
+ config.filter_run_excluding(:dragonfly) unless RbConfig::CONFIG['host_os'] =~ /dragonfly/i
11
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sys-filesystem'
4
+
5
+ RSpec.shared_examples Sys::Filesystem do
6
+ example 'version number is set to the expected value' do
7
+ expect(Sys::Filesystem::VERSION).to eq('1.5.1')
8
+ expect(Sys::Filesystem::VERSION).to be_frozen
9
+ end
10
+
11
+ example 'you cannot instantiate an instance' do
12
+ expect{ described_class.new }.to raise_error(NoMethodError)
13
+ end
14
+ end