From a15cfde0fcbdb29dafdb9ebe39fe53c8da4073be Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 25 Aug 2024 02:30:36 +0200 Subject: Combine tests from both local and samba Most the tests test the Tree implementation and thus should work on all such implementations. Current exception is symlinks which Samba backend doesn't (currently?) support. Improve the Samba remove methods to better match the expected behavior. --- .../the_jk/cleversync/io/samba/SambaDirectory.kt | 36 ++++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'libs/samba/src/main/java') 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) -- cgit v1.2.3-70-g09d2