summaryrefslogtreecommitdiff
path: root/libs/samba/src/main/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libs/samba/src/main/cpp')
-rw-r--r--libs/samba/src/main/cpp/jni.cpp100
-rw-r--r--libs/samba/src/main/cpp/jni.hpp142
-rw-r--r--libs/samba/src/main/cpp/samba.cpp155
3 files changed, 397 insertions, 0 deletions
diff --git a/libs/samba/src/main/cpp/jni.cpp b/libs/samba/src/main/cpp/jni.cpp
new file mode 100644
index 0000000..be13df7
--- /dev/null
+++ b/libs/samba/src/main/cpp/jni.cpp
@@ -0,0 +1,100 @@
+#include "jni.hpp"
+
+#include <android/log.h>
+
+namespace {
+
+JavaVM *g_vm;
+
+const char *_jni_error(jint err) {
+ switch (err) {
+ case JNI_OK:
+ return "OK";
+ case JNI_ERR:
+ return "Unknown error";
+ case JNI_EDETACHED:
+ return "Thread detached from the VM";
+ case JNI_EVERSION:
+ return "JNI version error";
+ case JNI_ENOMEM:
+ return "Not enough memory";
+ case JNI_EEXIST:
+ return "VM already created";
+ case JNI_EINVAL:
+ return "Invalid arguments";
+ default:
+ return "Unexpected error";
+ }
+}
+
+} // namespace
+
+namespace jni {
+
+namespace internal {
+
+void _abort_if_not_ok(const char *file, int line, jint ret) {
+ if (ret == JNI_OK) [[likely]] return;
+ __android_log_assert(nullptr, "jni", "JNI error: %s", _jni_error(ret));
+}
+
+void _abort_with_exception(const char* file, int line, JNIEnv* env) {
+ auto throwable = jni::ExceptionOccurred(env);
+ env->ExceptionClear();
+ if (throwable) {
+ auto throwable_class = jni::FindClass(env, "java/lang/Throwable");
+ if (throwable_class) {
+ auto throwable_toString = env->GetMethodID(throwable_class.get(),
+ "toString",
+ "()Ljava/lang/String;");
+ if (throwable_toString) {
+ auto description = jni::CallObjectMethod<jstring>(env, throwable,
+ throwable_toString);
+ auto str = jni::StringToUTF8(env, description);
+ __android_log_assert(nullptr, "jni", "JNI error: %s", str.c_str());
+ }
+ }
+ env->ExceptionClear();
+ __android_log_assert(nullptr, "jni",
+ "Unexpected NULL but no exception");
+ }
+}
+
+} // namespace internal
+
+JNIEnv* AttachCurrentThread() {
+ JNIEnv* env;
+ auto ret = g_vm->AttachCurrentThread(&env, nullptr);
+ ABORT_IF_NOT_OK(ret);
+ return env;
+}
+
+JNIEnv* OnLoad(JavaVM* vm) {
+ void* v_env;
+ auto ret = vm->GetEnv(&v_env, JNI_VERSION);
+ ABORT_IF_NOT_OK(ret);
+ return reinterpret_cast<JNIEnv*>(v_env);
+}
+
+LocalRef<jclass> FindClass(JNIEnv *env, const char *name) {
+ return {env, env->FindClass(name)};
+}
+
+LocalRef<jthrowable> ExceptionOccurred(JNIEnv* env) {
+ return {env, env->ExceptionOccurred()};
+}
+
+std::string StringToUTF8(JNIEnv* env, const Ref<jstring>& str) {
+ if (!str) return "null";
+ auto len = env->GetStringUTFLength(str.get());
+ std::string ret(len, ' ');
+ env->GetStringUTFRegion(str.get(), 0, len, ret.data());
+ // This returns modified UTF-8 encoding, don't care.
+ return ret;
+}
+
+LocalRef<jstring> UTF8ToString(JNIEnv* env, const std::string& str) {
+ return {env, env->NewStringUTF(str.c_str())};
+}
+
+} // namespace jni
diff --git a/libs/samba/src/main/cpp/jni.hpp b/libs/samba/src/main/cpp/jni.hpp
new file mode 100644
index 0000000..90ca011
--- /dev/null
+++ b/libs/samba/src/main/cpp/jni.hpp
@@ -0,0 +1,142 @@
+#ifndef CLEVERSYNC_JNI_HPP
+#define CLEVERSYNC_JNI_HPP
+
+#include <jni.h>
+#include <string>
+
+#define ABORT_IF_NOT_OK(x) (::jni::internal::_abort_if_not_ok(__FILE__, __LINE__, (x)))
+#define ABORT_IF_NULL(env, x) (::jni::internal::_abort_if_null(__FILE__, __LINE__, (env), (x)))
+
+namespace jni {
+
+namespace internal {
+
+void _abort_if_not_ok(const char *file, int line, jint ret);
+void _abort_with_exception(const char* file, int line, JNIEnv* env);
+
+} // namespace internal
+
+template<class T>
+class Ref {
+ public:
+ constexpr Ref() : env_(nullptr), ptr_(0) {}
+ Ref(const Ref<T>&) = delete;
+ Ref<T>& operator=(const Ref<T>&) = delete;
+
+ [[nodiscard]] T get() const { return ptr_; }
+ [[nodiscard]] T release() {
+ auto ret = release_to_local();
+ ptr_ = 0;
+ return ret;
+ }
+
+ void reset() {
+ del();
+ ptr_ = 0;
+ }
+
+ explicit operator bool() const { return ptr_ != 0; }
+
+ protected:
+ Ref(JNIEnv* env, T ptr): env_(env), ptr_(ptr) {}
+ virtual ~Ref() = default;
+
+ virtual T release_to_local() = 0;
+ virtual void del() = 0;
+
+ JNIEnv* const env_;
+ T ptr_;
+};
+
+template<class T>
+class LocalRef : public Ref<T> {
+ public:
+ LocalRef(JNIEnv* env, T ptr): Ref<T>(env, ptr) {}
+ ~LocalRef() override { free(); }
+
+ protected:
+ T release_to_local() override { return this->ptr_; }
+ void del() override { free(); }
+
+ private:
+ void free() {
+ if (this->ptr_)
+ this->env_->DeleteLocalRef(this->ptr_);
+ }
+};
+
+template<class T>
+class ParamRef : public Ref<T> {
+ public:
+ ParamRef(JNIEnv* env, T ptr) : Ref<T>(env, ptr) {}
+ ~ParamRef() override = default;
+
+ protected:
+ T release_to_local() override {
+ if (this->ptr_)
+ return static_cast<T>(this->env_->NewLocalRef(static_cast<jobject>(this->ptr_)));
+ return 0;
+ }
+ void del() override {}
+};
+
+template<class T>
+class GlobalRef : public Ref<T> {
+ public:
+ GlobalRef(JNIEnv* env, T ptr) : Ref<T>(env, ptr ? env->NewGlobalRef(ptr) : 0) {}
+ explicit GlobalRef(const Ref<T>& other) : Ref<T>(other.env_, other ? other.env_->NewGlobalRef(other.ptr_) : 0) {}
+
+ ~GlobalRef() override { free(); }
+
+ protected:
+ T release_to_local() override {
+ if (this->ptr_) {
+ auto ret = static_cast<T>(this->env_->NewLocalRef(
+ static_cast<jobject>(this->ptr_)));
+ free();
+ return ret;
+ }
+ return 0;
+ }
+ void del() override { free(); }
+
+ private:
+ void free() {
+ if (this->ptr_)
+ this->env_->DeleteGlobalRef(this->ptr_);
+ }
+};
+
+constexpr jint JNI_VERSION = JNI_VERSION_1_2;
+
+JNIEnv* AttachCurrentThread();
+
+JNIEnv* OnLoad(JavaVM* vm);
+
+LocalRef<jclass> FindClass(JNIEnv *env, const char *name);
+
+LocalRef<jthrowable> ExceptionOccurred(JNIEnv* env);
+
+template<typename Out, typename In>
+LocalRef<Out> CallObjectMethod(JNIEnv* env, const Ref<In>& object, jmethodID method) {
+ return {env, static_cast<Out>(env->CallObjectMethod(object.get(), method))};
+}
+
+std::string StringToUTF8(JNIEnv* env, const Ref<jstring>& str);
+
+LocalRef<jstring> UTF8ToString(JNIEnv* env, const std::string& str);
+
+namespace internal {
+
+template<typename T>
+void _abort_if_null(const char* file, int line, JNIEnv* env, const jni::Ref<T>& ref) {
+ if (ref) [[likely]] return;
+
+ _abort_with_exception(file, line, env);
+}
+
+} // namespace internal
+
+} // namespace jni
+
+#endif // CLEVERSYNC_JNI_HPP
diff --git a/libs/samba/src/main/cpp/samba.cpp b/libs/samba/src/main/cpp/samba.cpp
new file mode 100644
index 0000000..5eafacc
--- /dev/null
+++ b/libs/samba/src/main/cpp/samba.cpp
@@ -0,0 +1,155 @@
+#include <cassert>
+#include <jni.h>
+#include <memory>
+#include <string>
+#include <string_view>
+#include <utility>
+
+#include "jni.hpp"
+#include "libsmb2.h"
+
+namespace {
+
+class Dir {
+ public:
+ Dir(std::shared_ptr<smb2_context> context, smb2dir* dir) : context_(std::move(context)), dir_(dir) {
+ assert(context_ && dir_);
+ }
+
+ ~Dir() {
+ smb2_closedir(context_.get(), dir_);
+ }
+
+ Dir(const Dir&) = delete;
+ Dir& operator=(const Dir&) = delete;
+
+ private:
+ std::shared_ptr<smb2_context> context_;
+ smb2dir* const dir_;
+};
+
+class Url {
+ public:
+ explicit Url(smb2_url* url) : url_(url) {
+ assert(url_);
+ }
+
+ ~Url() {
+ smb2_destroy_url(url_);
+ }
+
+ Url(const Url&) = delete;
+ Url& operator=(const Url&) = delete;
+
+ [[nodiscard]] const char* path() const { return url_->path; }
+ [[nodiscard]] const char* server() const { return url_->server; }
+ [[nodiscard]] const char* share() const { return url_->share; }
+ [[nodiscard]] const char* user() const { return url_->user; }
+
+ private:
+ smb2_url* url_;
+};
+
+class Context {
+ public:
+ Context() : context_(smb2_init_context(), ContextDeleter{}) {}
+ ~Context() = default;
+
+ Context(const Context&) = delete;
+ Context& operator=(const Context&) = delete;
+
+ [[nodiscard]] std::unique_ptr<Url> ParseUrl(const std::string& url) {
+ auto* ptr = smb2_parse_url(context_.get(), url.c_str());
+ 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;
+ }
+
+ [[nodiscard]] std::string_view GetError() {
+ return smb2_get_error(context_.get());
+ }
+
+ [[nodiscard]] std::unique_ptr<Dir> OpenDir(const std::string& path) {
+ auto* ptr = smb2_opendir(context_.get(), path.c_str());
+ return ptr ? std::make_unique<Dir>(context_, ptr) : nullptr;
+ }
+
+ private:
+ struct ContextDeleter {
+ void operator()(smb2_context* context) {
+ smb2_destroy_context(context);
+ }
+ };
+
+ std::shared_ptr<smb2_context> context_;
+};
+
+jlong nativeContextNew(JNIEnv* env, jclass clazz) {
+ return reinterpret_cast<jlong>(new Context());
+}
+
+void nativeContextDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
+ delete reinterpret_cast<Context*>(ptr);
+}
+
+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) {
+ auto* url = reinterpret_cast<Url*>(url_ptr);
+ if (!url) return JNI_FALSE;
+ return reinterpret_cast<Context*>(context_ptr)->Connect(*url) ? JNI_TRUE : JNI_FALSE;
+}
+
+jstring nativeContextGetError(JNIEnv* env, jclass clazz, jlong ptr) {
+ return jni::UTF8ToString(env, std::string(reinterpret_cast<Context*>(ptr)->GetError())).release();
+}
+
+jlong nativeContextOpenDir(JNIEnv* env, jclass clazz, jlong ptr, jstring path) {
+ return reinterpret_cast<jlong>(reinterpret_cast<Context*>(ptr)->OpenDir(jni::StringToUTF8(env, jni::ParamRef<jstring>(env, path))).release());
+}
+
+void nativeUrlDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
+ delete reinterpret_cast<Url*>(ptr);
+}
+
+jstring nativeUrlPath(JNIEnv* env, jclass clazz, jlong ptr) {
+ return jni::UTF8ToString(env, std::string(reinterpret_cast<Url*>(ptr)->path())).release();
+}
+
+void nativeDirDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
+ delete reinterpret_cast<Dir*>(ptr);
+}
+
+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) },
+ { "nativeContextDestroy", "(J)V", reinterpret_cast<void*>(&nativeContextDestroy) },
+ { "nativeContextParseUrl", "(JLjava/lang/String;)J", reinterpret_cast<void*>(&nativeContextParseUrl) },
+ { "nativeContextConnect", "(JJ)Z", reinterpret_cast<void*>(&nativeContextConnect) },
+ { "nativeContextGetError", "(J)Ljava/lang/String;", reinterpret_cast<void*>(&nativeContextGetError) },
+ { "nativeContextOpenDir", "(JLjava/lang/String;)J", reinterpret_cast<void*>(&nativeContextOpenDir) },
+
+ { "nativeUrlDestroy", "(J)V", reinterpret_cast<void*>(&nativeUrlDestroy) },
+ { "nativeUrlPath", "(J)Ljava/lang/String;", reinterpret_cast<void*>(&nativeUrlPath) },
+
+ { "nativeDirDestroy", "(J)V", reinterpret_cast<void*>(&nativeDirDestroy) },
+ };
+ auto ret = env->RegisterNatives(clazz.get(), methods, sizeof(methods) / sizeof(methods[0]));
+ ABORT_IF_NOT_OK(ret);
+}
+
+} // namespace
+
+jint JNI_OnLoad(JavaVM *vm, void *reserved) {
+ auto* env = jni::OnLoad(vm);
+
+ RegisterSamba(env);
+
+ return jni::JNI_VERSION;
+}