Parent

Included Modules

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.

Public Class Methods

mount_point(file) click to toggle source

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/unix/sys/filesystem.rb, line 529
def self.mount_point(file)
  dev = File.stat(file).dev
  val = file

  self.mounts.each{ |mnt|
    mp = mnt.mount_point
    if File.stat(mp).dev == dev
      val = mp
      break
    end
  }

  val
end
mounts() click to toggle source

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:

Sys::Filesystem.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/unix/sys/filesystem.rb, line 418
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/
        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
stat(path) click to toggle source

Returns a Sys::Filesystem::Stat object containing information about the path on the filesystem.

# File lib/unix/sys/filesystem.rb, line 372
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/
    obj.block_size /= 256
  end

  if fs.members.include?(:f_basetype)
    obj.base_type = fs[:f_basetype].to_s
  end

  obj.freeze
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.