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)

View File

@@ -28,8 +28,9 @@
*/
#include "FileReader.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "fs/FileInfo.hxx"
#include "system/Error.hxx"
#include "system/FmtError.hxx"
#include "io/Open.hxx"
#include <cassert>
@@ -43,7 +44,7 @@ FileReader::FileReader(Path _path)
nullptr))
{
if (handle == INVALID_HANDLE_VALUE)
throw FormatLastError("Failed to open %s", path.ToUTF8().c_str());
throw FmtLastError("Failed to open {}", path);
}
FileInfo
@@ -61,8 +62,7 @@ FileReader::Read(void *data, std::size_t size)
DWORD nbytes;
if (!ReadFile(handle, data, size, &nbytes, nullptr))
throw FormatLastError("Failed to read from %s",
path.ToUTF8().c_str());
throw FmtLastError("Failed to read from %s", path);
return nbytes;
}
@@ -110,8 +110,7 @@ FileReader::GetFileInfo() const
FileInfo info;
const bool success = fstat(fd.Get(), &info.st) == 0;
if (!success)
throw FormatErrno("Failed to access %s",
path.ToUTF8().c_str());
throw FmtErrno("Failed to access {}", path);
return info;
}
@@ -123,7 +122,7 @@ FileReader::Read(void *data, std::size_t size)
ssize_t nbytes = fd.Read(data, size);
if (nbytes < 0)
throw FormatErrno("Failed to read from %s", path.ToUTF8().c_str());
throw FmtErrno("Failed to read from {}", path);
return nbytes;
}

View File

@@ -29,7 +29,7 @@
#include "Open.hxx"
#include "UniqueFileDescriptor.hxx"
#include "system/Error.hxx"
#include "system/FmtError.hxx"
#include <fcntl.h>
@@ -38,7 +38,7 @@ OpenReadOnly(const char *path, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(path, O_RDONLY|flags))
throw FormatErrno("Failed to open '%s'", path);
throw FmtErrno("Failed to open '{}'", path);
return fd;
}
@@ -48,7 +48,7 @@ OpenWriteOnly(const char *path, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(path, O_WRONLY|flags))
throw FormatErrno("Failed to open '%s'", path);
throw FmtErrno("Failed to open '{}'", path);
return fd;
}
@@ -60,7 +60,7 @@ OpenDirectory(const char *path, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(path, O_DIRECTORY|O_RDONLY|flags))
throw FormatErrno("Failed to open '%s'", path);
throw FmtErrno("Failed to open '{}'", path);
return fd;
}
@@ -74,7 +74,7 @@ OpenPath(const char *path, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(path, O_PATH|flags))
throw FormatErrno("Failed to open '%s'", path);
throw FmtErrno("Failed to open '{}'", path);
return fd;
}
@@ -84,7 +84,7 @@ OpenPath(FileDescriptor directory, const char *name, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(directory, name, O_PATH|flags))
throw FormatErrno("Failed to open '%s'", name);
throw FmtErrno("Failed to open '{}'", name);
return fd;
}
@@ -94,7 +94,7 @@ OpenReadOnly(FileDescriptor directory, const char *name, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(directory, name, O_RDONLY|flags))
throw FormatErrno("Failed to open '%s'", name);
throw FmtErrno("Failed to open '{}'", name);
return fd;
}
@@ -104,7 +104,7 @@ OpenWriteOnly(FileDescriptor directory, const char *name, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(directory, name, O_WRONLY|flags))
throw FormatErrno("Failed to open '%s'", name);
throw FmtErrno("Failed to open '{}'", name);
return fd;
}
@@ -114,7 +114,7 @@ OpenDirectory(FileDescriptor directory, const char *name, int flags)
{
UniqueFileDescriptor fd;
if (!fd.Open(directory, name, O_DIRECTORY|O_RDONLY|flags))
throw FormatErrno("Failed to open '%s'", name);
throw FmtErrno("Failed to open '{}'", name);
return fd;
}