Class: RStyx::Message::Stat
- Inherits:
-
Object
- Object
- RStyx::Message::Stat
- Defined in:
- lib/rstyx/messages.rb
Overview
Class representing an entry in a directory (e.g. the result of a stat message). See Inferno man page stat(5) for more details.
Instance Attribute Summary collapse
-
#atime ⇒ Object
Last access time.
-
#dev ⇒ Object
For kernel use.
-
#dtype ⇒ Object
For kernel use.
-
#gid ⇒ Object
Group name.
-
#length ⇒ Object
Length of the file in bytes.
-
#mode ⇒ Object
Permissions and flags.
-
#mtime ⇒ Object
Last modification time.
-
#muid ⇒ Object
Name of the user who last modified the file.
-
#name ⇒ Object
File name; must be / if the file is the root directory of the server.
-
#qid ⇒ Object
The Qid of the file represented by the stat object.
-
#size ⇒ Object
Total byte count of the following data.
-
#uid ⇒ Object
Owner name.
Class Method Summary collapse
-
.from_bytes(bytes) ⇒ Object
Unserialize a Stat.
-
.strextract(str, offset) ⇒ Object
Internal function for extrating strings.
Instance Method Summary collapse
-
#to_bytes ⇒ Object
Serialize a Stat.
-
#to_s ⇒ Object
convert a Stat to a human-readable string.
Instance Attribute Details
#atime ⇒ Object
Last access time
131 132 133 |
# File 'lib/rstyx/messages.rb', line 131 def atime @atime end |
#dev ⇒ Object
For kernel use
122 123 124 |
# File 'lib/rstyx/messages.rb', line 122 def dev @dev end |
#dtype ⇒ Object
For kernel use
119 120 121 |
# File 'lib/rstyx/messages.rb', line 119 def dtype @dtype end |
#gid ⇒ Object
Group name
147 148 149 |
# File 'lib/rstyx/messages.rb', line 147 def gid @gid end |
#length ⇒ Object
Length of the file in bytes
137 138 139 |
# File 'lib/rstyx/messages.rb', line 137 def length @length end |
#mode ⇒ Object
Permissions and flags
128 129 130 |
# File 'lib/rstyx/messages.rb', line 128 def mode @mode end |
#mtime ⇒ Object
Last modification time
134 135 136 |
# File 'lib/rstyx/messages.rb', line 134 def mtime @mtime end |
#muid ⇒ Object
Name of the user who last modified the file
150 151 152 |
# File 'lib/rstyx/messages.rb', line 150 def muid @muid end |
#name ⇒ Object
File name; must be / if the file is the root directory of the server
140 141 142 |
# File 'lib/rstyx/messages.rb', line 140 def name @name end |
#qid ⇒ Object
The Qid of the file represented by the stat object
125 126 127 |
# File 'lib/rstyx/messages.rb', line 125 def qid @qid end |
#size ⇒ Object
Total byte count of the following data
116 117 118 |
# File 'lib/rstyx/messages.rb', line 116 def size @size end |
#uid ⇒ Object
Owner name
144 145 146 |
# File 'lib/rstyx/messages.rb', line 144 def uid @uid end |
Class Method Details
.from_bytes(bytes) ⇒ Object
Unserialize a Stat
- bytes
- String
-
serialized string representation of a Stat
- return value
- Stat
-
the Stat corresponding to the
passed string
- raises
-
StyxException if bytes cannot be properly decoded as a Stat
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rstyx/messages.rb', line 200 def self.from_bytes(bytes) # From Inferno stat(5) # # 0-1 = size # 2-3 = type # 4-7 = dev # 8 = Qid.type # 9-12 = Qid.vers # 13-20 = Qid.path # 21-24 = mode # 25-28 = atime # 29-32 = mtime # 33-40 = length # 41-42 = name length # 43 to 42 + namelen = name # 43 + namelen to 44 + namelen = uid length # 45 + namelen to 44 + namelen + uidlen = uid # 45 + namelen + uidlen de = Stat.new de.size, de.dtype, de.dev = (bytes[0..7]).unpack("vvV") if de.size.nil? || de.dtype.nil? || de.dev.nil? raise RStyx::StyxException.new("failed to decode Stat") end de.qid = Qid.from_bytes(bytes[8..20]) de.mode, de.atime, de.mtime, lengthlo, lengthhi = (bytes[21..40]).unpack("VVVVV") if de.mode.nil? || de.atime.nil? || de.mtime.nil? || lengthlo.nil? || lengthhi.nil? raise RStyx::StyxException.new("failed to decode Stat") end # combine in little-endian de.length = lengthlo | (lengthhi << 32) de.name, offset = self.strextract(bytes, 41) de.uid, offset = self.strextract(bytes, offset) de.gid, offset = self.strextract(bytes, offset) de.muid, offset = self.strextract(bytes, offset) return(de) end |
.strextract(str, offset) ⇒ Object
Internal function for extrating strings
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/rstyx/messages.rb', line 154 def self.strextract(str, offset) # :nodoc: length = (str[offset..(offset + 1)].unpack("v"))[0] if length.nil? raise StyxException.new("invalid string, no length found") end offset += 2 nstr = (str[offset..(offset + length - 1)]) if (nstr.length != length) raise StyxException.new("invalid string") end return([nstr, offset + length]) end |
Instance Method Details
#to_bytes ⇒ Object
Serialize a Stat. This also calculates the size of the Stat in bytes.
- return value
- String
-
the serialized version of the Stat
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rstyx/messages.rb', line 174 def to_bytes str = [@dtype, @dev].pack("vV") str << @qid.to_bytes lengthlo = @length & 0xffffffff lengthhi = (@length >> 32) & 0xffffffff str << [@mode, @atime, @mtime, lengthlo, lengthhi].pack("VVVVV") str << [@name.length, @name].pack("va*") @uid ||= "" @gid ||= "" @muid ||= "" str << [@uid.length, @uid].pack("va*") str << [@gid.length, @gid].pack("va*") str << [@muid.length, @muid].pack("va*") @size = str.length return([@size].pack("v") + str) end |
#to_s ⇒ Object
convert a Stat to a human-readable string
- return value
-
a string representation of the Stat
245 246 247 248 249 250 251 252 |
# File 'lib/rstyx/messages.rb', line 245 def to_s s = sprintf("(Stat %d 0x%02x, 0x%04x ", @size, @dtype, @dev) s << sprintf("%s, 0x%04x 0x%04x 0x%04x ", @qid.to_s, @mode, @atime, @mtime) s << sprintf("%d %s %s ", @length, @name, @uid) s << sprintf("%s %s)", @gid, @muid) return(s) end |