Skip to content
Daniel Berger edited this page Oct 4, 2019 · 12 revisions

Overview

The sys-filesystem gem provides information about your filesystem.

This is a cross-platform library that should work on any Unixy flavors, as well as MS Windows.

It includes five main methods:

Filesystem.stat
Filesystem.mount
Filesystem.mounts
Filesystem.mount_point
Filesystem.umount

The Filesystem.stat method will return a Filesystem::Stat object with various properties that also includes two custom methods:

Filesystem::Stat#case_insensitive_filesystem?
Filesystem::Stat#case_sensitive_filesystem?

Filesystem.stat(path)

The Filesystem.stat singleton method takes a path and returns a Filesystem::Stat object which includes information on that path. You will typically want to pass a mount point, but any path on the given mount will work.

Filesystem.mount(source, target, fstype = 'ext2', flags = 0)

The Filesystem.mount singleton method attaches the filesystem specified by the source (which is often a pathname referring to a device, but can also be the path name of a directory or file, or a dummy string) to the location (a directory or file) specified by the path name in target.

On unixy platforms it also accepts a filesystem type ('ext2' by default) and flags (none by default).

Filesystem.mounts

The Filesystem.mounts singleton method returns an array of Filesystem::Mount objects which include information about each mount point, such as type, options, etc.

Filesystem.mount_point(path)

The Filesystem.mount_point singleton method takes a path and returns the corresponding mount point for that path. For example, if you have "/home" mounted and you pass Filesystem.mount_point('/home/dberger'), it will return "/home".

Filesystem.umount

The `Filesystem.umount singleton method detaches the specified file system(s) from the file hierarchy.

Example

require 'sys/filesystem'
include Sys
   
# Display information about a particular filesystem.
p Filesystem.stat('/')

# Sample output

#<Sys::Filesystem::Stat:0x517440
  @base_type = "ufs",
  @flags = 4,
  @files_available = 3817457,
  @block_size = 8192,
  @blocks_available = 19957633,
  @blocks = 34349612,
  @name_max = 255,
  @path = "/",
  @filesystem_id = 35651592,
  @files = 4135040,
  @fragment_size = 1024,
  @files_free = 3817457,
  @blocks_free = 20301129
>
   
# Describe all mount points on the system
Filesystem.mounts{ |mount| p mount }
   
# Find the mount point of any particular file
puts Filesystem.mount_point('/home/djberge/some_file.txt') => '/home'

Implementation Details

For both Windows and Unixy operating systems, various C functions are wrapped via FFI to provide you with the appropriate information.

Clone this wiki locally