system/FmtError: new library

Replaces the Format*() functions in system/Error.hxx.
This commit is contained in:
Max Kellermann
2022-11-28 18:49:35 +01:00
parent 124e75c286
commit 96ae659fdf
25 changed files with 305 additions and 186 deletions

View File

@@ -28,7 +28,8 @@
*/
#include "FileOutputStream.hxx"
#include "system/Error.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "system/FmtError.hxx"
#include "util/StringFormat.hxx"
#ifdef _WIN32
@@ -106,8 +107,7 @@ FileOutputStream::OpenCreate(bool visible)
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
nullptr);
if (!IsDefined())
throw FormatLastError("Failed to create %s",
path.ToUTF8().c_str());
throw FmtLastError("Failed to create {}", path);
}
inline void
@@ -118,14 +118,12 @@ FileOutputStream::OpenAppend(bool create)
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH,
nullptr);
if (!IsDefined())
throw FormatLastError("Failed to append to %s",
path.ToUTF8().c_str());
throw FmtLastError("Failed to append to {}", path);
if (!SeekEOF()) {
auto code = GetLastError();
Close();
throw FormatLastError(code, "Failed seek end-of-file of %s",
path.ToUTF8().c_str());
throw FmtLastError(code, "Failed seek end-of-file of {}", path);
}
}
@@ -148,12 +146,12 @@ FileOutputStream::Write(const void *data, size_t size)
DWORD nbytes;
if (!WriteFile(handle, data, size, &nbytes, nullptr))
throw FormatLastError("Failed to write to %s",
GetPath().c_str());
throw FmtLastError("Failed to write to {}", GetPath());
if (size_t(nbytes) != size)
throw FormatLastError(ERROR_DISK_FULL, "Failed to write to %s",
GetPath().c_str());
throw FmtLastError(DWORD{ERROR_DISK_FULL},
"Failed to write to {}",
GetPath());
}
void
@@ -162,7 +160,7 @@ FileOutputStream::Sync()
assert(IsDefined());
if (!FlushFileBuffers(handle))
throw FormatLastError("Failed to sync %s", GetPath().c_str());
throw FmtLastError("Failed to sync {}", GetPath());
}
void
@@ -249,8 +247,7 @@ FileOutputStream::OpenCreate([[maybe_unused]] bool visible)
GetPath().c_str(),
O_WRONLY|O_CREAT|O_TRUNC,
0666))
throw FormatErrno("Failed to create %s",
GetPath().c_str());
throw FmtErrno("Failed to create {}", GetPath());
}
inline void
@@ -265,8 +262,7 @@ FileOutputStream::OpenAppend(bool create)
directory_fd,
#endif
path.c_str(), flags))
throw FormatErrno("Failed to append to %s",
path.c_str());
throw FmtErrno("Failed to append to {}", path);
}
uint64_t
@@ -282,10 +278,9 @@ FileOutputStream::Write(const void *data, size_t size)
ssize_t nbytes = fd.Write(data, size);
if (nbytes < 0)
throw FormatErrno("Failed to write to %s", GetPath().c_str());
throw FmtErrno("Failed to write to {}", GetPath());
else if ((size_t)nbytes < size)
throw FormatErrno(ENOSPC, "Failed to write to %s",
GetPath().c_str());
throw FmtErrno(ENOSPC, "Failed to write to {}", GetPath());
}
void
@@ -299,7 +294,7 @@ FileOutputStream::Sync()
const bool success = fsync(fd.Get()) == 0;
#endif
if (!success)
throw FormatErrno("Failed to sync %s", GetPath().c_str());
throw FmtErrno("Failed to sync {}", GetPath());
}
void
@@ -316,13 +311,12 @@ try {
StringFormat<64>("/proc/self/fd/%d", fd.Get()),
directory_fd.Get(), path.c_str(),
AT_SYMLINK_FOLLOW) < 0)
throw FormatErrno("Failed to commit %s",
path.c_str());
throw FmtErrno("Failed to commit {}", path);
}
#endif
if (!Close()) {
throw FormatErrno("Failed to commit %s", path.c_str());
throw FmtErrno("Failed to commit {}", path);
}
if (tmp_path != nullptr)