From ae4ec897ccd1d85a7d7b1a4ecb36739307326c65 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Mon, 11 Aug 2025 19:57:17 +0200 Subject: add Stat constructor (that accepts raw bytes) and toRaw method --- src/main/kotlin/Stat.kt | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/main/kotlin/Stat.kt b/src/main/kotlin/Stat.kt index 4f0f625..e609b4c 100644 --- a/src/main/kotlin/Stat.kt +++ b/src/main/kotlin/Stat.kt @@ -82,3 +82,68 @@ class Stat { this.gid = gid this.muid = muid } + + /** + * Make an instance of [Stat] from raw data. + * + * @param fid The FID of the file associated with the stat instance. + * @param raw The raw stat data. + */ + constructor(fid: UInt, raw: List) { + var offset = 0 + val qid = QID(raw.slice(0..<13)) + offset += 13 + val mode = FilePermissions(raw.slice(offset+0.. = mutableListOf() + for (size in intFielSizes) { + intFields.add(InMessage.convInteger(raw, offset, size)) + offset += size + } + val atime = intFields[0].toInt().toUInt() + val mtime = intFields[1].toInt().toUInt() + val length = intFields[2].toLong().toULong() + + val strAmount = 4 + val strFields: MutableList = mutableListOf() + for (i in 0..strAmount) { + val str = InMessage.convString(raw, offset) + strFields.add(str) + offset += str.length + } + val name = strFields[0] + val uid = strFields[1] + val gid = strFields[2] + val muid = strFields[3] + + this.fid = fid + this.qid = qid + this.mode = mode + this.atime = atime + this.mtime = mtime + this.length = length + this.name = name + this.uid = uid + this.gid = gid + this.muid = muid + } + + /** + * Turn a [Stat] instance into raw data. This leaves out the [fid] field. + */ + fun toRaw(): List { + var bytes: List = emptyList() + bytes += this.qid.toRaw() + bytes += OutMessage.convIntegerToBytes(BigInteger(this.mode.toPermissionInt().toString()), 4u) + bytes += OutMessage.convIntegerToBytes(BigInteger(this.atime.toString()), 4u) + bytes += OutMessage.convIntegerToBytes(BigInteger(this.mtime.toString()), 4u) + bytes += OutMessage.convIntegerToBytes(BigInteger(this.length.toString()), 8u) + bytes += OutMessage.convStringToBytes(this.name) + bytes += OutMessage.convStringToBytes(this.uid) + bytes += OutMessage.convStringToBytes(this.gid) + bytes += OutMessage.convStringToBytes(this.muid) + return bytes + } +} \ No newline at end of file -- cgit v1.2.3