From 9bfd0ca33f60d42487ca2086b82f001e926cebae Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Sat, 25 Apr 2020 18:55:30 +0200
Subject: [PATCH] Try to create the snapshot subdir on startup to avoid
 surprises down the line

---
 src/common_io.c | 13 +++++++++++++
 src/common_io.h |  2 ++
 src/engine.c    | 10 +++-------
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/common_io.c b/src/common_io.c
index 652291a16e..aba1595df6 100644
--- a/src/common_io.c
+++ b/src/common_io.c
@@ -45,6 +45,7 @@
 #include "star_formation_io.h"
 #include "stars_io.h"
 #include "threadpool.h"
+#include "tools.h"
 #include "tracers_io.h"
 #include "units.h"
 #include "velociraptor_io.h"
@@ -2479,3 +2480,15 @@ void io_write_output_field_parameter(const char* filename) {
       "'%s'.\n",
       filename);
 }
+
+/**
+ * @brief Create the subdirectory for snapshots if the user demanded one.
+ *
+ * @param dirname The name of the directory.
+ */
+void io_make_snapshot_subdir(const char* dirname) {
+
+  if (strnlen(dirname, PARSER_MAX_LINE_SIZE) > 0) {
+    safe_checkdir(dirname, /*create=*/1);
+  }
+}
diff --git a/src/common_io.h b/src/common_io.h
index 0fe2427d53..bb1564cee5 100644
--- a/src/common_io.h
+++ b/src/common_io.h
@@ -172,4 +172,6 @@ void io_check_output_fields(const struct swift_params* params,
 
 void io_write_output_field_parameter(const char* filename);
 
+void io_make_snapshot_subdir(const char* dirname);
+
 #endif /* SWIFT_COMMON_IO_H */
diff --git a/src/engine.c b/src/engine.c
index 487e5cdcf3..a2622f70d2 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -3596,13 +3596,6 @@ void engine_dump_snapshot(struct engine *e) {
       error(
           "FILENAME_BUFFER_SIZE is too small for snapshot path and file name");
     }
-      /* Try to ensure the directory exists */
-#ifdef WITH_MPI
-    if (engine_rank == 0) mkdir(e->snapshot_subdir, 0777);
-    MPI_Barrier(MPI_COMM_WORLD);
-#else
-    mkdir(e->snapshot_subdir, 0777);
-#endif
   } else {
     if (snprintf(snapshotBase, FILENAME_BUFFER_SIZE, "%s",
                  e->snapshot_base_name) >= FILENAME_BUFFER_SIZE) {
@@ -4458,6 +4451,9 @@ void engine_config(int restart, int fof, struct engine *e,
       }
     }
 
+    /* Try to ensure the snapshot directory exists */
+    if (e->nodeID == 0) io_make_snapshot_subdir(e->snapshot_subdir);
+
     /* Get the total mass */
     e->total_mass = 0.;
     for (size_t i = 0; i < e->s->nr_gparts; ++i)
-- 
GitLab