Class: Sys::Filesystem::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/sys/filesystem.rb,
lib/sys/unix/sys/filesystem.rb,
lib/sys/windows/sys/filesystem.rb

Overview

Stat objects are returned by the Sys::Filesystem.stat method.

Constant Summary collapse

ZFS_PROPERTIES =
{
  zfs_atime: 'atime',
  zfs_casesensitivity: 'casesensitivity',
  zfs_compression: 'compression',
  zfs_compressratio: 'compressratio',
  zfs_devices: 'devices',
  zfs_exec: 'exec',
  zfs_quota: 'quota',
  zfs_readonly: 'readonly',
  zfs_recordsize: 'recordsize',
  zfs_reservation: 'reservation',
  zfs_setuid: 'setuid'
}.freeze
RDONLY =

Read-only filesystem

1
NOSUID =

Filesystem does not support suid or sgid semantics.

2
NOTRUNC =

Filesystem does not truncate file names longer than name_max.

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStat

Creates a new Sys::Filesystem::Stat object. This is meant for internal use only. Do not instantiate directly.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sys/unix/sys/filesystem.rb', line 120

def initialize
  @path             = nil
  @block_size       = nil
  @fragment_size    = nil
  @blocks           = nil
  @blocks_free      = nil
  @blocks_available = nil
  @files            = nil
  @files_free       = nil
  @files_available  = nil
  @filesystem_id    = nil
  @flags            = nil
  @name_max         = nil
  @base_type        = nil
  @mount_source     = nil
  @mount_point      = nil
  @mount_type       = nil
  @mount_options    = nil
  @filesystem_type  = nil
  @owner            = nil
  @sync_reads       = nil
  @sync_writes      = nil
  @async_reads      = nil
  @async_writes     = nil
end

Instance Attribute Details

#async_readsObject

Count of async reads since mount



108
109
110
# File 'lib/sys/unix/sys/filesystem.rb', line 108

def async_reads
  @async_reads
end

#async_writesObject

Count of async writes since mount



111
112
113
# File 'lib/sys/unix/sys/filesystem.rb', line 111

def async_writes
  @async_writes
end

#base_typeObject

The file system type, e.g. NTFS, FAT, etc.



81
82
83
# File 'lib/sys/unix/sys/filesystem.rb', line 81

def base_type
  @base_type
end

#block_sizeObject

The file system block size. MS Windows typically defaults to 4096.



48
49
50
# File 'lib/sys/unix/sys/filesystem.rb', line 48

def block_size
  @block_size
end

#blocksObject

The total number of blocks available (used or unused) on the file system.



54
55
56
# File 'lib/sys/unix/sys/filesystem.rb', line 54

def blocks
  @blocks
end

#blocks_availableObject

The total number of unused blocks available to unprivileged processes.



60
61
62
# File 'lib/sys/unix/sys/filesystem.rb', line 60

def blocks_available
  @blocks_available
end

#blocks_freeObject

The total number of unused blocks.



57
58
59
# File 'lib/sys/unix/sys/filesystem.rb', line 57

def blocks_free
  @blocks_free
end

#bytes_availableObject (readonly)

The amount of free space available to unprivileged processes.



157
158
159
# File 'lib/sys/unix/sys/filesystem.rb', line 157

def bytes_available
  blocks_available * fragment_size
end

#bytes_freeObject (readonly)

The total amount of free space on the partition.



152
153
154
# File 'lib/sys/unix/sys/filesystem.rb', line 152

def bytes_free
  blocks_free * fragment_size
end

#filesObject Also known as: inodes

Total number of files/inodes that can be created on the file system. This attribute is always nil on MS Windows.



63
64
65
# File 'lib/sys/unix/sys/filesystem.rb', line 63

def files
  @files
end

#files_availableObject Also known as: inodes_available

Total number of available files/inodes for unprivileged processes that can be created on the file system. This attribute is always nil on MS Windows.



69
70
71
# File 'lib/sys/unix/sys/filesystem.rb', line 69

def files_available
  @files_available
end

#files_freeObject Also known as: inodes_free

Total number of free files/inodes that can be created on the file system. This attribute is always nil on MS Windows.



66
67
68
# File 'lib/sys/unix/sys/filesystem.rb', line 66

def files_free
  @files_free
end

#filesystem_idObject

The file system volume id.



72
73
74
# File 'lib/sys/unix/sys/filesystem.rb', line 72

def filesystem_id
  @filesystem_id
end

#filesystem_typeObject

The filesystem type



96
97
98
# File 'lib/sys/unix/sys/filesystem.rb', line 96

def filesystem_type
  @filesystem_type
end

#flagsObject

A bit mask of file system flags.



75
76
77
# File 'lib/sys/unix/sys/filesystem.rb', line 75

def flags
  @flags
end

#fragment_sizeObject

Fragment size. Meaningless at the moment.



51
52
53
# File 'lib/sys/unix/sys/filesystem.rb', line 51

def fragment_size
  @fragment_size
end

#mount_optionsObject

A list of comma separated options for the mount, e.g. nosuid, etc.



93
94
95
# File 'lib/sys/unix/sys/filesystem.rb', line 93

def mount_options
  @mount_options
end

#mount_pointObject

The mount point/directory.



87
88
89
# File 'lib/sys/unix/sys/filesystem.rb', line 87

def mount_point
  @mount_point
end

#mount_sourceObject

The name of the mounted resource.



84
85
86
# File 'lib/sys/unix/sys/filesystem.rb', line 84

def mount_source
  @mount_source
end

#mount_typeObject

The type of filesystem mount, e.g. ufs, zfs, nfs, etc.



90
91
92
# File 'lib/sys/unix/sys/filesystem.rb', line 90

def mount_type
  @mount_type
end

#name_maxObject

The maximum length of a file name permitted on the file system.



78
79
80
# File 'lib/sys/unix/sys/filesystem.rb', line 78

def name_max
  @name_max
end

#ownerObject

The user that mounted the filesystem



99
100
101
# File 'lib/sys/unix/sys/filesystem.rb', line 99

def owner
  @owner
end

#pathObject

The path of the file system.



45
46
47
# File 'lib/sys/unix/sys/filesystem.rb', line 45

def path
  @path
end

#sync_readsObject

Count of sync reads since mount



102
103
104
# File 'lib/sys/unix/sys/filesystem.rb', line 102

def sync_reads
  @sync_reads
end

#sync_writesObject

Count of sync writes since mount



105
106
107
# File 'lib/sys/unix/sys/filesystem.rb', line 105

def sync_writes
  @sync_writes
end

Instance Method Details

#bytes_totalObject

Returns the total space on the partition.



147
148
149
# File 'lib/sys/unix/sys/filesystem.rb', line 147

def bytes_total
  blocks * fragment_size
end

#bytes_usedObject

Returns the total amount of used space on the partition.



162
163
164
# File 'lib/sys/unix/sys/filesystem.rb', line 162

def bytes_used
  bytes_total - bytes_free
end

#case_insensitive?Boolean

Returns true if the filesystem is case sensitive for the current path. Typically this will be any path on MS Windows or Macs using HFS.

For a root path (really any path without actual a-z characters) we take a best guess based on the host operating system. However, as a general rule, I do not recommend using this method for a root path.

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sys/filesystem.rb', line 45

def case_insensitive?
  if path =~ /\w+/
    File.identical?(path, path.swapcase)
  else
    zfs_case = zfs_case_insensitive?
    return zfs_case unless zfs_case.nil?

    if RbConfig::CONFIG['host_os'] =~ /darwin|mac|windows|mswin|mingw/i
      true # Assumes HFS/APFS on Mac
    else
      false
    end
  end
end

#case_sensitive?Boolean

Opposite of case_insensitive?

Returns:

  • (Boolean)


62
63
64
# File 'lib/sys/filesystem.rb', line 62

def case_sensitive?
  !case_insensitive?
end

#percent_usedObject

Returns the percentage of the partition that has been used.



167
168
169
# File 'lib/sys/unix/sys/filesystem.rb', line 167

def percent_used
  100 - (100.0 * bytes_free.to_f / bytes_total.to_f)
end

#zfs_property(property) ⇒ Object

Returns a native ZFS property value for this path’s dataset. Returns nil if the path is not on ZFS or libzfs is unavailable.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sys/filesystem.rb', line 68

def zfs_property(property)
  return nil unless base_type == 'zfs'
  return nil unless Sys::Filesystem.respond_to?(:zfs_property, true)

  dataset = zfs_dataset
  return nil unless dataset

  Sys::Filesystem.send(:zfs_property, dataset, property.to_s)
rescue SystemCallError
  nil
end