diff options
author | Edoardo La Greca | 2025-08-09 20:57:01 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-08-09 20:57:01 +0200 |
commit | c69d08ab05a044eb6b6135739de77b0711ebbdd3 (patch) | |
tree | 4fb0be96e922250e75ded18b3bafca079757e31a | |
parent | 2f255179d5cc3fb2e62f44fd34ff813e98e353bb (diff) |
add FilePermissions
-rw-r--r-- | src/main/kotlin/FilePermissions.kt | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/main/kotlin/FilePermissions.kt b/src/main/kotlin/FilePermissions.kt new file mode 100644 index 0000000..04561fe --- /dev/null +++ b/src/main/kotlin/FilePermissions.kt @@ -0,0 +1,34 @@ +/** + * The permissions of a newly created file. + * + * @param userPerms The permissions for the file's owning user. + * @param groupPerms The permissions for the file's owning group. + * @param elsePerms The permissions for everyone else. + * @param isDirectory Is the file a directory? If not, it's a regular file. + */ +class FilePermissions(val userPerms: Permissions, val groupPerms: Permissions, val elsePerms: Permissions, val isDirectory: Boolean) { + enum class Permissions(val bits: UByte) { + READ(0x4u), + WRITE(0x2u), + EXECUTE(0x1u), + READ_WRITE(READ.bits.or(WRITE.bits)), + READ_EXECUTE(READ.bits.or(EXECUTE.bits)), + WRITE_EXECUTE(WRITE.bits.or(EXECUTE.bits)), + READ_WRITE_EXECUTE(READ.bits.or(WRITE.bits.or(EXECUTE.bits))) + } + + /** + * Turn the permissions described by the [FilePermissions] fields into a permission integer (4 bytes). + */ + fun toPermissionInt(): UInt { + val permFileds = listOf(userPerms, groupPerms, elsePerms) + val perms: UInt = 0u + for (i in 0..permFileds.size) { + perms.or(permFileds[i].bits.toUInt().shl(8 * (permFileds.size - 1 - i))) + } + if (isDirectory) { + perms.or(0x80000000u) + } + return perms + } +}
\ No newline at end of file |