forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_linux.cpp
286 lines (251 loc) · 7.97 KB
/
platform_linux.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "private.h"
#include "platform/platform.hpp"
#include "platform/socket.hpp"
#include "coding/file_reader.hpp"
#include "base/exception.hpp"
#include "base/file_name_utils.hpp"
#include "base/logging.hpp"
#include "base/macros.hpp"
#include "base/scope_guard.hpp"
#include <algorithm>
#include <functional> // bind
#include <initializer_list>
#include <optional>
#include <string>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h> // strrchr
#include <unistd.h> // access, readlink
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <linux/limits.h> // PATH_MAX
#include <netinet/in.h>
#include <QStandardPaths> // writableLocation GenericConfigLocation
namespace
{
// Returns directory where binary resides, including slash at the end.
std::optional<std::string> GetExecutableDir()
{
char path[PATH_MAX] = {};
if (::readlink("/proc/self/exe", path, ARRAY_SIZE(path)) <= 0)
return {};
*(strrchr(path, '/') + 1) = '\0';
return path;
}
// Returns true if EULA file exists in a directory.
bool IsWelcomeExist(std::string const & dir)
{
return Platform::IsFileExistsByFullPath(base::JoinPath(dir, "welcome.html"));
}
// Returns string value of an environment variable.
std::optional<std::string> GetEnv(char const * var)
{
char const * value = ::getenv(var);
if (value == nullptr)
return {};
return value;
}
bool IsDirWritable(std::string const & dir)
{
return ::access(dir.c_str(), W_OK) == 0;
}
} // namespace
namespace platform
{
std::unique_ptr<Socket> CreateSocket()
{
return std::unique_ptr<Socket>();
}
} // namespace platform
Platform::Platform()
{
using base::JoinPath;
// Current executable's path with a trailing slash.
auto const execDir = GetExecutableDir();
CHECK(execDir, ("Can't retrieve the path to executable"));
// Home directory without a trailing slash.
auto const homeDir = GetEnv("HOME");
CHECK(homeDir, ("Can't retrieve home directory"));
// XDG config directory, usually ~/.config/OMaps/
m_settingsDir = JoinPath(
QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation).toStdString(),
"OMaps");
if (!IsFileExistsByFullPath(JoinPath(m_settingsDir, SETTINGS_FILE_NAME)) && !MkDirRecursively(m_settingsDir))
MYTHROW(FileSystemException, ("Can't create directory", m_settingsDir));
m_settingsDir += '/';
// Override dirs from the env.
if (auto const dir = GetEnv("MWM_WRITABLE_DIR"))
m_writableDir = *dir;
if (auto const dir = GetEnv("MWM_RESOURCES_DIR"))
m_resourcesDir = *dir;
else
{ // Guess the existing resources directory.
std::string const dirsToScan[] = {
"./data", // symlink in the current folder
"../data", // 'build' folder inside the repo
JoinPath(*execDir, "..", "organicmaps", "data"), // build-omim-{debug,release}
JoinPath(*execDir, "..", "share"), // installed version with packages
JoinPath(*execDir, "..", "OMaps"), // installed version without packages
JoinPath(*execDir, "..", "share", "organicmaps", "data"), // flatpak-build
};
for (auto const & dir : dirsToScan)
{
if (IsWelcomeExist(dir))
{
m_resourcesDir = dir;
if (m_writableDir.empty() && IsDirWritable(dir))
m_writableDir = m_resourcesDir;
break;
}
}
}
// Use ~/.local/share/OMaps if resources directory was not writable.
if (!m_resourcesDir.empty() && m_writableDir.empty())
{
// The writableLocation does the same for AppDataLocation, AppLocalDataLocation,
// and GenericDataLocation. Provided, that test mode is not enabled, then
// first it checks ${XDG_DATA_HOME}, if empty then it falls back to ${HOME}/.local/share
m_writableDir = JoinPath(QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).toStdString(), "OMaps");
if (!MkDirRecursively(m_writableDir))
MYTHROW(FileSystemException, ("Can't create writable directory:", m_writableDir));
}
// Here one or both m_resourcesDir and m_writableDir still may be empty.
// Tests or binary may initialize them later.
using base::AddSlashIfNeeded;
if (!m_writableDir.empty())
m_writableDir = AddSlashIfNeeded(m_writableDir);
if (!m_resourcesDir.empty())
m_resourcesDir = AddSlashIfNeeded(m_resourcesDir);
// Select directory for temporary files.
for (auto const & dir : { GetEnv("TMPDIR"), GetEnv("TMP"), GetEnv("TEMP"), {"/tmp"}})
{
if (dir && IsFileExistsByFullPath(*dir) && IsDirWritable(*dir))
{
m_tmpDir = AddSlashIfNeeded(*dir);
break;
}
}
m_guiThread = std::make_unique<platform::GuiThread>();
LOG(LDEBUG, ("Resources directory:", m_resourcesDir));
LOG(LDEBUG, ("Writable directory:", m_writableDir));
LOG(LDEBUG, ("Tmp directory:", m_tmpDir));
LOG(LDEBUG, ("Settings directory:", m_settingsDir));
}
std::string Platform::DeviceName() const
{
return OMIM_OS_NAME;
}
std::string Platform::DeviceModel() const
{
return {};
}
Platform::EConnectionType Platform::ConnectionStatus()
{
int socketFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SCOPE_GUARD(closeSocket, std::bind(&close, socketFd));
if (socketFd < 0)
return EConnectionType::CONNECTION_NONE;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
inet_pton(AF_INET, DEFAULT_CONNECTION_CHECK_IP, &addr.sin_addr);
if (connect(socketFd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) < 0)
return EConnectionType::CONNECTION_NONE;
return EConnectionType::CONNECTION_WIFI;
}
Platform::ChargingStatus Platform::GetChargingStatus()
{
return Platform::ChargingStatus::Plugged;
}
uint8_t Platform::GetBatteryLevel()
{
// This value is always 100 for desktop.
return 100;
}
void Platform::GetSystemFontNames(FilesList & res) const
{
char constexpr const * const fontsWhitelist[] = {
"Roboto-Medium.ttf",
"Roboto-Regular.ttf",
"DroidSansFallback.ttf",
"DroidSansFallbackFull.ttf",
"DroidSans.ttf",
"DroidSansArabic.ttf",
"DroidSansSemc.ttf",
"DroidSansSemcCJK.ttf",
"DroidNaskh-Regular.ttf",
"Lohit-Bengali.ttf",
"Lohit-Devanagari.ttf",
"Lohit-Tamil.ttf",
"PakType Naqsh.ttf",
"wqy-microhei.ttc",
"Jomolhari.ttf",
"Padauk.ttf",
"KhmerOS.ttf",
"Umpush.ttf",
"DroidSansThai.ttf",
"DroidSansArmenian.ttf",
"DroidSansEthiopic-Regular.ttf",
"DroidSansGeorgian.ttf",
"DroidSansHebrew-Regular.ttf",
"DroidSansHebrew.ttf",
"DroidSansJapanese.ttf",
"LTe50872.ttf",
"LTe50259.ttf",
"DevanagariOTS.ttf",
"FreeSans.ttf",
"DejaVuSans.ttf",
"arial.ttf",
"AbyssinicaSIL-R.ttf",
};
std::string const systemFontsPath[] = {
"/usr/share/fonts/truetype/roboto/",
"/usr/share/fonts/truetype/droid/",
"/usr/share/fonts/truetype/dejavu/",
"/usr/share/fonts/truetype/ttf-dejavu/",
"/usr/share/fonts/truetype/wqy/",
"/usr/share/fonts/truetype/freefont/",
"/usr/share/fonts/truetype/padauk/",
"/usr/share/fonts/truetype/dzongkha/",
"/usr/share/fonts/truetype/ttf-khmeros-core/",
"/usr/share/fonts/truetype/tlwg/",
"/usr/share/fonts/truetype/abyssinica/",
"/usr/share/fonts/truetype/paktype/",
};
for (auto font : fontsWhitelist)
{
for (auto sysPath : systemFontsPath)
{
std::string path = sysPath + font;
if (IsFileExistsByFullPath(path))
res.push_back(std::move(path));
}
}
}
// static
time_t Platform::GetFileCreationTime(std::string const & path)
{
// In older Linux versions there is no reliable way to get file creation time.
#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 28
struct statx st;
if (0 == statx(AT_FDCWD, path.c_str(), 0, STATX_BTIME, &st))
return st.stx_btime.tv_sec;
#else
struct stat st;
if (0 == stat(path.c_str(), &st))
return std::min(st.st_atim.tv_sec, st.st_mtim.tv_sec);
#endif
return 0;
}
// static
time_t Platform::GetFileModificationTime(std::string const & path)
{
struct stat st;
if (0 == stat(path.c_str(), &st))
return st.st_mtim.tv_sec;
return 0;
}