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
|
#include "vcpkg.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <functional>
#include <string>
#include <unordered_map>
#include <memory>
#include <vector>
#include "vcpkg_Files.h"
#include "Paragraphs.h"
#include <regex>
using namespace vcpkg;
static StatusParagraphs load_current_database(const fs::path& vcpkg_dir_status_file, const fs::path& vcpkg_dir_status_file_old)
{
if (!fs::exists(vcpkg_dir_status_file))
{
if (!fs::exists(vcpkg_dir_status_file_old))
{
// no status file, use empty db
return StatusParagraphs();
}
fs::rename(vcpkg_dir_status_file_old, vcpkg_dir_status_file);
}
auto text = Files::get_contents(vcpkg_dir_status_file).get_or_throw();
auto pghs = Paragraphs::parse_paragraphs(text);
std::vector<std::unique_ptr<StatusParagraph>> status_pghs;
for (auto&& p : pghs)
{
status_pghs.push_back(std::make_unique<StatusParagraph>(p));
}
return StatusParagraphs(std::move(status_pghs));
}
StatusParagraphs vcpkg::database_load_check(const vcpkg_paths& paths)
{
auto updates_dir = paths.vcpkg_dir_updates;
std::error_code ec;
fs::create_directory(paths.installed, ec);
fs::create_directory(paths.vcpkg_dir, ec);
fs::create_directory(paths.vcpkg_dir_info, ec);
fs::create_directory(updates_dir, ec);
const fs::path& status_file = paths.vcpkg_dir_status_file;
const fs::path status_file_old = status_file.parent_path() / "status-old";
const fs::path status_file_new = status_file.parent_path() / "status-new";
StatusParagraphs current_status_db = load_current_database(status_file, status_file_old);
auto b = fs::directory_iterator(updates_dir);
auto e = fs::directory_iterator();
if (b == e)
{
// updates directory is empty, control file is up-to-date.
return current_status_db;
}
for (; b != e; ++b)
{
if (!fs::is_regular_file(b->status()))
continue;
if (b->path().filename() == "incomplete")
continue;
auto text = Files::get_contents(b->path()).get_or_throw();
auto pghs = Paragraphs::parse_paragraphs(text);
for (auto&& p : pghs)
{
current_status_db.insert(std::make_unique<StatusParagraph>(p));
}
}
std::fstream(status_file_new, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc) << current_status_db;
if (fs::exists(status_file_old))
fs::remove(status_file_old);
if (fs::exists(status_file))
fs::rename(status_file, status_file_old);
fs::rename(status_file_new, status_file);
fs::remove(status_file_old);
b = fs::directory_iterator(updates_dir);
for (; b != e; ++b)
{
if (!fs::is_regular_file(b->status()))
continue;
fs::remove(b->path());
}
return current_status_db;
}
void vcpkg::write_update(const vcpkg_paths& paths, const StatusParagraph& p)
{
static int update_id = 0;
auto my_update_id = update_id++;
auto tmp_update_filename = paths.vcpkg_dir_updates / "incomplete";
auto update_filename = paths.vcpkg_dir_updates / std::to_string(my_update_id);
std::fstream fs(tmp_update_filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
fs << p;
fs.close();
fs::rename(tmp_update_filename, update_filename);
}
std::vector<StatusParagraph_and_associated_files> vcpkg::get_installed_files(const vcpkg_paths& paths, const StatusParagraphs& status_db)
{
static const std::string MARK_FOR_REMOVAL = "";
std::vector<StatusParagraph_and_associated_files> installed_files;
std::string line;
for (const std::unique_ptr<StatusParagraph>& pgh : status_db)
{
if (pgh->state != install_state_t::installed)
{
continue;
}
std::fstream listfile(paths.listfile_path(pgh->package));
std::vector<std::string> installed_files_of_current_pgh;
while (std::getline(listfile, line))
{
if (line.empty())
{
continue;
}
installed_files_of_current_pgh.push_back(line);
}
// Should already be sorted
std::sort(installed_files_of_current_pgh.begin(), installed_files_of_current_pgh.end());
// Since the files are sorted, we can detect the entries that represent directories
// by comparing every element with the next one and checking if the next has a slash immediately after the current one's length
for (int i = 1; i < installed_files_of_current_pgh.size(); i++)
{
std::string& current_string = installed_files_of_current_pgh.at(i - 1);
const std::string& next_string = installed_files_of_current_pgh.at(i);
const size_t potential_slash_char_index = current_string.length();
// Make sure the index exists first
if (next_string.size() > potential_slash_char_index && next_string.at(potential_slash_char_index) == '/')
{
current_string = MARK_FOR_REMOVAL;
}
}
installed_files_of_current_pgh.erase(std::remove_if(installed_files_of_current_pgh.begin(), installed_files_of_current_pgh.end(), [](const std::string& file)
{
return file == MARK_FOR_REMOVAL;
}),
installed_files_of_current_pgh.end());
const StatusParagraph_and_associated_files pgh_and_files = {*pgh, std::move(installed_files_of_current_pgh)};
installed_files.push_back(pgh_and_files);
}
return installed_files;
}
expected<SourceParagraph> vcpkg::try_load_port(const fs::path& path)
{
try
{
auto pghs = Paragraphs::get_paragraphs(path / "CONTROL");
Checks::check_exit(pghs.size() == 1, "Invalid control file at %s\\CONTROL", path.string());
return SourceParagraph(pghs[0]);
}
catch (std::runtime_error const&)
{
}
return std::errc::no_such_file_or_directory;
}
expected<BinaryParagraph> vcpkg::try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec)
{
const fs::path path = paths.package_dir(spec) / "CONTROL";
auto control_contents_maybe = Files::get_contents(path);
if (auto control_contents = control_contents_maybe.get())
{
std::vector<std::unordered_map<std::string, std::string>> pghs;
try
{
pghs = Paragraphs::parse_paragraphs(*control_contents);
}
catch (std::runtime_error)
{
}
Checks::check_exit(pghs.size() == 1, "Invalid control file at %s", path.string());
return BinaryParagraph(pghs[0]);
}
return control_contents_maybe.error_code();
}
|