2016-09-18 20:50:08 -07:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-13 18:37:41 -07:00
|
|
|
#include <vcpkg/base/expected.h>
|
|
|
|
|
2017-10-16 11:44:04 -07:00
|
|
|
#if defined(_WIN32)
|
2017-10-13 18:37:41 -07:00
|
|
|
#include <filesystem>
|
2017-10-16 11:44:04 -07:00
|
|
|
#else
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
#endif
|
2017-10-13 18:37:41 -07:00
|
|
|
|
|
|
|
namespace fs
|
|
|
|
{
|
|
|
|
namespace stdfs = std::experimental::filesystem;
|
|
|
|
|
|
|
|
using stdfs::copy_options;
|
|
|
|
using stdfs::file_status;
|
2018-02-16 15:40:22 -08:00
|
|
|
using stdfs::file_type;
|
2017-10-13 18:37:41 -07:00
|
|
|
using stdfs::path;
|
2017-11-20 19:15:47 -08:00
|
|
|
using stdfs::u8path;
|
2017-10-13 18:37:41 -07:00
|
|
|
|
|
|
|
inline bool is_regular_file(file_status s) { return stdfs::is_regular_file(s); }
|
|
|
|
inline bool is_directory(file_status s) { return stdfs::is_directory(s); }
|
|
|
|
inline bool status_known(file_status s) { return stdfs::status_known(s); }
|
|
|
|
}
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2017-01-05 12:47:08 -08:00
|
|
|
namespace vcpkg::Files
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
2017-10-16 11:44:04 -07:00
|
|
|
struct Filesystem
|
2017-04-08 16:26:26 -07:00
|
|
|
{
|
2017-04-08 20:19:35 -07:00
|
|
|
virtual Expected<std::string> read_contents(const fs::path& file_path) const = 0;
|
2017-04-12 23:00:42 -07:00
|
|
|
virtual Expected<std::vector<std::string>> read_lines(const fs::path& file_path) const = 0;
|
2017-04-08 20:19:35 -07:00
|
|
|
virtual fs::path find_file_recursively_up(const fs::path& starting_dir, const std::string& filename) const = 0;
|
2017-04-12 23:15:02 -07:00
|
|
|
virtual std::vector<fs::path> get_files_recursive(const fs::path& dir) const = 0;
|
|
|
|
virtual std::vector<fs::path> get_files_non_recursive(const fs::path& dir) const = 0;
|
2017-04-08 20:19:35 -07:00
|
|
|
|
2017-04-12 23:16:04 -07:00
|
|
|
virtual void write_lines(const fs::path& file_path, const std::vector<std::string>& lines) = 0;
|
2018-02-21 22:18:15 -08:00
|
|
|
virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) = 0;
|
2017-04-08 20:19:35 -07:00
|
|
|
virtual void rename(const fs::path& oldpath, const fs::path& newpath) = 0;
|
2017-04-11 15:16:39 -07:00
|
|
|
virtual bool remove(const fs::path& path) = 0;
|
|
|
|
virtual bool remove(const fs::path& path, std::error_code& ec) = 0;
|
|
|
|
virtual std::uintmax_t remove_all(const fs::path& path, std::error_code& ec) = 0;
|
2017-04-08 20:19:35 -07:00
|
|
|
virtual bool exists(const fs::path& path) const = 0;
|
|
|
|
virtual bool is_directory(const fs::path& path) const = 0;
|
|
|
|
virtual bool is_regular_file(const fs::path& path) const = 0;
|
|
|
|
virtual bool is_empty(const fs::path& path) const = 0;
|
|
|
|
virtual bool create_directory(const fs::path& path, std::error_code& ec) = 0;
|
2017-04-17 17:44:15 -07:00
|
|
|
virtual bool create_directories(const fs::path& path, std::error_code& ec) = 0;
|
2017-04-08 20:19:35 -07:00
|
|
|
virtual void copy(const fs::path& oldpath, const fs::path& newpath, fs::copy_options opts) = 0;
|
2017-10-16 11:44:04 -07:00
|
|
|
virtual bool copy_file(const fs::path& oldpath,
|
|
|
|
const fs::path& newpath,
|
|
|
|
fs::copy_options opts,
|
|
|
|
std::error_code& ec) = 0;
|
2017-04-11 15:16:39 -07:00
|
|
|
virtual fs::file_status status(const fs::path& path, std::error_code& ec) const = 0;
|
2018-02-21 22:18:15 -08:00
|
|
|
|
|
|
|
inline void write_contents(const fs::path& file_path, const std::string& data)
|
|
|
|
{
|
|
|
|
std::error_code ec;
|
|
|
|
write_contents(file_path, data, ec);
|
|
|
|
Checks::check_exit(
|
2018-02-21 22:21:19 -08:00
|
|
|
VCPKG_LINE_INFO, !ec, "error while writing file: %s: %s", file_path.u8string(), ec.message());
|
2018-02-21 22:18:15 -08:00
|
|
|
}
|
2017-04-08 16:26:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Filesystem& get_real_filesystem();
|
|
|
|
|
2016-09-23 17:57:18 -07:00
|
|
|
static const char* FILESYSTEM_INVALID_CHARACTERS = R"(\/:*?"<>|)";
|
|
|
|
|
2016-12-16 19:38:02 -08:00
|
|
|
bool has_invalid_chars_for_filesystem(const std::string& s);
|
2016-09-23 17:57:18 -07:00
|
|
|
|
2016-11-30 14:08:43 -08:00
|
|
|
void print_paths(const std::vector<fs::path>& paths);
|
2017-08-30 22:42:43 -07:00
|
|
|
|
2017-10-16 11:44:04 -07:00
|
|
|
std::vector<fs::path> find_from_PATH(const std::string& name);
|
2017-01-05 12:47:08 -08:00
|
|
|
}
|