Class: RubySMB::Server::Share::Provider::VirtualDisk::VirtualStat

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb

Overview

This object emulates Ruby's builtin File::Stat object but uses a virtual file system instead of the real local one. The current implementation is limited to only representing directories and standard files. All attributes are read-only and once the object is created, it is immutable.

Since:

  • 3.1.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ VirtualStat

All of the keyword arguments are the keys of the attributes to set. The names are left as is, maintaining a direct 1 to 1 relationship. See the Ruby docs for File::Stat (https://ruby-doc.org/core-3.0.2/File/Stat.html) for a list of all the attributes that can be set. Some values are calculated based on others such as the mode readable? being calculated based on the mode.

Raises:

  • (ArgumentError)

Since:

  • 3.1.1



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 15

def initialize(**kwargs)
  # directory and file both default to being the opposite of each other, one or both can be specified
  # but they can not both be true at the same time
  is_dir = !!kwargs.fetch(:directory?, !kwargs.fetch(:file?, false)) # defaults to not file which defaults to false
  is_fil = !!kwargs.fetch(:file?, !kwargs.fetch(:directory?, true)) # defaults to not directory which defaults to true
  raise ArgumentError.new('must be either a file or a directory') unless is_dir ^ is_fil

  @values = kwargs.dup
  # the default is a directory
  @values[:directory?] = !@values.delete(:file?) if @values.key?(:file?) # normalize on directory? if file? was specified.

  @birthtime = kwargs[:birthtime] || Time.now
end

Instance Attribute Details

#birthtimeObject (readonly)

Since:

  • 3.1.1



169
170
171
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 169

def birthtime
  @birthtime
end

Instance Method Details

#atimeObject

last access time

Since:

  • 3.1.1



108
109
110
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 108

def atime
  @values.fetch(:atime, @birthtime)
end

#blksizeObject

Since:

  • 3.1.1



29
30
31
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 29

def blksize
  @values.fetch(:blksize, 4096)
end

#blockdev?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



33
34
35
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 33

def blockdev?
  false
end

#blocksObject

Since:

  • 3.1.1



37
38
39
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 37

def blocks
  @values.fetch(:blocks, 0)
end

#chardev?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



41
42
43
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 41

def chardev?
  false
end

#ctimeObject

change time

Since:

  • 3.1.1



118
119
120
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 118

def ctime
  @values.fetch(:ctime, @birthtime)
end

#devObject

Since:

  • 3.1.1



83
84
85
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 83

def dev
  @values[:dev] ||= rand(1..0xfe)
end

#directory?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



57
58
59
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 57

def directory?
  @values.fetch(:directory?, true)
end

#executable?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



162
163
164
165
166
167
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 162

def executable?
  return true if owned? && (mode & 1 << 6 != 0)
  return true if grpowned? && (mode & 1 << 3 != 0)
  return true if mode & 1 != 0
  return false
end

#file?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



61
62
63
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 61

def file?
  !directory?
end

#ftypeObject

Raises:

  • (Errno::ENOENT)

Since:

  • 3.1.1



65
66
67
68
69
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 65

def ftype
  raise Errno::ENOENT.new('No such file or directory') unless file? || directory?

  file? ? 'file' : 'directory'
end

#gidObject

Since:

  • 3.1.1



91
92
93
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 91

def gid
  @values.fetch(:gid, Process.gid)
end

#grpowned?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



95
96
97
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 95

def grpowned?
  gid == Process.gid
end

#inoObject

Since:

  • 3.1.1



87
88
89
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 87

def ino
  @values[:ino] ||= rand(1..0xfffe)
end

#modeObject

the permission bits, normalized based on the standard GNU representation, see: https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html

Since:

  • 3.1.1



124
125
126
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 124

def mode
  @values.fetch(:mode, (file? ? 0o644 : 0o755))
end

#mtimeObject

modification time

Since:

  • 3.1.1



113
114
115
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 113

def mtime
  @values.fetch(:mtime, @birthtime)
end

Since:

  • 3.1.1



79
80
81
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 79

def nlink
  @values.fetch(:nlink, 0)
end

#owned?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



103
104
105
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 103

def owned?
  uid == Process.uid
end

#pipe?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



45
46
47
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 45

def pipe?
  false
end

#readable?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



140
141
142
143
144
145
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 140

def readable?
  return true if owned? && (mode & 1 << 8 != 0)
  return true if grpowned? && (mode & 1 << 5 != 0)
  return true if world_readable?
  return false
end

#setgid?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



132
133
134
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 132

def setgid?
  mode & 0o02000 != 0
end

#setuid?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



128
129
130
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 128

def setuid?
  mode & 0o04000 != 0
end

#sizeObject

Since:

  • 3.1.1



71
72
73
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 71

def size
  @values.fetch(:size, 0)
end

#socket?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



49
50
51
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 49

def socket?
  false
end

#sticky?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



136
137
138
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 136

def sticky?
  mode & 0o01000 != 0
end

#symlink?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



53
54
55
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 53

def symlink?
  false
end

#uidObject

Since:

  • 3.1.1



99
100
101
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 99

def uid
  @values.fetch(:uid, Process.uid)
end

#world_readable?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



147
148
149
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 147

def world_readable?
  mode & 1 << 2 != 0
end

#world_writable?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



158
159
160
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 158

def world_writable?
  mode & 1 << 1 != 0
end

#writable?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



151
152
153
154
155
156
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 151

def writable?
  return true if owned? && (mode & 1 << 7 != 0)
  return true if grpowned? && (mode & 1 << 4 != 0)
  return true if world_writable?
  return false
end

#zero?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.1



75
76
77
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 75

def zero?
  file? && size == 0
end