| 14 | namespace diffs |
| 15 | { |
| 16 | void ApplyDiff(ApplyDiffParams && p, base::Cancellable const & cancellable, OnDiffApplicationFinished const & task) |
| 17 | { |
| 18 | using namespace generator::mwm_diff; |
| 19 | |
| 20 | GetPlatform().RunTask(Platform::Thread::File, [p = std::move(p), &cancellable, task] |
| 21 | { |
| 22 | CHECK(p.m_diffFile, ()); |
| 23 | CHECK(p.m_oldMwmFile, ()); |
| 24 | |
| 25 | auto & diffReadyPath = p.m_diffReadyPath; |
| 26 | auto & diffFile = p.m_diffFile; |
| 27 | auto const diffPath = diffFile->GetPath(MapFileType::Diff); |
| 28 | auto result = DiffApplicationResult::Failed; |
| 29 | |
| 30 | diffFile->SyncWithDisk(); |
| 31 | if (!diffFile->OnDisk(MapFileType::Diff)) |
| 32 | { |
| 33 | base::RenameFileX(diffReadyPath, diffPath); |
| 34 | diffFile->SyncWithDisk(); |
| 35 | } |
| 36 | |
| 37 | auto const isFilePrepared = diffFile->OnDisk(MapFileType::Diff); |
| 38 | |
| 39 | if (isFilePrepared) |
| 40 | { |
| 41 | std::string const oldMwmPath = p.m_oldMwmFile->GetPath(MapFileType::Map); |
| 42 | std::string const newMwmPath = diffFile->GetPath(MapFileType::Map); |
| 43 | std::string const diffApplyingInProgressPath = newMwmPath + DIFF_APPLYING_FILE_EXTENSION; |
| 44 | |
| 45 | result = generator::mwm_diff::ApplyDiff(oldMwmPath, diffApplyingInProgressPath, diffPath, cancellable); |
| 46 | if (result == DiffApplicationResult::Ok && !base::RenameFileX(diffApplyingInProgressPath, newMwmPath)) |
| 47 | result = DiffApplicationResult::Failed; |
| 48 | |
| 49 | Platform::RemoveFileIfExists(diffApplyingInProgressPath); |
| 50 | |
| 51 | if (result != DiffApplicationResult::Ok) |
| 52 | Platform::RemoveFileIfExists(newMwmPath); |
| 53 | } |
| 54 | |
| 55 | switch (result) |
| 56 | { |
| 57 | case DiffApplicationResult::Ok: diffFile->DeleteFromDisk(MapFileType::Diff); break; |
| 58 | case DiffApplicationResult::Cancelled: |
| 59 | // The diff file will be deleted by storage. |
| 60 | // Another way would be to leave it on disk but all consequences |
| 61 | // of interacting with storage are much harder to be taken into account that way. |
| 62 | break; |
| 63 | case DiffApplicationResult::Failed: diffFile->DeleteFromDisk(MapFileType::Diff); break; |
| 64 | } |
| 65 | |
| 66 | GetPlatform().RunTask(Platform::Thread::Gui, [task, result]() { task(result); }); |
| 67 | }); |
| 68 | } |
| 69 | } // namespace diffs |
| 70 | } // namespace storage |
no test coverage detected