From 7baee7312614a32d00a051f0ce5246f66be0796b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 9 Jul 2018 18:18:26 +0200
Subject: [PATCH] Fix bug under MATLAB+Windows when "+" subdirectory is already
 present

Under MATLAB+Windows (but not under Octave nor under GNU/Linux or macOS), if we
directly remove the "+" subdirectory, then the preprocessor is not able to
recreate it afterwards (presumably because MATLAB maintains some sort of lock
on it). The workaround is to rename it before deleting it.
---
 src/ModFile.cc | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/ModFile.cc b/src/ModFile.cc
index 57cc6b78..c0d932d8 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -752,7 +752,17 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
   if (hasModelChanged)
     {
       // Erase possible remnants of previous runs
-      boost::filesystem::remove_all("+" + basename);
+      /* Under MATLAB+Windows (but not under Octave nor under GNU/Linux or
+         macOS), if we directly remove the "+" subdirectory, then the
+         preprocessor is not able to recreate it afterwards (presumably because
+         MATLAB maintains some sort of lock on it). The workaround is to rename
+         it before deleting it. */
+      if (boost::filesystem::exists("+" + basename))
+	{
+	  auto tmp = boost::filesystem::unique_path();
+	  boost::filesystem::rename("+" + basename, tmp);
+	  boost::filesystem::remove_all(tmp);
+	}
       boost::filesystem::remove_all(basename + "/model/src");
       boost::filesystem::remove_all(basename + "/model/bytecode");
     }
-- 
GitLab