From bad0c3cf271fd4a9ed7729096f32bd078e59f486 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 11 Sep 2019 17:03:37 +0200
Subject: [PATCH] Fix preprocessor failure to rename +<model> directory if /tmp
 is not on the same filesystem

Bug introduced in 0c755460, that manifested on the Gitlab runners.
---
 src/ModFile.cc | 6 ++++--
 src/ModFile.hh | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/ModFile.cc b/src/ModFile.cc
index e11d5e15..1feca9ef 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -853,7 +853,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
          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. */
+         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))
 	{
 	  auto tmp = unique_path();
@@ -1582,7 +1584,7 @@ ModFile::unique_path()
       string rand_str(rand_length, '\0');
       for (auto &dis : rand_str)
         dis = possible_characters[distribution(generator)];
-      path = filesystem::temp_directory_path() / rand_str;
+      path = rand_str;
     }
   while (filesystem::exists(path));
 
diff --git a/src/ModFile.hh b/src/ModFile.hh
index 5c3950d1..1bfe7cb7 100644
--- a/src/ModFile.hh
+++ b/src/ModFile.hh
@@ -131,7 +131,7 @@ private:
   void writeJsonComputingPassOutput(const string &basename, JsonFileOutputType json_output_mode, bool jsonderivsimple) const;
   void writeJsonFileHelper(const string &fname, ostringstream &output) const;
   vector<expr_t> pac_growth;
-  /* Generate a random temporary path. Equivalent to
+  /* Generate a random temporary path, in the current directory. Equivalent to
      boost::filesystem::unique_path(). Both are insecure, but currently there
      is no better portable solution. Maybe in a later C++ standard? */
   static filesystem::path unique_path();
-- 
GitLab