comparison Core/FileStorage/FilesystemStorage.cpp @ 1123:6c5a77637b23

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