Mercurial > hg > orthanc-webviewer
comparison Orthanc/FileStorage/FilesystemStorage.cpp @ 0:02f7a0400a91
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Feb 2015 13:45:35 +0100 |
parents | |
children | 7a0af291cc90 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:02f7a0400a91 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, 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 * 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. | |
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 "../PrecompiledHeaders.h" | |
34 #include "FilesystemStorage.h" | |
35 | |
36 // http://stackoverflow.com/questions/1576272/storing-large-number-of-files-in-file-system | |
37 // http://stackoverflow.com/questions/446358/storing-a-large-number-of-images | |
38 | |
39 #include "../OrthancException.h" | |
40 #include "../Toolbox.h" | |
41 #include "../Uuid.h" | |
42 | |
43 #include <boost/filesystem/fstream.hpp> | |
44 | |
45 static std::string ToString(const boost::filesystem::path& p) | |
46 { | |
47 #if BOOST_HAS_FILESYSTEM_V3 == 1 | |
48 return p.filename().string(); | |
49 #else | |
50 return p.filename(); | |
51 #endif | |
52 } | |
53 | |
54 | |
55 namespace Orthanc | |
56 { | |
57 boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const | |
58 { | |
59 namespace fs = boost::filesystem; | |
60 | |
61 if (!Toolbox::IsUuid(uuid)) | |
62 { | |
63 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
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; | |
71 | |
72 #if BOOST_HAS_FILESYSTEM_V3 == 1 | |
73 path.make_preferred(); | |
74 #endif | |
75 | |
76 return path; | |
77 } | |
78 | |
79 FilesystemStorage::FilesystemStorage(std::string root) | |
80 { | |
81 //root_ = boost::filesystem::absolute(root).string(); | |
82 root_ = root; | |
83 | |
84 Toolbox::CreateNewDirectory(root); | |
85 } | |
86 | |
87 void FilesystemStorage::Create(const std::string& uuid, | |
88 const void* content, | |
89 size_t size) | |
90 { | |
91 boost::filesystem::path path; | |
92 | |
93 path = GetPath(uuid); | |
94 | |
95 if (boost::filesystem::exists(path)) | |
96 { | |
97 // Extremely unlikely case: This Uuid has already been created | |
98 // in the past. | |
99 throw OrthancException(ErrorCode_InternalError); | |
100 } | |
101 | |
102 if (boost::filesystem::exists(path.parent_path())) | |
103 { | |
104 if (!boost::filesystem::is_directory(path.parent_path())) | |
105 { | |
106 throw OrthancException("The subdirectory to be created is already occupied by a regular file"); | |
107 } | |
108 } | |
109 else | |
110 { | |
111 if (!boost::filesystem::create_directories(path.parent_path())) | |
112 { | |
113 throw OrthancException("Unable to create a subdirectory in the file storage"); | |
114 } | |
115 } | |
116 | |
117 boost::filesystem::ofstream f; | |
118 f.open(path, std::ofstream::out | std::ios::binary); | |
119 if (!f.good()) | |
120 { | |
121 throw OrthancException("Unable to create a new file in the file storage"); | |
122 } | |
123 | |
124 if (size != 0) | |
125 { | |
126 f.write(static_cast<const char*>(content), size); | |
127 if (!f.good()) | |
128 { | |
129 f.close(); | |
130 throw OrthancException("Unable to write to the new file in the file storage"); | |
131 } | |
132 } | |
133 | |
134 f.close(); | |
135 } | |
136 | |
137 | |
138 void FilesystemStorage::Read(std::string& content, | |
139 const std::string& uuid) | |
140 { | |
141 content.clear(); | |
142 Toolbox::ReadFile(content, GetPath(uuid).string()); | |
143 } | |
144 | |
145 | |
146 uintmax_t FilesystemStorage::GetSize(const std::string& uuid) const | |
147 { | |
148 boost::filesystem::path path = GetPath(uuid); | |
149 return boost::filesystem::file_size(path); | |
150 } | |
151 | |
152 | |
153 | |
154 void FilesystemStorage::ListAllFiles(std::set<std::string>& result) const | |
155 { | |
156 namespace fs = boost::filesystem; | |
157 | |
158 result.clear(); | |
159 | |
160 if (fs::exists(root_) && fs::is_directory(root_)) | |
161 { | |
162 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current) | |
163 { | |
164 if (fs::is_regular_file(current->status())) | |
165 { | |
166 try | |
167 { | |
168 fs::path d = current->path(); | |
169 std::string uuid = ToString(d); | |
170 if (Toolbox::IsUuid(uuid)) | |
171 { | |
172 fs::path p0 = d.parent_path().parent_path().parent_path(); | |
173 std::string p1 = ToString(d.parent_path().parent_path()); | |
174 std::string p2 = ToString(d.parent_path()); | |
175 if (p1.length() == 2 && | |
176 p2.length() == 2 && | |
177 p1 == uuid.substr(0, 2) && | |
178 p2 == uuid.substr(2, 2) && | |
179 p0 == root_) | |
180 { | |
181 result.insert(uuid); | |
182 } | |
183 } | |
184 } | |
185 catch (fs::filesystem_error) | |
186 { | |
187 } | |
188 } | |
189 } | |
190 } | |
191 } | |
192 | |
193 | |
194 void FilesystemStorage::Clear() | |
195 { | |
196 namespace fs = boost::filesystem; | |
197 typedef std::set<std::string> List; | |
198 | |
199 List result; | |
200 ListAllFiles(result); | |
201 | |
202 for (List::const_iterator it = result.begin(); it != result.end(); ++it) | |
203 { | |
204 Remove(*it); | |
205 } | |
206 } | |
207 | |
208 | |
209 void FilesystemStorage::Remove(const std::string& uuid) | |
210 { | |
211 namespace fs = boost::filesystem; | |
212 | |
213 fs::path p = GetPath(uuid); | |
214 | |
215 try | |
216 { | |
217 fs::remove(p); | |
218 } | |
219 catch (...) | |
220 { | |
221 // Ignore the error | |
222 } | |
223 | |
224 // Remove the two parent directories, ignoring the error code if | |
225 // these directories are not empty | |
226 | |
227 try | |
228 { | |
229 #if BOOST_HAS_FILESYSTEM_V3 == 1 | |
230 boost::system::error_code err; | |
231 fs::remove(p.parent_path(), err); | |
232 fs::remove(p.parent_path().parent_path(), err); | |
233 #else | |
234 fs::remove(p.parent_path()); | |
235 fs::remove(p.parent_path().parent_path()); | |
236 #endif | |
237 } | |
238 catch (...) | |
239 { | |
240 // Ignore the error | |
241 } | |
242 } | |
243 | |
244 | |
245 uintmax_t FilesystemStorage::GetCapacity() const | |
246 { | |
247 return boost::filesystem::space(root_).capacity; | |
248 } | |
249 | |
250 uintmax_t FilesystemStorage::GetAvailableSpace() const | |
251 { | |
252 return boost::filesystem::space(root_).available; | |
253 } | |
254 } |