From 737cb2e898e6889d5bac9f8e0451232cbf0f19db Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Wed, 12 Sep 2018 14:33:36 +0300 Subject: [PATCH] Fixed unsafe handling of temporary directory deletion. gitlab #4 Change-Id: Iaa886932b71bc6bdb09b1f262135640a4c6458b4 --- .../src/org/simantics/fmil/core/FMIL.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java b/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java index 7301caf..f4c7ca4 100644 --- a/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java +++ b/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java @@ -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); -- 2.45.2