Skip to content

Commit af2a3a8

Browse files
committed
Added a simple try/catch re-entry guard in File.loadDLL()
1 parent ebf59c1 commit af2a3a8

1 file changed

Lines changed: 110 additions & 93 deletions

File tree

‎src/javaxt/io/File.java‎

Lines changed: 110 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ private void init(String Path){
153153
//**************************************************************************
154154
//** Get File Name
155155
//**************************************************************************
156-
/** Returns the name of the file, excluding the path. */
157-
156+
/** Returns the name of the file, excluding the path.
157+
*/
158158
public String getName(){
159159
return name;
160160
}
@@ -163,10 +163,10 @@ public String getName(){
163163
//**************************************************************************
164164
//** Get File Name
165165
//**************************************************************************
166-
/** Returns the name of the file, excluding the path.
166+
/** Returns the name of the file, excluding the path.
167167
*
168-
* @param IncludeFileExtension If true, includes the file extension.
169-
* Otherwise, will return the file name without the extension.
168+
* @param IncludeFileExtension If true, includes the file extension.
169+
* Otherwise, will return the file name without the extension.
170170
*/
171171
public String getName(boolean IncludeFileExtension){
172172
if (!IncludeFileExtension){
@@ -180,8 +180,8 @@ public String getName(boolean IncludeFileExtension){
180180
//**************************************************************************
181181
//** Get File Path
182182
//**************************************************************************
183-
/** Used to retrieve the path to the file, excluding the file name. Appends
184-
* a file separator to the end of the string.
183+
/** Used to retrieve the path to the file, excluding the file name. Appends
184+
* a file separator to the end of the string.
185185
*/
186186
public String getPath(){
187187
return path;
@@ -191,8 +191,8 @@ public String getPath(){
191191
//**************************************************************************
192192
//** Get Directory
193193
//**************************************************************************
194-
/** Returns the file's parent directory. Same as getParentDirectory() */
195-
194+
/** Returns the file's parent directory. Same as getParentDirectory()
195+
*/
196196
public Directory getDirectory(){
197197
return getParentDirectory();
198198
}
@@ -213,8 +213,8 @@ public Directory getParentDirectory(){
213213
//**************************************************************************
214214
//** toFile
215215
//**************************************************************************
216-
/** Returns the java.io.File representation of this object. */
217-
216+
/** Returns the java.io.File representation of this object.
217+
*/
218218
public java.io.File toFile(){
219219
return getFile();
220220
}
@@ -223,8 +223,8 @@ public java.io.File toFile(){
223223
//**************************************************************************
224224
//** getFile
225225
//**************************************************************************
226-
/** Returns the java.io.File representation of this object. */
227-
226+
/** Returns the java.io.File representation of this object.
227+
*/
228228
private java.io.File getFile(){
229229
if (file==null) file = new java.io.File(path + name);
230230
return file;
@@ -234,9 +234,9 @@ private java.io.File getFile(){
234234
//**************************************************************************
235235
//** Get File Extension
236236
//**************************************************************************
237-
/** Returns the file's extension, excluding the last dot/period
238-
* (e.g. "C:\image.jpg" will return "jpg"). Returns a zero-length string
239-
* if there is no extension.
237+
/** Returns the file's extension, excluding the last dot/period
238+
* (e.g. "C:\image.jpg" will return "jpg"). Returns a zero-length string
239+
* if there is no extension.
240240
*/
241241
public String getExtension(){
242242
int idx = name.lastIndexOf(".");
@@ -1562,99 +1562,111 @@ protected static synchronized boolean loadDLL(){
15621562
if (isWindows){
15631563

15641564
if (dllLoaded==null){ //haven't tried to load the dll yet...
1565-
String jvmPlatform = System.getProperty("os.arch");
1566-
String dllName;
1567-
if (jvmPlatform.equalsIgnoreCase("x86")){
1568-
dllName = "javaxt-core.dll";
1569-
}
1570-
else if(jvmPlatform.equalsIgnoreCase("amd64")){
1571-
dllName = "javaxt-core64.dll";
1572-
}
1573-
else{
1574-
dllLoaded = false;
1575-
return dllLoaded;
1576-
}
15771565

15781566

1579-
//Find dll entry in the zip/jar file
1580-
Jar jar = new Jar(Jar.class);
1581-
Jar.Entry entry = jar.getEntry(null, dllName);
1582-
if (entry==null){
1583-
dllLoaded = false;
1584-
return dllLoaded;
1585-
}
1586-
long checksum = entry.checksum();
1587-
1588-
1589-
//Construct list of possible file locations for the dll
1590-
java.util.ArrayList<java.io.File> files = new java.util.ArrayList<>();
1591-
files.add(new java.io.File(jar.getFile().getParentFile(), dllName));
1592-
javaxt.io.Directory dir = new javaxt.io.Directory(System.getProperty("user.home"));
1593-
for (String appDir : new String[]{"AppData\\Local", "Application Data"}){
1594-
javaxt.io.Directory d = new javaxt.io.Directory(dir + appDir);
1595-
java.io.File f = d.toFile(); //flip to file to prevent infinite recursion
1596-
if (f.exists() && f.canWrite()){
1597-
dir = d;
1598-
break;
1567+
if (loadingDLL) return false;
1568+
loadingDLL = true;
1569+
1570+
try{
1571+
String jvmPlatform = System.getProperty("os.arch");
1572+
String dllName;
1573+
if (jvmPlatform.equalsIgnoreCase("x86")){
1574+
dllName = "javaxt-core.dll";
15991575
}
1600-
}
1601-
files.add(new java.io.File(dir + "JavaXT\\" + dllName));
1602-
1603-
1604-
//Try to load dll
1605-
for (java.io.File dll : files){
1606-
1607-
if (dll.exists()){
1608-
1609-
//Check whether the dll equals the jar entry. Extract as needed.
1610-
byte[] b = new byte[(int)dll.length()];
1611-
java.io.DataInputStream is = null;
1612-
try{
1613-
is = new java.io.DataInputStream(new FileInputStream(dll));
1614-
is.readFully(b, 0, b.length);
1615-
is.close();
1616-
java.util.zip.CRC32 crc = new java.util.zip.CRC32();
1617-
crc.update(b);
1618-
if (checksum!=crc.getValue()){
1619-
dll.delete();
1620-
entry.extractFile(dll);
1621-
}
1622-
}
1623-
catch(Exception e){
1624-
try{is.close();}catch(Exception ex){}
1625-
}
1576+
else if(jvmPlatform.equalsIgnoreCase("amd64")){
1577+
dllName = "javaxt-core64.dll";
16261578
}
16271579
else{
1628-
1629-
//File does not exist so extract the dll
1630-
entry.extractFile(dll);
1580+
dllLoaded = false;
1581+
return dllLoaded;
16311582
}
16321583

16331584

1634-
//Load the dll
1635-
if (dll.exists()){
1636-
try{
1637-
System.load(dll.toString());
1638-
dllLoaded = true;
1585+
//Find dll entry in the zip/jar file
1586+
Jar jar = new Jar(Jar.class);
1587+
Jar.Entry entry = jar.getEntry(null, dllName);
1588+
if (entry==null){
1589+
dllLoaded = false;
1590+
return dllLoaded;
1591+
}
1592+
long checksum = entry.checksum();
1593+
1594+
1595+
//Construct list of possible file locations for the dll
1596+
java.util.ArrayList<java.io.File> files = new java.util.ArrayList<>();
1597+
java.io.File root = jar.getRoot(); //the jar file, or the classpath directory
1598+
files.add(new java.io.File(root.isDirectory() ? root : root.getParentFile(), dllName));
1599+
javaxt.io.Directory dir = new javaxt.io.Directory(System.getProperty("user.home"));
1600+
for (String appDir : new String[]{"AppData\\Local", "Application Data"}){
1601+
javaxt.io.Directory d = new javaxt.io.Directory(dir + appDir);
1602+
java.io.File f = d.toFile(); //flip to file to prevent infinite recursion
1603+
if (f.exists() && f.canWrite()){
1604+
dir = d;
16391605
break;
16401606
}
1641-
catch(Throwable t){
1642-
String msg = "already loaded in another classloader";
1643-
if (t.getMessage().contains(msg)){ //UnsatisfiedLinkError
1644-
System.err.println(System.mapLibraryName("javaxt_core") + " " + msg);
1607+
}
1608+
files.add(new java.io.File(dir + "JavaXT\\" + dllName));
1609+
1610+
1611+
//Try to load dll
1612+
for (java.io.File dll : files){
1613+
1614+
if (dll.exists()){
1615+
1616+
//Check whether the dll equals the jar entry. Extract as needed.
1617+
byte[] b = new byte[(int)dll.length()];
1618+
java.io.DataInputStream is = null;
1619+
try{
1620+
is = new java.io.DataInputStream(new FileInputStream(dll));
1621+
is.readFully(b, 0, b.length);
1622+
is.close();
1623+
java.util.zip.CRC32 crc = new java.util.zip.CRC32();
1624+
crc.update(b);
1625+
if (checksum!=crc.getValue()){
1626+
dll.delete();
1627+
entry.extractFile(dll);
1628+
}
1629+
}
1630+
catch(Exception e){
1631+
try{is.close();}catch(Exception ex){}
1632+
}
1633+
}
1634+
else{
1635+
1636+
//File does not exist so extract the dll
1637+
entry.extractFile(dll);
1638+
}
1639+
1640+
1641+
//Load the dll
1642+
if (dll.exists()){
1643+
try{
1644+
System.load(dll.toString());
1645+
dllLoaded = true;
1646+
break;
16451647
}
1646-
else{
1647-
t.printStackTrace();
1648+
catch(Throwable t){
1649+
String msg = "already loaded in another classloader";
1650+
if (t.getMessage().contains(msg)){ //UnsatisfiedLinkError
1651+
System.err.println(System.mapLibraryName("javaxt_core") + " " + msg);
1652+
}
1653+
else{
1654+
t.printStackTrace();
1655+
}
16481656
}
16491657
}
16501658
}
1651-
}
16521659

16531660

1654-
//Don't update the static variable to give users a chance to fix
1655-
//the load error (e.g. manually extract the dll and copy it into
1656-
//one of the directories).
1657-
//dllLoaded = false;
1661+
//Don't update the static variable to give users a chance to fix
1662+
//the load error (e.g. manually extract the dll and copy it into
1663+
//one of the directories).
1664+
//dllLoaded = false;
1665+
}
1666+
finally{
1667+
loadingDLL = false;
1668+
}
1669+
16581670
}
16591671

16601672
return dllLoaded==null ? false : dllLoaded;
@@ -1678,6 +1690,11 @@ else if(jvmPlatform.equalsIgnoreCase("amd64")){
16781690
*/
16791691
private static Boolean dllLoaded;
16801692

1693+
/** True while loadDLL() is running. Used to detect nested calls from the
1694+
* same thread (other threads wait on the lock).
1695+
*/
1696+
private static boolean loadingDLL = false;
1697+
16811698

16821699
/** JNI entry point to retrieve file attributes. */
16831700
private static native long[] GetFileAttributesEx(String lpPathName) throws Exception;

0 commit comments

Comments
 (0)