From 49750d70be34302b30808a34dc0df0a39ada8912 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Wed, 6 Aug 2025 21:24:33 +0200 Subject: add QID class --- src/main/kotlin/QID.kt | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/main/kotlin/QID.kt (limited to 'src/main/kotlin/QID.kt') diff --git a/src/main/kotlin/QID.kt b/src/main/kotlin/QID.kt new file mode 100644 index 0000000..93f9f68 --- /dev/null +++ b/src/main/kotlin/QID.kt @@ -0,0 +1,95 @@ +/** + * This class holds information about a single QID. + */ +class QID { + /** + * Does the QID represent a directory? + */ + val isDirectory get() = getIsDirectory() + + /** + * Does the QID represent an append-only file? + */ + val isAppendOnly get() = getIsAppendOnly() + + /** + * Does the QID represent an exclusive-use file? + */ + val isExclusive get() = getIsExclusive() + + /** + * Does the QID represent a temporary file? + */ + val isTemporary get() = getIsTemporary() + + /** + * The QID type. + */ + val type: UByte + + /** + * The QID file version. + */ + val version: UInt + + /** + * The QID path. + */ + val path: ULong + + /** + * Constructor for a QID with separate fields. See the `stat(5)` manual page for more information about [type], + * [version], and [path]. + * + * @param type The QID type. + * @param version The QID file version. + * @param path The QID path. + */ + constructor(type: UByte, version: UInt, path: ULong) { + this.type = type + this.version = version + this.path = path + } + + /** + * Constructor for raw QID data. Only the first 13 elements are read. + * + * @param raw The raw QID data. + * @throws IllegalArgumentException if [raw] does not have at least 13 elements. + */ + constructor(raw: Array) { + require(raw.size >= 13) + this.type = raw.first() + val rawVersion = raw.slice(1..4) + val rawPath = raw.slice(5..12) + + // TODO: find a way to use InMessage's convInteger method here + var version = 0u + for (i in 0..rawVersion.size) { + version += rawVersion[i].toUInt().shl(i*8) + } + this.version = version + + var path: ULong = 0u + for (i in 0..rawPath.size) { + path += rawPath[i].toULong().shl(i*8) + } + this.path = path + } + + private fun getIsDirectory(): Boolean { + return this.type.and(0x80.toUByte()) != 0u.toUByte() + } + + private fun getIsAppendOnly(): Boolean { + return this.type.and(0x40.toUByte()) != 0u.toUByte() + } + + private fun getIsExclusive(): Boolean { + return this.type.and(0x20.toUByte()) != 0u.toUByte() + } + + private fun getIsTemporary(): Boolean { + return this.type.and(0x04.toUByte()) != 0u.toUByte() + } +} \ No newline at end of file -- cgit v1.2.3