class Sys::Filesystem
The Filesystem class serves as an abstract base class. Its methods return objects of other types. Do not instantiate.
The Filesystem class encapsulates information about your filesystem.
Constants
- MOUNT_FILE
- VERSION
The version of the sys-filesystem library
Public Class Methods
Returns the mount point of the given file
, or itself if it
cannot be found.
Example:
Sys::Filesystem.mount_point('/home/some_user') # => /home
# File lib/sys/unix/sys/filesystem.rb, line 356 def self.mount_point(file) dev = File.stat(file).dev val = file self.mounts.each{ |mnt| mp = mnt.mount_point begin if File.stat(mp).dev == dev val = mp break end rescue Errno::EACCES next end } val end
In block form, yields a Sys::Filesystem::Mount object for each mounted filesytem on the host. Otherwise it returns an array of Mount objects.
Example:
::mounts{ |fs|
p fs.name # => '/dev/dsk/c0t0d0s0' p fs.mount_time # => Thu Dec 11 15:07:23 -0700 2008 p fs.mount_type # => 'ufs' p fs.mount_point # => '/' p fs.options # => local, noowner, nosuid
}
# File lib/sys/unix/sys/filesystem.rb, line 245 def self.mounts array = block_given? ? nil : [] if respond_to?(:getmntinfo, true) buf = FFI::MemoryPointer.new(:pointer) num = getmntinfo(buf, 2) if num == 0 raise Error, 'getmntinfo() function failed: ' + strerror(FFI.errno) end ptr = buf.get_pointer(0) num.times{ |i| mnt = Statfs.new(ptr) obj = Sys::Filesystem::Mount.new obj.name = mnt[:f_mntfromname].to_s obj.mount_point = mnt[:f_mntonname].to_s obj.mount_type = mnt[:f_fstypename].to_s string = "" flags = mnt[:f_flags] & MNT_VISFLAGMASK @@opt_names.each{ |key,val| if flags & key > 0 if string.empty? string << val else string << ", #{val}" end end flags &= ~key } obj.options = string if block_given? yield obj.freeze else array << obj.freeze end ptr += Statfs.size } else begin if respond_to?(:setmntent, true) fp = setmntent(MOUNT_FILE, 'r') else fp = fopen(MOUNT_FILE, 'r') end if RbConfig::CONFIG['host_os'] =~ /sunos|solaris/i mt = Mnttab.new while getmntent(fp, mt) == 0 obj = Sys::Filesystem::Mount.new obj.name = mt[:mnt_special].to_s obj.mount_point = mt[:mnt_mountp].to_s obj.mount_type = mt[:mnt_fstype].to_s obj.options = mt[:mnt_mntopts].to_s obj.mount_time = Time.at(Integer(mt[:mnt_time])) if block_given? yield obj.freeze else array << obj.freeze end end else while ptr = getmntent(fp) break if ptr.null? mt = Mntent.new(ptr) obj = Sys::Filesystem::Mount.new obj.name = mt[:mnt_fsname] obj.mount_point = mt[:mnt_dir] obj.mount_type = mt[:mnt_type] obj.options = mt[:mnt_opts] obj.mount_time = nil obj.dump_frequency = mt[:mnt_freq] obj.pass_number = mt[:mnt_passno] if block_given? yield obj.freeze else array << obj.freeze end end end ensure if fp && !fp.null? if respond_to?(:endmntent, true) endmntent(fp) else fclose(fp) end end end end array end
Returns a Sys::Filesystem::Stat object
containing information about the path
on the filesystem.
# File lib/sys/unix/sys/filesystem.rb, line 194 def self.stat(path) fs = Statvfs.new if statvfs(path, fs) < 0 raise Error, 'statvfs() function failed: ' + strerror(FFI.errno) end obj = Sys::Filesystem::Stat.new obj.path = path obj.block_size = fs[:f_bsize] obj.fragment_size = fs[:f_frsize] obj.blocks = fs[:f_blocks] obj.blocks_free = fs[:f_bfree] obj.blocks_available = fs[:f_bavail] obj.files = fs[:f_files] obj.files_free = fs[:f_ffree] obj.files_available = fs[:f_favail] obj.filesystem_id = fs[:f_fsid] obj.flags = fs[:f_flag] obj.name_max = fs[:f_namemax] # OSX does things a little differently if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i obj.block_size /= 256 end # FreeBSD 10 does things a little differently too if RbConfig::CONFIG['host_os'] =~ /freebsd10/i obj.block_size = obj.fragment_size end if fs.members.include?(:f_basetype) obj.base_type = fs[:f_basetype].to_s end obj.freeze end
Private Class Methods
This method is used to get the boot time of the system, which is used for the mount_time attribute within the File.mounts method.
# File lib/sys/windows/sys/filesystem.rb, line 351 def self.get_boot_time host = Socket.gethostname cs = "winmgmts://#{host}/root/cimv2" begin wmi = WIN32OLE.connect(cs) rescue WIN32OLERuntimeError => e raise Error, e else query = 'select LastBootupTime from Win32_OperatingSystem' results = wmi.ExecQuery(query) results.each{ |ole| time_array = Time.parse(ole.LastBootupTime.split('.').first) return Time.mktime(*time_array) } end end
Private method that converts filesystem flags into a comma separated list of strings. The presentation is meant as a rough analogue to the way options are presented for Unix filesystems.
# File lib/sys/windows/sys/filesystem.rb, line 372 def self.get_options(flags) str = "" str << " casepres" if CASE_PRESERVED_NAMES & flags > 0 str << " casesens" if CASE_SENSITIVE_SEARCH & flags > 0 str << " compression" if FILE_COMPRESSION & flags > 0 str << " namedstreams" if NAMED_STREAMS & flags > 0 str << " pacls" if PERSISTENT_ACLS & flags > 0 str << " ro" if READ_ONLY_VOLUME & flags > 0 str << " encryption" if SUPPORTS_ENCRYPTION & flags > 0 str << " objids" if SUPPORTS_OBJECT_IDS & flags > 0 str << " rpoints" if SUPPORTS_REPARSE_POINTS & flags > 0 str << " sparse" if SUPPORTS_SPARSE_FILES & flags > 0 str << " unicode" if UNICODE_ON_DISK & flags > 0 str << " compressed" if VOLUME_IS_COMPRESSED & flags > 0 str.tr!(' ', ',') str = str[1..-1] # Ignore the first comma str end