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