forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_country_file_utils.cpp
432 lines (365 loc) · 12.6 KB
/
local_country_file_utils.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "platform/local_country_file_utils.hpp"
#include "platform/country_file.hpp"
#include "platform/mwm_version.hpp"
#include "platform/platform.hpp"
#include "platform/settings.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/reader.hpp"
#include "base/assert.hpp"
#include "base/file_name_utils.hpp"
#include "base/logging.hpp"
#include "base/stl_helpers.hpp"
#include "base/string_utils.hpp"
#include <algorithm>
#include <cctype>
#include <memory>
#include <regex>
#include <sstream>
#include <unordered_set>
#include "defines.hpp"
namespace platform
{
using namespace std;
namespace
{
char const kBitsExt[] = ".bftsegbits";
char const kNodesExt[] = ".bftsegnodes";
char const kOffsetsExt[] = ".offsets";
string GetAdditionalWorldScope()
{
return "r";
}
/*
bool IsSpecialName(string const & name) { return name == "." || name == ".."; }
*/
bool IsDownloaderFile(string const & name)
{
static regex const filter(".*\\.(downloading|resume|ready)[0-9]?$");
return regex_match(name.begin(), name.end(), filter);
}
bool IsDiffFile(string const & name)
{
return name.ends_with(DIFF_FILE_EXTENSION) || name.ends_with(DIFF_APPLYING_FILE_EXTENSION);
}
/*
bool DirectoryHasIndexesOnly(string const & directory)
{
Platform::TFilesWithType fwts;
Platform::GetFilesByType(directory, Platform::EFileType::Regular | Platform::EFileType::Directory, fwts);
for (auto const & fwt : fwts)
{
auto const & name = fwt.first;
auto const & type = fwt.second;
if (type == Platform::EFileType::Directory)
{
if (!IsSpecialName(name))
return false;
}
else if (!CountryIndexes::IsIndexFile(name))
return false;
}
return true;
}
*/
inline string GetDataDirFullPath(string const & dataDir)
{
Platform & platform = GetPlatform();
return dataDir.empty() ? platform.WritableDir() : base::JoinPath(platform.WritableDir(), dataDir);
}
void FindAllDiffsInDirectory(string const & dir, vector<LocalCountryFile> & diffs)
{
Platform & platform = GetPlatform();
Platform::TFilesWithType files;
platform.GetFilesByType(dir, Platform::EFileType::Regular, files);
for (auto const & fileWithType : files)
{
string name = fileWithType.first;
auto const isDiffReady = name.ends_with(DIFF_FILE_EXTENSION READY_FILE_EXTENSION);
auto const isDiff = name.ends_with(DIFF_FILE_EXTENSION);
if (!isDiff && !isDiffReady)
continue;
base::GetNameWithoutExt(name);
if (isDiffReady)
base::GetNameWithoutExt(name);
diffs.emplace_back(dir, CountryFile(std::move(name)), 0 /* version */);
}
}
} // namespace
string GetFilePath(int64_t version, string const & dataDir, string const & countryName, MapFileType type)
{
string const filename = GetFileName(countryName, type);
string const dir = GetDataDirFullPath(dataDir);
if (version == 0)
return base::JoinPath(dir, filename);
return base::JoinPath(dir, strings::to_string(version), filename);
}
void DeleteDownloaderFilesForCountry(int64_t version, CountryFile const & countryFile)
{
DeleteDownloaderFilesForCountry(version, string(), countryFile);
}
void DeleteDownloaderFilesForCountry(int64_t version, string const & dataDir,
CountryFile const & countryFile)
{
for (size_t type = 0; type < base::Underlying(MapFileType::Count); ++type)
{
string const path = GetFileDownloadPath(version, dataDir, countryFile,
static_cast<MapFileType>(type));
ASSERT(path.ends_with(READY_FILE_EXTENSION), ());
Platform::RemoveFileIfExists(path);
Platform::RemoveFileIfExists(path + RESUME_FILE_EXTENSION);
Platform::RemoveFileIfExists(path + DOWNLOADING_FILE_EXTENSION);
}
// Delete the diff that was downloaded but wasn't applied.
{
string const path = GetFilePath(version, dataDir, countryFile.GetName(), MapFileType::Diff);
Platform::RemoveFileIfExists(path);
}
}
size_t FindAllLocalMapsInDirectoryAndCleanup(string const & directory, int64_t version,
int64_t latestVersion,
vector<LocalCountryFile> & localFiles)
{
Platform & platform = GetPlatform();
size_t const szBefore = localFiles.size();
Platform::TFilesWithType fwts;
platform.GetFilesByType(directory, Platform::EFileType::Regular | Platform::EFileType::Directory, fwts);
for (auto const & fwt : fwts)
{
if (fwt.second != Platform::EFileType::Regular)
continue;
string name = fwt.first;
// Remove downloader and diff files for old version directories.
if (version < latestVersion && (IsDownloaderFile(name) || IsDiffFile(name)))
{
base::DeleteFileX(base::JoinPath(directory, name));
continue;
}
if (!name.ends_with(DATA_FILE_EXTENSION))
continue;
// Remove DATA_FILE_EXTENSION and use base name as a country file name.
base::GetNameWithoutExt(name);
localFiles.emplace_back(directory, CountryFile(std::move(name)), version);
}
return localFiles.size() - szBefore;
// Probably, indices will appear in future.
/*
for (auto const & fwt : fwts)
{
if (fwt.second != Platform::EFileType::Directory)
continue;
string const & name = fwt.first;
if (IsSpecialName(name))
continue;
if (names.count(name) == 0 && DirectoryHasIndexesOnly(base::JoinPath(directory, name)))
{
// Directory which looks like a directory with indexes for absent country. It's OK to remove it.
LocalCountryFile absentCountry(directory, CountryFile(name), version);
CountryIndexes::DeleteFromDisk(absentCountry);
}
}
*/
}
void FindAllDiffs(string const & dataDir, vector<LocalCountryFile> & diffs)
{
string const dir = GetDataDirFullPath(dataDir);
FindAllDiffsInDirectory(dir, diffs);
Platform::TFilesWithType fwts;
Platform::GetFilesByType(dir, Platform::EFileType::Directory, fwts);
for (auto const & fwt : fwts)
FindAllDiffsInDirectory(base::JoinPath(dir, fwt.first /* subdir */), diffs);
}
void FindAllLocalMapsAndCleanup(int64_t latestVersion, vector<LocalCountryFile> & localFiles)
{
FindAllLocalMapsAndCleanup(latestVersion, string(), localFiles);
}
void FindAllLocalMapsAndCleanup(int64_t latestVersion, string const & dataDir,
vector<LocalCountryFile> & localFiles)
{
string const dir = GetDataDirFullPath(dataDir);
// Do not search root folder! We have separate World processing in Storage::GetForceDownloadWorlds.
//FindAllLocalMapsInDirectoryAndCleanup(dir, 0 /* version */, latestVersion, localFiles);
Platform::TFilesWithType fwts;
Platform::GetFilesByType(dir, Platform::EFileType::Directory, fwts);
for (auto const & fwt : fwts)
{
string const & subdir = fwt.first;
int64_t version;
if (!ParseVersion(subdir, version) || version > latestVersion)
continue;
string const fullPath = base::JoinPath(dir, subdir);
if (0 == FindAllLocalMapsInDirectoryAndCleanup(fullPath, version, latestVersion, localFiles))
{
Platform::EError err = Platform::RmDir(fullPath);
if (err != Platform::ERR_OK)
LOG(LWARNING, ("Can't remove directory:", fullPath, err));
}
}
// Check for World and WorldCoasts in app bundle or in resources.
Platform & platform = GetPlatform();
string const world(WORLD_FILE_NAME);
string const worldCoasts(WORLD_COASTS_FILE_NAME);
for (string const & file : {world, worldCoasts})
{
auto i = localFiles.begin();
for (; i != localFiles.end(); ++i)
{
if (i->GetCountryName() == file)
break;
}
try
{
ModelReaderPtr reader(platform.GetReader(file + DATA_FILE_EXTENSION, GetAdditionalWorldScope()));
// Empty path means the resource file.
LocalCountryFile worldFile(string(), CountryFile(file), version::ReadVersionDate(reader));
worldFile.m_files[base::Underlying(MapFileType::Map)] = reader.Size();
// Replace if newer only.
if (i != localFiles.end())
{
if (worldFile.GetVersion() > i->GetVersion())
*i = std::move(worldFile);
}
else
localFiles.push_back(std::move(worldFile));
}
catch (RootException const & ex)
{
if (i == localFiles.end())
{
// This warning is possible on android devices without bundled Worlds.
LOG(LWARNING, ("Can't find any:", file, "Reason:", ex.Msg()));
}
}
}
}
void CleanupMapsDirectory(int64_t latestVersion)
{
vector<LocalCountryFile> localFiles;
FindAllLocalMapsAndCleanup(latestVersion, localFiles);
}
bool ParseVersion(string const & s, int64_t & version)
{
// Folder version format is 211122. Unit tests use simple "1", "2" versions.
if (s.empty() || s.size() > 6)
return false;
int64_t v = 0;
for (char const c : s)
{
if (!isdigit(c))
return false;
v = v * 10 + c - '0';
}
version = v;
return true;
}
shared_ptr<LocalCountryFile> PreparePlaceForCountryFiles(int64_t version,
CountryFile const & countryFile)
{
return PreparePlaceForCountryFiles(version, string(), countryFile);
}
shared_ptr<LocalCountryFile> PreparePlaceForCountryFiles(int64_t version, string const & dataDir,
CountryFile const & countryFile)
{
string const dir = PrepareDirToDownloadCountry(version, dataDir);
return (!dir.empty() ? make_shared<LocalCountryFile>(dir, countryFile, version) : nullptr);
}
std::string PrepareDirToDownloadCountry(int64_t version, std::string const & dataDir)
{
string dir = GetDataDirFullPath(dataDir);
if (version == 0)
return dir;
dir = base::JoinPath(dir, strings::to_string(version));
return (Platform::MkDirChecked(dir) ? dir : std::string());
}
string GetFileDownloadPath(int64_t version, string const & dataDir, string const & countryName, MapFileType type)
{
return GetFilePath(version, dataDir, countryName, type) + READY_FILE_EXTENSION;
}
unique_ptr<ModelReader> GetCountryReader(LocalCountryFile const & file, MapFileType type)
{
Platform & platform = GetPlatform();
if (file.IsInBundle())
return platform.GetReader(file.GetFileName(type), GetAdditionalWorldScope());
else
return platform.GetReader(file.GetPath(type), "f");
}
// static
void CountryIndexes::PreparePlaceOnDisk(LocalCountryFile const & localFile)
{
string const dir = IndexesDir(localFile);
if (!Platform::MkDirChecked(dir))
MYTHROW(FileSystemException, ("Can't create directory", dir));
}
// static
bool CountryIndexes::DeleteFromDisk(LocalCountryFile const & localFile)
{
string const directory = IndexesDir(localFile);
bool ok = true;
for (auto index : {Index::Bits, Index::Nodes, Index::Offsets})
{
string const path = GetPath(localFile, index);
if (Platform::IsFileExistsByFullPath(path) && !base::DeleteFileX(path))
{
LOG(LWARNING, ("Can't remove country index:", path));
ok = false;
}
}
Platform::EError const ret = Platform::RmDir(directory);
if (ret != Platform::ERR_OK && ret != Platform::ERR_FILE_DOES_NOT_EXIST)
{
LOG(LWARNING, ("Can't remove indexes directory:", directory, ret));
ok = false;
}
return ok;
}
// static
string CountryIndexes::GetPath(LocalCountryFile const & localFile, Index index)
{
char const * ext = nullptr;
switch (index)
{
case Index::Bits: ext = kBitsExt; break;
case Index::Nodes: ext = kNodesExt; break;
case Index::Offsets: ext = kOffsetsExt; break;
}
return base::JoinPath(IndexesDir(localFile), localFile.GetCountryName() + ext);
}
// static
void CountryIndexes::GetIndexesExts(vector<string> & exts)
{
exts.push_back(kBitsExt);
exts.push_back(kNodesExt);
exts.push_back(kOffsetsExt);
}
// static
bool CountryIndexes::IsIndexFile(string const & file)
{
return file.ends_with(kBitsExt) || file.ends_with(kNodesExt) ||
file.ends_with(kOffsetsExt);
}
// static
string CountryIndexes::IndexesDir(LocalCountryFile const & localFile)
{
string dir = localFile.GetDirectory();
CountryFile const & file = localFile.GetCountryFile();
if (localFile.IsInBundle())
{
// Local file is stored in bundle. Need to prepare index folder in the writable directory.
int64_t const version = localFile.GetVersion();
ASSERT_GREATER(version, 0, ());
dir = base::JoinPath(GetPlatform().WritableDir(), strings::to_string(version));
if (!Platform::MkDirChecked(dir))
MYTHROW(FileSystemException, ("Can't create directory", dir));
}
return base::JoinPath(dir, file.GetName());
}
string DebugPrint(CountryIndexes::Index index)
{
switch (index)
{
case CountryIndexes::Index::Bits: return "Bits";
case CountryIndexes::Index::Nodes: return "Nodes";
case CountryIndexes::Index::Offsets: return "Offsets";
}
UNREACHABLE();
}
} // namespace platform