Mercurial > hg > orthanc
comparison Core/FileStorage.cpp @ 0:3959d33612cc
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Jul 2012 14:32:22 +0200 |
parents | |
children | a15e90e5d6fc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3959d33612cc |
---|---|
1 /** | |
2 * Palantir - A Lightweight, RESTful DICOM Store | |
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. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "FileStorage.h" | |
22 | |
23 // http://stackoverflow.com/questions/1576272/storing-large-number-of-files-in-file-system | |
24 // http://stackoverflow.com/questions/446358/storing-a-large-number-of-images | |
25 | |
26 #include "PalantirException.h" | |
27 #include "Toolbox.h" | |
28 #include "Uuid.h" | |
29 | |
30 #include <boost/filesystem/fstream.hpp> | |
31 | |
32 namespace Palantir | |
33 { | |
34 boost::filesystem::path FileStorage::GetPath(const std::string& uuid) const | |
35 { | |
36 namespace fs = boost::filesystem; | |
37 | |
38 if (!Toolbox::IsUuid(uuid)) | |
39 { | |
40 throw PalantirException(ErrorCode_ParameterOutOfRange); | |
41 } | |
42 | |
43 fs::path path = root_; | |
44 | |
45 path /= std::string(&uuid[0], &uuid[2]); | |
46 path /= std::string(&uuid[2], &uuid[4]); | |
47 path /= uuid; | |
48 path.make_preferred(); | |
49 | |
50 return path; | |
51 } | |
52 | |
53 FileStorage::FileStorage(std::string root) | |
54 { | |
55 namespace fs = boost::filesystem; | |
56 | |
57 root_ = fs::absolute(root); | |
58 | |
59 if (fs::exists(root)) | |
60 { | |
61 if (!fs::is_directory(root)) | |
62 { | |
63 throw PalantirException("The file storage root directory is a file"); | |
64 } | |
65 } | |
66 else | |
67 { | |
68 if (!fs::create_directories(root)) | |
69 { | |
70 throw PalantirException("Unable to create the file storage root directory"); | |
71 } | |
72 } | |
73 } | |
74 | |
75 std::string FileStorage::CreateFileWithoutCompression(const void* content, size_t size) | |
76 { | |
77 std::string uuid; | |
78 boost::filesystem::path path; | |
79 | |
80 for (;;) | |
81 { | |
82 uuid = Toolbox::GenerateUuid(); | |
83 path = GetPath(uuid); | |
84 | |
85 if (!boost::filesystem::exists(path)) | |
86 { | |
87 // OK, this is indeed a new file | |
88 break; | |
89 } | |
90 | |
91 // Extremely improbable case: This Uuid has already been created | |
92 // in the past. Try again. | |
93 } | |
94 | |
95 if (boost::filesystem::exists(path.parent_path())) | |
96 { | |
97 if (!boost::filesystem::is_directory(path.parent_path())) | |
98 { | |
99 throw PalantirException("The subdirectory to be created is already occupied by a regular file"); | |
100 } | |
101 } | |
102 else | |
103 { | |
104 if (!boost::filesystem::create_directories(path.parent_path())) | |
105 { | |
106 throw PalantirException("Unable to create a subdirectory in the file storage"); | |
107 } | |
108 } | |
109 | |
110 boost::filesystem::ofstream f; | |
111 f.open(path, std::ofstream::out | std::ios::binary); | |
112 if (!f.good()) | |
113 { | |
114 throw PalantirException("Unable to create a new file in the file storage"); | |
115 } | |
116 | |
117 if (size != 0) | |
118 { | |
119 f.write(static_cast<const char*>(content), size); | |
120 if (!f.good()) | |
121 { | |
122 f.close(); | |
123 throw PalantirException("Unable to write to the new file in the file storage"); | |
124 } | |
125 } | |
126 | |
127 f.close(); | |
128 | |
129 return uuid; | |
130 } | |
131 | |
132 | |
133 std::string FileStorage::Create(const void* content, size_t size) | |
134 { | |
135 if (!HasBufferCompressor() || size == 0) | |
136 { | |
137 return CreateFileWithoutCompression(content, size); | |
138 } | |
139 else | |
140 { | |
141 std::string compressed; | |
142 compressor_->Compress(compressed, content, size); | |
143 assert(compressed.size() > 0); | |
144 return CreateFileWithoutCompression(&compressed[0], compressed.size()); | |
145 } | |
146 } | |
147 | |
148 | |
149 std::string FileStorage::Create(const std::vector<uint8_t>& content) | |
150 { | |
151 if (content.size() == 0) | |
152 return Create(NULL, 0); | |
153 else | |
154 return Create(&content[0], content.size()); | |
155 } | |
156 | |
157 std::string FileStorage::Create(const std::string& content) | |
158 { | |
159 if (content.size() == 0) | |
160 return Create(NULL, 0); | |
161 else | |
162 return Create(&content[0], content.size()); | |
163 } | |
164 | |
165 void FileStorage::ReadFile(std::string& content, | |
166 const std::string& uuid) const | |
167 { | |
168 content.clear(); | |
169 | |
170 if (HasBufferCompressor()) | |
171 { | |
172 std::string compressed; | |
173 Toolbox::ReadFile(compressed, GetPath(uuid).string()); | |
174 | |
175 if (compressed.size() != 0) | |
176 { | |
177 compressor_->Uncompress(content, compressed); | |
178 } | |
179 } | |
180 else | |
181 { | |
182 Toolbox::ReadFile(content, GetPath(uuid).string()); | |
183 } | |
184 } | |
185 | |
186 | |
187 uintmax_t FileStorage::GetCompressedSize(const std::string& uuid) const | |
188 { | |
189 boost::filesystem::path path = GetPath(uuid); | |
190 return boost::filesystem::file_size(path); | |
191 } | |
192 | |
193 | |
194 | |
195 void FileStorage::ListAllFiles(std::set<std::string>& result) const | |
196 { | |
197 namespace fs = boost::filesystem; | |
198 | |
199 result.clear(); | |
200 | |
201 if (fs::exists(root_) && fs::is_directory(root_)) | |
202 { | |
203 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current) | |
204 { | |
205 if (fs::is_regular_file(current->status())) | |
206 { | |
207 try | |
208 { | |
209 fs::path d = current->path(); | |
210 std::string uuid = d.filename().string(); | |
211 if (Toolbox::IsUuid(uuid)) | |
212 { | |
213 fs::path p0 = d.parent_path().parent_path().parent_path(); | |
214 std::string p1 = d.parent_path().parent_path().filename().string(); | |
215 std::string p2 = d.parent_path().filename().string(); | |
216 if (p1.length() == 2 && | |
217 p2.length() == 2 && | |
218 p1 == uuid.substr(0, 2) && | |
219 p2 == uuid.substr(2, 2) && | |
220 p0 == root_) | |
221 { | |
222 result.insert(uuid); | |
223 } | |
224 } | |
225 } | |
226 catch (fs::filesystem_error) | |
227 { | |
228 } | |
229 } | |
230 } | |
231 } | |
232 } | |
233 | |
234 | |
235 void FileStorage::Clear() | |
236 { | |
237 namespace fs = boost::filesystem; | |
238 typedef std::set<std::string> List; | |
239 | |
240 List result; | |
241 ListAllFiles(result); | |
242 | |
243 for (List::const_iterator it = result.begin(); it != result.end(); it++) | |
244 { | |
245 Remove(*it); | |
246 } | |
247 } | |
248 | |
249 | |
250 void FileStorage::Remove(const std::string& uuid) | |
251 { | |
252 namespace fs = boost::filesystem; | |
253 | |
254 fs::path p = GetPath(uuid); | |
255 fs::remove(p); | |
256 | |
257 // Remove the two parent directories, ignoring the error code if | |
258 // these directories are not empty | |
259 boost::system::error_code err; | |
260 fs::remove(p.parent_path(), err); | |
261 fs::remove(p.parent_path().parent_path(), err); | |
262 } | |
263 | |
264 | |
265 uintmax_t FileStorage::GetCapacity() const | |
266 { | |
267 return boost::filesystem::space(root_).capacity; | |
268 } | |
269 | |
270 uintmax_t FileStorage::GetAvailableSpace() const | |
271 { | |
272 return boost::filesystem::space(root_).available; | |
273 } | |
274 } |