Skip to content

Commit 66b14db

Browse files
committed
Improved working directory detection
Fixes #417
1 parent 82718ec commit 66b14db

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/main/java/net/raphimc/viaproxy/ViaProxy.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import java.lang.instrument.Instrumentation;
6565
import java.lang.reflect.InvocationTargetException;
6666
import java.nio.file.Files;
67+
import java.nio.file.Path;
6768
import java.util.ArrayList;
6869
import java.util.List;
6970
import java.util.concurrent.CompletableFuture;
@@ -135,6 +136,21 @@ public static void injectedMain(final String injectionMethod, final String[] arg
135136
}
136137
failedCwds.add(potentialCwd);
137138
}
139+
if (CWD == null) { // Backup strategy for weird permission setups: Attempt to write a dummy file to check if the directory is writable
140+
for (File potentialCwd : potentialCwds) {
141+
if (potentialCwd.isDirectory()) {
142+
try {
143+
final Path testFile = new File(potentialCwd, "viaproxy_writable_test.txt").toPath();
144+
Files.deleteIfExists(testFile);
145+
Files.writeString(testFile, "This is just a test. This file can be deleted.");
146+
Files.deleteIfExists(testFile);
147+
CWD = potentialCwd;
148+
break;
149+
} catch (IOException ignored) {
150+
}
151+
}
152+
}
153+
}
138154
if (CWD != null) {
139155
System.setProperty("user.dir", CWD.getAbsolutePath());
140156
} else if (useUI) {

0 commit comments

Comments
 (0)