diff options
Diffstat (limited to 'libs/samba/src')
| -rw-r--r-- | libs/samba/src/main/java/org/the_jk/cleversync/io/samba/SambaDirectory.kt | 36 | ||||
| -rw-r--r-- | libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt | 176 |
2 files changed, 91 insertions, 121 deletions
diff --git a/libs/samba/src/main/java/org/the_jk/cleversync/io/samba/SambaDirectory.kt b/libs/samba/src/main/java/org/the_jk/cleversync/io/samba/SambaDirectory.kt index f5230b2..fc5b290 100644 --- a/libs/samba/src/main/java/org/the_jk/cleversync/io/samba/SambaDirectory.kt +++ b/libs/samba/src/main/java/org/the_jk/cleversync/io/samba/SambaDirectory.kt @@ -126,15 +126,45 @@ internal open class SambaDirectory( } override fun removeDirectory(name: String): Boolean { - return conn.removeDir(SambaConnection.join(path, name)) + val removePath = SambaConnection.join(path, name) + val entry = conn.entry(removePath) ?: return false + if (entry.type != NativeSamba.DirEntryType.DIR) return false + return removeRecursive(removePath) + } + + private fun removeRecursive(removePath: String): Boolean { + val dir = conn.openDir(removePath) ?: return false + try { + dir.list().forEach { entry -> + val entryPath = SambaConnection.join(removePath, entry.name) + if (!when (entry.type) { + NativeSamba.DirEntryType.FILE, + NativeSamba.DirEntryType.LINK, + -> conn.unlink(entryPath) + NativeSamba.DirEntryType.DIR + -> removeRecursive(entryPath) + }) { + return false + } + } + return conn.removeDir(removePath) + } finally { + dir.destroy() + } } override fun removeFile(name: String): Boolean { - return conn.unlink(SambaConnection.join(path, name)) + val removePath = SambaConnection.join(path, name) + val entry = conn.entry(removePath) ?: return false + if (entry.type != NativeSamba.DirEntryType.FILE) return false + return conn.unlink(removePath) } override fun removeLink(name: String): Boolean { - return conn.unlink(SambaConnection.join(path, name)) + val removePath = SambaConnection.join(path, name) + val entry = conn.entry(removePath) ?: return false + if (entry.type != NativeSamba.DirEntryType.LINK) return false + return conn.unlink(removePath) } override fun openDir(name: String) = modifiableOpenDir(name) diff --git a/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt b/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt index 61c8da7..fecd746 100644 --- a/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt +++ b/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt @@ -12,9 +12,8 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import org.robolectric.shadows.ShadowLooper -import org.the_jk.cleversync.io.Directory +import org.the_jk.cleversync.TreeAbstractTest import org.the_jk.cleversync.io.samba.SambaCredentials -import org.the_jk.cleversync.safeValue import java.io.File import java.nio.charset.StandardCharsets import java.nio.file.Files @@ -22,14 +21,18 @@ import java.util.concurrent.TimeUnit @Config(manifest=Config.NONE) @RunWith(RobolectricTestRunner::class) -class SambaTreeTest { +class SambaTreeTest : TreeAbstractTest() { @Before fun setUpTest() { assertThat(shareDir.listFiles()).isEmpty() + + tree = SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow() } @After fun tearDownTest() { + tree.close() + for (file in shareDir.listFiles()!!) { if (file.isDirectory) { file.deleteRecursively() @@ -48,156 +51,93 @@ class SambaTreeTest { } @Test - fun listEmptyRoot() { - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val content = root.list() - assertThat(content.directories).isEmpty() - assertThat(content.files).isEmpty() - assertThat(content.links).isEmpty() - } - } - - @Test - fun listEmptyRootLive() { - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val content = root.liveList().safeValue() - assertThat(content?.directories).isEmpty() - assertThat(content?.files).isEmpty() - assertThat(content?.links).isEmpty() - } - } - - @Test fun listRootWithSymlink() { File(shareDir, "dir").mkdir() File(shareDir, "file").writeText("foo") Files.createSymbolicLink(File(shareDir, "link").toPath(), File("file").toPath()) - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val content = root.list() + val content = tree.list() - assertThat(content.directories).hasSize(1) - assertThat(content.directories[0].name).isEqualTo("dir") - assertThat(content.files).hasSize(2) - if (content.files[0].name == "file") { - assertThat(content.files[0].name).isEqualTo("file") - assertThat(content.files[0].size).isEqualTo(3UL) - assertThat(content.files[1].name).isEqualTo("link") - assertThat(content.files[1].size).isEqualTo(3UL) - } else { - assertThat(content.files[0].name).isEqualTo("link") - assertThat(content.files[0].size).isEqualTo(3UL) - assertThat(content.files[1].name).isEqualTo("file") - assertThat(content.files[1].size).isEqualTo(3UL) - } - // libsmb uses SMB2/SMB3 and unix extensions are SMB1, so no symlinks for now - assertThat(content.links).isEmpty() + assertThat(content.directories).hasSize(1) + assertThat(content.directories[0].name).isEqualTo("dir") + assertThat(content.files).hasSize(2) + if (content.files[0].name == "file") { + assertThat(content.files[0].name).isEqualTo("file") + assertThat(content.files[0].size).isEqualTo(3UL) + assertThat(content.files[1].name).isEqualTo("link") + assertThat(content.files[1].size).isEqualTo(3UL) + } else { + assertThat(content.files[0].name).isEqualTo("link") + assertThat(content.files[0].size).isEqualTo(3UL) + assertThat(content.files[1].name).isEqualTo("file") + assertThat(content.files[1].size).isEqualTo(3UL) } + // libsmb uses SMB2/SMB3 and unix extensions are SMB1, so no symlinks for now + assertThat(content.links).isEmpty() } @Test - fun readFile() { + fun readExistingFile() { File(shareDir, "file").writeText("hello world") - SambaTreeFactory.tree(uri, credentials).getOrThrow().use { root -> - val file = root.openFile("file") - assertThat(file?.name).isEqualTo("file") - assertThat(file?.size).isEqualTo(11UL) + val file = tree.openFile("file") + assertThat(file?.name).isEqualTo("file") + assertThat(file?.size).isEqualTo(11UL) - file?.read().use { input -> - assertThat(input?.readAllBytes()?.toString(StandardCharsets.UTF_8)).isEqualTo("hello world") - } + file?.read().use { input -> + assertThat(input?.readAllBytes()?.toString(StandardCharsets.UTF_8)).isEqualTo("hello world") + } - file?.read().use { input -> - assertThat(input?.available()).isEqualTo(11) - assertThat(input?.markSupported()).isTrue() - val buffer = ByteArray(10) - assertThat(input?.read(buffer, 5, 5)).isEqualTo(5) - input?.mark(100) - assertThat(buffer.sliceArray(5..<10).toString(StandardCharsets.UTF_8)).isEqualTo("hello") - assertThat(input?.read(buffer)).isEqualTo(6) - assertThat(buffer.sliceArray(0..<6).toString(StandardCharsets.UTF_8)).isEqualTo(" world") - input?.reset() - assertThat(input?.read(buffer, 3, 5)).isEqualTo(5) - assertThat(buffer.sliceArray(3..<8).toString(StandardCharsets.UTF_8)).isEqualTo(" worl") - } + file?.read().use { input -> + assertThat(input?.available()).isEqualTo(11) + assertThat(input?.markSupported()).isTrue() + val buffer = ByteArray(10) + assertThat(input?.read(buffer, 5, 5)).isEqualTo(5) + input?.mark(100) + assertThat(buffer.sliceArray(5..<10).toString(StandardCharsets.UTF_8)).isEqualTo("hello") + assertThat(input?.read(buffer)).isEqualTo(6) + assertThat(buffer.sliceArray(0..<6).toString(StandardCharsets.UTF_8)).isEqualTo(" world") + input?.reset() + assertThat(input?.read(buffer, 3, 5)).isEqualTo(5) + assertThat(buffer.sliceArray(3..<8).toString(StandardCharsets.UTF_8)).isEqualTo(" worl") } } @Test - fun writeFile() { - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val file = root.createFile("file") - assertThat(file.name).isEqualTo("file") - - file.write().writer().use { output -> - output.write("hello world") - } - - assertThat(file.size).isEqualTo(11UL) - } + override fun createFile() { + super.createFile() - assertThat(File(shareDir, "file").readText()).isEqualTo("hello world") + assertThat(File(shareDir, "foo").readBytes()).isEqualTo(byteArrayOf(1, 2, 3, 4)) } @Test - fun overwriteFile() { - File(shareDir, "file").writeText("hello world") - - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val file = root.modifiableOpenFile("file") - assertThat(file?.name).isEqualTo("file") - assertThat(file?.size).isEqualTo(11UL) - - file?.write().use { output -> - val buffer = "foobar".toByteArray(StandardCharsets.UTF_8) - output?.write(buffer, 0, 1) - output?.write(buffer, 1, 2) - output?.write(buffer, 3, 3) - } - - assertThat(file?.size).isEqualTo(6UL) - } + override fun overwriteFile() { + super.overwriteFile() - assertThat(File(shareDir, "file").readText()).isEqualTo("foobar") + assertThat(File(shareDir, "foo").readBytes()).isEqualTo(byteArrayOf(127, 1)) } @Test - fun createDirectory() { - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val foo = root.createDirectory("foo") - assertThat(foo.name).isEqualTo("foo") - val fooContent = foo.list() - assertThat(fooContent.directories).isEmpty() - assertThat(fooContent.files).isEmpty() - assertThat(fooContent.links).isEmpty() - val content = root.list() - assertThat(content.directories).contains(foo) - assertThat(content.files).isEmpty() - assertThat(content.links).isEmpty() - } + override fun createDirectory() { + super.createDirectory() assertThat(File(shareDir, "foo").isDirectory).isTrue() } @Test(timeout = 10000) - fun observeCreateDirectory() { - SambaTreeFactory.modifiableTree(uri, credentials).getOrThrow().use { root -> - val content = root.liveList() - var dir: Directory? = null - content.observeForever { - if (it.directories.size == 1) dir = it.directories[0] - } - root.createDirectory("foo") - while (dir == null) { - ShadowLooper.idleMainLooper(10, TimeUnit.SECONDS) - } - assertThat(dir?.name).isEqualTo("foo") - } + override fun observeCreateDirectory() { + super.observeCreateDirectory() assertThat(File(shareDir, "foo").isDirectory).isTrue() } + // libsmb uses SMB2/SMB3 and unix extensions are SMB1, so no symlinks for now + override fun supportSymlinks() = false + + override fun idle() { + ShadowLooper.idleMainLooper(10, TimeUnit.SECONDS) + } + companion object { private lateinit var uri: String private lateinit var credentials: SambaCredentials |
