forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_linux.cpp
276 lines (236 loc) · 7.25 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
#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 "std/algorithm.hpp"
#include "std/bind.hpp"
#include "std/initializer_list.hpp"
#include "std/string.hpp"
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
namespace
{
// Web service ip to check internet connection. Now it's a mail.ru ip.
char constexpr kSomeWorkingWebServer[] = "217.69.139.202";
// Returns directory where binary resides, including slash at the end.
bool GetBinaryDir(string & outPath)
{
char path[4096] = {};
if (::readlink("/proc/self/exe", path, ARRAY_SIZE(path)) <= 0)
return false;
outPath = path;
outPath.erase(outPath.find_last_of('/') + 1);
return true;
}
// Returns true if EULA file exists in directory.
bool IsEulaExist(string const & directory)
{
return Platform::IsFileExistsByFullPath(base::JoinPath(directory, "eula.html"));
}
// Makes base::JoinPath(path, dirs) and all intermediate dirs.
// The directory |path| is assumed to exist already.
bool MkDirsChecked(string path, initializer_list<string> const & dirs)
{
string accumulatedDirs = path;
// Storing full paths is redundant but makes the implementation easier.
vector<string> madeDirs;
bool ok = true;
for (auto const & dir : dirs)
{
accumulatedDirs = base::JoinPath(accumulatedDirs, dir);
auto const result = Platform::MkDir(accumulatedDirs);
switch (result)
{
case Platform::ERR_OK: madeDirs.push_back(accumulatedDirs); break;
case Platform::ERR_FILE_ALREADY_EXISTS:
{
Platform::EFileType type;
if (Platform::GetFileType(accumulatedDirs, type) != Platform::ERR_OK ||
type != Platform::FILE_TYPE_DIRECTORY)
{
ok = false;
}
}
break;
default: ok = false; break;
}
}
if (ok)
return true;
for (; !madeDirs.empty(); madeDirs.pop_back())
Platform::RmDir(madeDirs.back());
return false;
}
string HomeDir()
{
char const * homePath = ::getenv("HOME");
if (homePath == nullptr)
MYTHROW(RootException, ("The environment variable HOME is not set"));
return homePath;
}
// Returns the default path to the writable dir, creating the dir if needed.
// An exception is thrown if the default dir is not already there and we were unable to create it.
string DefaultWritableDir()
{
initializer_list<string> const dirs = {".local", "share", "MapsWithMe"};
string result;
for (auto const & dir : dirs)
result = base::JoinPath(result, dir);
result = base::AddSlashIfNeeded(result);
auto const home = HomeDir();
if (!MkDirsChecked(home, dirs))
MYTHROW(FileSystemException, ("Cannot create directory:", result));
return result;
}
} // namespace
namespace platform
{
unique_ptr<Socket> CreateSocket()
{
return unique_ptr<Socket>();
}
}
Platform::Platform()
{
// Init directories.
string path;
CHECK(GetBinaryDir(path), ("Can't retrieve path to executable"));
m_settingsDir = base::JoinPath(HomeDir(), ".config", "MapsWithMe");
if (!IsFileExistsByFullPath(base::JoinPath(m_settingsDir, SETTINGS_FILE_NAME)))
{
auto const configDir = base::JoinPath(HomeDir(), ".config");
if (!MkDirChecked(configDir))
MYTHROW(FileSystemException, ("Can't create directory", configDir));
if (!MkDirChecked(m_settingsDir))
MYTHROW(FileSystemException, ("Can't create directory", m_settingsDir));
}
char const * resDir = ::getenv("MWM_RESOURCES_DIR");
char const * writableDir = ::getenv("MWM_WRITABLE_DIR");
if (resDir && writableDir)
{
m_resourcesDir = resDir;
m_writableDir = writableDir;
}
else if (resDir)
{
m_resourcesDir = resDir;
m_writableDir = DefaultWritableDir();
}
else
{
string const devBuildWithSymlink = base::JoinPath(path, "..", "..", "data");
string const devBuildWithoutSymlink = base::JoinPath(path, "..", "..", "..", "omim", "data");
string const installedVersionWithPackages = base::JoinPath(path, "..", "share");
string const installedVersionWithoutPackages = base::JoinPath(path, "..", "MapsWithMe");
string const customInstall = path;
if (IsEulaExist(devBuildWithSymlink))
{
m_resourcesDir = devBuildWithSymlink;
m_writableDir = writableDir != nullptr ? writableDir : m_resourcesDir;
}
else if (IsEulaExist(devBuildWithoutSymlink))
{
m_resourcesDir = devBuildWithoutSymlink;
m_writableDir = writableDir != nullptr ? writableDir : m_resourcesDir;
}
else if (IsEulaExist(installedVersionWithPackages))
{
m_resourcesDir = installedVersionWithPackages;
m_writableDir = writableDir != nullptr ? writableDir : DefaultWritableDir();
}
else if (IsEulaExist(installedVersionWithoutPackages))
{
m_resourcesDir = installedVersionWithoutPackages;
m_writableDir = writableDir != nullptr ? writableDir : DefaultWritableDir();
}
else if (IsEulaExist(customInstall))
{
m_resourcesDir = path;
m_writableDir = writableDir != nullptr ? writableDir : DefaultWritableDir();
}
}
m_resourcesDir += '/';
m_settingsDir += '/';
m_writableDir += '/';
char const * tmpDir = ::getenv("TMPDIR");
if (tmpDir)
m_tmpDir = tmpDir;
else
m_tmpDir = "/tmp";
m_tmpDir += '/';
m_privateDir = m_settingsDir;
m_guiThread = 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));
LOG(LDEBUG, ("Client ID:", UniqueClientId()));
}
string Platform::UniqueClientId() const
{
string machineFile = "/var/lib/dbus/machine-id";
if (IsFileExistsByFullPath("/etc/machine-id"))
machineFile = "/etc/machine-id";
if (IsFileExistsByFullPath(machineFile))
{
string content;
FileReader(machineFile).ReadAsString(content);
return content.substr(0, 32);
}
return "n0dbus0n0lsb00000000000000000000";
}
string Platform::AdvertisingId() const
{
return {};
}
string Platform::MacAddress(bool md5Decoded) const
{
// Not implemented.
UNUSED_VALUE(md5Decoded);
return {};
}
string Platform::DeviceName() const
{
return OMIM_OS_NAME;
}
string Platform::DeviceModel() const
{
return {};
}
Platform::EConnectionType Platform::ConnectionStatus()
{
int socketFd = socket(AF_INET, SOCK_STREAM, 0);
SCOPE_GUARD(closeSocket, 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, kSomeWorkingWebServer, &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::SetGuiThread(unique_ptr<base::TaskLoop> guiThread)
{
m_guiThread = move(guiThread);
}