From 8962a9d5897e3b835c19bdc1b3132c2eebee4af5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 6 Dec 2019 15:19:23 +0100
Subject: [PATCH] Windows: apply the workaround for +<basename> removal to
 +objective subfolder
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Because MATLAB under Windows maintains a lock on the “+<basename>” subdirectory,
we use a workaround consisting in first renaming the directory, then deleting
it.

But this is not enough when the “ramsey_policy” command is used, because the
latter creates a “+objective” subfolder inside “+<basename>”, on which MATLAB
also maintains a lock.

The fix consists in recursively using the same workaround as for the top-level
directory: renaming before deleting.
---
 src/ModFile.cc | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/ModFile.cc b/src/ModFile.cc
index 11552662..837de22f 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -847,10 +847,18 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
          it before deleting it (the renaming must occur in the same directory,
          otherwise it may file if the destination is not on the same
          filesystem). */
-      if (filesystem::exists("+" + basename))
+      if (filesystem::path plusfolder{"+" + basename}; filesystem::exists(plusfolder))
 	{
+          if (filesystem::exists(plusfolder / "+objective"))
+            {
+              // Do it recursively for the +objective folder, created by ramsey_policy
+              auto tmp2 = unique_path();
+              filesystem::rename(plusfolder / "+objective", tmp2);
+              filesystem::remove_all(tmp2);
+            }
+
 	  auto tmp = unique_path();
-	  filesystem::rename("+" + basename, tmp);
+	  filesystem::rename(plusfolder, tmp);
 	  filesystem::remove_all(tmp);
 	}
       filesystem::remove_all(basename + "/model/src");
-- 
GitLab