From 816f1fa334bf4a0aa912360258b4571c9cada9f2 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Thu, 11 Oct 2012 14:37:10 +0000
Subject: [PATCH] enable auto load of onelab.db + archival of db+output files
 in archive/ subdir

---
 Common/DefaultOptions.h |  2 +-
 Common/OS.cpp           | 12 ++++++++++++
 Common/OS.h             |  1 +
 Fltk/onelabWindow.cpp   | 43 ++++++++++++++++++++++++++---------------
 4 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/Common/DefaultOptions.h b/Common/DefaultOptions.h
index ce78bb7536..e8880776e4 100644
--- a/Common/DefaultOptions.h
+++ b/Common/DefaultOptions.h
@@ -1108,7 +1108,7 @@ StringXNumber SolverOptions_Number[] = {
     "Always listen to incoming connection requests?" },
   { F|O, "AutoArchiveSolutions" , opt_solver_auto_archive_solutions , 0. ,
     "Automatically archive solutions after each computation" },
-  { F|O, "AutoSaveDatabase" , opt_solver_auto_save_database , 0. ,
+  { F|O, "AutoSaveDatabase" , opt_solver_auto_save_database , 1. ,
     "Automatically save database after each computation" },
   { F|O, "AutoMesh" , opt_solver_auto_mesh , 1. ,
     "Automatically mesh if necesssary" },
diff --git a/Common/OS.cpp b/Common/OS.cpp
index f7f764dcf1..5a95bc21ff 100644
--- a/Common/OS.cpp
+++ b/Common/OS.cpp
@@ -161,6 +161,18 @@ int StatFile(const std::string &fileName)
 #endif
 }
 
+int CreateDirectory(const std::string &dirName)
+{
+#if !defined(WIN32) || defined(__CYGWIN__)
+  if(mkdir(dirName.c_str(), 0777))
+    return 0;
+#else
+  if(_mkdir(dirName.c_str()))
+    return 0;
+#endif
+  return 1;
+}
+
 int KillProcess(int pid)
 {
 #if !defined(WIN32) || defined(__CYGWIN__)
diff --git a/Common/OS.h b/Common/OS.h
index 6a17031a58..0bb1cf2c57 100644
--- a/Common/OS.h
+++ b/Common/OS.h
@@ -19,6 +19,7 @@ std::string GetHostName();
 int UnlinkFile(const std::string &fileName);
 int StatFile(const std::string &fileName);
 int KillProcess(int pid);
+int CreateDirectory(const std::string &dirName);
 int SystemCall(const std::string &command, bool blocking=false);
 
 #endif
diff --git a/Fltk/onelabWindow.cpp b/Fltk/onelabWindow.cpp
index 7a297e8998..96eaeddc01 100644
--- a/Fltk/onelabWindow.cpp
+++ b/Fltk/onelabWindow.cpp
@@ -396,8 +396,19 @@ static std::string timeStamp()
   return std::string(stamp);
 }
 
-static void archiveSolutions(const std::string &timeStamp)
+static void saveDb(const std::string &fileName)
 {
+  Msg::StatusBar(2, true, "Saving database '%s'...", fileName.c_str());
+  if(onelab::server::instance()->toFile(fileName))
+    Msg::StatusBar(2, true, "Done saving database '%s'", fileName.c_str());
+  else
+    Msg::Error("Could not save database '%s'", fileName.c_str());
+}
+
+static void archiveSolutions(const std::string &fileName)
+{
+  std::string stamp = timeStamp();
+
   // add time stamp in all output files in the db, and rename them on disk
   std::vector<onelab::string> strings;
   onelab::server::instance()->get(strings);
@@ -411,7 +422,8 @@ static void archiveSolutions(const std::string &timeStamp)
         if(n < 18 || split[1][n-3] != '-' || split[1][n-6] != '-' ||
            split[1][n-9] != '_'){
           std::string old = names[j];
-          names[j] = split[0] + split[1] + timeStamp + split[2];
+          CreateDirectory(split[0] + "archive/");
+          names[j] = split[0] + "archive/" + split[1] + stamp + split[2];
           Msg::Info("Renaming '%s' into '%s'", old.c_str(), names[j].c_str());
           rename(old.c_str(), names[j].c_str());
         }
@@ -421,18 +433,15 @@ static void archiveSolutions(const std::string &timeStamp)
       onelab::server::instance()->set(strings[i]);
     }
   }
-  FlGui::instance()->onelab->rebuildTree();
-}
 
-static void saveDb(const std::string &fileName, const std::string &timeStamp="")
-{
-  std::vector<std::string> split = SplitFileName(fileName);
-  std::string name = split[0] + split[1] + timeStamp + split[2];
-  Msg::StatusBar(2, true, "Saving database '%s'...", name.c_str());
-  if(onelab::server::instance()->toFile(name))
-    Msg::StatusBar(2, true, "Done saving database '%s'", name.c_str());
-  else
-    Msg::Error("Could not save database '%s'", name.c_str());
+  // save stamped db
+  {
+    std::vector<std::string> split = SplitFileName(fileName);
+    CreateDirectory(split[0] + "archive/");
+    saveDb(split[0] + "archive/" + split[1] + stamp + split[2]);
+  }
+
+  FlGui::instance()->onelab->rebuildTree();
 }
 
 static void loadDb(const std::string &name)
@@ -582,9 +591,8 @@ void onelab_cb(Fl_Widget *w, void *data)
   if(action == "compute" && (CTX::instance()->solver.autoSaveDatabase ||
                              CTX::instance()->solver.autoArchiveSolutions)){
     std::string db = SplitFileName(GModel::current()->getFileName())[0] + "onelab.db";
-    std::string stamp = timeStamp();
-    if(CTX::instance()->solver.autoArchiveSolutions) archiveSolutions(stamp);
-    if(CTX::instance()->solver.autoSaveDatabase) saveDb(db, stamp);
+    if(CTX::instance()->solver.autoArchiveSolutions) archiveSolutions(db);
+    if(CTX::instance()->solver.autoSaveDatabase) saveDb(db);
   }
 
   FlGui::instance()->onelab->stop(false);
@@ -1297,6 +1305,9 @@ void solver_cb(Fl_Widget *w, void *data)
   else
     FlGui::instance()->onelab->rebuildSolverList();
 
+  std::string db = SplitFileName(GModel::current()->getFileName())[0] + "onelab.db";
+  if(!StatFile(db)) loadDb(db);
+
   if(FlGui::instance()->onelab->isBusy())
     FlGui::instance()->onelab->show();
   else
-- 
GitLab