Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created January 5, 2012 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djberg96/1563695 to your computer and use it in GitHub Desktop.
Save djberg96/1563695 to your computer and use it in GitHub Desktop.
Trying to use getmntinfo with ffi
require 'ffi'
class Filesystem
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function(:getmntinfo, [:pointer, :int], :int)
attach_function(:strerror, [:int], :string)
class Statfs < FFI::Struct
layout(
:f_otype, :short,
:f_oflags, :short,
:f_bsize, :long,
:f_iosize, :long,
:f_blocks, :long,
:f_bfree, :long,
:f_bavail, :long,
:f_files, :long,
:f_ffree, :long,
:f_fsid, :long,
:f_owner, :long,
:f_reserved1, :short,
:f_type, :short,
:f_flags, :long,
:f_reserved2, [:long,2],
:f_fstypename, [:char, 16],
:f_mntonname, [:char,16],
:f_mntfromname, [:char,16],
:f_reserved3, :char,
:f_reserved4, [:long,4]
)
end
def self.mounts
mntbuf = FFI::MemoryPointer.new(Statfs)
num = getmntinfo(mntbuf, 2)
if num == 0
raise Error, 'getmntinfo() function failed: ' + strerror(FFI.errno)
end
0.upto(num){ |n|
ptr = mntbuf.get_pointer(n)
mnt = Statfs.new(ptr)
p mnt[:f_otype]
}
end
end
Filesystem.mounts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment