Skip to content

Commit 118c53e

Browse files
committed
use the connonnical path to the root RootPackageDir and ApplicationFilesPath. (fixes bug where the canonical path is different than the device path due to sym links)
1 parent 5578cd4 commit 118c53e

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/src/com/tns/FileSystem.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.json.JSONException;
1515
import org.json.JSONObject;
1616

17+
import android.util.Log;
18+
1719
public class FileSystem
1820
{
1921
public static String readAll(InputStream inputStream) throws IOException
@@ -94,15 +96,17 @@ public static JSONObject readJSONFile(File file) throws IOException, JSONExcepti
9496
return new JSONObject(content);
9597
}
9698

97-
public static String resolveRelativePath(String path, String currentDirectory){
99+
public static String resolveRelativePath(String path, String currentDirectory)
100+
{
98101
File temp = new File(currentDirectory, path);
99102
try
100103
{
101104
return temp.getCanonicalPath();
102105
}
103106
catch (IOException e)
104107
{
105-
try{
108+
try
109+
{
106110
URI uri = new URI(currentDirectory);
107111
return uri.resolve(path).getPath();
108112
}

src/src/com/tns/Require.java

+37-28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
57
import java.util.HashMap;
68

79
import org.json.JSONException;
@@ -28,10 +30,17 @@ public static void init(Context context)
2830
{
2931
return;
3032
}
31-
32-
RootPackageDir = context.getApplicationInfo().dataDir;
33-
34-
ApplicationFilesPath = context.getApplicationContext().getFilesDir().getPath();
33+
34+
try
35+
{
36+
RootPackageDir = new File(context.getApplicationInfo().dataDir).getCanonicalPath();
37+
ApplicationFilesPath = context.getApplicationContext().getFilesDir().getCanonicalPath();
38+
}
39+
catch (IOException e)
40+
{
41+
e.printStackTrace();
42+
}
43+
3544
ModulesFilesPath = "/app/";
3645

3746
NativeScriptModulesFilesPath = "/app/tns_modules/";
@@ -80,43 +89,43 @@ public static String getModulePath(String moduleName, String callingDirName)
8089
{
8190
// This method is called my the NativeScriptRuntime.cpp RequireCallback method.
8291
// The currentModuleDir is the directory path of the calling module.
83-
// checkForExternalPath = true;
92+
checkForExternalPath = true;
8493
File file = findModuleFile(moduleName, callingDirName);
8594

8695
if (file != null && file.exists())
8796
{
8897
File projectRootDir = new File(RootPackageDir);
89-
// if (checkForExternalPath && isFileExternal(file, projectRootDir))
90-
// {
91-
// return "EXTERNAL_FILE_ERROR";
92-
// }
93-
// else
94-
// {
98+
if (checkForExternalPath && isFileExternal(file, projectRootDir))
99+
{
100+
return "EXTERNAL_FILE_ERROR";
101+
}
102+
else
103+
{
95104
return file.getAbsolutePath();
96-
// }
105+
}
97106
}
98107

99108
// empty path will be handled by the NativeScriptRuntime.cpp and a JS error will be thrown
100109
return "";
101110
}
102111

103-
// private static boolean isFileExternal(File source, File target)
104-
// {
105-
// File currentParentDir = source.getParentFile();
106-
//
107-
// while (currentParentDir != null)
108-
// {
109-
// if (currentParentDir.equals(target))
110-
// {
111-
// return false;
112-
// }
113-
//
114-
// currentParentDir = currentParentDir.getParentFile();
115-
// }
116-
//
117-
// return true;
118-
// }
112+
private static boolean isFileExternal(File source, File target)
113+
{
114+
File currentParentDir = source.getParentFile();
115+
116+
while (currentParentDir != null)
117+
{
118+
if (currentParentDir.equals(target))
119+
{
120+
return false;
121+
}
119122

123+
currentParentDir = currentParentDir.getParentFile();
124+
}
125+
126+
return true;
127+
}
128+
120129
private static File findModuleFile(String moduleName, String currentDirectory)
121130
{
122131
File directory = null;

0 commit comments

Comments
 (0)