Mercurial > hg > orthanc
annotate Core/FileStorage/FileStorage.cpp @ 813:b640aeee11ba
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 08 May 2014 15:15:55 +0200 |
parents | 4689e400e0fa |
children | a811bdf8b8eb |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "FileStorage.h" | |
34 | |
35 // http://stackoverflow.com/questions/1576272/storing-large-number-of-files-in-file-system | |
36 // http://stackoverflow.com/questions/446358/storing-a-large-number-of-images | |
37 | |
234 | 38 #include "../OrthancException.h" |
39 #include "../Toolbox.h" | |
40 #include "../Uuid.h" | |
0 | 41 |
42 #include <boost/filesystem/fstream.hpp> | |
201
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
148
diff
changeset
|
43 #include <glog/logging.h> |
0 | 44 |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
45 static std::string ToString(const boost::filesystem::path& p) |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
46 { |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
47 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
48 return p.filename().string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
49 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
50 return p.filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
51 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
52 } |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
53 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
54 |
59 | 55 namespace Orthanc |
0 | 56 { |
57 boost::filesystem::path FileStorage::GetPath(const std::string& uuid) const | |
58 { | |
59 namespace fs = boost::filesystem; | |
60 | |
61 if (!Toolbox::IsUuid(uuid)) | |
62 { | |
59 | 63 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 64 } |
65 | |
66 fs::path path = root_; | |
67 | |
68 path /= std::string(&uuid[0], &uuid[2]); | |
69 path /= std::string(&uuid[2], &uuid[4]); | |
70 path /= uuid; | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
71 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
72 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 73 path.make_preferred(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
74 #endif |
0 | 75 |
76 return path; | |
77 } | |
78 | |
79 FileStorage::FileStorage(std::string root) | |
80 { | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
81 //root_ = boost::filesystem::absolute(root).string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
82 root_ = root; |
0 | 83 |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
84 Toolbox::CreateDirectory(root); |
0 | 85 } |
86 | |
87 std::string FileStorage::CreateFileWithoutCompression(const void* content, size_t size) | |
88 { | |
89 std::string uuid; | |
90 boost::filesystem::path path; | |
91 | |
92 for (;;) | |
93 { | |
94 uuid = Toolbox::GenerateUuid(); | |
95 path = GetPath(uuid); | |
96 | |
97 if (!boost::filesystem::exists(path)) | |
98 { | |
99 // OK, this is indeed a new file | |
100 break; | |
101 } | |
102 | |
103 // Extremely improbable case: This Uuid has already been created | |
104 // in the past. Try again. | |
105 } | |
106 | |
107 if (boost::filesystem::exists(path.parent_path())) | |
108 { | |
109 if (!boost::filesystem::is_directory(path.parent_path())) | |
110 { | |
59 | 111 throw OrthancException("The subdirectory to be created is already occupied by a regular file"); |
0 | 112 } |
113 } | |
114 else | |
115 { | |
116 if (!boost::filesystem::create_directories(path.parent_path())) | |
117 { | |
59 | 118 throw OrthancException("Unable to create a subdirectory in the file storage"); |
0 | 119 } |
120 } | |
121 | |
122 boost::filesystem::ofstream f; | |
123 f.open(path, std::ofstream::out | std::ios::binary); | |
124 if (!f.good()) | |
125 { | |
59 | 126 throw OrthancException("Unable to create a new file in the file storage"); |
0 | 127 } |
128 | |
129 if (size != 0) | |
130 { | |
131 f.write(static_cast<const char*>(content), size); | |
132 if (!f.good()) | |
133 { | |
134 f.close(); | |
59 | 135 throw OrthancException("Unable to write to the new file in the file storage"); |
0 | 136 } |
137 } | |
138 | |
139 f.close(); | |
140 | |
141 return uuid; | |
142 } | |
143 | |
144 | |
145 std::string FileStorage::Create(const void* content, size_t size) | |
146 { | |
147 if (!HasBufferCompressor() || size == 0) | |
148 { | |
149 return CreateFileWithoutCompression(content, size); | |
150 } | |
151 else | |
152 { | |
153 std::string compressed; | |
154 compressor_->Compress(compressed, content, size); | |
155 assert(compressed.size() > 0); | |
156 return CreateFileWithoutCompression(&compressed[0], compressed.size()); | |
157 } | |
158 } | |
159 | |
160 | |
161 std::string FileStorage::Create(const std::vector<uint8_t>& content) | |
162 { | |
163 if (content.size() == 0) | |
164 return Create(NULL, 0); | |
165 else | |
166 return Create(&content[0], content.size()); | |
167 } | |
168 | |
169 std::string FileStorage::Create(const std::string& content) | |
170 { | |
171 if (content.size() == 0) | |
172 return Create(NULL, 0); | |
173 else | |
174 return Create(&content[0], content.size()); | |
175 } | |
176 | |
177 void FileStorage::ReadFile(std::string& content, | |
178 const std::string& uuid) const | |
179 { | |
180 content.clear(); | |
181 | |
182 if (HasBufferCompressor()) | |
183 { | |
184 std::string compressed; | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
185 Toolbox::ReadFile(compressed, ToString(GetPath(uuid))); |
0 | 186 |
187 if (compressed.size() != 0) | |
188 { | |
189 compressor_->Uncompress(content, compressed); | |
190 } | |
191 } | |
192 else | |
193 { | |
194 Toolbox::ReadFile(content, GetPath(uuid).string()); | |
195 } | |
196 } | |
197 | |
198 | |
199 uintmax_t FileStorage::GetCompressedSize(const std::string& uuid) const | |
200 { | |
201 boost::filesystem::path path = GetPath(uuid); | |
202 return boost::filesystem::file_size(path); | |
203 } | |
204 | |
205 | |
206 | |
207 void FileStorage::ListAllFiles(std::set<std::string>& result) const | |
208 { | |
209 namespace fs = boost::filesystem; | |
210 | |
211 result.clear(); | |
212 | |
213 if (fs::exists(root_) && fs::is_directory(root_)) | |
214 { | |
215 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current) | |
216 { | |
217 if (fs::is_regular_file(current->status())) | |
218 { | |
219 try | |
220 { | |
221 fs::path d = current->path(); | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
222 std::string uuid = ToString(d); |
0 | 223 if (Toolbox::IsUuid(uuid)) |
224 { | |
225 fs::path p0 = d.parent_path().parent_path().parent_path(); | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
226 std::string p1 = ToString(d.parent_path().parent_path()); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
227 std::string p2 = ToString(d.parent_path()); |
0 | 228 if (p1.length() == 2 && |
229 p2.length() == 2 && | |
230 p1 == uuid.substr(0, 2) && | |
231 p2 == uuid.substr(2, 2) && | |
232 p0 == root_) | |
233 { | |
234 result.insert(uuid); | |
235 } | |
236 } | |
237 } | |
238 catch (fs::filesystem_error) | |
239 { | |
240 } | |
241 } | |
242 } | |
243 } | |
244 } | |
245 | |
246 | |
247 void FileStorage::Clear() | |
248 { | |
249 namespace fs = boost::filesystem; | |
250 typedef std::set<std::string> List; | |
251 | |
252 List result; | |
253 ListAllFiles(result); | |
254 | |
656 | 255 for (List::const_iterator it = result.begin(); it != result.end(); ++it) |
0 | 256 { |
257 Remove(*it); | |
258 } | |
259 } | |
260 | |
261 | |
262 void FileStorage::Remove(const std::string& uuid) | |
263 { | |
201
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
148
diff
changeset
|
264 LOG(INFO) << "Deleting file " << uuid; |
0 | 265 namespace fs = boost::filesystem; |
266 | |
267 fs::path p = GetPath(uuid); | |
147 | 268 |
269 try | |
270 { | |
271 fs::remove(p); | |
272 } | |
148 | 273 catch (...) |
147 | 274 { |
275 // Ignore the error | |
276 } | |
0 | 277 |
278 // Remove the two parent directories, ignoring the error code if | |
279 // these directories are not empty | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
280 |
142 | 281 try |
282 { | |
147 | 283 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
284 boost::system::error_code err; | |
285 fs::remove(p.parent_path(), err); | |
286 fs::remove(p.parent_path().parent_path(), err); | |
287 #else | |
288 fs::remove(p.parent_path()); | |
289 fs::remove(p.parent_path().parent_path()); | |
290 #endif | |
142 | 291 } |
148 | 292 catch (...) |
142 | 293 { |
294 // Ignore the error | |
295 } | |
296 } | |
297 | |
298 | |
0 | 299 uintmax_t FileStorage::GetCapacity() const |
300 { | |
301 return boost::filesystem::space(root_).capacity; | |
302 } | |
303 | |
304 uintmax_t FileStorage::GetAvailableSpace() const | |
305 { | |
306 return boost::filesystem::space(root_).available; | |
307 } | |
308 } |