Skip to content

Commit

Permalink
Fix native implementation of chmod (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Dec 20, 2023
1 parent d7ff664 commit d7c5234
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@
**/
package io.matthewnelson.kmp.file

class FileChmodUnitTest {

// TODO
}
class ChmodJsUnitTest: ChmodBaseTest()
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package io.matthewnelson.kmp.file

import io.matthewnelson.kmp.file.internal.IsWindows
import platform.posix.*
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@OptIn(DelicateFileApi::class)
class ChmodNativeUnitTest: ChmodBaseTest() {

@Test
fun givenFile_whenChmod_thenReturnsExpected() {
if (IsWindows) return

val tmpDir = randomTemp()
assertTrue(tmpDir.mkdir())

try {
listOf(
Triple("100", intArrayOf(X_OK), listOf(R_OK, W_OK)),
Triple("200", intArrayOf(W_OK), listOf(R_OK, X_OK)),
Triple("300", intArrayOf(W_OK, X_OK), listOf(R_OK)),
Triple("400", intArrayOf(R_OK), listOf(W_OK, X_OK)),
Triple("500", intArrayOf(R_OK, X_OK), listOf(W_OK)),
Triple("600", intArrayOf(R_OK, W_OK), listOf(X_OK)),
Triple("700", intArrayOf(R_OK, W_OK, X_OK), emptyList()),
Triple("110", intArrayOf(X_OK), listOf(R_OK, W_OK)),
Triple("220", intArrayOf(W_OK), listOf(R_OK, X_OK)),
Triple("330", intArrayOf(W_OK, X_OK), listOf(R_OK)),
Triple("440", intArrayOf(R_OK), listOf(W_OK, X_OK)),
Triple("550", intArrayOf(R_OK, X_OK), listOf(W_OK)),
Triple("660", intArrayOf(R_OK, W_OK), listOf(X_OK)),
Triple("770", intArrayOf(R_OK, W_OK, X_OK), emptyList()),
Triple("111", intArrayOf(X_OK), listOf(R_OK, W_OK)),
Triple("222", intArrayOf(W_OK), listOf(R_OK, X_OK)),
Triple("333", intArrayOf(W_OK, X_OK), listOf(R_OK)),
Triple("444", intArrayOf(R_OK), listOf(W_OK, X_OK)),
Triple("555", intArrayOf(R_OK, X_OK), listOf(W_OK)),
Triple("666", intArrayOf(R_OK, W_OK), listOf(X_OK)),
Triple("777", intArrayOf(R_OK, W_OK, X_OK), emptyList()),
).forEach { (mode, accessTrue, accessFalse) ->
val tmpFile = tmpDir.resolve("chmod_${mode}_${randomName()}")
tmpFile.writeUtf8("Hello World!")

try {
assertTrue(tmpFile.checkAccess(R_OK, W_OK))
tmpFile.chmod(mode)
assertTrue(tmpFile.checkAccess(*accessTrue))

accessFalse.forEach { access ->
assertFalse(tmpFile.checkAccess(access))
}
} finally {
tmpFile.delete()
}
}
} finally {
tmpDir.delete()
}
}

@Test
fun givenFile_whenCheckAccess_thenReturnsExpected() {
if (IsWindows) return

// simple test for our helper function
val tmp = randomTemp()
tmp.writeUtf8("Hello World!")

try {
assertFalse(tmp.checkAccess(X_OK))
assertTrue(tmp.checkAccess(R_OK))
tmp.chmod("500")
assertTrue(tmp.checkAccess(X_OK))
} finally {
tmp.delete()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
**/
package io.matthewnelson.kmp.file

import kotlinx.cinterop.ExperimentalForeignApi
import platform.posix.F_OK
import platform.posix.X_OK
import platform.posix.access
import platform.posix.errno
import kotlin.experimental.ExperimentalNativeApi

actual val isJvm: Boolean = false
Expand All @@ -29,3 +34,17 @@ actual val isSimulator: Boolean by lazy {
else -> false
}
}

@Throws(IOException::class, IndexOutOfBoundsException::class)
@OptIn(DelicateFileApi::class, ExperimentalForeignApi::class)
fun File.checkAccess(vararg access: Int): Boolean {
if (!exists()) throw FileNotFoundException()

var final = access.first()
for (i in 1 until access.size) {
final = final or access[i]
}

// Will be -1 Permission Denied if false
return access(path, final) == 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package io.matthewnelson.kmp.file

import io.matthewnelson.kmp.file.internal.IsWindows
import kotlin.test.Test
import kotlin.test.assertTrue
import kotlin.test.fail

@OptIn(DelicateFileApi::class)
abstract class ChmodBaseTest {

@Test
fun givenFile_whenIllegalMode_thenThrowsIOException() {
if (IsWindows) return

val tmp = randomTemp()
assertTrue(tmp.mkdir())

try {
listOf(
"855",
"75a",
"787",
"b64"
).forEach { mode ->
try {
tmp.chmod(mode)
fail(message = mode)
} catch (_: IOException) {
// pass
}
}
} finally {
tmp.delete()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ constructor(private val value: String) {
}

companion object {
val Owner = Mask(400, 200, 100)
val Group = Mask(40, 20, 10)
val Other = Mask(4, 2, 1)
val Owner = Mask(S_IRUSR, S_IWUSR, S_IXUSR)
val Group = Mask(S_IRGRP, S_IWGRP, S_IXGRP)
val Other = Mask(S_IROTH, S_IWOTH, S_IXOTH)
}
}
}

0 comments on commit d7c5234

Please sign in to comment.