Skip to content

Commit b93bb8a

Browse files
committed
port hack here
1 parent b722ecb commit b93bb8a

7 files changed

Lines changed: 124 additions & 40 deletions

File tree

app/CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ set(CMAKE_CXX_EXTENSIONS OFF)
77

88
find_package(odrcore REQUIRED)
99

10+
add_library(tmpfile_hack STATIC
11+
src/main/cpp/tmpfile_hack.cpp)
12+
target_include_directories(tmpfile_hack
13+
PRIVATE src/main/cpp)
14+
target_link_libraries(tmpfile_hack
15+
PRIVATE log)
16+
1017
add_library(odr-core SHARED
11-
src/main/cpp/CoreWrapper.cpp)
18+
src/main/cpp/core_wrapper.cpp)
1219
target_include_directories(odr-core
1320
PRIVATE src/main/cpp)
1421
target_link_libraries(odr-core
15-
PRIVATE odrcore::odrcore log)
22+
PRIVATE odrcore::odrcore tmpfile_hack log)

app/conanfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ CMakeToolchain
66
CMakeDeps
77

88
[options]
9-
odrcore/*:shared=True
9+
odrcore/*:shared=False
1010
odrcore/*:with_pdf2htmlEX=True
1111
odrcore/*:with_wvWare=False

app/src/main/cpp/CoreWrapper.hpp

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#include "CoreWrapper.hpp"
1+
#include "core_wrapper.hpp"
2+
3+
#include "tmpfile_hack.hpp"
24

35
#include <odr/document.hpp>
46
#include <odr/file.hpp>
@@ -92,9 +94,8 @@ Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jcl
9294
odr::GlobalParams::set_fontconfig_data_path(fontconfigDataPath);
9395
odr::GlobalParams::set_poppler_data_path(popplerDataPath);
9496
odr::GlobalParams::set_pdf2htmlex_data_path(pdf2htmlexDataPath);
95-
odr::GlobalParams::set_custom_tmpfile_path(customTmpfilePath);
9697

97-
setenv("TMPDIR", customTmpfilePath.c_str(), 1);
98+
tmpfile_hack::set_tmpfile_directory(customTmpfilePath);
9899
}
99100

100101
JNIEXPORT jobject JNICALL

app/src/main/cpp/core_wrapper.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include <jni.h>
4+
5+
extern "C" {
6+
7+
JNIEXPORT void JNICALL
8+
Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz,
9+
jobject params);
10+
11+
JNIEXPORT jobject JNICALL
12+
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
13+
jobject options);
14+
15+
JNIEXPORT jobject JNICALL
16+
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz,
17+
jobject options,
18+
jstring htmlDiff);
19+
20+
JNIEXPORT void JNICALL
21+
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz,
22+
jobject options);
23+
24+
JNIEXPORT void JNICALL
25+
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz,
26+
jstring outputPath);
27+
28+
JNIEXPORT jobject JNICALL
29+
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz,
30+
jstring prefix, jobject options);
31+
32+
JNIEXPORT void JNICALL
33+
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz,
34+
jint port);
35+
36+
JNIEXPORT void JNICALL
37+
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz);
38+
39+
}

app/src/main/cpp/tmpfile_hack.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "tmpfile_hack.hpp"
2+
3+
#include <string>
4+
#include <optional>
5+
#include <random>
6+
7+
#include <android/log.h>
8+
9+
#include <cstdlib>
10+
#include <unistd.h>
11+
12+
static constexpr std::string_view s_filename_template = "tmpfile-XXXXXX";
13+
static constexpr std::string_view s_default_directory = "/data/local/tmp";
14+
15+
static std::optional<std::string> s_tmpfile_directory;
16+
17+
std::string tmpfile_hack::get_tmpfile_directory() {
18+
if (s_tmpfile_directory.has_value()) {
19+
return s_tmpfile_directory.value();
20+
}
21+
22+
if (const char *tmpfile_directory = std::getenv("TMPDIR"); tmpfile_directory != nullptr) {
23+
return tmpfile_directory;
24+
}
25+
26+
return std::string(s_default_directory);
27+
}
28+
29+
void tmpfile_hack::set_tmpfile_directory(std::string_view tmpfile_dir) {
30+
s_tmpfile_directory = std::string(tmpfile_dir);
31+
}
32+
33+
extern "C" {
34+
35+
extern FILE *tmpfile() {
36+
std::string tmpfile_path =
37+
get_tmpfile_directory() + "/" + std::string(s_filename_template);
38+
39+
int descriptor = mkstemp(tmpfile_path.data());
40+
if (descriptor == -1) {
41+
__android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack",
42+
"Failed to create temporary file: %s", tmpfile_path.c_str());
43+
return nullptr;
44+
}
45+
__android_log_print(ANDROID_LOG_VERBOSE, "tmpfile_hack", "Temporary file created: %s",
46+
tmpfile_path.c_str());
47+
48+
FILE *handle = fdopen(descriptor, "w+b");
49+
unlink(tmpfile_path.c_str());
50+
51+
if (handle == nullptr) {
52+
close(descriptor);
53+
__android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack", "Failed to open temporary file: %s",
54+
tmpfile_path.c_str());
55+
}
56+
return handle;
57+
}
58+
59+
}

app/src/main/cpp/tmpfile_hack.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <string_view>
5+
6+
namespace tmpfile_hack {
7+
8+
std::string get_tmpfile_directory();
9+
10+
void set_tmpfile_directory(std::string_view tmpfile_dir);
11+
12+
}

0 commit comments

Comments
 (0)