Skip to content

Instantly share code, notes, and snippets.

@pudquick
Forked from djberg96/flag_to_string.rb
Created September 5, 2016 18:23
Show Gist options
  • Save pudquick/9dbf28dd8d49256fc69a8d9298271854 to your computer and use it in GitHub Desktop.
Save pudquick/9dbf28dd8d49256fc69a8d9298271854 to your computer and use it in GitHub Desktop.
Trying to stringify filesystem flags
require 'ffi'
class Filesystem
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function(:strerror, [:int], :string)
attach_function(:getmntinfo64, [:pointer, :int], :int)
class Statfs < FFI::Struct
layout(
:f_bsize, :uint32,
:f_iosize, :int32,
:f_blocks, :uint64,
:f_bfree, :uint64,
:f_bavail, :uint64,
:f_files, :uint64,
:f_ffree, :uint64,
:f_fsid, [:int32, 2],
:f_owner, :int32,
:f_type, :uint32,
:f_flags, :uint32,
:f_fssubtype, :uint32,
:f_fstypename, [:char, 16],
:f_mntonname, [:char, 1024],
:f_mntfromname, [:char, 1024],
:f_reserved, [:uint32, 8]
)
end
MNT_RDONLY = 0x00000001
MNT_SYNCHRONOUS = 0x00000002
MNT_NOEXEC = 0x00000004
MNT_NOSUID = 0x00000008
MNT_NODEV = 0x00000010
MNT_UNION = 0x00000020
MNT_ASYNC = 0x00000040
MNT_CPROTECT = 0x00000080
MNT_EXPORTED = 0x00000100
MNT_QUARANTINE = 0x00000400
MNT_LOCAL = 0x00001000
MNT_QUOTA = 0x00002000
MNT_ROOTFS = 0x00004000
MNT_DOVOLFS = 0x00008000
MNT_DONTBROWSE = 0x00100000
MNT_IGNORE_OWNERSHIP = 0x00200000
MNT_AUTOMOUNTED = 0x00400000
MNT_JOURNALED = 0x00800000
MNT_NOUSERXATTR = 0x01000000
MNT_DEFWRITE = 0x02000000
MNT_MULTILABEL = 0x04000000
MNT_NOATIME = 0x10000000
MNT_VISFLAGMASK = (
MNT_RDONLY | MNT_SYNCHRONOUS | MNT_NOEXEC |
MNT_NOSUID | MNT_NODEV | MNT_UNION |
MNT_ASYNC | MNT_EXPORTED | MNT_QUARANTINE |
MNT_LOCAL | MNT_QUOTA |
MNT_ROOTFS | MNT_DOVOLFS | MNT_DONTBROWSE |
MNT_IGNORE_OWNERSHIP | MNT_AUTOMOUNTED | MNT_JOURNALED |
MNT_NOUSERXATTR | MNT_DEFWRITE | MNT_MULTILABEL |
MNT_NOATIME | MNT_CPROTECT
)
@@opt_names = {
MNT_RDONLY => 'read-only',
MNT_SYNCHRONOUS => 'synchronous',
MNT_NOEXEC => 'noexec',
MNT_NOSUID => 'nosuid',
MNT_NODEV => 'nodev',
MNT_UNION => 'union',
MNT_ASYNC => 'asynchronous',
MNT_CPROTECT => 'content-protection',
MNT_EXPORTED => 'exported',
MNT_QUARANTINE => 'quarantined',
MNT_LOCAL => 'local',
MNT_QUOTA => 'quotas',
MNT_ROOTFS => 'rootfs',
MNT_DONTBROWSE => 'nobrowse',
MNT_IGNORE_OWNERSHIP => 'noowners',
MNT_AUTOMOUNTED => 'automounted',
MNT_JOURNALED => 'journaled',
MNT_NOUSERXATTR => 'nouserxattr',
MNT_DEFWRITE => 'defwrite',
MNT_NOATIME => 'noatime'
}
def self.info
buf = FFI::MemoryPointer.new(:pointer)
num = getmntinfo64(buf,0)
raise "getmntinfo64() failed: " + strerror(FFI.errno) if num == 0
ptr = buf.get_pointer(0)
num.times{ |i|
mnt = Statfs.new(ptr)
p mnt[:f_mntonname].to_s
p mnt[:f_flags]
flags = mnt[:f_flags] & MNT_VISFLAGMASK
opts = ""
@@opt_names.each{ |key,val|
if flags & key > 0
if opts.empty?
opts = val
else
opts << ", #{val}"
end
flags &= ~key
end
}
p opts
puts "=" * 40
ptr += Statfs.size
}
end
end
Filesystem.info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment