annotate OrthancFramework/Sources/FileStorage/FilesystemStorage.cpp @ 4185:b289a1234822

giving a try to cross-platform compilation of SyncStorageArea
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 14 Sep 2020 18:09:30 +0200
parents bf7b9edf6b81
children 0034f855c023
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
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
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * the License, or (at your option) any later version.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 110
diff changeset
11 *
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
15 * Lesser General Public License for more details.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
18 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * <http://www.gnu.org/licenses/>.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 803
diff changeset
23 #include "../PrecompiledHeaders.h"
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
24 #include "FilesystemStorage.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 // http://stackoverflow.com/questions/1576272/storing-large-number-of-files-in-file-system
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 // http://stackoverflow.com/questions/446358/storing-a-large-number-of-images
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1397
diff changeset
29 #include "../Logging.h"
234
7c1faef915a4 cleaning room
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
30 #include "../OrthancException.h"
7c1faef915a4 cleaning room
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
31 #include "../Toolbox.h"
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2142
diff changeset
32 #include "../SystemToolbox.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include <boost/filesystem/fstream.hpp>
1397
704de8c30ff5 modularization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
35
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
110
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
37 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
38 {
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
39 #if BOOST_HAS_FILESYSTEM_V3 == 1
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
40 return p.filename().string();
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
41 #else
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
42 return p.filename();
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
43 #endif
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
44 }
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
45
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
46
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
47 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
49 boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 namespace fs = boost::filesystem;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 if (!Toolbox::IsUuid(uuid))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
55 throw OrthancException(ErrorCode_ParameterOutOfRange);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 fs::path path = root_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 path /= std::string(&uuid[0], &uuid[2]);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 path /= std::string(&uuid[2], &uuid[4]);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 path /= uuid;
110
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
63
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
64 #if BOOST_HAS_FILESYSTEM_V3 == 1
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 path.make_preferred();
110
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
66 #endif
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 return path;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
71 void FilesystemStorage::Setup(const std::string& root)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
110
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
73 //root_ = boost::filesystem::absolute(root).string();
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
74 root_ = root;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2129
diff changeset
76 SystemToolbox::MakeDirectory(root);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
2087
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
79
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
80
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
81 static const char* GetDescriptionInternal(FileContentType content)
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
82 {
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
83 // 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
84 // fully-featured version is available in ServerEnumerations.cpp
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
85 switch (content)
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
86 {
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
87 case FileContentType_Unknown:
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
88 return "Unknown";
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 case FileContentType_Dicom:
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
91 return "DICOM";
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 case FileContentType_DicomAsJson:
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
94 return "JSON summary of DICOM";
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
95
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
96 default:
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
97 return "User-defined";
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
98 }
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
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
101
1135
67c3c1e4a6e0 index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1126
diff changeset
102 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
103 const void* content,
67c3c1e4a6e0 index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1126
diff changeset
104 size_t size,
2087
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
105 FileContentType type)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
2087
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
107 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
108 << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)";
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
109
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 boost::filesystem::path path;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
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 path = GetPath(uuid);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
1135
67c3c1e4a6e0 index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1126
diff changeset
114 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
115 {
67c3c1e4a6e0 index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1126
diff changeset
116 // 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
117 // in the past.
67c3c1e4a6e0 index-only mode, and custom storage area with plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1126
diff changeset
118 throw OrthancException(ErrorCode_InternalError);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 if (boost::filesystem::exists(path.parent_path()))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 if (!boost::filesystem::is_directory(path.parent_path()))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
1582
bd1889029cbb encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1487
diff changeset
125 throw OrthancException(ErrorCode_DirectoryOverFile);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 if (!boost::filesystem::create_directories(path.parent_path()))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
1582
bd1889029cbb encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1487
diff changeset
132 throw OrthancException(ErrorCode_FileStorageCannotWrite);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
136 SystemToolbox::WriteFile(content, size, path.string(), fsyncOnWrite_);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
140 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
141 const std::string& uuid,
2087
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
142 FileContentType type)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
2087
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
144 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
145 << "\" content type";
e9e6ffbf0fd5 improved logging in FilesystemStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1911
diff changeset
146
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 content.clear();
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2129
diff changeset
148 SystemToolbox::ReadFile(content, GetPath(uuid).string());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
152 uintmax_t FilesystemStorage::GetSize(const std::string& uuid) const
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 boost::filesystem::path path = GetPath(uuid);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 return boost::filesystem::file_size(path);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
160 void FilesystemStorage::ListAllFiles(std::set<std::string>& result) const
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 namespace fs = boost::filesystem;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 result.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 if (fs::exists(root_) && fs::is_directory(root_))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2129
diff changeset
170 if (SystemToolbox::IsRegularFile(current->path().string()))
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 try
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 if (Toolbox::IsUuid(uuid))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 if (p1.length() == 2 &&
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 p2.length() == 2 &&
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 p1 == uuid.substr(0, 2) &&
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 p2 == uuid.substr(2, 2) &&
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 p0 == root_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 result.insert(uuid);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 }
2836
7133ad478eea fix Debian warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
191 catch (fs::filesystem_error&)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
200 void FilesystemStorage::Clear()
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 namespace fs = boost::filesystem;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 typedef std::set<std::string> List;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 List result;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 ListAllFiles(result);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
656
08eca5d86aad fixes to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
208 for (List::const_iterator it = result.begin(); it != result.end(); ++it)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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,
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
216 FileContentType type)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 {
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
218 LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type);
1397
704de8c30ff5 modularization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
219
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 namespace fs = boost::filesystem;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 fs::path p = GetPath(uuid);
147
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
223
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
224 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
225 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
226 fs::remove(p);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
227 }
148
jodogne
parents: 147
diff changeset
228 catch (...)
147
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
229 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
230 // Ignore the error
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
231 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 // Remove the two parent directories, ignoring the error code if
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 // these directories are not empty
110
fd7b0a3e6260 support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
235
142
jodogne
parents: 136
diff changeset
236 try
jodogne
parents: 136
diff changeset
237 {
147
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
238 #if BOOST_HAS_FILESYSTEM_V3 == 1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
239 boost::system::error_code err;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
240 fs::remove(p.parent_path(), err);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
241 fs::remove(p.parent_path().parent_path(), err);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
242 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
243 fs::remove(p.parent_path());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
244 fs::remove(p.parent_path().parent_path());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 142
diff changeset
245 #endif
142
jodogne
parents: 136
diff changeset
246 }
148
jodogne
parents: 147
diff changeset
247 catch (...)
142
jodogne
parents: 136
diff changeset
248 {
jodogne
parents: 136
diff changeset
249 // Ignore the error
jodogne
parents: 136
diff changeset
250 }
jodogne
parents: 136
diff changeset
251 }
jodogne
parents: 136
diff changeset
252
jodogne
parents: 136
diff changeset
253
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
254 uintmax_t FilesystemStorage::GetCapacity() const
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 return boost::filesystem::space(root_).capacity;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
1123
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1122
diff changeset
259 uintmax_t FilesystemStorage::GetAvailableSpace() const
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 return boost::filesystem::space(root_).available;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 }