Audacity 3.2.0
CloudSyncHousekeeper.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 CloudSyncHousekeeper.cpp
7
8 Dmitry Vedenko
9
10**********************************************************************/
11
12#include <algorithm>
13#include <atomic>
14#include <chrono>
15#include <future>
16
17#include <wx/file.h>
18
19#include "AppEvents.h"
20#include "CodeConversions.h"
21
24
26{
27namespace
28{
29class Housekeeper final
30{
31public:
33 {
36 }
37
38private:
40 {
41 mHousekeepingOperation = std::async([this] { PerformHousekeeping(); });
42 }
43
45 {
46 const auto timeToKeep = std::chrono::hours(24 * DaysToKeepFiles.Read());
47 const auto now = std::chrono::system_clock::now();
48
49 auto& cloudProjectsDatabase = CloudProjectsDatabase::Get();
50
51 auto projects = cloudProjectsDatabase.GetCloudProjects();
52
53 for (const auto& project : projects)
54 {
55 if (mHousekeepingCancelled.load())
56 return;
57
58 const auto path = ToWXString(project.LocalPath);
59
60 if (!wxFileExists(path))
61 {
62 cloudProjectsDatabase.DeleteProject(project.ProjectId);
63 continue;
64 }
65
66 const auto lastAccess =
67 std::max(project.LastModified, project.LastRead);
68
69 const auto discardTreshold =
70 std::chrono::system_clock::from_time_t(lastAccess) + timeToKeep;
71
72 if (discardTreshold > now)
73 continue;
74
75 if (wxRemoveFile(path))
76 cloudProjectsDatabase.DeleteProject(project.ProjectId);
77 }
78
79 // Do we need to remove the files that are not in the database?
80 }
81
83 {
84 mHousekeepingCancelled.store(true);
85 if (mHousekeepingOperation.valid())
86 mHousekeepingOperation.wait();
87
88 auto& cloudProjectsDatabase = CloudProjectsDatabase::Get();
89
90 auto connectionLock = cloudProjectsDatabase.GetConnection();
91
92 if (!connectionLock)
93 return;
94
95 auto vacuumStatement = connectionLock->CreateStatement("VACUUM");
96
97 if (!vacuumStatement)
98 return;
99
100 vacuumStatement->Prepare().Run();
101 }
102
103 std::future<void> mHousekeepingOperation;
104 std::atomic<bool> mHousekeepingCancelled { false };
105}; // class Housekeeper
106
108} // namespace
109
110} // namespace audacity::cloud::audiocom::sync
Declare functions to perform UTF-8 to std::wstring conversions.
const auto project
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:207
void OnAppClosing(std::function< void()> callback)
Definition: AppEvents.cpp:57
void OnAppInitialized(std::function< void()> callback)
Definition: AppEvents.cpp:42
wxString ToWXString(const std::string &str)