Mercurial > hg > orthanc
annotate Core/FileStorage/FilesystemStorage.cpp @ 2426:23f5ff059ce2
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Oct 2017 12:51:11 +0200 |
parents | a3a65de1840f |
children | 878b59270859 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1250
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
136 | 11 * |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
0 | 23 * |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
34 #include "../PrecompiledHeaders.h" |
1123 | 35 #include "FilesystemStorage.h" |
0 | 36 |
37 // http://stackoverflow.com/questions/1576272/storing-large-number-of-files-in-file-system | |
38 // http://stackoverflow.com/questions/446358/storing-a-large-number-of-images | |
39 | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1397
diff
changeset
|
40 #include "../Logging.h" |
234 | 41 #include "../OrthancException.h" |
42 #include "../Toolbox.h" | |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
43 #include "../SystemToolbox.h" |
0 | 44 |
45 #include <boost/filesystem/fstream.hpp> | |
1397 | 46 |
0 | 47 |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
48 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
|
49 { |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
50 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
51 return p.filename().string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
52 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
53 return p.filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
54 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
55 } |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
56 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
57 |
59 | 58 namespace Orthanc |
0 | 59 { |
1123 | 60 boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const |
0 | 61 { |
62 namespace fs = boost::filesystem; | |
63 | |
64 if (!Toolbox::IsUuid(uuid)) | |
65 { | |
59 | 66 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 67 } |
68 | |
69 fs::path path = root_; | |
70 | |
71 path /= std::string(&uuid[0], &uuid[2]); | |
72 path /= std::string(&uuid[2], &uuid[4]); | |
73 path /= uuid; | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
74 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
75 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 76 path.make_preferred(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
77 #endif |
0 | 78 |
79 return path; | |
80 } | |
81 | |
1123 | 82 FilesystemStorage::FilesystemStorage(std::string root) |
0 | 83 { |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
84 //root_ = boost::filesystem::absolute(root).string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
85 root_ = root; |
0 | 86 |
2140 | 87 SystemToolbox::MakeDirectory(root); |
0 | 88 } |
89 | |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
90 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
91 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
92 static const char* GetDescriptionInternal(FileContentType content) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
93 { |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
94 // This function is for logging only (internal use), a more |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
95 // fully-featured version is available in ServerEnumerations.cpp |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
96 switch (content) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
97 { |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
98 case FileContentType_Unknown: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
99 return "Unknown"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
100 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
101 case FileContentType_Dicom: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
102 return "DICOM"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
103 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
104 case FileContentType_DicomAsJson: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
105 return "JSON summary of DICOM"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
106 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
107 default: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
108 return "User-defined"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
109 } |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
110 } |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
111 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
112 |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
113 void FilesystemStorage::Create(const std::string& uuid, |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
114 const void* content, |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
115 size_t size, |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
116 FileContentType type) |
0 | 117 { |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
118 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
119 << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
120 |
0 | 121 boost::filesystem::path path; |
122 | |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
123 path = GetPath(uuid); |
0 | 124 |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
125 if (boost::filesystem::exists(path)) |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
126 { |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
127 // Extremely unlikely case: This Uuid has already been created |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
128 // in the past. |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
129 throw OrthancException(ErrorCode_InternalError); |
0 | 130 } |
131 | |
132 if (boost::filesystem::exists(path.parent_path())) | |
133 { | |
134 if (!boost::filesystem::is_directory(path.parent_path())) | |
135 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1487
diff
changeset
|
136 throw OrthancException(ErrorCode_DirectoryOverFile); |
0 | 137 } |
138 } | |
139 else | |
140 { | |
141 if (!boost::filesystem::create_directories(path.parent_path())) | |
142 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1487
diff
changeset
|
143 throw OrthancException(ErrorCode_FileStorageCannotWrite); |
0 | 144 } |
145 } | |
146 | |
2140 | 147 SystemToolbox::WriteFile(content, size, path.string()); |
0 | 148 } |
149 | |
150 | |
1123 | 151 void FilesystemStorage::Read(std::string& content, |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
152 const std::string& uuid, |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
153 FileContentType type) |
0 | 154 { |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
155 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
156 << "\" content type"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
157 |
0 | 158 content.clear(); |
2140 | 159 SystemToolbox::ReadFile(content, GetPath(uuid).string()); |
0 | 160 } |
161 | |
162 | |
1123 | 163 uintmax_t FilesystemStorage::GetSize(const std::string& uuid) const |
0 | 164 { |
165 boost::filesystem::path path = GetPath(uuid); | |
166 return boost::filesystem::file_size(path); | |
167 } | |
168 | |
169 | |
170 | |
1123 | 171 void FilesystemStorage::ListAllFiles(std::set<std::string>& result) const |
0 | 172 { |
173 namespace fs = boost::filesystem; | |
174 | |
175 result.clear(); | |
176 | |
177 if (fs::exists(root_) && fs::is_directory(root_)) | |
178 { | |
179 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current) | |
180 { | |
2140 | 181 if (SystemToolbox::IsRegularFile(current->path().string())) |
0 | 182 { |
183 try | |
184 { | |
185 fs::path d = current->path(); | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
186 std::string uuid = ToString(d); |
0 | 187 if (Toolbox::IsUuid(uuid)) |
188 { | |
189 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
|
190 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
|
191 std::string p2 = ToString(d.parent_path()); |
0 | 192 if (p1.length() == 2 && |
193 p2.length() == 2 && | |
194 p1 == uuid.substr(0, 2) && | |
195 p2 == uuid.substr(2, 2) && | |
196 p0 == root_) | |
197 { | |
198 result.insert(uuid); | |
199 } | |
200 } | |
201 } | |
202 catch (fs::filesystem_error) | |
203 { | |
204 } | |
205 } | |
206 } | |
207 } | |
208 } | |
209 | |
210 | |
1123 | 211 void FilesystemStorage::Clear() |
0 | 212 { |
213 namespace fs = boost::filesystem; | |
214 typedef std::set<std::string> List; | |
215 | |
216 List result; | |
217 ListAllFiles(result); | |
218 | |
656 | 219 for (List::const_iterator it = result.begin(); it != result.end(); ++it) |
0 | 220 { |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
221 Remove(*it, FileContentType_Unknown /*ignored in this class*/); |
0 | 222 } |
223 } | |
224 | |
225 | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
226 void FilesystemStorage::Remove(const std::string& uuid, |
2129
0c09d1af22f3
"/tools/invalidate-tags" to invalidate the JSON summary of all the DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2087
diff
changeset
|
227 FileContentType type) |
0 | 228 { |
2129
0c09d1af22f3
"/tools/invalidate-tags" to invalidate the JSON summary of all the DICOM files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2087
diff
changeset
|
229 LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type); |
1397 | 230 |
0 | 231 namespace fs = boost::filesystem; |
232 | |
233 fs::path p = GetPath(uuid); | |
147 | 234 |
235 try | |
236 { | |
237 fs::remove(p); | |
238 } | |
148 | 239 catch (...) |
147 | 240 { |
241 // Ignore the error | |
242 } | |
0 | 243 |
244 // Remove the two parent directories, ignoring the error code if | |
245 // these directories are not empty | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
246 |
142 | 247 try |
248 { | |
147 | 249 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
250 boost::system::error_code err; | |
251 fs::remove(p.parent_path(), err); | |
252 fs::remove(p.parent_path().parent_path(), err); | |
253 #else | |
254 fs::remove(p.parent_path()); | |
255 fs::remove(p.parent_path().parent_path()); | |
256 #endif | |
142 | 257 } |
148 | 258 catch (...) |
142 | 259 { |
260 // Ignore the error | |
261 } | |
262 } | |
263 | |
264 | |
1123 | 265 uintmax_t FilesystemStorage::GetCapacity() const |
0 | 266 { |
267 return boost::filesystem::space(root_).capacity; | |
268 } | |
269 | |
1123 | 270 uintmax_t FilesystemStorage::GetAvailableSpace() const |
0 | 271 { |
272 return boost::filesystem::space(root_).available; | |
273 } | |
274 } |