]> gerrit.simantics Code Review - simantics/fmil.git/commitdiff
Fixed unsafe handling of temporary directory deletion. 63/2163/2
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 12 Sep 2018 11:33:36 +0000 (14:33 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Wed, 12 Sep 2018 11:58:42 +0000 (14:58 +0300)
gitlab #4

Change-Id: Iaa886932b71bc6bdb09b1f262135640a4c6458b4

org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java

index 7301caf4c8209883e9d77aaaa3fc3e5bc6a5aa17..f4c7ca4f2fbbf7d044be10c09a2b4f896b31fe0b 100644 (file)
@@ -6,6 +6,7 @@ import java.io.RandomAccessFile;
 import java.net.URL;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
+import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -193,21 +194,23 @@ public class FMIL {
                 fmuN = 0;
             }
 
-            File tempDir = new File(fmuDir);
-            if(tempDir.isDirectory()) {
+            java.nio.file.Path tempDir = Paths.get(fmuDir);
+            if(Files.exists(tempDir) && Files.isDirectory(tempDir)) {
                 try {
-                    FileUtils.deleteAll(tempDir);
+                       FileUtils.emptyDirectory(tempDir);
                 } catch (IOException e) {
-                    throw new FMILException("Could not create temp folder for fmu");
+                    throw new FMILException("Could not delete existing files from temp folder for fmu " + path, e);
                 }
-                tempDir.mkdir();
             } else {
-                tempDir.mkdir();
+                try {
+                       Files.createDirectory(tempDir);
+                } catch (IOException e) {
+                    throw new FMILException("Could not create temp folder for fmu " + path, e);
+                }
             }
 
-
             try {
-                String tmpPath = tempDir.getAbsolutePath();
+                String tmpPath = tempDir.toString();
                 if(!tmpPath.endsWith("\\") && !tmpPath.endsWith("/"))
                     tmpPath = tmpPath + "/";
                 id = loadFMUFile_(path, tmpPath);