summaryrefslogtreecommitdiff
path: root/src/main/kotlin/QID.kt
blob: 93f9f6841f3843a2e2b16d6f68bf6951fa8131e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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()
    }
}