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/functions.rb,
lib/sys/windows/sys/filesystem/functions.rb,
lib/sys/unix/sys/filesystem/constants/bsd.rb,
lib/sys/unix/sys/filesystem/constants/linux.rb,
lib/sys/unix/sys/filesystem/constants/darwin.rb,
lib/sys/unix/sys/filesystem/constants/freebsd.rb,
lib/sys/unix/sys/filesystem/constants/generic.rb,
lib/sys/windows/sys/filesystem/constants/windows.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.6.0'
Constants included from Constants
Constants::CASE_PRESERVED_NAMES, Constants::CASE_SENSITIVE_SEARCH, Constants::FILE_COMPRESSION, Constants::MAXPATH, Constants::MNT_ACLS, Constants::MNT_ASYNC, Constants::MNT_AUTOMOUNTED, Constants::MNT_BYFSID, Constants::MNT_CPROTECT, Constants::MNT_DEFERRED, Constants::MNT_DEFEXPORTED, Constants::MNT_DEFWRITE, Constants::MNT_DELEXPORT, Constants::MNT_DETACH, Constants::MNT_DONTBROWSE, Constants::MNT_DOVOLFS, Constants::MNT_EMPTYDIR, Constants::MNT_EXKERB, Constants::MNT_EXPIRE, Constants::MNT_EXPORTANON, Constants::MNT_EXPORTED, Constants::MNT_EXPUBLIC, Constants::MNT_EXRDONLY, Constants::MNT_FORCE, Constants::MNT_GJOURNAL, Constants::MNT_IGNORE, Constants::MNT_IGNORE_OWNERSHIP, Constants::MNT_JOURNALED, Constants::MNT_LOCAL, Constants::MNT_MULTILABEL, Constants::MNT_NAMEDATTR, Constants::MNT_NFS4ACLS, Constants::MNT_NOATIME, Constants::MNT_NOCLUSTERR, Constants::MNT_NOCLUSTERW, Constants::MNT_NOCOVER, Constants::MNT_NODEV, Constants::MNT_NOEXEC, Constants::MNT_NONBUSY, Constants::MNT_NOSUID, Constants::MNT_NOSYMFOLLOW, Constants::MNT_NOUSERXATTR, Constants::MNT_QUARANTINE, Constants::MNT_QUOTA, Constants::MNT_RDONLY, Constants::MNT_RECURSE, Constants::MNT_RELOAD, Constants::MNT_ROOTFS, Constants::MNT_SNAPSHOT, Constants::MNT_SOFTDEP, Constants::MNT_SUIDDIR, Constants::MNT_SUJ, Constants::MNT_SYNCHRONOUS, Constants::MNT_UNION, Constants::MNT_UNTRUSTED, Constants::MNT_UPDATE, Constants::MNT_USER, Constants::MNT_VERIFIED, Constants::MNT_VISFLAGMASK, Constants::MOUNT_OPTION_NAMES, 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
pathfile 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.
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/sys/unix/sys/filesystem.rb', line 540 def self.mount(source, target, fstype = 'ext2', flags = 0, data = nil) result = if respond_to?(:nmount_c, true) iov = nmount_iovec(source, target, fstype, data) nmount_c(iov[:pointer], iov[:count], flags) elsif RbConfig::CONFIG['host_os'] =~ /linux/i mount_c(source, target, fstype, flags, data) else mount_c(fstype, target, flags, mount_data_pointer(source, data)) end if result != 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:\\'
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/sys/unix/sys/filesystem.rb', line 506 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
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/sys/unix/sys/filesystem.rb', line 421 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 obj. = (mnt[:f_flags]) 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.
235 236 237 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/sys/unix/sys/filesystem.rb', line 235 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 # Keep statvfs as the portable source for capacity/inode data, but use # native statfs where available for mount metadata that POSIX statvfs # does not expose. On FreeBSD this includes the filesystem type name, # mount source/target, native MNT_* flags, owner, and I/O counters. if respond_to?(:statfs, true) native_fs = Statfs.new if statfs(path, native_fs) < 0 raise Error, "statfs() function failed: #{strerror(FFI.errno)}" end obj.flags = native_fs[:f_flags] if native_fs.members.include?(:f_flags) obj.name_max = native_fs[:f_namemax] if native_fs.members.include?(:f_namemax) obj.base_type = native_fs[:f_fstypename].to_s if native_fs.members.include?(:f_fstypename) obj.mount_type = native_fs[:f_fstypename].to_s if native_fs.members.include?(:f_fstypename) obj.mount_source = native_fs[:f_mntfromname].to_s if native_fs.members.include?(:f_mntfromname) obj.mount_point = native_fs[:f_mntonname].to_s if native_fs.members.include?(:f_mntonname) obj. = (native_fs[:f_flags]) if native_fs.members.include?(:f_flags) obj.filesystem_type = native_fs[:f_type] if native_fs.members.include?(:f_type) obj.owner = native_fs[:f_owner] if native_fs.members.include?(:f_owner) obj.sync_reads = native_fs[:f_syncreads] if native_fs.members.include?(:f_syncreads) obj.async_reads = native_fs[:f_asyncreads] if native_fs.members.include?(:f_asyncreads) obj.sync_writes = native_fs[:f_syncwrites] if native_fs.members.include?(:f_syncwrites) obj.async_writes = native_fs[:f_asyncwrites] if native_fs.members.include?(:f_asyncwrites) if native_fs.members.include?(:f_fsid) obj.filesystem_id = normalize_filesystem_id(native_fs[:f_fsid]) end 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) unless obj.mount_point obj.freeze end |
.umount(mount_point) ⇒ Object
Deletes a drive letter or mounted folder.
619 620 621 622 623 624 625 |
# File 'lib/sys/unix/sys/filesystem.rb', line 619 def self.umount(target, flags = 0) if umount_c(target, flags) != 0 raise Error, "umount function failed: #{strerror(FFI.errno)}" end self end |