Skip to content

Instantly share code, notes, and snippets.

@threez
Created June 3, 2013 21:11
Show Gist options
  • Save threez/5701451 to your computer and use it in GitHub Desktop.
Save threez/5701451 to your computer and use it in GitHub Desktop.
Ruby fstab ffi implementation for unix (mac os x / bsd)
require "ffi"
module Mount
class Fstab < FFI::Struct
layout :fs_spec, :string, # block special device name
:fs_file, :string, # file system path prefix
:fs_vfstype, :string, # File system type, ufs, nfs
:fs_mntops, :string, # Mount options ala -o
:fs_type, :string, # FSTAB_* from fs_mntops
:fs_freq, :int, # dump frequency, in days
:fs_passno, :int # pass number on parallel fsck
end
class Volume < Struct.new(:name, :path, :type, :options, :type, :freq, :pass)
end
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :getfsent, [], :pointer
attach_function :endfsent, [], :void
def self.fstab
volumes = []
while !(fstab = getfsent).null?
fstab = Mount::Fstab.new(fstab)
volumes << Volume.new(fstab[:fs_spec],
fstab[:fs_file],
fstab[:fs_vfstype],
fstab[:fs_mntops],
fstab[:fs_type],
fstab[:fs_freq],
fstab[:fs_passno])
end
volumes
ensure
endfsent
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment