Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/Sources/VersionFiles.cpp
blob: 2b11cee28ad4ea69e802dabccdef6350ceca4434 (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
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

#include <cstdlib>
#include <fmt/format.h>
#include <fstream>
#include <system_error>
#include <utility>

#include "Progmasoft/Tazelish/VersionFiles.hpp"

namespace Progmasoft::Tazelish
{
    namespace
    {

        namespace fs = std::filesystem;

        // A version file is short; anything longer names no version.
        constexpr std::size_t kLongestContent = 64;

        FileError
        Fail(const fs::path &file, std::string message)
        {
            return FileError{ file, std::move(message) };
        }

        // The text of a file without the line break and blanks around the
        // version.
        std::string_view
        Trim(std::string_view text)
        {
            constexpr std::string_view kBlanks = " \t\r\n";
            constexpr std::string_view kByteOrderMark = "\xEF\xBB\xBF";
            if (text.substr(0, kByteOrderMark.size()) == kByteOrderMark)
            {
                text.remove_prefix(kByteOrderMark.size());
            }
            const std::size_t first = text.find_first_not_of(kBlanks);
            if (first == std::string_view::npos)
            {
                return {};
            }
            const std::size_t last = text.find_last_not_of(kBlanks);
            return text.substr(first, last - first + 1);
        }

    } // namespace

    std::optional<fs::path>
    HomeDirectory()
    {
#ifdef _WIN32
        constexpr const char *kVariable = "USERPROFILE";
#else
        constexpr const char *kVariable = "HOME";
#endif
        // The environment is read once, before any thread starts.
        const char *value
            = std::getenv(kVariable); // NOLINT(concurrency-mt-unsafe)
        if (value == nullptr || *value == '\0')
        {
            return std::nullopt;
        }
        return fs::path(value);
    }

    fs::path
    StateDirectory(const fs::path &home)
    {
        return home / fs::path(kStateDirectoryName);
    }

    fs::path
    ConfigFile(const fs::path &home)
    {
        return StateDirectory(home) / fs::path(kConfigFileName);
    }

    std::variant<NoSelection, Version, FileError>
    ReadVersionFile(const fs::path &file)
    {
        std::error_code error;
        const fs::file_status status = fs::status(file, error);
        if (status.type() == fs::file_type::not_found)
        {
            return NoSelection{};
        }
        if (error)
        {
            return Fail(file, error.message());
        }
        if (status.type() != fs::file_type::regular)
        {
            return Fail(file, "is not a regular file");
        }
        std::ifstream stream(file, std::ios::binary);
        if (!stream)
        {
            return Fail(file, "cannot be read");
        }
        std::string content;
        content.resize(kLongestContent + 1);
        stream.read(content.data(),
                    static_cast<std::streamsize>(content.size()));
        content.resize(static_cast<std::size_t>(stream.gcount()));
        if (stream.bad())
        {
            return Fail(file, "cannot be read");
        }
        const std::string_view text = Trim(content);
        if (content.size() > kLongestContent)
        {
            return Fail(file, "holds more than a version");
        }
        if (auto version = ParseVersion(text))
        {
            return *version;
        }
        return Fail(file,
                    fmt::format("holds '{}', which is not a version", text));
    }

    std::variant<NoSelection, Selection, FileError>
    SelectVersion(const fs::path &directory,
                  const std::optional<fs::path> &home)
    {
        for (fs::path folder = directory;; folder = folder.parent_path())
        {
            const fs::path file = folder / fs::path(kProjectFileName);
            auto read = ReadVersionFile(file);
            if (auto *failure = std::get_if<FileError>(&read))
            {
                return std::move(*failure);
            }
            if (auto *version = std::get_if<Version>(&read))
            {
                return Selection{ *version, file, VersionSource::kProject };
            }
            if (folder == folder.parent_path())
            {
                break;
            }
        }
        if (!home)
        {
            return NoSelection{};
        }
        const fs::path file = ConfigFile(*home);
        auto read = ReadVersionFile(file);
        if (auto *failure = std::get_if<FileError>(&read))
        {
            return std::move(*failure);
        }
        if (auto *version = std::get_if<Version>(&read))
        {
            return Selection{ *version, file, VersionSource::kUser };
        }
        return NoSelection{};
    }

    std::optional<FileError>
    WriteVersionFile(const fs::path &file, const Version &version, bool force)
    {
        std::error_code error;
        const bool exists = fs::exists(fs::symlink_status(file, error));
        if (error && error != std::errc::no_such_file_or_directory)
        {
            return Fail(file, error.message());
        }
        if (exists && !force)
        {
            return Fail(file, "already exists; --force writes over it");
        }
        if (file.has_parent_path())
        {
            fs::create_directories(file.parent_path(), error);
            if (error)
            {
                return Fail(file.parent_path(), error.message());
            }
        }
        std::ofstream stream(file, std::ios::binary | std::ios::trunc);
        const std::string content = FormatVersion(version) + "\n";
        stream.write(content.data(),
                     static_cast<std::streamsize>(content.size()));
        stream.close();
        if (!stream)
        {
            return Fail(file, "cannot be written");
        }
        return std::nullopt;
    }

} // namespace Progmasoft::Tazelish