io/FileReader: do not copy the path

This commit is contained in:
Max Kellermann 2023-10-05 17:50:12 +02:00
parent 9365f68454
commit 1ca5d6baa6
2 changed files with 13 additions and 15 deletions

View File

@ -3,6 +3,7 @@
#include "FileReader.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/FileInfo.hxx"
#include "lib/fmt/SystemError.hxx"
#include "io/Open.hxx"
@ -11,9 +12,8 @@
#ifdef _WIN32
FileReader::FileReader(Path _path)
:path(_path),
handle(CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
FileReader::FileReader(Path path)
:handle(CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr))
{
@ -36,7 +36,7 @@ FileReader::Read(std::span<std::byte> dest)
DWORD nbytes;
if (!ReadFile(handle, dest.data(), dest.size(), &nbytes, nullptr))
throw FmtLastError("Failed to read from {}", path);
throw MakeLastError("Failed to read from file");
return nbytes;
}
@ -63,8 +63,8 @@ FileReader::Skip(off_t offset)
#else
FileReader::FileReader(Path _path)
:path(_path), fd(OpenReadOnly(path.c_str()))
FileReader::FileReader(Path path)
:fd(OpenReadOnly(path.c_str()))
{
}
@ -76,7 +76,7 @@ FileReader::GetFileInfo() const
FileInfo info;
const bool success = fstat(fd.Get(), &info.st) == 0;
if (!success)
throw FmtErrno("Failed to access {}", path);
throw MakeErrno("Failed to access file");
return info;
}
@ -88,7 +88,7 @@ FileReader::Read(std::span<std::byte> dest)
ssize_t nbytes = fd.Read(dest);
if (nbytes < 0)
throw FmtErrno("Failed to read from {}", path);
throw MakeErrno("Failed to read from file");
return nbytes;
}

View File

@ -4,7 +4,6 @@
#pragma once
#include "Reader.hxx"
#include "fs/AllocatedPath.hxx"
#ifdef _WIN32
#include <fileapi.h>
@ -16,13 +15,14 @@
#endif
#include <cstdint>
#include <utility> // for std::exchange()
#include <sys/types.h> // for off_t
class Path;
class FileInfo;
class FileReader final : public Reader {
AllocatedPath path;
#ifdef _WIN32
HANDLE handle;
#else
@ -34,8 +34,7 @@ public:
#ifdef _WIN32
FileReader(FileReader &&other) noexcept
:path(std::move(other.path)),
handle(std::exchange(other.handle, INVALID_HANDLE_VALUE)) {}
:handle(std::exchange(other.handle, INVALID_HANDLE_VALUE)) {}
~FileReader() noexcept {
if (handle != INVALID_HANDLE_VALUE)
@ -43,8 +42,7 @@ public:
}
#else
FileReader(FileReader &&other) noexcept
:path(std::move(other.path)),
fd(std::move(other.fd)) {}
:fd(std::move(other.fd)) {}
#endif