11
11
12
12
import java .io .File ;
13
13
import java .io .IOException ;
14
+ import java .util .Locale ;
14
15
15
16
import com .oracle .truffle .api .nodes .Node ;
16
17
import org .graalvm .collections .Pair ;
@@ -56,6 +57,7 @@ public static void ensureReadable(RubyContext context, TruffleFile file, Node cu
56
57
}
57
58
}
58
59
60
+
59
61
public Pair <Source , TStringWithEncoding > loadFile (String path ) throws IOException {
60
62
if (context .getOptions ().LOG_LOAD ) {
61
63
RubyLanguage .LOGGER .info ("loading " + path );
@@ -77,10 +79,6 @@ public Pair<Source, TStringWithEncoding> loadFile(String path) throws IOExceptio
77
79
78
80
public static TruffleFile getSafeTruffleFile (RubyLanguage language , RubyContext context , String path ) {
79
81
final Env env = context .getEnv ();
80
- if (env .isFileIOAllowed ()) {
81
- return env .getPublicTruffleFile (path );
82
- }
83
-
84
82
final TruffleFile file ;
85
83
try {
86
84
file = env .getInternalTruffleFile (path ).getCanonicalFile ();
@@ -90,29 +88,36 @@ public static TruffleFile getSafeTruffleFile(RubyLanguage language, RubyContext
90
88
context .getCoreExceptions ().loadError ("Failed to canonicalize -- " + path , path , null ));
91
89
}
92
90
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 ));
99
104
}
100
105
}
106
+ }
101
107
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 ;
111
118
}
112
- }
113
119
114
- private static boolean isStdLibRubyOrCExtFile (String path ) {
115
- return path .endsWith (TruffleRuby .EXTENSION ) || path .endsWith (RubyLanguage .CEXT_EXTENSION );
120
+ return relativePathFromHome .startsWith ("lib" );
116
121
}
117
122
118
123
Source buildSource (TruffleFile file , String path , TStringWithEncoding sourceTStringWithEncoding , boolean internal ,
0 commit comments