Mercurial > hg > orthanc
annotate Core/FileStorage.cpp @ 191:bff0b77b02fa
refactoring getalluuids
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 13 Nov 2012 11:22:33 +0100 |
parents | 0fadb04728ba |
children | bee20e978835 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
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 | |
59 | 38 #include "OrthancException.h" |
0 | 39 #include "Toolbox.h" |
40 #include "Uuid.h" | |
41 | |
42 #include <boost/filesystem/fstream.hpp> | |
43 | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
44 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
|
45 { |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
46 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
47 return p.filename().string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
48 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
49 return p.filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
50 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
51 } |
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 |
59 | 54 namespace Orthanc |
0 | 55 { |
56 boost::filesystem::path FileStorage::GetPath(const std::string& uuid) const | |
57 { | |
58 namespace fs = boost::filesystem; | |
59 | |
60 if (!Toolbox::IsUuid(uuid)) | |
61 { | |
59 | 62 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 63 } |
64 | |
65 fs::path path = root_; | |
66 | |
67 path /= std::string(&uuid[0], &uuid[2]); | |
68 path /= std::string(&uuid[2], &uuid[4]); | |
69 path /= uuid; | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
70 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
71 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 72 path.make_preferred(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
73 #endif |
0 | 74 |
75 return path; | |
76 } | |
77 | |
78 FileStorage::FileStorage(std::string root) | |
79 { | |
80 namespace fs = boost::filesystem; | |
81 | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
82 //root_ = boost::filesystem::absolute(root).string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
83 root_ = root; |
0 | 84 |
85 if (fs::exists(root)) | |
86 { | |
87 if (!fs::is_directory(root)) | |
88 { | |
59 | 89 throw OrthancException("The file storage root directory is a file"); |
0 | 90 } |
91 } | |
92 else | |
93 { | |
94 if (!fs::create_directories(root)) | |
95 { | |
59 | 96 throw OrthancException("Unable to create the file storage root directory"); |
0 | 97 } |
98 } | |
99 } | |
100 | |
101 std::string FileStorage::CreateFileWithoutCompression(const void* content, size_t size) | |
102 { | |
103 std::string uuid; | |
104 boost::filesystem::path path; | |
105 | |
106 for (;;) | |
107 { | |
108 uuid = Toolbox::GenerateUuid(); | |
109 path = GetPath(uuid); | |
110 | |
111 if (!boost::filesystem::exists(path)) | |
112 { | |
113 // OK, this is indeed a new file | |
114 break; | |
115 } | |
116 | |
117 // Extremely improbable case: This Uuid has already been created | |
118 // in the past. Try again. | |
119 } | |
120 | |
121 if (boost::filesystem::exists(path.parent_path())) | |
122 { | |
123 if (!boost::filesystem::is_directory(path.parent_path())) | |
124 { | |
59 | 125 throw OrthancException("The subdirectory to be created is already occupied by a regular file"); |
0 | 126 } |
127 } | |
128 else | |
129 { | |
130 if (!boost::filesystem::create_directories(path.parent_path())) | |
131 { | |
59 | 132 throw OrthancException("Unable to create a subdirectory in the file storage"); |
0 | 133 } |
134 } | |
135 | |
136 boost::filesystem::ofstream f; | |
137 f.open(path, std::ofstream::out | std::ios::binary); | |
138 if (!f.good()) | |
139 { | |
59 | 140 throw OrthancException("Unable to create a new file in the file storage"); |
0 | 141 } |
142 | |
143 if (size != 0) | |
144 { | |
145 f.write(static_cast<const char*>(content), size); | |
146 if (!f.good()) | |
147 { | |
148 f.close(); | |
59 | 149 throw OrthancException("Unable to write to the new file in the file storage"); |
0 | 150 } |
151 } | |
152 | |
153 f.close(); | |
154 | |
155 return uuid; | |
156 } | |
157 | |
158 | |
159 std::string FileStorage::Create(const void* content, size_t size) | |
160 { | |
161 if (!HasBufferCompressor() || size == 0) | |
162 { | |
163 return CreateFileWithoutCompression(content, size); | |
164 } | |
165 else | |
166 { | |
167 std::string compressed; | |
168 compressor_->Compress(compressed, content, size); | |
169 assert(compressed.size() > 0); | |
170 return CreateFileWithoutCompression(&compressed[0], compressed.size()); | |
171 } | |
172 } | |
173 | |
174 | |
175 std::string FileStorage::Create(const std::vector<uint8_t>& content) | |
176 { | |
177 if (content.size() == 0) | |
178 return Create(NULL, 0); | |
179 else | |
180 return Create(&content[0], content.size()); | |
181 } | |
182 | |
183 std::string FileStorage::Create(const std::string& content) | |
184 { | |
185 if (content.size() == 0) | |
186 return Create(NULL, 0); | |
187 else | |
188 return Create(&content[0], content.size()); | |
189 } | |
190 | |
191 void FileStorage::ReadFile(std::string& content, | |
192 const std::string& uuid) const | |
193 { | |
194 content.clear(); | |
195 | |
196 if (HasBufferCompressor()) | |
197 { | |
198 std::string compressed; | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
199 Toolbox::ReadFile(compressed, ToString(GetPath(uuid))); |
0 | 200 |
201 if (compressed.size() != 0) | |
202 { | |
203 compressor_->Uncompress(content, compressed); | |
204 } | |
205 } | |
206 else | |
207 { | |
208 Toolbox::ReadFile(content, GetPath(uuid).string()); | |
209 } | |
210 } | |
211 | |
212 | |
213 uintmax_t FileStorage::GetCompressedSize(const std::string& uuid) const | |
214 { | |
215 boost::filesystem::path path = GetPath(uuid); | |
216 return boost::filesystem::file_size(path); | |
217 } | |
218 | |
219 | |
220 | |
221 void FileStorage::ListAllFiles(std::set<std::string>& result) const | |
222 { | |
223 namespace fs = boost::filesystem; | |
224 | |
225 result.clear(); | |
226 | |
227 if (fs::exists(root_) && fs::is_directory(root_)) | |
228 { | |
229 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current) | |
230 { | |
231 if (fs::is_regular_file(current->status())) | |
232 { | |
233 try | |
234 { | |
235 fs::path d = current->path(); | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
236 std::string uuid = ToString(d); |
0 | 237 if (Toolbox::IsUuid(uuid)) |
238 { | |
239 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
|
240 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
|
241 std::string p2 = ToString(d.parent_path()); |
0 | 242 if (p1.length() == 2 && |
243 p2.length() == 2 && | |
244 p1 == uuid.substr(0, 2) && | |
245 p2 == uuid.substr(2, 2) && | |
246 p0 == root_) | |
247 { | |
248 result.insert(uuid); | |
249 } | |
250 } | |
251 } | |
252 catch (fs::filesystem_error) | |
253 { | |
254 } | |
255 } | |
256 } | |
257 } | |
258 } | |
259 | |
260 | |
261 void FileStorage::Clear() | |
262 { | |
263 namespace fs = boost::filesystem; | |
264 typedef std::set<std::string> List; | |
265 | |
266 List result; | |
267 ListAllFiles(result); | |
268 | |
269 for (List::const_iterator it = result.begin(); it != result.end(); it++) | |
270 { | |
271 Remove(*it); | |
272 } | |
273 } | |
274 | |
275 | |
276 void FileStorage::Remove(const std::string& uuid) | |
277 { | |
278 namespace fs = boost::filesystem; | |
279 | |
280 fs::path p = GetPath(uuid); | |
147 | 281 |
282 try | |
283 { | |
284 fs::remove(p); | |
285 } | |
148 | 286 catch (...) |
147 | 287 { |
288 // Ignore the error | |
289 } | |
0 | 290 |
291 // Remove the two parent directories, ignoring the error code if | |
292 // these directories are not empty | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
293 |
142 | 294 try |
295 { | |
147 | 296 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
297 boost::system::error_code err; | |
298 fs::remove(p.parent_path(), err); | |
299 fs::remove(p.parent_path().parent_path(), err); | |
300 #else | |
301 fs::remove(p.parent_path()); | |
302 fs::remove(p.parent_path().parent_path()); | |
303 #endif | |
142 | 304 } |
148 | 305 catch (...) |
142 | 306 { |
307 // Ignore the error | |
308 } | |
309 } | |
310 | |
311 | |
0 | 312 uintmax_t FileStorage::GetCapacity() const |
313 { | |
314 return boost::filesystem::space(root_).capacity; | |
315 } | |
316 | |
317 uintmax_t FileStorage::GetAvailableSpace() const | |
318 { | |
319 return boost::filesystem::space(root_).available; | |
320 } | |
321 } |