comparison Common/FileSystemStorage.cpp @ 78:d7295e8678d7

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