aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg_Files.cpp
blob: 7b12ea699c976b69f7f80f97d76578e1f44870bf (plain)
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
#include "pch.h"

#include "vcpkg_Files.h"
#include "vcpkg_System.h"
#include <thread>

namespace vcpkg::Files
{
    static const std::regex FILESYSTEM_INVALID_CHARACTERS_REGEX = std::regex(R"([\/:*?"<>|])");

    struct RealFilesystem final : Filesystem
    {
        virtual Expected<std::string> read_contents(const fs::path& file_path) const override
        {
            std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary);
            if (file_stream.fail())
            {
                return std::make_error_code(std::errc::no_such_file_or_directory);
            }

            file_stream.seekg(0, file_stream.end);
            auto length = file_stream.tellg();
            file_stream.seekg(0, file_stream.beg);

            if (length > SIZE_MAX)
            {
                return std::make_error_code(std::errc::file_too_large);
            }

            std::string output;
            output.resize(static_cast<size_t>(length));
            file_stream.read(&output[0], length);
            file_stream.close();

            return std::move(output);
        }
        virtual Expected<std::vector<std::string>> read_lines(const fs::path& file_path) const override
        {
            std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary);
            if (file_stream.fail())
            {
                return std::make_error_code(std::errc::no_such_file_or_directory);
            }

            std::vector<std::string> output;
            std::string line;
            while (std::getline(file_stream, line))
            {
                output.push_back(line);
            }
            file_stream.close();

            return std::move(output);
        }
        virtual fs::path find_file_recursively_up(const fs::path& starting_dir,
                                                  const std::string& filename) const override
        {
            fs::path current_dir = starting_dir;
            for (; !current_dir.empty(); current_dir = current_dir.parent_path())
            {
                const fs::path candidate = current_dir / filename;
                if (exists(candidate))
                {
                    break;
                }
            }

            return current_dir;
        }

        virtual std::vector<fs::path> get_files_recursive(const fs::path& dir) const override
        {
            std::vector<fs::path> ret;

            fs::stdfs::recursive_directory_iterator b(dir), e{};
            for (; b != e; ++b)
            {
                ret.push_back(b->path());
            }

            return ret;
        }

        virtual std::vector<fs::path> get_files_non_recursive(const fs::path& dir) const override
        {
            std::vector<fs::path> ret;

            fs::stdfs::directory_iterator b(dir), e{};
            for (; b != e; ++b)
            {
                ret.push_back(b->path());
            }

            return ret;
        }

        virtual void write_lines(const fs::path& file_path, const std::vector<std::string>& lines) override
        {
            std::fstream output(file_path, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
            for (const std::string& line : lines)
            {
                output << line << "\n";
            }
            output.close();
        }

        virtual void rename(const fs::path& oldpath, const fs::path& newpath) override
        {
            fs::stdfs::rename(oldpath, newpath);
        }
        virtual bool remove(const fs::path& path) override { return fs::stdfs::remove(path); }
        virtual bool remove(const fs::path& path, std::error_code& ec) override { return fs::stdfs::remove(path, ec); }
        virtual std::uintmax_t remove_all(const fs::path& path, std::error_code& ec) override
        {
            // Working around the currently buggy remove_all()
            std::uintmax_t out = fs::stdfs::remove_all(path, ec);

            for (int i = 0; i < 5 && this->exists(path); i++)
            {
                using namespace std::chrono_literals;
                std::this_thread::sleep_for(i * 100ms);
                out += fs::stdfs::remove_all(path, ec);
            }

            if (this->exists(path))
            {
                System::println(System::Color::warning,
                                "Some files in %s were unable to be removed. Close any editors operating in this "
                                "directory and retry.",
                                path.string());
            }

            return out;
        }
        virtual bool exists(const fs::path& path) const override { return fs::stdfs::exists(path); }
        virtual bool is_directory(const fs::path& path) const override { return fs::stdfs::is_directory(path); }
        virtual bool is_regular_file(const fs::path& path) const override { return fs::stdfs::is_regular_file(path); }
        virtual bool is_empty(const fs::path& path) const override { return fs::stdfs::is_empty(path); }
        virtual bool create_directory(const fs::path& path, std::error_code& ec) override
        {
            return fs::stdfs::create_directory(path, ec);
        }
        virtual bool create_directories(const fs::path& path, std::error_code& ec) override
        {
            return fs::stdfs::create_directories(path, ec);
        }
        virtual void copy(const fs::path& oldpath, const fs::path& newpath, fs::copy_options opts) override
        {
            fs::stdfs::copy(oldpath, newpath, opts);
        }
        virtual bool copy_file(const fs::path& oldpath,
                               const fs::path& newpath,
                               fs::copy_options opts,
                               std::error_code& ec) override
        {
            return fs::stdfs::copy_file(oldpath, newpath, opts, ec);
        }

        virtual fs::file_status status(const fs::path& path, std::error_code& ec) const override
        {
            return fs::stdfs::status(path, ec);
        }
        virtual void write_contents(const fs::path& file_path, const std::string& data) override
        {
            FILE* f = nullptr;
            auto ec = _wfopen_s(&f, file_path.native().c_str(), L"wb");
            Checks::check_exit(VCPKG_LINE_INFO, ec == 0);
            auto count = fwrite(data.data(), sizeof(data[0]), data.size(), f);
            fclose(f);

            Checks::check_exit(VCPKG_LINE_INFO, count == data.size());
        }
    };

    Filesystem& get_real_filesystem()
    {
        static RealFilesystem real_fs;
        return real_fs;
    }

    bool has_invalid_chars_for_filesystem(const std::string& s)
    {
        return std::regex_search(s, FILESYSTEM_INVALID_CHARACTERS_REGEX);
    }

    void print_paths(const std::vector<fs::path>& paths)
    {
        System::println("");
        for (const fs::path& p : paths)
        {
            System::println("    %s", p.generic_string());
        }
        System::println("");
    }
}