Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RuntimeLoader fall back to temp directory #5851

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion wpiutil/src/main/java/edu/wpi/first/util/RuntimeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public final class RuntimeLoader<T> {
private static String defaultExtractionRoot;
private static final String fallbackExtractionRoot;

/**
* Gets the default extraction root location (~/.wpilib/nativecache).
Expand All @@ -34,6 +35,20 @@ public static synchronized String getDefaultExtractionRoot() {
return defaultExtractionRoot;
}

/**
* Gets the fallback extraction root location (/tmp/.wpilib/nativecache).
*
* @return The fallback extraction root location.
*/
public static synchronized String getFallbackExtractionRoot() {
if (fallbackExtractionRoot != null) {
return fallbackExtractionRoot;
}
String home = System.getProperty("java.io.tmpdir");
fallbackExtractionRoot = Paths.get(home, ".wpilib", "nativecache").toString();
return fallbackExtractionRoot;
}

private final String m_libraryName;
private final Class<T> m_loadClass;
private final String m_extractionRoot;
Expand Down Expand Up @@ -81,6 +96,20 @@ private String getLoadErrorMessage(UnsatisfiedLinkError ule) {
* @throws IOException if the library fails to load
*/
public void loadLibrary() throws IOException {
try {
loadLibrary(m_extractionRoot)
} catch (NoSuchFileException e) {
// thrown if filesystem is read-only and folders/files cannot be created -- try again in user temp directory
loadLibrary(getFallbackExtractionRoot());
}
}

/**
* Loads a native library.
* @param tryFallback if we should try falling back to extracting to a tempfolder
* @throws IOException if the library fails to load
*/
public void loadLibrary(String extractionRoot) throws IOException {
try {
// First, try loading path
System.loadLibrary(m_libraryName);
Expand All @@ -94,7 +123,7 @@ public void loadLibrary() throws IOException {
}
try (Scanner scanner = new Scanner(hashIs, StandardCharsets.UTF_8)) {
String hash = scanner.nextLine();
File jniLibrary = new File(m_extractionRoot, resName + "." + hash);
File jniLibrary = new File(extractionRoot, resName + "." + hash);
try {
// Try to load from an already extracted hash
System.load(jniLibrary.getAbsolutePath());
Expand Down