Mercurial > hg > orthanc
annotate Core/FileStorage/FilesystemStorage.cpp @ 2133:15ae532af70e
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 10:21:37 +0100 |
parents | 0c09d1af22f3 |
children | aa4b8895cd23 |
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 |
0 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
1123 | 34 #include "FilesystemStorage.h" |
0 | 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 | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1397
diff
changeset
|
39 #include "../Logging.h" |
234 | 40 #include "../OrthancException.h" |
41 #include "../Toolbox.h" | |
42 #include "../Uuid.h" | |
0 | 43 |
44 #include <boost/filesystem/fstream.hpp> | |
1397 | 45 |
0 | 46 |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
47 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
|
48 { |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
49 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
50 return p.filename().string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
51 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
52 return p.filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
53 #endif |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
54 } |
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 |
59 | 57 namespace Orthanc |
0 | 58 { |
1123 | 59 boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const |
0 | 60 { |
61 namespace fs = boost::filesystem; | |
62 | |
63 if (!Toolbox::IsUuid(uuid)) | |
64 { | |
59 | 65 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 66 } |
67 | |
68 fs::path path = root_; | |
69 | |
70 path /= std::string(&uuid[0], &uuid[2]); | |
71 path /= std::string(&uuid[2], &uuid[4]); | |
72 path /= uuid; | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
73 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
74 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 75 path.make_preferred(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
76 #endif |
0 | 77 |
78 return path; | |
79 } | |
80 | |
1123 | 81 FilesystemStorage::FilesystemStorage(std::string root) |
0 | 82 { |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
83 //root_ = boost::filesystem::absolute(root).string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
84 root_ = root; |
0 | 85 |
1397 | 86 Toolbox::MakeDirectory(root); |
0 | 87 } |
88 | |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
89 |
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 static const char* GetDescriptionInternal(FileContentType content) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
92 { |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
93 // 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
|
94 // fully-featured version is available in ServerEnumerations.cpp |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
95 switch (content) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
96 { |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
97 case FileContentType_Unknown: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
98 return "Unknown"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
99 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
100 case FileContentType_Dicom: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
101 return "DICOM"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
102 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
103 case FileContentType_DicomAsJson: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
104 return "JSON summary of DICOM"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
105 |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
106 default: |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
107 return "User-defined"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
108 } |
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 |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
112 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
|
113 const void* content, |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
114 size_t size, |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
115 FileContentType type) |
0 | 116 { |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
117 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
118 << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
119 |
0 | 120 boost::filesystem::path path; |
121 | |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
122 path = GetPath(uuid); |
0 | 123 |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
124 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
|
125 { |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
126 // 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
|
127 // in the past. |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
128 throw OrthancException(ErrorCode_InternalError); |
0 | 129 } |
130 | |
131 if (boost::filesystem::exists(path.parent_path())) | |
132 { | |
133 if (!boost::filesystem::is_directory(path.parent_path())) | |
134 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1487
diff
changeset
|
135 throw OrthancException(ErrorCode_DirectoryOverFile); |
0 | 136 } |
137 } | |
138 else | |
139 { | |
140 if (!boost::filesystem::create_directories(path.parent_path())) | |
141 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1487
diff
changeset
|
142 throw OrthancException(ErrorCode_FileStorageCannotWrite); |
0 | 143 } |
144 } | |
145 | |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
146 Toolbox::WriteFile(content, size, path.string()); |
0 | 147 } |
148 | |
149 | |
1123 | 150 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
|
151 const std::string& uuid, |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
152 FileContentType type) |
0 | 153 { |
2087
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
154 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type) |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
155 << "\" content type"; |
e9e6ffbf0fd5
improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1911
diff
changeset
|
156 |
0 | 157 content.clear(); |
1122
1d60316c3618
simplifications in FileStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
158 Toolbox::ReadFile(content, GetPath(uuid).string()); |
0 | 159 } |
160 | |
161 | |
1123 | 162 uintmax_t FilesystemStorage::GetSize(const std::string& uuid) const |
0 | 163 { |
164 boost::filesystem::path path = GetPath(uuid); | |
165 return boost::filesystem::file_size(path); | |
166 } | |
167 | |
168 | |
169 | |
1123 | 170 void FilesystemStorage::ListAllFiles(std::set<std::string>& result) const |
0 | 171 { |
172 namespace fs = boost::filesystem; | |
173 | |
174 result.clear(); | |
175 | |
176 if (fs::exists(root_) && fs::is_directory(root_)) | |
177 { | |
178 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current) | |
179 { | |
1911 | 180 if (Toolbox::IsRegularFile(current->path().string())) |
0 | 181 { |
182 try | |
183 { | |
184 fs::path d = current->path(); | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
185 std::string uuid = ToString(d); |
0 | 186 if (Toolbox::IsUuid(uuid)) |
187 { | |
188 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
|
189 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
|
190 std::string p2 = ToString(d.parent_path()); |
0 | 191 if (p1.length() == 2 && |
192 p2.length() == 2 && | |
193 p1 == uuid.substr(0, 2) && | |
194 p2 == uuid.substr(2, 2) && | |
195 p0 == root_) | |
196 { | |
197 result.insert(uuid); | |
198 } | |
199 } | |
200 } | |
201 catch (fs::filesystem_error) | |
202 { | |
203 } | |
204 } | |
205 } | |
206 } | |
207 } | |
208 | |
209 | |
1123 | 210 void FilesystemStorage::Clear() |
0 | 211 { |
212 namespace fs = boost::filesystem; | |
213 typedef std::set<std::string> List; | |
214 | |
215 List result; | |
216 ListAllFiles(result); | |
217 | |
656 | 218 for (List::const_iterator it = result.begin(); it != result.end(); ++it) |
0 | 219 { |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
220 Remove(*it, FileContentType_Unknown /*ignored in this class*/); |
0 | 221 } |
222 } | |
223 | |
224 | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
225 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
|
226 FileContentType type) |
0 | 227 { |
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
|
228 LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type); |
1397 | 229 |
0 | 230 namespace fs = boost::filesystem; |
231 | |
232 fs::path p = GetPath(uuid); | |
147 | 233 |
234 try | |
235 { | |
236 fs::remove(p); | |
237 } | |
148 | 238 catch (...) |
147 | 239 { |
240 // Ignore the error | |
241 } | |
0 | 242 |
243 // Remove the two parent directories, ignoring the error code if | |
244 // these directories are not empty | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
245 |
142 | 246 try |
247 { | |
147 | 248 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
249 boost::system::error_code err; | |
250 fs::remove(p.parent_path(), err); | |
251 fs::remove(p.parent_path().parent_path(), err); | |
252 #else | |
253 fs::remove(p.parent_path()); | |
254 fs::remove(p.parent_path().parent_path()); | |
255 #endif | |
142 | 256 } |
148 | 257 catch (...) |
142 | 258 { |
259 // Ignore the error | |
260 } | |
261 } | |
262 | |
263 | |
1123 | 264 uintmax_t FilesystemStorage::GetCapacity() const |
0 | 265 { |
266 return boost::filesystem::space(root_).capacity; | |
267 } | |
268 | |
1123 | 269 uintmax_t FilesystemStorage::GetAvailableSpace() const |
0 | 270 { |
271 return boost::filesystem::space(root_).available; | |
272 } | |
273 } |