Skip to content

Commit 9010522

Browse files
committed
[GR-49849] Revert "Create an internal resource TruffleFile for FileLoader"
* This reverts commit 53f7afc. * No longer necessary, now getInternalTruffleFile() handles InternalResource.
1 parent 5dbdd31 commit 9010522

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/main/java/org/truffleruby/language/loader/FileLoader.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.File;
1313
import java.io.IOException;
14+
import java.util.Locale;
1415

1516
import com.oracle.truffle.api.nodes.Node;
1617
import org.graalvm.collections.Pair;
@@ -56,6 +57,7 @@ public static void ensureReadable(RubyContext context, TruffleFile file, Node cu
5657
}
5758
}
5859

60+
5961
public Pair<Source, TStringWithEncoding> loadFile(String path) throws IOException {
6062
if (context.getOptions().LOG_LOAD) {
6163
RubyLanguage.LOGGER.info("loading " + path);
@@ -77,10 +79,6 @@ public Pair<Source, TStringWithEncoding> loadFile(String path) throws IOExceptio
7779

7880
public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext context, String path) {
7981
final Env env = context.getEnv();
80-
if (env.isFileIOAllowed()) {
81-
return env.getPublicTruffleFile(path);
82-
}
83-
8482
final TruffleFile file;
8583
try {
8684
file = env.getInternalTruffleFile(path).getCanonicalFile();
@@ -90,29 +88,36 @@ public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext
9088
context.getCoreExceptions().loadError("Failed to canonicalize -- " + path, path, null));
9189
}
9290

93-
String homeLibDir = language.getRubyHome() + "/lib/";
94-
if (file.getPath().startsWith(homeLibDir)) {
95-
String homeRelativePath = file.getPath().substring(language.getRubyHome().length() + 1);
96-
TruffleFile internalResourceFile = language.getRubyHomeTruffleFile().resolve(homeRelativePath);
97-
if (isStdLibRubyOrCExtFile(internalResourceFile.getPath())) {
98-
return internalResourceFile;
91+
final TruffleFile home = language.getRubyHomeTruffleFile();
92+
if (file.startsWith(home) && isStdLibRubyOrCExtFile(home.relativize(file))) {
93+
return file;
94+
} else {
95+
try {
96+
return env.getPublicTruffleFile(path);
97+
} catch (SecurityException e) {
98+
throw new RaiseException(
99+
context,
100+
context.getCoreExceptions().loadError(
101+
"Permission denied (" + e.getMessage() + ") -- " + path,
102+
path,
103+
null));
99104
}
100105
}
106+
}
101107

102-
try {
103-
return env.getPublicTruffleFile(path);
104-
} catch (SecurityException e) {
105-
throw new RaiseException(
106-
context,
107-
context.getCoreExceptions().loadError(
108-
"Permission denied (" + e.getMessage() + ") -- " + path,
109-
path,
110-
null));
108+
private static boolean isStdLibRubyOrCExtFile(TruffleFile relativePathFromHome) {
109+
final String fileName = relativePathFromHome.getName();
110+
if (fileName == null) {
111+
return false;
112+
}
113+
114+
final String lowerCaseFileName = fileName.toLowerCase(Locale.ROOT);
115+
if (!lowerCaseFileName.endsWith(TruffleRuby.EXTENSION) &&
116+
!lowerCaseFileName.endsWith(RubyLanguage.CEXT_EXTENSION)) {
117+
return false;
111118
}
112-
}
113119

114-
private static boolean isStdLibRubyOrCExtFile(String path) {
115-
return path.endsWith(TruffleRuby.EXTENSION) || path.endsWith(RubyLanguage.CEXT_EXTENSION);
120+
return relativePathFromHome.startsWith("lib");
116121
}
117122

118123
Source buildSource(TruffleFile file, String path, TStringWithEncoding sourceTStringWithEncoding, boolean internal,

0 commit comments

Comments
 (0)