annotate Common/FileSystemStorage.cpp @ 111:407bd022b0cf

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