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

#attr_accessor, #attr_reader, #attr_writer, #ffi_attr_accessor, #ffi_attr_reader, #ffi_attr_reader_method, #ffi_attr_readers, #ffi_attr_writer, #ffi_attr_writer_method, #ffi_attr_writers, #ffi_bitflag_accessor, #ffi_bitflag_reader, #ffi_bitflag_writer, #ffi_public_attr_readers, #ffi_public_attr_writers

Methods included from Accessors

#ffi_attr_fill, #ffi_attr_reader_member, #ffi_attr_writer_member, #fill, #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(**fields) ⇒ Stat Also known as: directory

Returns:

Raises:

  • (SystemCallError)

See Also:



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

.file(**fields) ⇒ Stat

Returns:

Raises:

  • (SystemCallError)

See Also:



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

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

Returns:

Raises:

  • (SystemCallError)

See Also:



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

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

Returns:

Raises:

  • (SystemCallError)

See Also:



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

%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 196

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

Returns:

Raises:

  • (SystemCallError)

See Also:



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

Returns:

Raises:

  • (SystemCallError)

See Also:



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

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

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)


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

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)


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

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)


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

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)


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

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)


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

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



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

%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 114

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

Apply permissions mask to mode

Parameters:

  • mask (Integer) (defaults to: S_ISUID)

    (see umask)

  • overrides (Hash)

Returns:

  • self



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

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

#setgid?Boolean

Returns:

  • (Boolean)


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

def setgid?
  mode & S_ISGID != 0
end

#setuid?Boolean

Returns:

  • (Boolean)


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

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 108

#sticky?Boolean

Returns:

  • (Boolean)


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

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)


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

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)


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

def symlink?
  mode & S_IFLNK != 0
end