Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/global/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#ifdef Q_OS_WIN
#include "WinSock.h"

#include <io.h>
#include <windows.h>
#endif

namespace io {
Expand Down Expand Up @@ -61,7 +64,9 @@ bool fsync(QFile &file) CAN_THROW
{
const int handle = file.handle();
#ifdef Q_OS_WIN
return false;
if (::FlushFileBuffers(reinterpret_cast<HANDLE>(::_get_osfhandle(handle))) == 0) {
throw IOException::withErrorNumber(static_cast<int>(::GetLastError()));
}
#elif defined(Q_OS_MAC)
if (::fcntl(handle, F_FULLFSYNC) == -1) {
throw IOException::withCurrentErrno();
Expand All @@ -74,6 +79,39 @@ bool fsync(QFile &file) CAN_THROW
return true;
}

void rename(const QString &from, const QString &to) CAN_THROW
{
#ifdef Q_OS_WIN
const std::wstring fromW = from.toStdWString();
const std::wstring toW = to.toStdWString();
if (::ReplaceFileW(toW.c_str(),
fromW.c_str(),
nullptr,
REPLACEFILE_IGNORE_MERGE_ERRORS,
nullptr,
nullptr)
== 0) {
const auto err = ::GetLastError();
if (err == ERROR_FILE_NOT_FOUND) {
if (::MoveFileExW(fromW.c_str(),
toW.c_str(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)
== 0) {
throw IOException::withErrorNumber(static_cast<int>(::GetLastError()));
}
} else {
throw IOException::withErrorNumber(static_cast<int>(err));
}
}
#else
const auto fromEncoded = QFile::encodeName(from);
const auto toEncoded = QFile::encodeName(to);
if (::rename(fromEncoded.data(), toEncoded.data()) == -1) {
throw IOException::withCurrentErrno();
}
#endif
}

IOResultEnum fsyncNoexcept(QFile &file) noexcept
{
try {
Expand Down
2 changes: 2 additions & 0 deletions src/global/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ static_assert(sizeof(ErrorNumberMessage) == 1024);

NODISCARD extern bool fsync(QFile &) CAN_THROW;

extern void rename(const QString &from, const QString &to) CAN_THROW;

NODISCARD extern IOResultEnum fsyncNoexcept(QFile &) noexcept;

NODISCARD extern bool tuneKeepAlive(qintptr socketDescriptor,
Expand Down
15 changes: 4 additions & 11 deletions src/mapstorage/filesaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,18 @@

#include <QIODevice>

static constexpr const bool USE_TMP_SUFFIX = CURRENT_PLATFORM != PlatformEnum::Windows;

static const char *const TMP_FILE_SUFFIX = ".tmp";
NODISCARD static auto maybe_add_suffix(const QString &filename)
{
return USE_TMP_SUFFIX ? (filename + TMP_FILE_SUFFIX) : filename;
return filename + TMP_FILE_SUFFIX;
}

static void remove_tmp_suffix(const QString &filename) CAN_THROW
{
if (!USE_TMP_SUFFIX) {
return;
}
const QString from = filename + TMP_FILE_SUFFIX;
const QString to = filename;

const auto from = QFile::encodeName(filename + TMP_FILE_SUFFIX);
const auto to = QFile::encodeName(filename);
if (::rename(from.data(), to.data()) == -1) {
throw io::IOException::withCurrentErrno();
}
io::rename(from, to);
}

FileSaver::~FileSaver()
Expand Down
Loading