2017-01-28 04:49:09 +08:00
|
|
|
#include "pch.h"
|
2017-04-28 09:08:52 +08:00
|
|
|
|
2016-11-08 08:10:41 +08:00
|
|
|
#include "Paragraphs.h"
|
2016-12-16 06:28:43 +08:00
|
|
|
#include "metrics.h"
|
2017-04-28 09:08:52 +08:00
|
|
|
#include "vcpkg_Files.h"
|
2017-03-11 08:39:04 +08:00
|
|
|
#include "vcpkg_Strings.h"
|
2017-04-13 13:39:14 +08:00
|
|
|
#include "vcpkg_Util.h"
|
2017-04-28 09:08:52 +08:00
|
|
|
#include "vcpkglib.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
namespace vcpkg
|
2016-11-08 08:10:41 +08:00
|
|
|
{
|
2017-04-28 09:08:52 +08:00
|
|
|
static StatusParagraphs load_current_database(Files::Filesystem& fs,
|
|
|
|
const fs::path& vcpkg_dir_status_file,
|
|
|
|
const fs::path& vcpkg_dir_status_file_old)
|
2016-11-08 08:10:41 +08:00
|
|
|
{
|
2017-04-12 06:16:39 +08:00
|
|
|
if (!fs.exists(vcpkg_dir_status_file))
|
2016-11-08 08:10:41 +08:00
|
|
|
{
|
2017-04-12 06:16:39 +08:00
|
|
|
if (!fs.exists(vcpkg_dir_status_file_old))
|
2017-02-28 06:59:56 +08:00
|
|
|
{
|
|
|
|
// no status file, use empty db
|
|
|
|
return StatusParagraphs();
|
|
|
|
}
|
|
|
|
|
2017-04-12 06:16:39 +08:00
|
|
|
fs.rename(vcpkg_dir_status_file_old, vcpkg_dir_status_file);
|
2016-11-08 08:10:41 +08:00
|
|
|
}
|
|
|
|
|
2017-04-09 07:26:26 +08:00
|
|
|
auto pghs = Paragraphs::get_paragraphs(fs, vcpkg_dir_status_file).value_or_exit(VCPKG_LINE_INFO);
|
2016-11-08 08:10:41 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
std::vector<std::unique_ptr<StatusParagraph>> status_pghs;
|
|
|
|
for (auto&& p : pghs)
|
|
|
|
{
|
2017-06-17 17:39:14 +08:00
|
|
|
status_pghs.push_back(std::make_unique<StatusParagraph>(std::move(p)));
|
2017-02-28 06:59:56 +08:00
|
|
|
}
|
2016-11-08 08:10:41 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
return StatusParagraphs(std::move(status_pghs));
|
2016-11-08 08:10:41 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 07:29:11 +08:00
|
|
|
StatusParagraphs database_load_check(const VcpkgPaths& paths)
|
2017-02-28 06:59:56 +08:00
|
|
|
{
|
2017-04-09 11:19:35 +08:00
|
|
|
auto& fs = paths.get_filesystem();
|
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
auto updates_dir = paths.vcpkg_dir_updates;
|
2016-11-08 08:10:41 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
std::error_code ec;
|
2017-04-09 11:19:35 +08:00
|
|
|
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);
|
2016-11-08 08:10:41 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
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";
|
2016-11-08 08:10:41 +08:00
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
StatusParagraphs current_status_db = load_current_database(fs, status_file, status_file_old);
|
2016-11-08 08:10:41 +08:00
|
|
|
|
2017-04-13 14:15:02 +08:00
|
|
|
auto update_files = fs.get_files_non_recursive(updates_dir);
|
2017-04-09 11:19:35 +08:00
|
|
|
if (update_files.empty())
|
2017-02-28 06:59:56 +08:00
|
|
|
{
|
|
|
|
// updates directory is empty, control file is up-to-date.
|
|
|
|
return current_status_db;
|
|
|
|
}
|
2017-04-09 11:19:35 +08:00
|
|
|
for (auto&& file : update_files)
|
2016-11-08 08:10:41 +08:00
|
|
|
{
|
2017-04-28 09:08:52 +08:00
|
|
|
if (!fs.is_regular_file(file)) continue;
|
|
|
|
if (file.filename() == "incomplete") continue;
|
2017-02-28 06:59:56 +08:00
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
auto pghs = Paragraphs::get_paragraphs(fs, file).value_or_exit(VCPKG_LINE_INFO);
|
2017-02-28 06:59:56 +08:00
|
|
|
for (auto&& p : pghs)
|
|
|
|
{
|
2017-06-17 17:39:14 +08:00
|
|
|
current_status_db.insert(std::make_unique<StatusParagraph>(std::move(p)));
|
2017-02-28 06:59:56 +08:00
|
|
|
}
|
2016-11-08 08:10:41 +08:00
|
|
|
}
|
|
|
|
|
2017-04-13 13:48:52 +08:00
|
|
|
fs.write_contents(status_file_new, Strings::serialize(current_status_db));
|
2016-11-08 08:10:41 +08:00
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
fs.rename(status_file_new, status_file);
|
2016-12-16 06:28:43 +08:00
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
for (auto&& file : update_files)
|
2017-02-28 06:59:56 +08:00
|
|
|
{
|
2017-04-28 09:08:52 +08:00
|
|
|
if (!fs.is_regular_file(file)) continue;
|
2017-04-09 11:19:35 +08:00
|
|
|
|
|
|
|
fs.remove(file);
|
2017-02-28 06:59:56 +08:00
|
|
|
}
|
2016-12-16 06:28:43 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
return current_status_db;
|
2016-12-16 06:28:43 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 07:29:11 +08:00
|
|
|
void write_update(const VcpkgPaths& paths, const StatusParagraph& p)
|
2016-12-16 06:28:43 +08:00
|
|
|
{
|
2017-02-28 06:59:56 +08:00
|
|
|
static int update_id = 0;
|
2017-04-13 13:48:52 +08:00
|
|
|
auto& fs = paths.get_filesystem();
|
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
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);
|
2017-04-13 13:48:52 +08:00
|
|
|
|
|
|
|
fs.write_contents(tmp_update_filename, Strings::serialize(p));
|
|
|
|
fs.rename(tmp_update_filename, update_filename);
|
2016-12-16 06:28:43 +08:00
|
|
|
}
|
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
static void upgrade_to_slash_terminated_sorted_format(Files::Filesystem& fs,
|
|
|
|
std::vector<std::string>* lines,
|
|
|
|
const fs::path& listfile_path)
|
2016-12-16 06:28:43 +08:00
|
|
|
{
|
2017-02-28 06:59:56 +08:00
|
|
|
static bool was_tracked = false;
|
2016-12-16 06:28:43 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
if (lines->empty())
|
2016-12-16 06:28:43 +08:00
|
|
|
{
|
2017-02-28 06:59:56 +08:00
|
|
|
return;
|
2016-12-16 06:28:43 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
if (lines->at(0).back() == '/')
|
|
|
|
{
|
|
|
|
return; // File already in the new format
|
|
|
|
}
|
2016-12-16 06:28:43 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
if (!was_tracked)
|
|
|
|
{
|
|
|
|
was_tracked = true;
|
2017-04-04 05:41:36 +08:00
|
|
|
Metrics::track_property("listfile", "update to new format");
|
2017-02-28 06:59:56 +08:00
|
|
|
}
|
2016-12-02 07:36:39 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
// The files are sorted such that directories are placed just before the files they contain
|
|
|
|
// (They are not necessarily sorted alphabetically, e.g. libflac)
|
|
|
|
// Therefore 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 (size_t i = 0; i < lines->size() - 1; i++)
|
2016-12-02 07:36:39 +08:00
|
|
|
{
|
2017-02-28 06:59:56 +08:00
|
|
|
std::string& current_string = lines->at(i);
|
|
|
|
const std::string& next_string = lines->at(i + 1);
|
|
|
|
|
|
|
|
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 as a directory
|
|
|
|
}
|
2016-12-02 07:36:39 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
// After suffixing the directories with a slash, we can now sort.
|
|
|
|
// We cannot sort before adding the suffixes because the following (actual example):
|
|
|
|
/*
|
|
|
|
x86-windows/include/FLAC <<<<<< This would be separated from its group due to sorting
|
|
|
|
x86-windows/include/FLAC/all.h
|
|
|
|
x86-windows/include/FLAC/assert.h
|
|
|
|
x86-windows/include/FLAC/callback.h
|
|
|
|
x86-windows/include/FLAC++
|
|
|
|
x86-windows/include/FLAC++/all.h
|
|
|
|
x86-windows/include/FLAC++/decoder.h
|
|
|
|
x86-windows/include/FLAC++/encoder.h
|
|
|
|
*
|
|
|
|
x86-windows/include/FLAC/ <<<<<< This will now be kept with its group when sorting
|
|
|
|
x86-windows/include/FLAC/all.h
|
|
|
|
x86-windows/include/FLAC/assert.h
|
|
|
|
x86-windows/include/FLAC/callback.h
|
|
|
|
x86-windows/include/FLAC++/
|
|
|
|
x86-windows/include/FLAC++/all.h
|
|
|
|
x86-windows/include/FLAC++/decoder.h
|
|
|
|
x86-windows/include/FLAC++/encoder.h
|
2017-03-11 08:39:04 +08:00
|
|
|
*/
|
2017-02-28 06:59:56 +08:00
|
|
|
// Note that after sorting, the FLAC++/ group will be placed before the FLAC/ group
|
|
|
|
// The new format is lexicographically sorted
|
|
|
|
std::sort(lines->begin(), lines->end());
|
|
|
|
|
|
|
|
// Replace the listfile on disk
|
|
|
|
const fs::path updated_listfile_path = listfile_path.generic_string() + "_updated";
|
2017-04-13 14:16:04 +08:00
|
|
|
fs.write_lines(updated_listfile_path, *lines);
|
2017-04-09 11:19:35 +08:00
|
|
|
fs.rename(updated_listfile_path, listfile_path);
|
2016-12-02 07:36:39 +08:00
|
|
|
}
|
|
|
|
|
2017-03-30 07:00:41 +08:00
|
|
|
std::vector<StatusParagraph*> get_installed_ports(const StatusParagraphs& status_db)
|
|
|
|
{
|
|
|
|
std::vector<StatusParagraph*> installed_packages;
|
|
|
|
for (auto&& pgh : status_db)
|
|
|
|
{
|
2017-04-28 09:08:52 +08:00
|
|
|
if (pgh->state != InstallState::INSTALLED || pgh->want != Want::INSTALL) continue;
|
2017-03-30 07:00:41 +08:00
|
|
|
installed_packages.push_back(pgh.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return installed_packages;
|
|
|
|
}
|
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
std::vector<StatusParagraphAndAssociatedFiles> get_installed_files(const VcpkgPaths& paths,
|
|
|
|
const StatusParagraphs& status_db)
|
2016-11-16 03:56:46 +08:00
|
|
|
{
|
2017-04-09 11:19:35 +08:00
|
|
|
auto& fs = paths.get_filesystem();
|
|
|
|
|
2017-04-04 07:32:14 +08:00
|
|
|
std::vector<StatusParagraphAndAssociatedFiles> installed_files;
|
2016-11-16 03:56:46 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
for (const std::unique_ptr<StatusParagraph>& pgh : status_db)
|
|
|
|
{
|
2017-07-26 12:29:31 +08:00
|
|
|
if (pgh->state != InstallState::INSTALLED || pgh->package.feature != "")
|
2017-02-28 06:59:56 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fs::path listfile_path = paths.listfile_path(pgh->package);
|
2017-04-28 09:08:52 +08:00
|
|
|
std::vector<std::string> installed_files_of_current_pgh =
|
|
|
|
fs.read_lines(listfile_path).value_or_exit(VCPKG_LINE_INFO);
|
2017-02-28 06:59:56 +08:00
|
|
|
Strings::trim_all_and_remove_whitespace_strings(&installed_files_of_current_pgh);
|
2017-04-09 11:19:35 +08:00
|
|
|
upgrade_to_slash_terminated_sorted_format(fs, &installed_files_of_current_pgh, listfile_path);
|
2017-02-28 06:59:56 +08:00
|
|
|
|
|
|
|
// Remove the directories
|
2017-04-28 09:08:52 +08:00
|
|
|
Util::erase_remove_if(installed_files_of_current_pgh,
|
|
|
|
[](const std::string& file) { return file.back() == '/'; });
|
2017-02-28 06:59:56 +08:00
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
StatusParagraphAndAssociatedFiles pgh_and_files = {
|
2017-04-29 03:55:50 +08:00
|
|
|
*pgh, SortedVector<std::string>(std::move(installed_files_of_current_pgh))};
|
2017-02-28 06:59:56 +08:00
|
|
|
installed_files.push_back(std::move(pgh_and_files));
|
|
|
|
}
|
2016-11-16 03:56:46 +08:00
|
|
|
|
2017-02-28 06:59:56 +08:00
|
|
|
return installed_files;
|
|
|
|
}
|
2017-03-11 08:39:04 +08:00
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
CMakeVariable::CMakeVariable(const CWStringView varname, const wchar_t* varvalue)
|
|
|
|
: s(Strings::wformat(LR"("-D%s=%s")", varname, varvalue))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CMakeVariable::CMakeVariable(const CWStringView varname, const std::string& varvalue)
|
2017-05-05 05:54:23 +08:00
|
|
|
: CMakeVariable(varname, Strings::to_utf16(varvalue).c_str())
|
2017-04-28 09:08:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
CMakeVariable::CMakeVariable(const CWStringView varname, const std::wstring& varvalue)
|
|
|
|
: CMakeVariable(varname, varvalue.c_str())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CMakeVariable::CMakeVariable(const CWStringView varname, const fs::path& path)
|
|
|
|
: CMakeVariable(varname, path.generic_wstring())
|
|
|
|
{
|
|
|
|
}
|
2017-03-11 08:39:04 +08:00
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
std::wstring make_cmake_cmd(const fs::path& cmake_exe,
|
|
|
|
const fs::path& cmake_script,
|
|
|
|
const std::vector<CMakeVariable>& pass_variables)
|
2017-03-11 08:39:04 +08:00
|
|
|
{
|
2017-04-01 16:38:32 +08:00
|
|
|
std::wstring cmd_cmake_pass_variables = Strings::join(L" ", pass_variables, [](auto&& v) { return v.s; });
|
2017-04-28 09:08:52 +08:00
|
|
|
return Strings::wformat(
|
|
|
|
LR"("%s" %s -P "%s")", cmake_exe.native(), cmd_cmake_pass_variables, cmake_script.generic_wstring());
|
2017-03-11 08:39:04 +08:00
|
|
|
}
|
2017-06-17 17:39:14 +08:00
|
|
|
|
2017-08-23 06:59:27 +08:00
|
|
|
std::string shorten_text(const std::string& desc, size_t length)
|
2017-06-17 17:39:14 +08:00
|
|
|
{
|
2017-08-23 06:59:27 +08:00
|
|
|
Checks::check_exit(VCPKG_LINE_INFO, length >= 3);
|
2017-06-17 17:39:14 +08:00
|
|
|
auto simple_desc = std::regex_replace(desc, std::regex("\\s+"), " ");
|
2017-08-23 06:59:27 +08:00
|
|
|
return simple_desc.size() <= length ? simple_desc : simple_desc.substr(0, length - 3) + "...";
|
2017-06-17 17:39:14 +08:00
|
|
|
}
|
2016-11-16 03:56:46 +08:00
|
|
|
}
|