Skip to content

Instantly share code, notes, and snippets.

@pudquick
Last active August 27, 2020 05:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pudquick/2d8ac7d6ab46715fe5995997c97101ac to your computer and use it in GitHub Desktop.
Save pudquick/2d8ac7d6ab46715fe5995997c97101ac to your computer and use it in GitHub Desktop.
Calling statfs from python on OS X via ctypes for inspecting filesystem information
from ctypes import CDLL, Structure, c_uint32, c_int64, c_uint64, c_char, create_string_buffer, byref, c_ubyte, c_int16, c_int64, c_int32
# when _DARWIN_FEATURE_64_BIT_INODE is not defined
class statfs32(Structure):
_fields_ = [
("f_otype", c_int16),
("f_oflags", c_int16),
("f_bsize", c_int64),
("f_iosize", c_int64),
("f_blocks", c_int64),
("f_bfree", c_int64),
("f_bavail", c_int64),
("f_files", c_int64),
("f_ffree", c_int64),
("f_fsid", c_uint64),
("f_owner", c_uint32),
("f_reserved1", c_int16),
("f_type", c_int16),
("f_flags", c_int64),
("f_reserved2", c_int64*2),
("f_fstypename", c_char*15),
("f_mntonname", c_char*90),
("f_mntfromname", c_char*90),
("f_reserved3", c_char),
("f_reserved4", c_int64*4),
]
# when _DARWIN_FEATURE_64_BIT_INODE is defined
class statfs64(Structure):
_fields_ = [
("f_bsize", c_uint32),
("f_iosize", c_int32),
("f_blocks", c_uint64),
("f_bfree", c_uint64),
("f_bavail", c_uint64),
("f_files", c_uint64),
("f_ffree", c_uint64),
("f_fsid", c_uint64),
("f_owner", c_uint32),
("f_type", c_uint32),
("f_flags", c_uint32),
("f_fssubtype", c_uint32),
("f_fstypename", c_char*16),
("f_mntonname", c_char*1024),
("f_mntfromname", c_char*1024),
("f_reserved", c_uint32*8),
]
kern = CDLL('/usr/lib/system/libsystem_kernel.dylib')
fs_info = statfs32()
# put the path to any file on the mounted file system here
root_volume = create_string_buffer('/')
result = kern.statfs(root_volume, byref(fs_info))
# >>> fs_info.f_fstypename
# 'hfs'
# >>> fs_info.f_mntonname
# '/'
# >>> fs_info.f_mntfromname
# '/dev/disk1'
# >>> fs_info.f_type
# 17
@harshit94
Copy link

Can you give more info when using statfs32 becomes unsafe? Or would it always work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment