Class: RubySMB::Server::Share::Provider::VirtualDisk::VirtualStat
- Inherits:
-
Object
- Object
- RubySMB::Server::Share::Provider::VirtualDisk::VirtualStat
- 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.
Instance Attribute Summary collapse
- #birthtime ⇒ Object readonly
Instance Method Summary collapse
-
#atime ⇒ Object
last access time.
- #blksize ⇒ Object
- #blockdev? ⇒ Boolean
- #blocks ⇒ Object
- #chardev? ⇒ Boolean
-
#ctime ⇒ Object
change time.
- #dev ⇒ Object
- #directory? ⇒ Boolean
- #executable? ⇒ Boolean
- #file? ⇒ Boolean
- #ftype ⇒ Object
- #gid ⇒ Object
- #grpowned? ⇒ Boolean
-
#initialize(**kwargs) ⇒ VirtualStat
constructor
All of the keyword arguments are the keys of the attributes to set.
- #ino ⇒ Object
-
#mode ⇒ Object
the permission bits, normalized based on the standard GNU representation, see: https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html.
-
#mtime ⇒ Object
modification time.
- #nlink ⇒ Object
- #owned? ⇒ Boolean
- #pipe? ⇒ Boolean
- #readable? ⇒ Boolean
- #setgid? ⇒ Boolean
- #setuid? ⇒ Boolean
- #size ⇒ Object
- #socket? ⇒ Boolean
- #sticky? ⇒ Boolean
- #symlink? ⇒ Boolean
- #uid ⇒ Object
- #world_readable? ⇒ Boolean
- #world_writable? ⇒ Boolean
- #writable? ⇒ Boolean
- #zero? ⇒ Boolean
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.
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
#birthtime ⇒ Object (readonly)
169 170 171 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 169 def birthtime @birthtime end |
Instance Method Details
#atime ⇒ Object
last access time
108 109 110 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 108 def atime @values.fetch(:atime, @birthtime) end |
#blksize ⇒ Object
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
33 34 35 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 33 def blockdev? false end |
#blocks ⇒ Object
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
41 42 43 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 41 def chardev? false end |
#ctime ⇒ Object
change time
118 119 120 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 118 def ctime @values.fetch(:ctime, @birthtime) end |
#dev ⇒ Object
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
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
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
61 62 63 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 61 def file? !directory? end |
#ftype ⇒ Object
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 |
#gid ⇒ Object
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
95 96 97 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 95 def grpowned? gid == Process.gid end |
#ino ⇒ Object
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 |
#mode ⇒ Object
the permission bits, normalized based on the standard GNU representation, see: https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html
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 |
#mtime ⇒ Object
modification time
113 114 115 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 113 def mtime @values.fetch(:mtime, @birthtime) end |
#nlink ⇒ Object
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
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
45 46 47 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 45 def pipe? false end |
#readable? ⇒ Boolean
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
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
128 129 130 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 128 def setuid? mode & 0o04000 != 0 end |
#size ⇒ Object
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
49 50 51 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 49 def socket? false end |
#sticky? ⇒ Boolean
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
53 54 55 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 53 def symlink? false end |
#uid ⇒ Object
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
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
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
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
75 76 77 |
# File 'lib/ruby_smb/server/share/provider/virtual_disk/virtual_stat.rb', line 75 def zero? file? && size == 0 end |