diff options
author | Edoardo La Greca | 2025-08-06 21:24:33 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-08-06 21:26:09 +0200 |
commit | 49750d70be34302b30808a34dc0df0a39ada8912 (patch) | |
tree | d290ba3e16420a7cffe539f7788c21651d173852 /src/main | |
parent | e8fdfbcea6f2028972d74b65c52d6e7d1aea214c (diff) |
add QID class
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/kotlin/QID.kt | 95 |
1 files changed, 95 insertions, 0 deletions
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<UByte>) { + 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 |