Mercurial > hg > orthanc
comparison 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 |
comparison
equal
deleted
inserted
replaced
3180:07a2f637b76d | 3181:6fd38327e777 |
---|---|
32 | 32 |
33 | 33 |
34 #include "PrecompiledHeaders.h" | 34 #include "PrecompiledHeaders.h" |
35 #include "TemporaryFile.h" | 35 #include "TemporaryFile.h" |
36 | 36 |
37 #include "OrthancException.h" | |
37 #include "SystemToolbox.h" | 38 #include "SystemToolbox.h" |
38 #include "Toolbox.h" | 39 #include "Toolbox.h" |
39 | 40 |
40 #include <boost/filesystem.hpp> | 41 #include <boost/filesystem.hpp> |
41 | 42 |
42 namespace Orthanc | 43 namespace Orthanc |
43 { | 44 { |
44 static std::string CreateTemporaryPath(const char* extension) | 45 static std::string CreateTemporaryPath(const char* temporaryDirectory, |
46 const char* extension) | |
45 { | 47 { |
48 boost::filesystem::path dir; | |
49 | |
50 if (temporaryDirectory == NULL) | |
51 { | |
46 #if BOOST_HAS_FILESYSTEM_V3 == 1 | 52 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
47 boost::filesystem::path tmpDir = boost::filesystem::temp_directory_path(); | 53 dir = boost::filesystem::temp_directory_path(); |
48 #elif defined(__linux__) | 54 #elif defined(__linux__) |
49 boost::filesystem::path tmpDir("/tmp"); | 55 dir = "/tmp"; |
50 #else | 56 #else |
51 #error Support your platform here | 57 # error Support your platform here |
52 #endif | 58 #endif |
59 } | |
60 else | |
61 { | |
62 dir = temporaryDirectory; | |
63 } | |
53 | 64 |
54 // We use UUID to create unique path to temporary files | 65 // We use UUID to create unique path to temporary files |
55 std::string filename = "Orthanc-" + Orthanc::Toolbox::GenerateUuid(); | 66 std::string filename = "Orthanc-" + Orthanc::Toolbox::GenerateUuid(); |
56 | 67 |
57 if (extension != NULL) | 68 if (extension != NULL) |
58 { | 69 { |
59 filename.append(extension); | 70 filename.append(extension); |
60 } | 71 } |
61 | 72 |
62 tmpDir /= filename; | 73 dir /= filename; |
63 return tmpDir.string(); | 74 return dir.string(); |
64 } | 75 } |
65 | 76 |
66 | 77 |
67 TemporaryFile::TemporaryFile() : | 78 TemporaryFile::TemporaryFile() : |
68 path_(CreateTemporaryPath(NULL)) | 79 path_(CreateTemporaryPath(NULL, NULL)) |
69 { | 80 { |
70 } | 81 } |
71 | 82 |
72 | 83 |
73 TemporaryFile::TemporaryFile(const char* extension) : | 84 TemporaryFile::TemporaryFile(const std::string& temporaryDirectory, |
74 path_(CreateTemporaryPath(extension)) | 85 const std::string& extension) : |
86 path_(CreateTemporaryPath(temporaryDirectory.c_str(), extension.c_str())) | |
75 { | 87 { |
76 } | 88 } |
77 | 89 |
78 | 90 |
79 TemporaryFile::~TemporaryFile() | 91 TemporaryFile::~TemporaryFile() |
82 } | 94 } |
83 | 95 |
84 | 96 |
85 void TemporaryFile::Write(const std::string& content) | 97 void TemporaryFile::Write(const std::string& content) |
86 { | 98 { |
87 SystemToolbox::WriteFile(content, path_); | 99 try |
100 { | |
101 SystemToolbox::WriteFile(content, path_); | |
102 } | |
103 catch (OrthancException&) | |
104 { | |
105 LOG(ERROR) << "Can't create temporary file \"" << path_ | |
106 << "\" with " << content.size() | |
107 << " bytes: Check you have write access to the " | |
108 << "temporary directory and that it is not full"; | |
109 throw; | |
110 } | |
88 } | 111 } |
89 | 112 |
90 | 113 |
91 void TemporaryFile::Read(std::string& content) const | 114 void TemporaryFile::Read(std::string& content) const |
92 { | 115 { |
93 SystemToolbox::ReadFile(content, path_); | 116 try |
117 { | |
118 SystemToolbox::ReadFile(content, path_); | |
119 } | |
120 catch (OrthancException&) | |
121 { | |
122 LOG(ERROR) << "Can't read temporary file \"" << path_ | |
123 << "\": Another process has corrupted the temporary directory"; | |
124 throw; | |
125 } | |
126 } | |
127 | |
128 | |
129 void TemporaryFile::Touch() | |
130 { | |
131 std::string empty; | |
132 Write(empty); | |
94 } | 133 } |
95 } | 134 } |