summaryrefslogtreecommitdiff
path: root/src/main/kotlin/FileMode.kt
blob: 81712e64a311a70a37724853eefd1cc6fd1e6909 (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
/**
 * The mode in which to open or create a file. For directories, it's illegal for directories to be written, truncated,
 * or removed on close.
 *
 * @param mode The actual mode, as described by the [Mode] enum class.
 * @param truncate Set or unset the truncation bit. It requires the write permission.
 * @param removeClose Set or unset the "remove on close" bit. It requires the remove permission in the parent directory.
 */
data class FileMode(val mode: Mode, val truncate: Boolean, val removeClose: Boolean) {
    enum class Mode(val value: UByte) {
        READ(0u),
        WRITE(1u),
        READ_WRITE(2u),
        EXECUTE(3u)
    }

    /**
     * Turn the mode described by the [FileMode] fields into a mode byte.
     */
    fun toModeByte(): UByte {
        var byte: UByte = 0u
        byte = byte.or(this.mode.value)
        if (truncate) {
            byte = byte.or(0x10u)
        }
        if (removeClose) {
            byte = byte.or(0x40u)
        }

        return byte
    }
}