diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 700b05db2722..727761674a88 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -7,9 +7,16 @@ set(CMAKE_CXX_EXTENSIONS OFF) find_package(odrcore REQUIRED) +add_library(tmpfile_hack STATIC + src/main/cpp/tmpfile_hack.cpp) +target_include_directories(tmpfile_hack + PRIVATE src/main/cpp) +target_link_libraries(tmpfile_hack + PRIVATE log) + add_library(odr-core SHARED - src/main/cpp/CoreWrapper.cpp) + src/main/cpp/core_wrapper.cpp) target_include_directories(odr-core PRIVATE src/main/cpp) target_link_libraries(odr-core - PRIVATE odrcore::odrcore log) + PRIVATE odrcore::odrcore tmpfile_hack log) diff --git a/app/conanprofile.txt b/app/conanprofile.txt index 19fd9e44bb3f..ca13515ba7c9 100644 --- a/app/conanprofile.txt +++ b/app/conanprofile.txt @@ -9,7 +9,7 @@ compiler.cppstd=20 compiler.libcxx=c++_shared &:build_type=RelWithDebInfo odrcore/*:build_type=RelWithDebInfo -build_type=Release +build_type=RelWithDebInfo [conf] tools.android:ndk_path=@NDK_PATH@ diff --git a/app/src/main/cpp/CoreWrapper.hpp b/app/src/main/cpp/CoreWrapper.hpp deleted file mode 100644 index bad6da67a653..000000000000 --- a/app/src/main/cpp/CoreWrapper.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef ANDROID_CORE_WRAPPER_H -#define ANDROID_CORE_WRAPPER_H - -#include - -extern "C" { - - JNIEXPORT void JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz, - jobject params); - - JNIEXPORT jobject JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz, jobject options); - - JNIEXPORT jobject JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz, jobject options, jstring htmlDiff); - - JNIEXPORT void JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz, jobject options); - - JNIEXPORT void JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, jstring outputPath); - - JNIEXPORT jobject JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jstring prefix, jobject options); - - JNIEXPORT void JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz, jint port); - - JNIEXPORT void JNICALL - Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz); -} - -#endif //ANDROID_CORE_WRAPPER_H diff --git a/app/src/main/cpp/CoreWrapper.cpp b/app/src/main/cpp/core_wrapper.cpp similarity index 98% rename from app/src/main/cpp/CoreWrapper.cpp rename to app/src/main/cpp/core_wrapper.cpp index b2aa86f677b9..d2ad09aedfff 100644 --- a/app/src/main/cpp/CoreWrapper.cpp +++ b/app/src/main/cpp/core_wrapper.cpp @@ -1,4 +1,6 @@ -#include "CoreWrapper.hpp" +#include "core_wrapper.hpp" + +#include "tmpfile_hack.hpp" #include #include @@ -80,19 +82,20 @@ std::optional s_document; JNIEXPORT void JNICALL Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz, jobject params) { - jboolean isCopy; - jclass paramsClass = env->GetObjectClass(params); std::string odrCoreDataPath = getStringField(env, paramsClass, params, "coreDataPath"); std::string fontconfigDataPath = getStringField(env, paramsClass, params, "fontconfigDataPath"); std::string popplerDataPath = getStringField(env, paramsClass, params, "popplerDataPath"); std::string pdf2htmlexDataPath = getStringField(env, paramsClass, params, "pdf2htmlexDataPath"); + std::string customTmpfilePath = getStringField(env, paramsClass, params, "customTmpfilePath"); odr::GlobalParams::set_odr_core_data_path(odrCoreDataPath); odr::GlobalParams::set_fontconfig_data_path(fontconfigDataPath); odr::GlobalParams::set_poppler_data_path(popplerDataPath); odr::GlobalParams::set_pdf2htmlex_data_path(pdf2htmlexDataPath); + + tmpfile_hack::set_tmpfile_directory(customTmpfilePath); } JNIEXPORT jobject JNICALL diff --git a/app/src/main/cpp/core_wrapper.hpp b/app/src/main/cpp/core_wrapper.hpp new file mode 100644 index 000000000000..9c29e6175192 --- /dev/null +++ b/app/src/main/cpp/core_wrapper.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include + +extern "C" { + +JNIEXPORT void JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz, + jobject params); + +JNIEXPORT jobject JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz, + jobject options); + +JNIEXPORT jobject JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz, + jobject options, + jstring htmlDiff); + +JNIEXPORT void JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz, + jobject options); + +JNIEXPORT void JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, + jstring outputPath); + +JNIEXPORT jobject JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, + jstring prefix, jobject options); + +JNIEXPORT void JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz, + jint port); + +JNIEXPORT void JNICALL +Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz); + +} diff --git a/app/src/main/cpp/tmpfile_hack.cpp b/app/src/main/cpp/tmpfile_hack.cpp new file mode 100644 index 000000000000..b01e0a565640 --- /dev/null +++ b/app/src/main/cpp/tmpfile_hack.cpp @@ -0,0 +1,59 @@ +#include "tmpfile_hack.hpp" + +#include +#include +#include + +#include + +#include +#include + +static constexpr std::string_view s_filename_template = "tmpfile-XXXXXX"; +static constexpr std::string_view s_default_directory = "/data/local/tmp"; + +static std::optional s_tmpfile_directory; + +std::string tmpfile_hack::get_tmpfile_directory() { + if (s_tmpfile_directory.has_value()) { + return s_tmpfile_directory.value(); + } + + if (const char *tmpfile_directory = std::getenv("TMPDIR"); tmpfile_directory != nullptr) { + return tmpfile_directory; + } + + return std::string(s_default_directory); +} + +void tmpfile_hack::set_tmpfile_directory(std::string_view tmpfile_dir) { + s_tmpfile_directory = std::string(tmpfile_dir); +} + +extern "C" { + +extern FILE *tmpfile() { + std::string tmpfile_path = + tmpfile_hack::get_tmpfile_directory() + "/" + std::string(s_filename_template); + + int descriptor = mkstemp(tmpfile_path.data()); + if (descriptor == -1) { + __android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack", + "Failed to create temporary file: %s", tmpfile_path.c_str()); + return nullptr; + } + __android_log_print(ANDROID_LOG_VERBOSE, "tmpfile_hack", "Temporary file created: %s", + tmpfile_path.c_str()); + + FILE *handle = fdopen(descriptor, "w+b"); + unlink(tmpfile_path.c_str()); + + if (handle == nullptr) { + close(descriptor); + __android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack", "Failed to open temporary file: %s", + tmpfile_path.c_str()); + } + return handle; +} + +} diff --git a/app/src/main/cpp/tmpfile_hack.hpp b/app/src/main/cpp/tmpfile_hack.hpp new file mode 100644 index 000000000000..cf15d3756569 --- /dev/null +++ b/app/src/main/cpp/tmpfile_hack.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace tmpfile_hack { + + std::string get_tmpfile_directory(); + + void set_tmpfile_directory(std::string_view tmpfile_dir); + +} diff --git a/app/src/main/java/at/tomtasche/reader/background/CoreLoader.java b/app/src/main/java/at/tomtasche/reader/background/CoreLoader.java index 997609a17d34..bbaef268b988 100644 --- a/app/src/main/java/at/tomtasche/reader/background/CoreLoader.java +++ b/app/src/main/java/at/tomtasche/reader/background/CoreLoader.java @@ -7,7 +7,6 @@ import android.webkit.MimeTypeMap; import java.io.File; -import java.util.Objects; import at.tomtasche.reader.nonfree.AnalyticsManager; import at.tomtasche.reader.nonfree.ConfigManager; diff --git a/app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java b/app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java index 4c337f9c3b11..3e15ba00fcfa 100644 --- a/app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java +++ b/app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java @@ -18,6 +18,7 @@ public static class GlobalParams { public String fontconfigDataPath; public String popplerDataPath; public String pdf2htmlexDataPath; + public String customTmpfilePath; } public static native void setGlobalParams(GlobalParams params); @@ -41,6 +42,7 @@ public static void initialize(Context context) { globalParams.fontconfigDataPath = fontconfigDataDirectory.getAbsolutePath(); globalParams.popplerDataPath = popplerDataDirectory.getAbsolutePath(); globalParams.pdf2htmlexDataPath = pdf2htmlexDataDirectory.getAbsolutePath(); + globalParams.customTmpfilePath = context.getCacheDir().getAbsolutePath(); CoreWrapper.setGlobalParams(globalParams); }