summaryrefslogtreecommitdiff
path: root/libs/sftp/src
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-10-14 21:41:06 +0200
committerJoel Klinghed <the_jk@spawned.biz>2024-10-14 22:45:57 +0200
commitea9621389bfa62cb4e63688249c52ac0e41ff282 (patch)
tree4932900d0c058aa7e187fc02b214a024f801db18 /libs/sftp/src
parent2be5a5171de2ecd51973862c243aecc0be4a0876 (diff)
Add tests for create dir/file/link that already exists
Fix implementations to work as expected (that createDirectory/File/Link fails if an entry with that name already exists).
Diffstat (limited to 'libs/sftp/src')
-rw-r--r--libs/sftp/src/main/cpp/sftp.cpp2
-rw-r--r--libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpDirectory.kt3
-rw-r--r--libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpLink.kt2
3 files changed, 5 insertions, 2 deletions
diff --git a/libs/sftp/src/main/cpp/sftp.cpp b/libs/sftp/src/main/cpp/sftp.cpp
index 0feafb1..1bc4fcb 100644
--- a/libs/sftp/src/main/cpp/sftp.cpp
+++ b/libs/sftp/src/main/cpp/sftp.cpp
@@ -218,8 +218,6 @@ class SftpSession {
}
bool Symlink(const std::string &target, const std::string &path) {
- // symlink fails is path already exists, so remove any existing entry first.
- libssh2_sftp_unlink_ex(sftp_->get(), path.data(), path.size());
// The argument order does not seem to match the documentation, so this
// might change?
return libssh2_sftp_symlink_ex(sftp_->get(), target.data(), target.size(),
diff --git a/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpDirectory.kt b/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpDirectory.kt
index 90a3127..f1ae513 100644
--- a/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpDirectory.kt
+++ b/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpDirectory.kt
@@ -12,6 +12,7 @@ import org.the_jk.cleversync.io.ModifiableDirectory
import org.the_jk.cleversync.io.ModifiableFile
import org.the_jk.cleversync.io.ModifiableLink
import java.io.IOException
+import java.nio.file.FileAlreadyExistsException
import java.time.Instant
import kotlin.time.Duration.Companion.seconds
@@ -111,6 +112,8 @@ internal open class SftpDirectory(
override fun createFile(name: String): ModifiableFile {
val newPath = PathUtils.join(path, name)
+ val entry = conn.entry(newPath, followLink = false)
+ if (entry != null) throw FileAlreadyExistsException(name)
return SftpFile(conn, newPath, name, 0UL, Instant.EPOCH, Instant.EPOCH)
}
diff --git a/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpLink.kt b/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpLink.kt
index a922e26..0766f1a 100644
--- a/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpLink.kt
+++ b/libs/sftp/src/main/java/org/the_jk/cleversync/io/sftp/SftpLink.kt
@@ -40,6 +40,8 @@ internal class SftpLink(
}
private fun target(name: String, rawTarget: Boolean) {
+ // conn.symlink fails if path already exists
+ conn.unlink(path)
if (!conn.symlink(name, rawTarget, path)) throw IOException(conn.error)
}