Skip to content

Commit 62e5ff8

Browse files
authored
fix tmpfile (#424)
1 parent bc63434 commit 62e5ff8

9 files changed

Lines changed: 128 additions & 41 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/conanprofile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ compiler.cppstd=20
99
compiler.libcxx=c++_shared
1010
&:build_type=RelWithDebInfo
1111
odrcore/*:build_type=RelWithDebInfo
12-
build_type=Release
12+
build_type=RelWithDebInfo
1313

1414
[conf]
1515
tools.android:ndk_path=@NDK_PATH@

app/src/main/cpp/CoreWrapper.hpp

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 6 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>
@@ -80,19 +82,20 @@ std::optional<odr::Document> s_document;
8082
JNIEXPORT void JNICALL
8183
Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz,
8284
jobject params) {
83-
jboolean isCopy;
84-
8585
jclass paramsClass = env->GetObjectClass(params);
8686

8787
std::string odrCoreDataPath = getStringField(env, paramsClass, params, "coreDataPath");
8888
std::string fontconfigDataPath = getStringField(env, paramsClass, params, "fontconfigDataPath");
8989
std::string popplerDataPath = getStringField(env, paramsClass, params, "popplerDataPath");
9090
std::string pdf2htmlexDataPath = getStringField(env, paramsClass, params, "pdf2htmlexDataPath");
91+
std::string customTmpfilePath = getStringField(env, paramsClass, params, "customTmpfilePath");
9192

9293
odr::GlobalParams::set_odr_core_data_path(odrCoreDataPath);
9394
odr::GlobalParams::set_fontconfig_data_path(fontconfigDataPath);
9495
odr::GlobalParams::set_poppler_data_path(popplerDataPath);
9596
odr::GlobalParams::set_pdf2htmlex_data_path(pdf2htmlexDataPath);
97+
98+
tmpfile_hack::set_tmpfile_directory(customTmpfilePath);
9699
}
97100

98101
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+
tmpfile_hack::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+
}

app/src/main/java/at/tomtasche/reader/background/CoreLoader.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import android.webkit.MimeTypeMap;
88

99
import java.io.File;
10-
import java.util.Objects;
1110

1211
import at.tomtasche.reader.nonfree.AnalyticsManager;
1312
import at.tomtasche.reader.nonfree.ConfigManager;

app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class GlobalParams {
1818
public String fontconfigDataPath;
1919
public String popplerDataPath;
2020
public String pdf2htmlexDataPath;
21+
public String customTmpfilePath;
2122
}
2223

2324
public static native void setGlobalParams(GlobalParams params);
@@ -41,6 +42,7 @@ public static void initialize(Context context) {
4142
globalParams.fontconfigDataPath = fontconfigDataDirectory.getAbsolutePath();
4243
globalParams.popplerDataPath = popplerDataDirectory.getAbsolutePath();
4344
globalParams.pdf2htmlexDataPath = pdf2htmlexDataDirectory.getAbsolutePath();
45+
globalParams.customTmpfilePath = context.getCacheDir().getAbsolutePath();
4446
CoreWrapper.setGlobalParams(globalParams);
4547
}
4648

0 commit comments

Comments
 (0)