Parent

Included Modules

Sys::Filesystem

The Filesystem class encapsulates information about your filesystem.


The Filesystem class serves as an abstract base class. Its methods return objects of other types. Do not instantiate.

Public Class Methods

mount_point(file) click to toggle source

Returns the mount point for the given file. For MS Windows this means the root of the path.

Example:

File.mount_point("C:\\Documents and Settings") # => "C:\\'
# File lib/windows/sys/filesystem.rb, line 248
def self.mount_point(file)
   file = file.tr(File::SEPARATOR, File::ALT_SEPARATOR)
   PathStripToRoot(file)
   file[/^[^\00]]*/]
end
mounts() click to toggle source

Yields a Filesystem::Mount object for each volume on your system in block form. Returns an array of Filesystem::Mount objects in non-block form.

Example:

Sys::Filesystem.mounts{ |mount|
   p mt.name        # => \\Device\\HarddiskVolume1
   p mt.mount_point # => C:\
   p mt.mount_time  # => Thu Dec 18 20:12:08 -0700 2008
   p mt.mount_type  # => NTFS
   p mt.options     # => casepres,casesens,ro,unicode 
   p mt.pass_number # => nil
   p mt.dump_freq   # => nil
}

This method is a bit of a fudge for MS Windows in the name of interface compatibility because this method deals with volumes, not actual mount points. But, I believe it provides the sort of information many users want at a glance.

The possible values for the options and their meanings are as follows:

casepres => The filesystem preserves the case of file names when it places a name on disk. casesens => The filesystem supports case-sensitive file names. compression => The filesystem supports file-based compression. namedstreams => The filesystem supports named streams. pacls => The filesystem preserves and enforces access control lists. ro => The filesystem is read-only. encryption => The filesystem supports the Encrypted File System (EFS). objids => The filesystem supports object identifiers. rpoints => The filesystem supports reparse points. sparse => The filesystem supports sparse files. unicode => The filesystem supports Unicode in file names as they appear on disk. compressed => The filesystem is compressed.

# File lib/windows/sys/filesystem.rb, line 164
def self.mounts
   buffer = 0.chr * MAXPATH
   length = GetLogicalDriveStrings(buffer.size, buffer)

   if length == 0
      raise Error, get_last_error
   end

   mounts = block_given? ? nil : []

   # Try again if it fails because the buffer is too small
   if length > buffer.size
      buffer = 0.chr * length
      if GetLogicalDriveStrings(buffer.size, buffer) == 0
         raise Error, get_last_error
      end
   end

   boot_time = get_boot_time

   drives = buffer.strip.split("\00"")

   drives.each{ |drive|
      mount  = Mount.new
      volume = 0.chr * MAXPATH
      fsname = 0.chr * MAXPATH

      mount.instance_variable_set(:@mount_point, drive)
      mount.instance_variable_set(:@mount_time, boot_time)

      volume_serial_number = [0].pack('L')
      max_component_length = [0].pack('L')
      filesystem_flags     = [0].pack('L')

      bool = GetVolumeInformation(
         drive,
         volume,
         volume.size,
         volume_serial_number,
         max_component_length,
         filesystem_flags,
         fsname,
         fsname.size
      )

      # Skip unmounted floppies or cd-roms
      unless bool
         errnum = GetLastError()
         if errnum == ERROR_NOT_READY
            next
         else
            raise Error, get_last_error(errnum)
         end
      end

      filesystem_flags = filesystem_flags.unpack('L')[0]

      name = 0.chr * MAXPATH

      if QueryDosDevice(drive[0,2], name, name.size) == 0
         raise Error, get_last_error
      end

      mount.instance_variable_set(:@name, name.strip)
      mount.instance_variable_set(:@mount_type, fsname.strip)
      mount.instance_variable_set(:@options, get_options(filesystem_flags))

      if block_given?
         yield mount
      else
         mounts << mount
      end
   }

   mounts # Nil if the block form was used.
end
stat(path) click to toggle source

Returns a Filesystem::Stat object that contains information about the path file system.

Examples:

File.stat("C:\\")
File.stat("C:\\Documents and Settings\\some_user")
# File lib/windows/sys/filesystem.rb, line 262
def self.stat(path)
   bytes_avail = [0].pack('Q')
   bytes_free  = [0].pack('Q')
   total_bytes = [0].pack('Q')

   unless GetDiskFreeSpaceEx(path, bytes_avail, total_bytes, bytes_free)
      raise Error, get_last_error 
   end                           

   bytes_avail = bytes_avail.unpack('Q').first
   bytes_free  = bytes_free.unpack('Q').first
   total_bytes = total_bytes.unpack('Q').first

   sectors = [0].pack('Q')
   bytes   = [0].pack('Q')
   free    = [0].pack('Q')
   total   = [0].pack('Q')

   unless GetDiskFreeSpace(path, sectors, bytes, free, total)
      raise Error, get_last_error
   end

   sectors = sectors.unpack('Q').first
   bytes   = bytes.unpack('Q').first
   free    = free.unpack('Q').first
   total   = total.unpack('Q').first

   block_size   = sectors * bytes
   blocks_avail = total_bytes / block_size
   blocks_free  = bytes_free / block_size

   vol_name   = 0.chr * 260
   base_type  = 0.chr * 260
   vol_serial = [0].pack('L')
   name_max   = [0].pack('L')
   flags      = [0].pack('L')

   bool = GetVolumeInformation(
      path,
      vol_name,
      vol_name.size,
      vol_serial,
      name_max,
      flags,
      base_type,
      base_type.size
   )

   unless bool
      raise Error, get_last_error
   end

   vol_serial = vol_serial.unpack('L').first
   name_max   = name_max.unpack('L').first
   flags      = flags.unpack('L').first
   base_type  = base_type[/^[^\00]]*/]

   stat_obj = Stat.new
   stat_obj.instance_variable_set(:@path, path)
   stat_obj.instance_variable_set(:@block_size, block_size)
   stat_obj.instance_variable_set(:@blocks, blocks_avail)
   stat_obj.instance_variable_set(:@blocks_available, blocks_avail)
   stat_obj.instance_variable_set(:@blocks_free, blocks_free)
   stat_obj.instance_variable_set(:@name_max, name_max)
   stat_obj.instance_variable_set(:@base_type, base_type)
   stat_obj.instance_variable_set(:@flags, flags)
   stat_obj.instance_variable_set(:@filesystem_id, vol_serial)
   
   stat_obj.freeze # Read-only object
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.