annotate Common/FileSystemStorage.cpp @ 210:408c90c9027f default tip

todo: google soft delete
author Alain Mazy <am@orthanc.team>
date Wed, 09 Oct 2024 11:48:14 +0200
parents d62f52be1943
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 /**
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 * Cloud storage plugins for Orthanc
145
3c7e0374f28e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 111
diff changeset
3 * Copyright (C) 2020-2023 Osimis S.A., Belgium
3c7e0374f28e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 111
diff changeset
4 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
3c7e0374f28e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 111
diff changeset
5 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
6 *
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
10 * the License, or (at your option) any later version.
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
11 *
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 * Affero General Public License for more details.
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
16 *
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
19 **/
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
20
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
21 #include "FileSystemStorage.h"
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
22 #include "BaseStorage.h"
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
23
152
d62f52be1943 use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 145
diff changeset
24 #include <Logging.h>
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
25 #include <SystemToolbox.h>
152
d62f52be1943 use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 145
diff changeset
26
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
27 #include <boost/filesystem.hpp>
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 #include <boost/filesystem/fstream.hpp>
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
29
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
30 namespace fs = boost::filesystem;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
31
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
32 void FileSystemStoragePlugin::FileSystemWriter::Write(const char* data, size_t size)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
33 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
34 Orthanc::SystemToolbox::MakeDirectory(path_.parent_path().string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
35
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
36 Orthanc::SystemToolbox::WriteFile(reinterpret_cast<const void*>(data), size, path_.string(), fsync_);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
37 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
38
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
39 size_t FileSystemStoragePlugin::FileSystemReader::GetSize()
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
40 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
41 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string()))
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
42 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
43 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
44 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
45
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
46 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
47 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
48 fs::ifstream f;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
49 f.open(path_.string(), std::ifstream::in | std::ifstream::binary);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
50 if (!f.good())
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
51 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
52 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
53 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
54
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
55 f.seekg(0, std::ios::end);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
56 std::streamsize fileSize = f.tellg();
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
57 f.seekg(0, std::ios::beg);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
58
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
59 f.close();
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
60
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
61 return fileSize;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
62 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
63 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
64 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
65 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
66 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
67
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
68 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
69
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
70 void FileSystemStoragePlugin::FileSystemReader::ReadWhole(char* data, size_t size)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
71 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
72 ReadRange(data, size, 0);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
74
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
75 void FileSystemStoragePlugin::FileSystemReader::ReadRange(char* data, size_t size, size_t fromOffset)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
77 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string()))
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
78 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
79 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
80 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
81
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
82 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
83 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
84 fs::ifstream f;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
85 f.open(path_.string(), std::ifstream::in | std::ifstream::binary);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
86 if (!f.good())
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
87 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
88 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
89 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
90
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
91 f.seekg(fromOffset, std::ios::beg);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
92
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
93 f.read(reinterpret_cast<char*>(data), size);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
94
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
95 f.close();
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
97 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
100 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
101 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
102
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
103
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
104
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
105 IStorage::IWriter* FileSystemStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
106 {
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
107 return new FileSystemWriter(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_), fsync_);
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
109
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
110 IStorage::IReader* FileSystemStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
111 {
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
112 return new FileSystemReader(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_));
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
113 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
114
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
115 void FileSystemStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
116 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
117 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
118 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
119 namespace fs = boost::filesystem;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
120
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
121 fs::path path = BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_);
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
122
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
123 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
124 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
125 fs::remove(path.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
126 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
127 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
129 // Ignore the error
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
130 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
131
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
132 // Remove the two parent directories, ignoring the error code if
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 // these directories are not empty
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
134
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
135 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
136 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
137 boost::system::error_code err;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
138 fs::remove(path.parent_path().string(), err);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
139 fs::remove(path.parent_path().parent_path().string(), err);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
140 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
141 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
142 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
143 // Ignore the error
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
144 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
145 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
146 catch(Orthanc::OrthancException& e)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
147 {
152
d62f52be1943 use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 145
diff changeset
148 LOG(ERROR) << GetNameForLogs() << ": error while deleting object " << uuid << ": " << e.What();
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
149 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
150
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
151 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
152
111
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
153 bool FileSystemStoragePlugin::FileExists(const std::string& uuid, OrthancPluginContentType type, bool encryptionEnabled)
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
154 {
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
155 namespace fs = boost::filesystem;
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
156
111
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
157 fs::path path = BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_);
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
158
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
159 return Orthanc::SystemToolbox::IsExistingFile(path.string());
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
160 }
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
161