Mercurial > hg > orthanc
annotate OrthancFramework/Sources/TemporaryFile.cpp @ 5345:34781fb0172a
fix unit test Versions.BoostStatic
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 28 Jun 2023 12:40:46 +0200 |
parents | 0ea402b4d901 |
children | 48b8dae6dc77 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * 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
|
9 * 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
|
10 * 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
|
11 * the License, or (at your option) any later version. |
136 | 12 * |
0 | 13 * This program is distributed in the hope that it will be useful, but |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * 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
|
16 * Lesser General Public License for more details. |
0 | 17 * |
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
|
18 * 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
|
19 * 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
|
20 * <http://www.gnu.org/licenses/>. |
0 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
24 #include "PrecompiledHeaders.h" |
2142 | 25 #include "TemporaryFile.h" |
0 | 26 |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
27 #include "OrthancException.h" |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
28 #include "SystemToolbox.h" |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
29 #include "Toolbox.h" |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
30 |
82 | 31 #include <boost/filesystem.hpp> |
4304 | 32 #include <boost/lexical_cast.hpp> |
82 | 33 |
59 | 34 namespace Orthanc |
0 | 35 { |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
36 static std::string CreateTemporaryPath(const char* temporaryDirectory, |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
37 const char* extension) |
2140 | 38 { |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
39 boost::filesystem::path dir; |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
40 |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
41 if (temporaryDirectory == NULL) |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
42 { |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
43 #if BOOST_HAS_FILESYSTEM_V3 == 1 |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
44 dir = boost::filesystem::temp_directory_path(); |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
45 #elif defined(__linux__) |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
46 dir = "/tmp"; |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
47 #else |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
48 # error Support your platform here |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
49 #endif |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
50 } |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
51 else |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
52 { |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
53 dir = temporaryDirectory; |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
54 } |
110
fd7b0a3e6260
support of boost 1.42 for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
82
diff
changeset
|
55 |
2140 | 56 // We use UUID to create unique path to temporary files |
4297 | 57 const std::string uuid = Toolbox::GenerateUuid(); |
3508
70524b4acc72
Name of temporary files now include the process ID to ease design of scripts cleaning /tmp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3184
diff
changeset
|
58 |
70524b4acc72
Name of temporary files now include the process ID to ease design of scripts cleaning /tmp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3184
diff
changeset
|
59 // New in Orthanc 1.5.8: Prefix the process ID to the name of the |
70524b4acc72
Name of temporary files now include the process ID to ease design of scripts cleaning /tmp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3184
diff
changeset
|
60 // temporary files, in order to locate orphan temporary files that |
70524b4acc72
Name of temporary files now include the process ID to ease design of scripts cleaning /tmp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3184
diff
changeset
|
61 // were left by instances of Orthanc that exited in non-clean way |
70524b4acc72
Name of temporary files now include the process ID to ease design of scripts cleaning /tmp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3184
diff
changeset
|
62 // https://groups.google.com/d/msg/orthanc-users/MSJX53bw6Lw/d3S3lRRLAwAJ |
70524b4acc72
Name of temporary files now include the process ID to ease design of scripts cleaning /tmp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3184
diff
changeset
|
63 std::string filename = "Orthanc-" + boost::lexical_cast<std::string>(SystemToolbox::GetProcessId()) + "-" + uuid; |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
64 |
2140 | 65 if (extension != NULL) |
66 { | |
67 filename.append(extension); | |
68 } | |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
69 |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
70 dir /= filename; |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
71 return dir.string(); |
2140 | 72 } |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
73 |
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
74 |
2140 | 75 TemporaryFile::TemporaryFile() : |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
76 path_(CreateTemporaryPath(NULL, NULL)) |
2140 | 77 { |
78 } | |
366
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
79 |
68fd4de63eae
create temporary files file a given extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
80 |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
81 TemporaryFile::TemporaryFile(const std::string& temporaryDirectory, |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
82 const std::string& extension) : |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
83 path_(CreateTemporaryPath(temporaryDirectory.c_str(), extension.c_str())) |
2140 | 84 { |
85 } | |
82 | 86 |
87 | |
2140 | 88 TemporaryFile::~TemporaryFile() |
89 { | |
90 boost::filesystem::remove(path_); | |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
91 } |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
92 |
4300 | 93 const std::string &TemporaryFile::GetPath() const |
94 { | |
95 return path_; | |
96 } | |
97 | |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
98 |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
99 void TemporaryFile::Write(const std::string& content) |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
100 { |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
101 try |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
102 { |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
103 SystemToolbox::WriteFile(content, path_); |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
104 } |
3184 | 105 catch (OrthancException& e) |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
106 { |
3184 | 107 throw OrthancException(e.GetErrorCode(), |
108 "Can't create temporary file \"" + path_ + | |
109 "\" with " + boost::lexical_cast<std::string>(content.size()) + | |
110 " bytes: Check you have write access to the " | |
111 "temporary directory and that it is not full"); | |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
112 } |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
113 } |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
114 |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
115 |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
116 void TemporaryFile::Read(std::string& content) const |
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
117 { |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
118 try |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
119 { |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
120 SystemToolbox::ReadFile(content, path_); |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
121 } |
3184 | 122 catch (OrthancException& e) |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
123 { |
3184 | 124 throw OrthancException(e.GetErrorCode(), |
125 "Can't read temporary file \"" + path_ + | |
126 "\": Another process has corrupted the temporary directory"); | |
3181
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
127 } |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
128 } |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
129 |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
130 |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
131 void TemporaryFile::Touch() |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
132 { |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
133 std::string empty; |
6fd38327e777
Fix issue #130 (Orthanc failed to start when /tmp partition was full)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
134 Write(empty); |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
135 } |
4230 | 136 |
137 | |
138 uint64_t TemporaryFile::GetFileSize() const | |
139 { | |
140 return SystemToolbox::GetFileSize(path_); | |
141 } | |
4342
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
142 |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
143 |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
144 void TemporaryFile::ReadRange(std::string& content, |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
145 uint64_t start, |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
146 uint64_t end, |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
147 bool throwIfOverflow) const |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
148 { |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
149 SystemToolbox::ReadFileRange(content, path_, start, end, throwIfOverflow); |
52166629239f
SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
150 } |
0 | 151 } |