Trouble getting ffi and getmntinfo to work

Hi,

I’m trying to use getmntinfo on OSX (Snow leopard), but I can’t seem to
get meaningful data. The char fields tend to be blank or just not what I
would expect.

I think I have the statfs struct declared properly, but it’s possible
I made a mistake somewhere. It’s the same size at least. Any ideas?

Regards,

Dan

getmntinfo.rb

require ‘ffi’
require ‘mkmf/lite’

class Filesystem
extend FFI::Library
extend Mkmf::Lite
ffi_lib FFI::Library::LIBC

attach_function(:getmntinfo, [:pointer, :int], :int)
attach_function(:strerror, [:int], :string)

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

Both say 2168

#p Statfs.size
#p check_sizeof(‘struct statfs’, ‘sys/mount.h’)

def self.mounts
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)

 0.upto(num-1){ |i|
   mnt = Statfs.new(ptr)
   #p mnt[:f_bsize]
   #p mnt[:f_iosize]
   #p mnt[:f_blocks]
   #p mnt[:f_bfree]
   #p mnt[:f_bavail]
   #p mnt[:f_files]
   #p mnt[:f_ffree]
   #p mnt[:f_fsid]
   #p mnt[:f_owner]
   #p mnt[:f_type]
   #p mnt[:f_flags]
   #p mnt[:f_fssubtype]
   p mnt[:f_fstypename]
   p mnt[:f_mntonname]
   p mnt[:f_mntfromname]
   p "=" * 40
   ptr += buf.size
 }

end
end

Filesystem.mounts

On Jan 5, 2:03pm, Daniel B. [email protected] wrote:

Hi,

I’m trying to usegetmntinfoon OSX (Snow leopard), but I can’t seem to
get meaningful data. The char fields tend to be blank or just not what I
would expect.

I think I have the statfs struct declared properly, but it’s possible
I made a mistake somewhere. It’s the same size at least. Any ideas?

Oops, apparently I have to explicitly call getmntinfo64 because,
unlike the C function, it doesn’t automatically return the proper
struct.

Anyway, figured it out.

Regards,

Dan