forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckpointManager.cpp
More file actions
169 lines (145 loc) · 6.49 KB
/
Copy pathCheckpointManager.cpp
File metadata and controls
169 lines (145 loc) · 6.49 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "CheckpointManager.h"
#include "Command.h"
#include "ExecutionContextData.h"
#include <AppInstallerRuntime.h>
using namespace AppInstaller::CLI;
using namespace AppInstaller::Repository::Microsoft;
using namespace AppInstaller::SQLite;
namespace AppInstaller::Checkpoints
{
// This checkpoint name is reserved for the starting checkpoint which captures the automatic metadata.
constexpr std::string_view s_AutomaticCheckpoint = "automatic"sv;
constexpr std::string_view s_CheckpointsFileName = "checkpoints.db"sv;
std::filesystem::path CheckpointManager::GetCheckpointDatabasePath(const std::string_view& resumeId, bool createCheckpointDirectory)
{
const auto checkpointsDirectory = Runtime::GetPathTo(Runtime::PathName::CheckpointsLocation) / resumeId;
if (createCheckpointDirectory)
{
if (!std::filesystem::exists(checkpointsDirectory))
{
AICLI_LOG(Repo, Info, << "Creating checkpoint database directory: " << checkpointsDirectory);
std::filesystem::create_directories(checkpointsDirectory);
}
else
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_CANNOT_MAKE), !std::filesystem::is_directory(checkpointsDirectory));
}
}
auto recordPath = checkpointsDirectory / s_CheckpointsFileName;
return recordPath;
}
CheckpointManager::CheckpointManager()
{
GUID resumeId;
std::ignore = CoCreateGuid(&resumeId);
m_resumeId = Utility::ConvertGuidToString(resumeId);
const auto& checkpointDatabasePath = GetCheckpointDatabasePath(m_resumeId, true);
m_checkpointDatabase = CheckpointDatabase::CreateNew(checkpointDatabasePath.u8string());
}
CheckpointManager::CheckpointManager(const std::string& resumeId)
{
m_resumeId = resumeId;
const auto& checkpointDatabasePath = GetCheckpointDatabasePath(m_resumeId);
m_checkpointDatabase = CheckpointDatabase::Open(checkpointDatabasePath.u8string());
}
void CheckpointManager::CreateAutomaticCheckpoint(CLI::Execution::Context& context)
{
CheckpointDatabase::IdType startCheckpointId = m_checkpointDatabase->AddCheckpoint(s_AutomaticCheckpoint);
Checkpoint<AutomaticCheckpointData> automaticCheckpoint{ m_checkpointDatabase, startCheckpointId };
automaticCheckpoint.Set(AutomaticCheckpointData::ClientVersion, {}, AppInstaller::Runtime::GetClientVersion());
const auto& executingCommand = context.GetExecutingCommand();
if (executingCommand != nullptr)
{
automaticCheckpoint.Set(AutomaticCheckpointData::Command, {}, std::string{ executingCommand->FullName() });
}
const auto& argTypes = context.Args.GetTypes();
for (auto type : argTypes)
{
const auto& argument = std::to_string(static_cast<int>(type));
auto argumentType = Argument::ForType(type).Type();
if (argumentType == ArgumentType::Flag)
{
automaticCheckpoint.Set(AutomaticCheckpointData::Arguments, argument, {});
}
else
{
const auto& values = *context.Args.GetArgs(type);
automaticCheckpoint.SetMany(AutomaticCheckpointData::Arguments, argument, values);
}
}
automaticCheckpoint.Set(AutomaticCheckpointData::ResumeCount, {}, std::to_string(0));
}
void LoadCommandArgsFromAutomaticCheckpoint(CLI::Execution::Context& context, Checkpoint<AutomaticCheckpointData>& automaticCheckpoint)
{
for (const auto& fieldName : automaticCheckpoint.GetFieldNames(AutomaticCheckpointData::Arguments))
{
// Command arguments are represented as integer strings in the checkpoint record.
Execution::Args::Type type = static_cast<Execution::Args::Type>(std::stoi(fieldName));
auto argumentType = Argument::ForType(type).Type();
if (argumentType == ArgumentType::Flag)
{
context.Args.AddArg(type);
}
else
{
const auto& values = automaticCheckpoint.GetMany(AutomaticCheckpointData::Arguments, fieldName);
for (const auto& value : values)
{
context.Args.AddArg(type, value);
}
}
}
}
std::optional<Checkpoint<AutomaticCheckpointData>> CheckpointManager::GetAutomaticCheckpoint()
{
const auto& checkpointIds = m_checkpointDatabase->GetCheckpointIds();
if (checkpointIds.empty())
{
return {};
}
CheckpointDatabase::IdType automaticCheckpointId = checkpointIds.back();
return Checkpoint<AutomaticCheckpointData>{ m_checkpointDatabase, automaticCheckpointId };
}
Checkpoint<CLI::Execution::Data> CheckpointManager::CreateCheckpoint(std::string_view checkpointName)
{
CheckpointDatabase::IdType checkpointId = m_checkpointDatabase->AddCheckpoint(checkpointName);
Checkpoint<CLI::Execution::Data> checkpoint{ m_checkpointDatabase, checkpointId };
return checkpoint;
}
std::vector<Checkpoint<CLI::Execution::Data>> CheckpointManager::GetCheckpoints()
{
auto checkpointIds = m_checkpointDatabase->GetCheckpointIds();
if (checkpointIds.empty())
{
return {};
}
// Remove the last checkpoint (automatic)
checkpointIds.pop_back();
std::vector<Checkpoint<CLI::Execution::Data>> checkpoints;
for (const auto& checkpointId : checkpointIds)
{
checkpoints.emplace_back(Checkpoint<CLI::Execution::Data>{ m_checkpointDatabase, checkpointId });
}
return checkpoints;
}
void CheckpointManager::CleanUpDatabase()
{
if (m_checkpointDatabase)
{
m_checkpointDatabase.reset();
}
if (!m_resumeId.empty())
{
const auto& checkpointDatabasePath = GetCheckpointDatabasePath(m_resumeId);
if (std::filesystem::exists(checkpointDatabasePath))
{
const auto& checkpointDatabaseParentDirectory = checkpointDatabasePath.parent_path();
AICLI_LOG(CLI, Info, << "Deleting Checkpoint database directory: " << checkpointDatabaseParentDirectory);
std::filesystem::remove_all(checkpointDatabaseParentDirectory);
}
}
}
}