diff options
author | Edoardo La Greca | 2025-08-11 19:57:17 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-08-11 19:57:17 +0200 |
commit | ae4ec897ccd1d85a7d7b1a4ecb36739307326c65 (patch) | |
tree | 926e27cfc1d8753afd26c1be76bc780491fcbd77 | |
parent | 0c6c60897395982caa998953d10ec40b831951b5 (diff) |
add Stat constructor (that accepts raw bytes) and toRaw method
-rw-r--r-- | src/main/kotlin/Stat.kt | 65 |
1 files changed, 65 insertions, 0 deletions
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<UByte>) { + var offset = 0 + val qid = QID(raw.slice(0..<13)) + offset += 13 + val mode = FilePermissions(raw.slice(offset+0..<offset+4)) + offset += 4 + + val intFielSizes = listOf(4, 4, 8) + val intFields: MutableList<BigInteger> = 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<String> = 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<UByte> { + var bytes: List<UByte> = 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 |