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

View File

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