Class: FFI::Stat

Inherits:
Object
  • Object
show all
Extended by:
Library
Includes:
StructWrapper
Defined in:
lib/ffi/stat.rb,
lib/ffi/stat/native.rb,
lib/ffi/stat/constants.rb,
lib/ffi/stat/time_spec.rb

Overview

Ruby representation of stat.h struct

Defined Under Namespace

Classes: TimeSpec

Constant Summary collapse

S_IFMT =

File type mask

0o170000
S_IFIFO =

FIFO

0o010000
S_IFCHR =

Character device

0o020000
S_IFDIR =

Directory

0o040000
S_IFBLK =

Block device

0o060000
S_IFREG =

Regular file

0o100000
S_IFLNK =

Symbolic link

0o120000
S_IFSOCK =

Socket

0o140000
S_ISUID =

SetUID

0o004000
S_ISGID =

SetGID

0o002000
S_ISVTX =

Sticky Bit

0o001000

Instance Attribute Summary collapse

Attributes included from StructWrapper

#native

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StructWrapper

#[], #[]=, #method_missing

Methods included from FFI::StructWrapper::ClassMethods

#by_ref, #by_value

Methods included from Accessors::ClassMethods

#ffi_attr_accessor, #ffi_attr_reader, #ffi_attr_readers, #ffi_attr_writer, #ffi_attr_writers, #ffi_bitflag_accessor, #ffi_bitflag_reader, #ffi_bitflag_writer

Methods included from Accessors

#fill, included, #inspect, #to_h

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FFI::StructWrapper

Instance Attribute Details

#atimeTime

Returns time of last access.

Returns:

  • (Time)

    time of last access



# File 'lib/ffi/stat.rb', line 41

#modeInteger

Returns file mode (type | perms).

Returns:

  • (Integer)

    file mode (type | perms)



# File 'lib/ffi/stat.rb', line 19

#mtimeTime

Returns time of last modification.

Returns:

  • (Time)

    time of last modification



# File 'lib/ffi/stat.rb', line 44

Returns number of links.

Returns:

  • (Integer)

    number of links



# File 'lib/ffi/stat.rb', line 25

#sizeInteger

Returns size of file in bytes.

Returns:

  • (Integer)

    size of file in bytes



# File 'lib/ffi/stat.rb', line 22

#uidInteger

Returns owner user id.

Returns:

  • (Integer)

    owner user id



# File 'lib/ffi/stat.rb', line 28

Class Method Details

.dir(stat, **fields) ⇒ Stat Also known as: directory

Returns:

Raises:

  • (SystemCallError)

See Also:



176
# File 'lib/ffi/stat.rb', line 176

%i[file dir].each { |m| define_method(m) { |stat = new, **args| stat.send(m, **args) } }

.file(stat, **fields) ⇒ Stat

Returns:

Raises:

  • (SystemCallError)

See Also:



# File 'lib/ffi/stat.rb', line 167

.from(file, stat = new(), follow: false) ⇒ Stat

Returns:

Raises:

  • (SystemCallError)

See Also:



# File 'lib/ffi/stat.rb', line 179

.fstat(file, stat = new()) ⇒ Stat

Returns:

Raises:

  • (SystemCallError)

See Also:



198
# File 'lib/ffi/stat.rb', line 198

%i[from stat lstat fstat].each { |m| define_method(m) { |file, stat = new, **args| stat.send(m, file, **args) } }

.lstat(file, stat = new()) ⇒ Stat

Returns:

Raises:

  • (SystemCallError)

See Also:



# File 'lib/ffi/stat.rb', line 189

.stat(file, stat = new()) ⇒ Stat

Returns:

Raises:

  • (SystemCallError)

See Also:



# File 'lib/ffi/stat.rb', line 184

Instance Method Details

#dir(mode:, nlink: 3, uid: Process.uid, gid: Process.gid, **args) ⇒ self Also known as: directory

Fill content for a directory

Parameters:

  • mode (Integer)
  • nlink (Integer) (defaults to: 3)
  • uid (Integer) (defaults to: Process.uid)
  • gid (Integer) (defaults to: Process.gid)
  • args (Hash)

    additional system specific stat fields

Returns:

  • (self)


77
78
79
80
# File 'lib/ffi/stat.rb', line 77

def dir(mode:, nlink: 3, uid: Process.uid, gid: Process.gid, **args)
  mode = ((S_IFDIR & S_IFMT) | (mode & 0o777))
  fill(mode: mode, uid: uid, gid: gid, nlink: nlink, **args)
end

#directory?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/ffi/stat.rb', line 146

def directory?
  mode & S_IFDIR != 0
end

#file(mode:, size:, nlink: 1, uid: Process.uid, gid: Process.gid, **args) ⇒ self

Fill content for a regular file

Parameters:

  • mode (Integer)
  • size (Integer)
  • uid (Integer) (defaults to: Process.uid)
  • gid (Integer) (defaults to: Process.gid)
  • args (Hash)

    additional system specific stat fields

Returns:

  • (self)


65
66
67
68
# File 'lib/ffi/stat.rb', line 65

def file(mode:, size:, nlink: 1, uid: Process.uid, gid: Process.gid, **args)
  mode = ((S_IFREG & S_IFMT) | (mode & 0o777))
  fill(mode: mode, size: size, nlink: nlink, uid: uid, gid: gid, **args)
end

#file?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/ffi/stat.rb', line 142

def file?
  mode & S_IFREG != 0
end

#from(file, follow: true) ⇒ self

Fill attributes from file (using native LIBC calls)

Parameters:

  • file (Integer|:to_s)

    descriptor or a file path

  • follow (Boolean) (defaults to: true)

    links

Returns:

  • (self)


99
100
101
102
103
104
105
# File 'lib/ffi/stat.rb', line 99

def from(file, follow: true)
  return fstat(file) if file.is_a?(Integer)

  return stat(file.to_s) if follow

  lstat(file.to_s)
end

#fstat(fileno) ⇒ self

Fill attributes from file descriptor

Parameters:

  • fileno (:to_i)

    file descriptor

Returns:

  • (self)

Raises:

  • (SystemCallError)

    on error



125
126
127
128
129
130
131
132
# File 'lib/ffi/stat.rb', line 125

%i[stat lstat fstat].each do |m|
  define_method(m) do |file|
    res = self.class.send("native_#{m}", (m == :fstat ? file.to_i : file.to_s), native)
    raise SystemCallError.new('', FFI::LastError.error) unless res.zero?

    self
  end
end

#lstat(path) ⇒ self

Fill attributes from file path, without following links

Parameters:

  • path (:to_s)

Returns:

  • (self)

Raises:

  • (SystemCallError)

    on error



# File 'lib/ffi/stat.rb', line 113

#mask(mask = S_ISUID, **overrides) ⇒ Object

Apply permissions mask to mode

Parameters:

  • mask (Integer) (defaults to: S_ISUID)

    (see umask)

  • overrides (Hash)

Returns:

  • self



138
139
140
# File 'lib/ffi/stat.rb', line 138

def mask(mask = S_ISUID, **overrides)
  fill(mode: mode & (~mask), **overrides)
end

#setgid?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/ffi/stat.rb', line 154

def setgid?
  mode & S_ISGID != 0
end

#setuid?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/ffi/stat.rb', line 150

def setuid?
  mode & S_ISUID != 0
end

#stat(path) ⇒ self

Fill attributes from file, following links

Parameters:

  • path (:to_s)

    a file path

Returns:

  • (self)

Raises:

  • (SystemCallError)

    on error



# File 'lib/ffi/stat.rb', line 107

#sticky?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/ffi/stat.rb', line 158

def sticky?
  mode & S_ISVTX != 0
end

Fill content for a symbolic link

Parameters:

  • size (Integer)

    length of the target name (including null terminator)

  • mode (Integer) (defaults to: 0o777)
  • uid (Integer) (defaults to: Process.uid)
  • gid (Integer) (defaults to: Process.gid)
  • args (Hash)

    additional system specific stat fields

Returns:

  • (self)


90
91
92
93
# File 'lib/ffi/stat.rb', line 90

def symlink(size:, mode: 0o777, nlink: 1, uid: Process.uid, gid: Process.gid, **args)
  mode = ((S_IFLNK & S_IFMT) | (mode & 0o777))
  fill(mode: mode, nlink: nlink, size: size, uid: uid, gid: gid, **args)
end

#symlink?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/ffi/stat.rb', line 162

def symlink?
  mode & S_IFLNK != 0
end