Mercurial > hg > orthanc
annotate Core/FileStorage/FilesystemStorage.cpp @ 1461:d659b6a4e736 Orthanc-0.9.1
set version
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 02 Jul 2015 16:50:33 +0200 |
parents | 704de8c30ff5 |
children | f967bdf8534e |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1250
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
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 | |
234 | 39 #include "../OrthancException.h" |
40 #include "../Toolbox.h" | |
41 #include "../Uuid.h" | |
0 | 42 |
43 #include <boost/filesystem/fstream.hpp> | |
1397 | 44 |
45 #if HAVE_GOOGLE_LOG == 1 | |
201
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
148
diff
changeset
|
46 #include <glog/logging.h> |
1397 | 47 #endif |
0 | 48 |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
49 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
|
50 { |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
51 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
52 return p.filename().string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
53 #else |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
54 return p.filename(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
55 #endif |
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 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
58 |
59 | 59 namespace Orthanc |
0 | 60 { |
1123 | 61 boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const |
0 | 62 { |
63 namespace fs = boost::filesystem; | |
64 | |
65 if (!Toolbox::IsUuid(uuid)) | |
66 { | |
59 | 67 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 68 } |
69 | |
70 fs::path path = root_; | |
71 | |
72 path /= std::string(&uuid[0], &uuid[2]); | |
73 path /= std::string(&uuid[2], &uuid[4]); | |
74 path /= uuid; | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
75 |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
76 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
0 | 77 path.make_preferred(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
78 #endif |
0 | 79 |
80 return path; | |
81 } | |
82 | |
1123 | 83 FilesystemStorage::FilesystemStorage(std::string root) |
0 | 84 { |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
85 //root_ = boost::filesystem::absolute(root).string(); |
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
86 root_ = root; |
0 | 87 |
1397 | 88 Toolbox::MakeDirectory(root); |
0 | 89 } |
90 | |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
91 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
|
92 const void* content, |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
93 size_t size, |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
94 FileContentType /*type*/) |
0 | 95 { |
96 boost::filesystem::path path; | |
97 | |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
98 path = GetPath(uuid); |
0 | 99 |
1135
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
100 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
|
101 { |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
102 // 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
|
103 // in the past. |
67c3c1e4a6e0
index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
104 throw OrthancException(ErrorCode_InternalError); |
0 | 105 } |
106 | |
107 if (boost::filesystem::exists(path.parent_path())) | |
108 { | |
109 if (!boost::filesystem::is_directory(path.parent_path())) | |
110 { | |
59 | 111 throw OrthancException("The subdirectory to be created is already occupied by a regular file"); |
0 | 112 } |
113 } | |
114 else | |
115 { | |
116 if (!boost::filesystem::create_directories(path.parent_path())) | |
117 { | |
59 | 118 throw OrthancException("Unable to create a subdirectory in the file storage"); |
0 | 119 } |
120 } | |
121 | |
122 boost::filesystem::ofstream f; | |
123 f.open(path, std::ofstream::out | std::ios::binary); | |
124 if (!f.good()) | |
125 { | |
59 | 126 throw OrthancException("Unable to create a new file in the file storage"); |
0 | 127 } |
128 | |
129 if (size != 0) | |
130 { | |
131 f.write(static_cast<const char*>(content), size); | |
132 if (!f.good()) | |
133 { | |
134 f.close(); | |
59 | 135 throw OrthancException("Unable to write to the new file in the file storage"); |
0 | 136 } |
137 } | |
138 | |
139 f.close(); | |
140 } | |
141 | |
142 | |
1123 | 143 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
|
144 const std::string& uuid, |
1250
2ffe07abd9d8
removal of const on IStorageArea::Read()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1135
diff
changeset
|
145 FileContentType /*type*/) |
0 | 146 { |
147 content.clear(); | |
1122
1d60316c3618
simplifications in FileStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
148 Toolbox::ReadFile(content, GetPath(uuid).string()); |
0 | 149 } |
150 | |
151 | |
1123 | 152 uintmax_t FilesystemStorage::GetSize(const std::string& uuid) const |
0 | 153 { |
154 boost::filesystem::path path = GetPath(uuid); | |
155 return boost::filesystem::file_size(path); | |
156 } | |
157 | |
158 | |
159 | |
1123 | 160 void FilesystemStorage::ListAllFiles(std::set<std::string>& result) const |
0 | 161 { |
162 namespace fs = boost::filesystem; | |
163 | |
164 result.clear(); | |
165 | |
166 if (fs::exists(root_) && fs::is_directory(root_)) | |
167 { | |
168 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current) | |
169 { | |
170 if (fs::is_regular_file(current->status())) | |
171 { | |
172 try | |
173 { | |
174 fs::path d = current->path(); | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
175 std::string uuid = ToString(d); |
0 | 176 if (Toolbox::IsUuid(uuid)) |
177 { | |
178 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
|
179 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
|
180 std::string p2 = ToString(d.parent_path()); |
0 | 181 if (p1.length() == 2 && |
182 p2.length() == 2 && | |
183 p1 == uuid.substr(0, 2) && | |
184 p2 == uuid.substr(2, 2) && | |
185 p0 == root_) | |
186 { | |
187 result.insert(uuid); | |
188 } | |
189 } | |
190 } | |
191 catch (fs::filesystem_error) | |
192 { | |
193 } | |
194 } | |
195 } | |
196 } | |
197 } | |
198 | |
199 | |
1123 | 200 void FilesystemStorage::Clear() |
0 | 201 { |
202 namespace fs = boost::filesystem; | |
203 typedef std::set<std::string> List; | |
204 | |
205 List result; | |
206 ListAllFiles(result); | |
207 | |
656 | 208 for (List::const_iterator it = result.begin(); it != result.end(); ++it) |
0 | 209 { |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
210 Remove(*it, FileContentType_Unknown /*ignored in this class*/); |
0 | 211 } |
212 } | |
213 | |
214 | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
215 void FilesystemStorage::Remove(const std::string& uuid, |
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
216 FileContentType /*type*/) |
0 | 217 { |
1397 | 218 #if HAVE_GOOGLE_LOG == 1 |
201
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
148
diff
changeset
|
219 LOG(INFO) << "Deleting file " << uuid; |
1397 | 220 #endif |
221 | |
0 | 222 namespace fs = boost::filesystem; |
223 | |
224 fs::path p = GetPath(uuid); | |
147 | 225 |
226 try | |
227 { | |
228 fs::remove(p); | |
229 } | |
148 | 230 catch (...) |
147 | 231 { |
232 // Ignore the error | |
233 } | |
0 | 234 |
235 // Remove the two parent directories, ignoring the error code if | |
236 // these directories are not empty | |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
237 |
142 | 238 try |
239 { | |
147 | 240 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
241 boost::system::error_code err; | |
242 fs::remove(p.parent_path(), err); | |
243 fs::remove(p.parent_path().parent_path(), err); | |
244 #else | |
245 fs::remove(p.parent_path()); | |
246 fs::remove(p.parent_path().parent_path()); | |
247 #endif | |
142 | 248 } |
148 | 249 catch (...) |
142 | 250 { |
251 // Ignore the error | |
252 } | |
253 } | |
254 | |
255 | |
1123 | 256 uintmax_t FilesystemStorage::GetCapacity() const |
0 | 257 { |
258 return boost::filesystem::space(root_).capacity; | |
259 } | |
260 | |
1123 | 261 uintmax_t FilesystemStorage::GetAvailableSpace() const |
0 | 262 { |
263 return boost::filesystem::space(root_).available; | |
264 } | |
265 } |