diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2024-07-25 19:09:41 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2024-07-25 19:09:41 +0200 |
| commit | b0d90f32974f6473552d8b1bf5387f9fc4995970 (patch) | |
| tree | 8908d0271fadd125f97243c1e1f1ff908d08376a /libs/samba/src/main/cpp/samba.cpp | |
| parent | c857f1cd645ba379573e4266fb185b5432e66404 (diff) | |
samba: Add credentials object
Ask for enough permissions to be able to create sockets.
Diffstat (limited to 'libs/samba/src/main/cpp/samba.cpp')
| -rw-r--r-- | libs/samba/src/main/cpp/samba.cpp | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/libs/samba/src/main/cpp/samba.cpp b/libs/samba/src/main/cpp/samba.cpp index 5eafacc..e3b689d 100644 --- a/libs/samba/src/main/cpp/samba.cpp +++ b/libs/samba/src/main/cpp/samba.cpp @@ -1,6 +1,7 @@ #include <cassert> #include <jni.h> #include <memory> +#include <optional> #include <string> #include <string_view> #include <utility> @@ -52,7 +53,9 @@ class Url { class Context { public: - Context() : context_(smb2_init_context(), ContextDeleter{}) {} + explicit Context(int timeout) : context_(smb2_init_context(), ContextDeleter{}) { + smb2_set_timeout(context_.get(), timeout); + } ~Context() = default; Context(const Context&) = delete; @@ -63,8 +66,10 @@ class Context { return ptr ? std::make_unique<Url>(ptr): nullptr; } - bool Connect(const Url& url) { - return smb2_connect_share(context_.get(), url.server(), url.share(), url.user()) == 0; + bool Connect(const Url& url, const std::optional<std::string>& username, const std::optional<std::string>& password) { + if (password.has_value()) smb2_set_password(context_.get(), password->c_str()); + auto* user = username.has_value() ? username->c_str() : url.user(); + return smb2_connect_share(context_.get(), url.server(), url.share(), user) == 0; } [[nodiscard]] std::string_view GetError() { @@ -86,8 +91,8 @@ class Context { std::shared_ptr<smb2_context> context_; }; -jlong nativeContextNew(JNIEnv* env, jclass clazz) { - return reinterpret_cast<jlong>(new Context()); +jlong nativeContextNew(JNIEnv* env, jclass clazz, jint timeout) { + return reinterpret_cast<jlong>(new Context(static_cast<int>(timeout))); } void nativeContextDestroy(JNIEnv* env, jclass clazz, jlong ptr) { @@ -98,10 +103,14 @@ jlong nativeContextParseUrl(JNIEnv* env, jclass clazz, jlong ptr, jstring url) { return reinterpret_cast<jlong>(reinterpret_cast<Context*>(ptr)->ParseUrl(jni::StringToUTF8(env, jni::ParamRef(env, url))).release()); } -jboolean nativeContextConnect(JNIEnv* env, jclass clazz, jlong context_ptr, jlong url_ptr) { +jboolean nativeContextConnect(JNIEnv* env, jclass clazz, jlong context_ptr, jlong url_ptr, jstring j_username, jstring j_password) { auto* url = reinterpret_cast<Url*>(url_ptr); if (!url) return JNI_FALSE; - return reinterpret_cast<Context*>(context_ptr)->Connect(*url) ? JNI_TRUE : JNI_FALSE; + std::optional<std::string> username; + std::optional<std::string> password; + if (j_username) username = jni::StringToUTF8(env, jni::ParamRef<jstring>(env, j_username)); + if (j_password) password = jni::StringToUTF8(env, jni::ParamRef<jstring>(env, j_password)); + return reinterpret_cast<Context*>(context_ptr)->Connect(*url, username, password) ? JNI_TRUE : JNI_FALSE; } jstring nativeContextGetError(JNIEnv* env, jclass clazz, jlong ptr) { @@ -128,10 +137,10 @@ void RegisterSamba(JNIEnv* env) { auto clazz = jni::FindClass(env, "org/the_jk/cleversync/io/samba/NativeSamba"); ABORT_IF_NULL(env, clazz); static const JNINativeMethod methods[] = { - { "nativeContextNew", "()J", reinterpret_cast<void*>(&nativeContextNew) }, + { "nativeContextNew", "(I)J", reinterpret_cast<void*>(&nativeContextNew) }, { "nativeContextDestroy", "(J)V", reinterpret_cast<void*>(&nativeContextDestroy) }, { "nativeContextParseUrl", "(JLjava/lang/String;)J", reinterpret_cast<void*>(&nativeContextParseUrl) }, - { "nativeContextConnect", "(JJ)Z", reinterpret_cast<void*>(&nativeContextConnect) }, + { "nativeContextConnect", "(JJLjava/lang/String;Ljava/lang/String;)Z", reinterpret_cast<void*>(&nativeContextConnect) }, { "nativeContextGetError", "(J)Ljava/lang/String;", reinterpret_cast<void*>(&nativeContextGetError) }, { "nativeContextOpenDir", "(JLjava/lang/String;)J", reinterpret_cast<void*>(&nativeContextOpenDir) }, |
