lib/ffi/stat_vfs.rb in ffi-libfuse-0.0.1.rctest12 vs lib/ffi/stat_vfs.rb in ffi-libfuse-0.1.0.rc20220550

- old
+ new

@@ -73,9 +73,67 @@ # @return [Integer] Mount flags # @!attribute [rw] namemax # @return [Integer] Maximum filename length - int_members = members.select { |m| m =~ /^f_/ }.map { |m| m[2..].to_sym } + int_members = members.grep(/^f_/).map { |m| m[2..].to_sym } ffi_attr_accessor(*int_members, format: 'f_%s') + + extend FFI::Library + ffi_lib FFI::Library::LIBC + + attach_function :native_statvfs, :statvfs, [:string, by_ref], :int + attach_function :native_fstatvfs, :fstatvfs, [:int, by_ref], :int + + # Fill from native statvfs for path + # @param [:to_s] path + # @return [self] + def statvfs(path) + res = self.class.native_statvfs(path.to_s, self) + raise SystemCallError.new('', FFI::LastError.errno) unless res.zero? + + self + end + + # Fill from native fstatvfs for fileno + # @param [Integer] fileno + # @return [self] + def fstatvfs(fileno) + res = self.class.native_fstatvfs(fileno, self) + raise SystemCallError.new('', FFI::LastError.errno) unless res.zero? + + self + end + + # File from native LIBC calls for file + # @param [Integer|:to_s] file a file descriptor or a file path + # @return [self] + def from(file) + return fstatvfs(file) if file.is_a?(Integer) + + statvfs(file) + end + + class << self + # @!method from(file) + # @return [StatVfs] + # @raise [SystemCallError] + # @see StatVfs#from + + # @!method statvfs(file) + # @return [StatVfs] + # @raise [SystemCallError] + # @see StatVfs#statvfs + + # @!method fstatvfs(file) + # @return [StatVfs] + # @raise [SystemCallError] + # @see StatVfs#fstatvfs + %i[from statvfs fstatvfs].each { |m| define_method(m) { |file, stat = new, **args| stat.send(m, file, **args) } } + + # @!visibility private + + # @!method native_statvfs(path, statvfs_buf) + # @!method native_fstatvfs(fd, statvfs_buf) + end end end