annotate Resources/Orthanc/Core/SystemToolbox.cpp @ 59:d2adc6189a95 wasm

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 May 2017 11:34:06 +0200
parents 7207a407bcd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
40
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 39
diff changeset
5 * Copyright (C) 2017 Osimis, Belgium
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "PrecompiledHeaders.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "SystemToolbox.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #if BOOST_HAS_DATE_TIME == 1
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
39 # include <boost/date_time/posix_time/posix_time.hpp>
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #if defined(_WIN32)
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
44 # include <windows.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
45 # include <process.h> // For "_spawnvp()" and "_getpid()"
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 #else
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
47 # include <unistd.h> // For "execvp()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
48 # include <sys/wait.h> // For "waitpid()"
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
51
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 #if defined(__APPLE__) && defined(__MACH__)
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
53 # include <mach-o/dyld.h> /* _NSGetExecutablePath */
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
54 # include <limits.h> /* PATH_MAX */
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
57
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
23
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
59 # include <limits.h> /* PATH_MAX */
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
60 # include <signal.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
61 # include <unistd.h>
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
22
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
65 // Inclusions for UUID
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
66 // http://stackoverflow.com/a/1626302
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
67
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
68 extern "C"
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
69 {
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
70 #ifdef WIN32
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
71 # include <rpc.h>
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
72 #else
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
73 # include <uuid/uuid.h>
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
74 #endif
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
75 }
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
76
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
77
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 #include "Logging.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 #include "OrthancException.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 #include "Toolbox.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 #include <boost/filesystem.hpp>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 #include <boost/filesystem/fstream.hpp>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 namespace Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 static bool finish_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 static ServerBarrierEvent barrierEvent_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 finish_ = true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 static void SignalHandler(int signal)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 if (signal == SIGHUP)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 barrierEvent_ = ServerBarrierEvent_Reload;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 finish_ = true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 static ServerBarrierEvent ServerBarrierInternal(const bool* stopFlag)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 SetConsoleCtrlHandler(ConsoleControlHandler, true);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 signal(SIGINT, SignalHandler);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 signal(SIGQUIT, SignalHandler);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 signal(SIGTERM, SignalHandler);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 signal(SIGHUP, SignalHandler);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 // Active loop that awakens every 100ms
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 finish_ = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 barrierEvent_ = ServerBarrierEvent_Stop;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 while (!(*stopFlag || finish_))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
39
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
127 SystemToolbox::USleep(100 * 1000);
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 SetConsoleCtrlHandler(ConsoleControlHandler, false);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 signal(SIGINT, NULL);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 signal(SIGQUIT, NULL);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 signal(SIGTERM, NULL);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 signal(SIGHUP, NULL);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 return barrierEvent_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 ServerBarrierEvent SystemToolbox::ServerBarrier(const bool& stopFlag)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 return ServerBarrierInternal(&stopFlag);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 ServerBarrierEvent SystemToolbox::ServerBarrier()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 const bool stopFlag = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 return ServerBarrierInternal(&stopFlag);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
39
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
156 void SystemToolbox::USleep(uint64_t microSeconds)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
157 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
158 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
159 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
160 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__native_client__)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
161 usleep(microSeconds);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
162 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
163 #error Support your platform here
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
164 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
165 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
166
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
167
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 static std::streamsize GetStreamSize(std::istream& f)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 // http://www.cplusplus.com/reference/iostream/istream/tellg/
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 f.seekg(0, std::ios::end);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 std::streamsize size = f.tellg();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 f.seekg(0, std::ios::beg);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 return size;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 void SystemToolbox::ReadFile(std::string& content,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 if (!IsRegularFile(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 throw OrthancException(ErrorCode_RegularFileExpected);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 boost::filesystem::ifstream f;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 f.open(path, std::ifstream::in | std::ifstream::binary);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 if (!f.good())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 throw OrthancException(ErrorCode_InexistentFile);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 std::streamsize size = GetStreamSize(f);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 content.resize(size);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 if (size != 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 f.read(reinterpret_cast<char*>(&content[0]), size);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 f.close();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 bool SystemToolbox::ReadHeader(std::string& header,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 const std::string& path,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 size_t headerSize)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 if (!IsRegularFile(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 throw OrthancException(ErrorCode_RegularFileExpected);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 boost::filesystem::ifstream f;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 f.open(path, std::ifstream::in | std::ifstream::binary);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 if (!f.good())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 throw OrthancException(ErrorCode_InexistentFile);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 bool full = true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 std::streamsize size = GetStreamSize(f);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 if (size <= 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 headerSize = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 full = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 else if (static_cast<size_t>(size) < headerSize)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 headerSize = size; // Truncate to the size of the file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 full = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 header.resize(headerSize);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 if (headerSize != 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 f.read(reinterpret_cast<char*>(&header[0]), headerSize);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 f.close();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 return full;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 void SystemToolbox::WriteFile(const void* content,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 size_t size,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 boost::filesystem::ofstream f;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 f.open(path, std::ofstream::out | std::ofstream::binary);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 if (!f.good())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 throw OrthancException(ErrorCode_CannotWriteFile);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 if (size != 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 f.write(reinterpret_cast<const char*>(content), size);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 if (!f.good())
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 f.close();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 throw OrthancException(ErrorCode_FileStorageCannotWrite);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 f.close();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 void SystemToolbox::WriteFile(const std::string& content,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 WriteFile(content.size() > 0 ? content.c_str() : NULL,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 content.size(), path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 void SystemToolbox::RemoveFile(const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 if (boost::filesystem::exists(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 if (IsRegularFile(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 boost::filesystem::remove(path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 throw OrthancException(ErrorCode_RegularFileExpected);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 uint64_t SystemToolbox::GetFileSize(const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 return static_cast<uint64_t>(boost::filesystem::file_size(path));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 catch (boost::filesystem::filesystem_error&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 throw OrthancException(ErrorCode_InexistentFile);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 void SystemToolbox::MakeDirectory(const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 if (boost::filesystem::exists(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 if (!boost::filesystem::is_directory(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 throw OrthancException(ErrorCode_DirectoryOverFile);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 if (!boost::filesystem::create_directories(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 throw OrthancException(ErrorCode_MakeDirectory);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 bool SystemToolbox::IsExistingFile(const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 return boost::filesystem::exists(path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 static std::string GetPathToExecutableInternal()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 // Yes, this is ugly, but there is no simple way to get the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 // required buffer size, so we use a big constant
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 std::vector<char> buffer(32768);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 return std::string(&buffer[0]);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 static std::string GetPathToExecutableInternal()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 std::vector<char> buffer(PATH_MAX + 1);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 if (bytes == 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 throw OrthancException(ErrorCode_PathToExecutable);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 return std::string(&buffer[0]);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 #elif defined(__APPLE__) && defined(__MACH__)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 static std::string GetPathToExecutableInternal()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 char pathbuf[PATH_MAX + 1];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 unsigned int bufsize = static_cast<int>(sizeof(pathbuf));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 _NSGetExecutablePath( pathbuf, &bufsize);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 return std::string(pathbuf);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 #error Support your platform here
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 std::string SystemToolbox::GetPathToExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 boost::filesystem::path p(GetPathToExecutableInternal());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 return boost::filesystem::absolute(p).string();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 std::string SystemToolbox::GetDirectoryOfExecutable()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 boost::filesystem::path p(GetPathToExecutableInternal());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 return boost::filesystem::absolute(p.parent_path()).string();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 void SystemToolbox::ExecuteSystemCommand(const std::string& command,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 const std::vector<std::string>& arguments)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 // Convert the arguments as a C array
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 std::vector<char*> args(arguments.size() + 2);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 args.front() = const_cast<char*>(command.c_str());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 for (size_t i = 0; i < arguments.size(); i++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 args[i + 1] = const_cast<char*>(arguments[i].c_str());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 args.back() = NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 int status;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 // http://msdn.microsoft.com/en-us/library/275khfab.aspx
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 status = static_cast<int>(_spawnvp(_P_OVERLAY, command.c_str(), &args[0]));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 int pid = fork();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 if (pid == -1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 // Error in fork()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 #if ORTHANC_ENABLE_LOGGING == 1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 LOG(ERROR) << "Cannot fork a child process";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 throw OrthancException(ErrorCode_SystemCommand);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 else if (pid == 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 // Execute the system command in the child process
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 execvp(command.c_str(), &args[0]);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 // We should never get here
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 _exit(1);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 // Wait for the system command to exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 waitpid(pid, &status, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 if (status != 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 #if ORTHANC_ENABLE_LOGGING == 1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 LOG(ERROR) << "System command failed with status code " << status;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 throw OrthancException(ErrorCode_SystemCommand);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 int SystemToolbox::GetProcessId()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 return static_cast<int>(_getpid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 #else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 return static_cast<int>(getpid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 bool SystemToolbox::IsRegularFile(const std::string& path)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 namespace fs = boost::filesystem;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465 try
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 if (fs::exists(path))
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 fs::file_status status = fs::status(path);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 return (status.type() == boost::filesystem::regular_file ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 status.type() == boost::filesystem::reparse_file); // Fix BitBucket issue #11
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 catch (fs::filesystem_error&)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 FILE* SystemToolbox::OpenFile(const std::string& path,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 FileMode mode)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 #if defined(_WIN32)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 // TODO Deal with special characters by converting to the current locale
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 const char* m;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 switch (mode)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 case FileMode_ReadBinary:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 m = "rb";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 case FileMode_WriteBinary:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 m = "wb";
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 break;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 default:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 throw OrthancException(ErrorCode_ParameterOutOfRange);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 return fopen(path.c_str(), m);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507
22
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
508 std::string SystemToolbox::GenerateUuid()
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
509 {
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
510 #ifdef WIN32
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
511 UUID uuid;
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
512 UuidCreate ( &uuid );
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
513
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
514 unsigned char * str;
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
515 UuidToStringA ( &uuid, &str );
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
516
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
517 std::string s( ( char* ) str );
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
518
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
519 RpcStringFreeA ( &str );
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
520 #else
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
521 uuid_t uuid;
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
522 uuid_generate_random ( uuid );
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
523 char s[37];
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
524 uuid_unparse ( uuid, s );
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
525 #endif
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
526 return s;
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
527 }
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
528
b01d46e5a2b3 support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
529
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 #if BOOST_HAS_DATE_TIME == 1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 std::string SystemToolbox::GetNowIsoString()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 return boost::posix_time::to_iso_string(now);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 void SystemToolbox::GetNowDicom(std::string& date,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 std::string& time)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541 tm tm = boost::posix_time::to_tm(now);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 char s[32];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 sprintf(s, "%04d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545 date.assign(s);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547 // TODO milliseconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 sprintf(s, "%02d%02d%02d.%06d", tm.tm_hour, tm.tm_min, tm.tm_sec, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549 time.assign(s);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552 }