diff Core/TemporaryFile.cpp @ 3181:6fd38327e777

Fix issue #130 (Orthanc failed to start when /tmp partition was full)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 31 Jan 2019 15:33:27 +0100
parents 4e43e67f8ecf
children 5d1f5984dc41
line wrap: on
line diff
--- a/Core/TemporaryFile.cpp	Wed Jan 30 17:50:51 2019 +0100
+++ b/Core/TemporaryFile.cpp	Thu Jan 31 15:33:27 2019 +0100
@@ -34,6 +34,7 @@
 #include "PrecompiledHeaders.h"
 #include "TemporaryFile.h"
 
+#include "OrthancException.h"
 #include "SystemToolbox.h"
 #include "Toolbox.h"
 
@@ -41,15 +42,25 @@
 
 namespace Orthanc
 {
-  static std::string CreateTemporaryPath(const char* extension)
+  static std::string CreateTemporaryPath(const char* temporaryDirectory,
+                                         const char* extension)
   {
+    boost::filesystem::path dir;
+
+    if (temporaryDirectory == NULL)
+    {
 #if BOOST_HAS_FILESYSTEM_V3 == 1
-    boost::filesystem::path tmpDir = boost::filesystem::temp_directory_path();
+      dir = boost::filesystem::temp_directory_path();
 #elif defined(__linux__)
-    boost::filesystem::path tmpDir("/tmp");
+      dir = "/tmp";
 #else
-#error Support your platform here
+#  error Support your platform here
 #endif
+    }
+    else
+    {
+      dir = temporaryDirectory;
+    }
 
     // We use UUID to create unique path to temporary files
     std::string filename = "Orthanc-" + Orthanc::Toolbox::GenerateUuid();
@@ -59,19 +70,20 @@
       filename.append(extension);
     }
 
-    tmpDir /= filename;
-    return tmpDir.string();
+    dir /= filename;
+    return dir.string();
   }
 
 
   TemporaryFile::TemporaryFile() : 
-    path_(CreateTemporaryPath(NULL))
+    path_(CreateTemporaryPath(NULL, NULL))
   {
   }
 
 
-  TemporaryFile::TemporaryFile(const char* extension) :
-    path_(CreateTemporaryPath(extension))
+  TemporaryFile::TemporaryFile(const std::string& temporaryDirectory,
+                               const std::string& extension) :
+    path_(CreateTemporaryPath(temporaryDirectory.c_str(), extension.c_str()))
   {
   }
 
@@ -84,12 +96,39 @@
 
   void TemporaryFile::Write(const std::string& content)
   {
-    SystemToolbox::WriteFile(content, path_);
+    try
+    {
+      SystemToolbox::WriteFile(content, path_);
+    }
+    catch (OrthancException&)
+    {
+      LOG(ERROR) << "Can't create temporary file \"" << path_
+                 << "\" with " << content.size()
+                 << " bytes: Check you have write access to the "
+                 << "temporary directory and that it is not full";
+      throw;
+    }
   }
 
 
   void TemporaryFile::Read(std::string& content) const
   {
-    SystemToolbox::ReadFile(content, path_);
+    try
+    {
+      SystemToolbox::ReadFile(content, path_);
+    }
+    catch (OrthancException&)
+    {
+      LOG(ERROR) << "Can't read temporary file \"" << path_
+                 << "\": Another process has corrupted the temporary directory";
+      throw;
+    }
+  }
+
+
+  void TemporaryFile::Touch()
+  {
+    std::string empty;
+    Write(empty);
   }
 }