Skip to content

Commit 35f2f94

Browse files
Expose no forking mode
1 parent b10907d commit 35f2f94

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

pdf2htmlEX/src/main/cpp/pdf2htmlEX.cc

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_set_1environment_1value(JN
8383
setenv(key.c_str(), value.c_str(), 1);
8484
}
8585

86+
static bool forkBeforeConverting = true;
87+
JNIEXPORT void JNICALL
88+
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_setNoForking(JNIEnv *, jobject) {
89+
forkBeforeConverting = false;
90+
}
91+
8692
extern "C"
8793
JNIEXPORT jint JNICALL
8894
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_call_1pdf2htmlEX(JNIEnv *env, jobject,
@@ -128,16 +134,26 @@ Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_call_1pdf2htmlEX(JNIEnv *e
128134
// Upstream library is actually an executable, not a library.
129135
// May have some global state, initialized as static constructor, poisoned after first use.
130136
// Workaround: fork process before use.
131-
pid_t pid = fork();
137+
pid_t pid = 0;
138+
if (forkBeforeConverting) {
139+
pid = fork();
140+
if (0 < pid) {
141+
int waitStatus;
142+
waitpid(pid, &waitStatus, 0);
143+
if (WIFEXITED(waitStatus)) {
144+
retVal = WEXITSTATUS(waitStatus);
145+
} else {
146+
retVal = 4;
147+
}
148+
} else if (-1 == pid) {
149+
retVal = 3;
150+
}
151+
}
152+
132153
if (0 == pid) {
133154
retVal = pdf2htmlEX_main(argc, argv);
134-
exit(retVal);
135-
}
136-
else if (0 < pid) {
137-
int wstatus;
138-
waitpid(pid, &wstatus, 0);
139-
if (WIFEXITED(wstatus)) {
140-
retVal = WEXITSTATUS(wstatus);
155+
if (forkBeforeConverting) {
156+
exit(retVal);
141157
}
142158
}
143159

@@ -148,3 +164,4 @@ Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_call_1pdf2htmlEX(JNIEnv *e
148164

149165
return retVal;
150166
}
167+

pdf2htmlEX/src/main/java/com/viliussutkus89/android/pdf2htmlex/pdf2htmlEX.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public final class pdf2htmlEX {
4343
private String p_ownerPassword = "";
4444
private String p_userPassword = "";
4545

46+
private boolean m_noForkingConversionAlreadyDone = false;
47+
4648
public static class ConversionFailedException extends Exception {
4749
public ConversionFailedException(String errorMessage) {
4850
super(errorMessage);
@@ -110,7 +112,19 @@ public pdf2htmlEX setUserPassword(@NonNull String userPassword) {
110112
return this;
111113
}
112114

115+
public pdf2htmlEX setNoForking(@NonNull boolean iDoUnderstandThatIWillHaveToRestartTheAppBeforeICanRunConversionForTheSecondTime) throws IllegalArgumentException {
116+
if (!iDoUnderstandThatIWillHaveToRestartTheAppBeforeICanRunConversionForTheSecondTime) {
117+
throw new IllegalArgumentException();
118+
}
119+
setNoForking();
120+
return this;
121+
}
122+
113123
public File convert() throws IOException, ConversionFailedException {
124+
if (this.m_noForkingConversionAlreadyDone) {
125+
throw new ConversionFailedException("No forking mode allows only one conversion!");
126+
}
127+
114128
if (null == this.p_inputPDF) {
115129
throw new ConversionFailedException("No Input PDF given!");
116130
}
@@ -135,6 +149,8 @@ public File convert() throws IOException, ConversionFailedException {
135149
this.p_ownerPassword, this.p_userPassword
136150
);
137151

152+
this.m_noForkingConversionAlreadyDone = true;
153+
138154
if (0 != retVal) {
139155
outputHtml.delete();
140156
throw new ConversionFailedException("Conversion failed. Return value from pdf2htmlEX: " + retVal.toString());
@@ -148,4 +164,5 @@ public File convert() throws IOException, ConversionFailedException {
148164
// Because Java cannot setenv for the current process
149165
static native void set_environment_value(String key, String value);
150166

167+
private native void setNoForking();
151168
}

0 commit comments

Comments
 (0)