annotate Common/FileSystemStorage.cpp @ 151:00cd1f01dd5d

fix initialization and finalization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 21 Jun 2024 10:29:52 +0200
parents 3c7e0374f28e
children d62f52be1943
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
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
24 #include <SystemToolbox.h>
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
25 #include <boost/filesystem.hpp>
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 #include <boost/filesystem/fstream.hpp>
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
27
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 namespace fs = boost::filesystem;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
29
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
30 void FileSystemStoragePlugin::FileSystemWriter::Write(const char* data, size_t size)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
31 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
32 Orthanc::SystemToolbox::MakeDirectory(path_.parent_path().string());
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::WriteFile(reinterpret_cast<const void*>(data), size, path_.string(), fsync_);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
36
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
37 size_t FileSystemStoragePlugin::FileSystemReader::GetSize()
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
38 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
39 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string()))
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
40 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
41 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
42 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
43
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
44 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
45 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
46 fs::ifstream f;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
47 f.open(path_.string(), std::ifstream::in | std::ifstream::binary);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
48 if (!f.good())
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
49 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
50 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
51 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
52
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
53 f.seekg(0, std::ios::end);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
54 std::streamsize fileSize = f.tellg();
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
55 f.seekg(0, std::ios::beg);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
56
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
57 f.close();
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
58
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
59 return fileSize;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
60 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
61 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
62 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
63 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
64 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
65
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 void FileSystemStoragePlugin::FileSystemReader::ReadWhole(char* data, size_t size)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
69 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
70 ReadRange(data, size, 0);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
71 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
72
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 void FileSystemStoragePlugin::FileSystemReader::ReadRange(char* data, size_t size, size_t fromOffset)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
74 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
75 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string()))
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
77 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
78 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
79
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
80 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
81 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
82 fs::ifstream f;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
83 f.open(path_.string(), std::ifstream::in | std::ifstream::binary);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
84 if (!f.good())
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
85 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
86 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
87 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
88
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
89 f.seekg(fromOffset, std::ios::beg);
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.read(reinterpret_cast<char*>(data), size);
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.close();
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
94 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
95 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
97 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 }
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
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
103 IStorage::IWriter* FileSystemStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
77
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 return new FileSystemWriter(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_), fsync_);
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
106 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
107
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
108 IStorage::IReader* FileSystemStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
77
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 return new FileSystemReader(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_));
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
111 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
112
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
113 void FileSystemStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
114 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
115 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
116 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
117 namespace fs = boost::filesystem;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
118
78
d7295e8678d7 renames
Alain Mazy <am@osimis.io>
parents: 77
diff changeset
119 fs::path path = BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_);
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
120
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
121 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
122 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
123 fs::remove(path.string());
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
124 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
125 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
126 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
127 // Ignore the error
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
129
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
130 // Remove the two parent directories, ignoring the error code if
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
131 // these directories are not empty
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
132
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 try
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
134 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
135 boost::system::error_code err;
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
136 fs::remove(path.parent_path().string(), err);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
137 fs::remove(path.parent_path().parent_path().string(), err);
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
138 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
139 catch (...)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
140 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
141 // Ignore the error
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
142 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
143 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
144 catch(Orthanc::OrthancException& e)
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
145 {
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
146 OrthancPlugins::LogError(GetNameForLogs() + ": error while deleting object " + std::string(uuid) + ": " + std::string(e.What()));
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
147 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
148
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
149 }
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
150
111
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
151 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
152 {
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
153 namespace fs = boost::filesystem;
77
80792bb9600e new HybridMode
Alain Mazy <am@osimis.io>
parents:
diff changeset
154
111
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
155 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
156
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
157 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
158 }
407bd022b0cf in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents: 78
diff changeset
159