Class: Sys::Filesystem
- Inherits:
-
Object
- Object
- Sys::Filesystem
- Extended by:
- Functions
- Defined in:
- lib/sys/filesystem.rb,
lib/sys/unix/sys/filesystem.rb,
lib/sys/windows/sys/filesystem.rb,
lib/sys/unix/sys/filesystem/structs.rb,
lib/sys/unix/sys/filesystem/constants.rb,
lib/sys/unix/sys/filesystem/functions.rb,
lib/sys/windows/sys/filesystem/constants.rb,
lib/sys/windows/sys/filesystem/functions.rb
Overview
The Filesystem class encapsulates information about your filesystem.
Defined Under Namespace
Modules: Constants, Functions, Structs Classes: Error, Mount, Stat
Constant Summary collapse
- VERSION =
The version of the sys-filesystem library
'1.5.3'
Constants included from Constants
Constants::CASE_PRESERVED_NAMES, Constants::CASE_SENSITIVE_SEARCH, Constants::FILE_COMPRESSION, Constants::MAXPATH, Constants::MNT_ASYNC, Constants::MNT_AUTOMOUNTED, Constants::MNT_CPROTECT, Constants::MNT_DEFWRITE, Constants::MNT_DETACH, Constants::MNT_DONTBROWSE, Constants::MNT_DOVOLFS, Constants::MNT_EXPIRE, Constants::MNT_EXPORTED, Constants::MNT_FORCE, Constants::MNT_IGNORE_OWNERSHIP, Constants::MNT_JOURNALED, Constants::MNT_LOCAL, Constants::MNT_MULTILABEL, Constants::MNT_NOATIME, Constants::MNT_NOCLUSTERR, Constants::MNT_NOCLUSTERW, Constants::MNT_NODEV, Constants::MNT_NOEXEC, Constants::MNT_NOSUID, Constants::MNT_NOUSERXATTR, Constants::MNT_QUARANTINE, Constants::MNT_QUOTA, Constants::MNT_RDONLY, Constants::MNT_ROOTFS, Constants::MNT_SYNCHRONOUS, Constants::MNT_UNION, Constants::MNT_VISFLAGMASK, Constants::MS_ACTIVE, Constants::MS_BIND, Constants::MS_DIRSYNC, Constants::MS_I_VERSION, Constants::MS_KERNMOUNT, Constants::MS_MANDLOCK, Constants::MS_MOVE, Constants::MS_NOATIME, Constants::MS_NODEV, Constants::MS_NODIRATIME, Constants::MS_NOEXEC, Constants::MS_NOSUID, Constants::MS_NOUSER, Constants::MS_POSIXACL, Constants::MS_PRIVATE, Constants::MS_RDONLY, Constants::MS_REC, Constants::MS_RELATIME, Constants::MS_REMOUNT, Constants::MS_SHARED, Constants::MS_SILENT, Constants::MS_SLAVE, Constants::MS_STRICTATIME, Constants::MS_SYNCHRONOUS, Constants::MS_UNBINDABLE, Constants::NAMED_STREAMS, Constants::PERSISTENT_ACLS, Constants::READ_ONLY_VOLUME, Constants::SUPPORTS_ENCRYPTION, Constants::SUPPORTS_OBJECT_IDS, Constants::SUPPORTS_REMOTE_STORAGE, Constants::SUPPORTS_REPARSE_POINTS, Constants::SUPPORTS_SPARSE_FILES, Constants::UMOUNT_NOFOLLOW, Constants::UNICODE_ON_DISK, Constants::VOLUME_IS_COMPRESSED, Constants::VOLUME_QUOTAS
Class Method Summary collapse
-
.mount(target, source) ⇒ Object
Associate a volume with a drive letter or a directory on another volume.
-
.mount_point(file) ⇒ Object
Returns the mount point for the given
file
. -
.mounts ⇒ Object
Yields a Filesystem::Mount object for each volume on your system in block form.
-
.stat(path) ⇒ Object
Returns a Filesystem::Stat object that contains information about the
path
file system. -
.umount(mount_point) ⇒ Object
Deletes a drive letter or mounted folder.
Class Method Details
.mount(target, source) ⇒ Object
Associate a volume with a drive letter or a directory on another volume.
430 431 432 433 434 435 436 |
# File 'lib/sys/unix/sys/filesystem.rb', line 430 def self.mount(source, target, fstype = 'ext2', flags = 0, data = nil) if mount_c(source, target, fstype, flags, data) != 0 raise Error, "mount() function failed: #{strerror(FFI.errno)}" end self end |
.mount_point(file) ⇒ Object
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:\\'
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/sys/unix/sys/filesystem.rb', line 396 def self.mount_point(file) dev = File.stat(file).dev val = file mounts.each do |mnt| mp = mnt.mount_point begin if File.stat(mp).dev == dev val = mp break end rescue Errno::EACCES next end end val end |
.mounts ⇒ Object
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. # => 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.
– I couldn’t really find a good reason to use the wide functions for this method. If you have one, patches welcome. – rubocop:disable Metrics/BlockLength
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/sys/unix/sys/filesystem.rb', line 297 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 do 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 do |key, val| if flags & key > 0 if string.empty? string << val else string << ", #{val}" end end flags &= ~key end obj. = string if block_given? yield obj.freeze else array << obj.freeze end ptr += Statfs.size end else begin if respond_to?(:setmntent, true) method_name = 'setmntent' fp = setmntent(MOUNT_FILE, 'r') else method_name = 'fopen' fp = fopen(MOUNT_FILE, 'r') end if fp.null? raise SystemCallError.new(method_name, FFI.errno) end 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. = 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 ensure if fp && !fp.null? if respond_to?(:endmntent, true) endmntent(fp) else fclose(fp) end end end end array end |
.stat(path) ⇒ Object
Returns a Filesystem::Stat object that contains information about the path
file system. On Windows this will default to using the root path for volume information.
Examples:
# Regular directory
Sys::Filesystem.stat("C:\\")
Sys::Filesystem.stat("C:\\Documents and Settings\\some_user")
# Pathname
pathname = Pathname.new("C:\\")
Sys::Filesystem.stat(pathname)
# Dir
dir = Dir.open("C:\\")
Sys::Filesystem.stat(dir)
Note that on Windows you cannot stat a regular file because Windows won’t like it. It must be a directory in some form.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/sys/unix/sys/filesystem.rb', line 238 def self.stat(path) path = path.path if path.respond_to?(:path) # File, Dir path = path.to_s if path.respond_to?(:to_s) # Pathname 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 if fs.members.include?(:f_basetype) obj.base_type = fs[:f_basetype].to_s end # DragonFlyBSD has additional struct members if RbConfig::CONFIG['host_os'] =~ /dragonfly/i obj.owner = fs[:f_owner] obj.filesystem_type = fs[:f_type] obj.sync_reads = fs[:f_syncreads] obj.async_reads = fs[:f_asyncreads] obj.sync_writes = fs[:f_syncwrites] obj.async_writes = fs[:f_asyncwrites] end obj.freeze end |
.umount(mount_point) ⇒ Object
Deletes a drive letter or mounted folder.
454 455 456 457 458 459 460 |
# File 'lib/sys/unix/sys/filesystem.rb', line 454 def self.umount(target, flags = 0) if umount_c(target, flags) != 0 raise Error, "umount function failed: #{strerror(FFI.errno)}" end self end |