324 lines
12 KiB
C++
324 lines
12 KiB
C++
#include "files_text_document.h"
|
|
|
|
#include <objbase.h>
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
#include <system_error>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace ks::files
|
|
{
|
|
namespace
|
|
{
|
|
[[noreturn]] void fail(const char *operation)
|
|
{
|
|
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), operation);
|
|
}
|
|
|
|
struct File
|
|
{
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
~File() { if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle); }
|
|
HANDLE release() { return std::exchange(handle, INVALID_HANDLE_VALUE); }
|
|
};
|
|
|
|
struct PendingSave
|
|
{
|
|
std::filesystem::path path;
|
|
~PendingSave() { if (!path.empty()) DeleteFileW(path.c_str()); }
|
|
};
|
|
|
|
HANDLE openSource(const std::filesystem::path &path, bool editable)
|
|
{
|
|
const auto sharing = FILE_SHARE_READ | FILE_SHARE_DELETE | (editable ? 0 : FILE_SHARE_WRITE);
|
|
const auto result = CreateFileW(path.c_str(), GENERIC_READ, sharing,
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (result == INVALID_HANDLE_VALUE) fail("Open file");
|
|
return result;
|
|
}
|
|
|
|
uint64_t fileSize(HANDLE file)
|
|
{
|
|
LARGE_INTEGER size{};
|
|
if (!GetFileSizeEx(file, &size)) fail("Read file size");
|
|
return size.QuadPart;
|
|
}
|
|
|
|
void seek(HANDLE file, uint64_t offset)
|
|
{
|
|
LARGE_INTEGER position{};
|
|
position.QuadPart = static_cast<LONGLONG>(offset);
|
|
if (!SetFilePointerEx(file, position, nullptr, FILE_BEGIN)) fail("Seek");
|
|
}
|
|
|
|
std::string read(HANDLE file, uint64_t offset, size_t length)
|
|
{
|
|
if (!length) return {};
|
|
seek(file, offset);
|
|
std::string result(length, '\0');
|
|
DWORD count{};
|
|
if (!ReadFile(file, result.data(), static_cast<DWORD>(length), &count, nullptr)) fail("Read file");
|
|
if (count != length) throw std::runtime_error("The file ended unexpectedly.");
|
|
return result;
|
|
}
|
|
|
|
void write(HANDLE file, std::string_view bytes)
|
|
{
|
|
if (bytes.empty()) return;
|
|
DWORD written{};
|
|
if (!WriteFile(file, bytes.data(), static_cast<DWORD>(bytes.size()), &written, nullptr))
|
|
fail("Write file");
|
|
if (written != bytes.size()) throw std::runtime_error("The complete block could not be written.");
|
|
}
|
|
|
|
bool sameFile(HANDLE a, HANDLE b)
|
|
{
|
|
BY_HANDLE_FILE_INFORMATION first{}, second{};
|
|
if (!GetFileInformationByHandle(a, &first) || !GetFileInformationByHandle(b, &second))
|
|
fail("Check file identity");
|
|
return first.dwVolumeSerialNumber == second.dwVolumeSerialNumber &&
|
|
first.nFileIndexHigh == second.nFileIndexHigh && first.nFileIndexLow == second.nFileIndexLow;
|
|
}
|
|
|
|
std::filesystem::path temporaryPath(const std::filesystem::path &directory)
|
|
{
|
|
GUID id{};
|
|
wchar_t name[40]{};
|
|
if (FAILED(CoCreateGuid(&id)) || !StringFromGUID2(id, name, 40))
|
|
throw std::runtime_error("Cannot create a temporary file name.");
|
|
return directory / (std::wstring(L".kswitcher-") + name + L".tmp");
|
|
}
|
|
|
|
std::filesystem::path createTemporary(const std::filesystem::path &directory, File &file, DWORD flags)
|
|
{
|
|
auto path = temporaryPath(directory);
|
|
file.handle = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, flags, nullptr);
|
|
if (file.handle == INVALID_HANDLE_VALUE) fail("Create temporary file");
|
|
return path;
|
|
}
|
|
}
|
|
|
|
TextDocument::TextDocument(std::filesystem::path path, bool editable) : path_(std::move(path)), editable_(editable)
|
|
{
|
|
File source{openSource(path_, editable)};
|
|
if (editable)
|
|
{
|
|
std::wstring resolved(32768, L'\0');
|
|
const auto length = GetFinalPathNameByHandleW(source.handle, resolved.data(),
|
|
static_cast<DWORD>(resolved.size()), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
|
|
if (length && length < resolved.size())
|
|
{
|
|
resolved.resize(length);
|
|
path_ = resolved;
|
|
}
|
|
}
|
|
size_ = fileSize(source.handle);
|
|
const auto prefix = read(source.handle, 0, static_cast<size_t>(std::min<uint64_t>(3, size_)));
|
|
if (prefix.starts_with("\xef\xbb\xbf")) bom_ = 3;
|
|
else if (prefix.starts_with("\xff\xfe") || prefix.starts_with("\xfe\xff"))
|
|
{
|
|
encoding_ = prefix.starts_with("\xff") ? Encoding::utf16le : Encoding::utf16be;
|
|
bom_ = 2;
|
|
if (size_ % 2) throw std::runtime_error("The UTF-16 file has an incomplete code unit.");
|
|
}
|
|
source_ = source.release();
|
|
}
|
|
|
|
TextDocument::~TextDocument()
|
|
{
|
|
if (source_ != INVALID_HANDLE_VALUE) CloseHandle(source_);
|
|
if (spool_ != INVALID_HANDLE_VALUE) CloseHandle(spool_);
|
|
}
|
|
|
|
const wchar_t *TextDocument::encodingName() const
|
|
{
|
|
switch (encoding_)
|
|
{
|
|
case Encoding::utf16le: return L"UTF-16 LE";
|
|
case Encoding::utf16be: return L"UTF-16 BE";
|
|
default: return bom_ ? L"UTF-8 BOM" : L"UTF-8";
|
|
}
|
|
}
|
|
|
|
uint64_t TextDocument::pages() const
|
|
{
|
|
return std::max<uint64_t>(1, (size_ - bom_ + pageBytes - 1) / pageBytes);
|
|
}
|
|
|
|
uint64_t TextDocument::boundary(uint64_t page) const
|
|
{
|
|
if (!page) return bom_;
|
|
if (page >= pages()) return size_;
|
|
|
|
const auto offset = bom_ + page * pageBytes;
|
|
const auto unit = encoding_ == Encoding::utf8 ? 1u : 2u;
|
|
const auto sample = read(source_, offset - unit,
|
|
static_cast<size_t>(std::min<uint64_t>(4096 + unit, size_ - offset + unit)));
|
|
const auto getUnit = [&](size_t i) -> unsigned {
|
|
const auto first = static_cast<unsigned char>(sample[i]);
|
|
if (unit == 1) return first;
|
|
const auto second = static_cast<unsigned char>(sample[i + 1]);
|
|
return encoding_ == Encoding::utf16le ? first | (second << 8) : (first << 8) | second;
|
|
};
|
|
if (getUnit(0) == '\n') return offset;
|
|
for (size_t i = unit; i + unit < sample.size(); i += unit)
|
|
if (getUnit(i) == '\n') return offset + i;
|
|
|
|
size_t i = unit;
|
|
if (unit == 1)
|
|
{
|
|
// Advance past at most one UTF-8 code point, even on malformed input.
|
|
while (i < sample.size() && i <= 3 && (getUnit(i) & 0xc0) == 0x80) ++i;
|
|
}
|
|
else if (const auto value = getUnit(i); value >= 0xdc00 && value <= 0xdfff) i += unit;
|
|
if (i == unit && getUnit(0) == '\r' && getUnit(i) == '\n') i += unit;
|
|
return offset + i - unit;
|
|
}
|
|
|
|
std::string TextDocument::decode(std::string bytes) const
|
|
{
|
|
if (encoding_ == Encoding::utf8 || bytes.empty()) return bytes;
|
|
std::wstring units;
|
|
units.resize(bytes.size() / 2);
|
|
for (size_t i = 0; i < units.size(); ++i)
|
|
{
|
|
const auto a = static_cast<unsigned char>(bytes[2 * i]);
|
|
const auto b = static_cast<unsigned char>(bytes[2 * i + 1]);
|
|
units[i] = static_cast<wchar_t>(encoding_ == Encoding::utf16le ? a | (b << 8) : (a << 8) | b);
|
|
}
|
|
const auto length = static_cast<int>(units.size());
|
|
const int required = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, units.data(), length, nullptr, 0, nullptr, nullptr);
|
|
if (!required) fail("Decode UTF-16");
|
|
std::string result(required, '\0');
|
|
if (!WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, units.data(), length, result.data(), required, nullptr, nullptr))
|
|
fail("Decode UTF-16");
|
|
return result;
|
|
}
|
|
|
|
std::string TextDocument::encode(std::string_view utf8) const
|
|
{
|
|
if (encoding_ == Encoding::utf8 || utf8.empty()) return std::string(utf8);
|
|
const auto length = static_cast<int>(utf8.size());
|
|
const int required = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.data(), length, nullptr, 0);
|
|
if (!required) fail("Encode UTF-16");
|
|
std::wstring units(required, L'\0');
|
|
if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.data(), length, units.data(), required))
|
|
fail("Encode UTF-16");
|
|
std::string bytes(units.size() * 2, '\0');
|
|
const int low = encoding_ == Encoding::utf16le ? 0 : 1;
|
|
for (size_t i = 0; i < units.size(); ++i)
|
|
{
|
|
bytes[2 * i + low] = static_cast<char>(units[i] & 0xff);
|
|
bytes[2 * i + 1 - low] = static_cast<char>(units[i] >> 8);
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
std::string TextDocument::readPage(uint64_t page) const
|
|
{
|
|
if (const auto it = changes_.find(page); it != changes_.end())
|
|
return read(spool_, it->second.offset, it->second.size);
|
|
return readOriginal(page);
|
|
}
|
|
|
|
std::string TextDocument::readOriginal(uint64_t page) const
|
|
{
|
|
const auto start = boundary(page);
|
|
return decode(read(source_, start, static_cast<size_t>(boundary(page + 1) - start)));
|
|
}
|
|
|
|
bool TextDocument::modifiedWith(uint64_t page, std::string_view current) const
|
|
{
|
|
const size_t changesOnPage = changes_.contains(page) ? 1 : 0;
|
|
return changes_.size() > changesOnPage || current != readOriginal(page);
|
|
}
|
|
|
|
void TextDocument::stage(uint64_t page, std::string_view utf8)
|
|
{
|
|
if (!editable_) throw std::logic_error("The file is open for viewing.");
|
|
if (utf8.size() > editLimit) throw std::runtime_error("The edit exceeds 8 MiB on this page.");
|
|
if (utf8 == readOriginal(page))
|
|
{
|
|
changes_.erase(page);
|
|
return;
|
|
}
|
|
if (spool_ == INVALID_HANDLE_VALUE)
|
|
{
|
|
wchar_t directory[32768]{};
|
|
const auto length = GetTempPathW(static_cast<DWORD>(std::size(directory)), directory);
|
|
if (!length || length >= std::size(directory)) fail("Locate temporary directory");
|
|
File file;
|
|
createTemporary(directory, file, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE);
|
|
spool_ = file.release();
|
|
}
|
|
seek(spool_, spoolSize_);
|
|
write(spool_, utf8);
|
|
changes_.insert_or_assign(page, Change{spoolSize_, utf8.size()});
|
|
spoolSize_ += utf8.size();
|
|
}
|
|
|
|
bool TextDocument::save()
|
|
{
|
|
if (!modified()) return true;
|
|
PendingSave pending;
|
|
File output;
|
|
pending.path = createTemporary(path_.parent_path(), output, FILE_ATTRIBUTE_NORMAL);
|
|
|
|
uint64_t written{};
|
|
const auto append = [&](std::string_view bytes) {
|
|
write(output.handle, bytes);
|
|
written += bytes.size();
|
|
};
|
|
const auto copyUntil = [&](uint64_t &offset, uint64_t end) {
|
|
while (offset < end)
|
|
{
|
|
const auto block = read(source_, offset, static_cast<size_t>(std::min(pageBytes, end - offset)));
|
|
append(block);
|
|
offset += block.size();
|
|
}
|
|
};
|
|
uint64_t offset{};
|
|
for (const auto &[page, change] : changes_)
|
|
{
|
|
copyUntil(offset, boundary(page));
|
|
append(encode(read(spool_, change.offset, change.size)));
|
|
offset = boundary(page + 1);
|
|
}
|
|
copyUntil(offset, size_);
|
|
if (!FlushFileBuffers(output.handle)) fail("Flush saved file");
|
|
CloseHandle(output.release());
|
|
|
|
// A metadata-only handle survives the rename without blocking ReplaceFile's exclusive open.
|
|
File replacement{CreateFileW(pending.path.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)};
|
|
if (replacement.handle == INVALID_HANDLE_VALUE) fail("Track replacement file");
|
|
File current{openSource(path_, true)};
|
|
if (!sameFile(source_, current.handle)) throw std::runtime_error("The file was replaced outside KSwitcher.");
|
|
const auto backup = temporaryPath(path_.parent_path());
|
|
if (!ReplaceFileW(path_.c_str(), pending.path.c_str(), backup.c_str(), 0, nullptr, nullptr))
|
|
{
|
|
const auto error = GetLastError();
|
|
if (GetFileAttributesW(backup.c_str()) != INVALID_FILE_ATTRIBUTES &&
|
|
!MoveFileExW(backup.c_str(), path_.c_str(), MOVEFILE_WRITE_THROUGH))
|
|
{
|
|
const auto location = backup.u8string();
|
|
throw std::runtime_error("Could not restore the original name after a save error. The original is at " +
|
|
std::string(location.begin(), location.end()));
|
|
}
|
|
SetLastError(error);
|
|
fail("Replace original file");
|
|
}
|
|
pending.path.clear();
|
|
const auto newSource = ReOpenFile(replacement.handle, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, 0);
|
|
DeleteFileW(backup.c_str());
|
|
CloseHandle(source_);
|
|
source_ = newSource;
|
|
size_ = written;
|
|
changes_.clear();
|
|
if (spool_ != INVALID_HANDLE_VALUE) CloseHandle(std::exchange(spool_, INVALID_HANDLE_VALUE));
|
|
spoolSize_ = 0;
|
|
return source_ != INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|