Skip to content

Commit

Permalink
io/FileReader: do not copy the path
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Oct 7, 2023
1 parent 9365f68 commit 1ca5d6b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
16 changes: 8 additions & 8 deletions src/io/FileReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
{
Expand All @@ -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;
}
Expand All @@ -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()))
{
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
12 changes: 5 additions & 7 deletions src/io/FileReader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#pragma once

#include "Reader.hxx"
#include "fs/AllocatedPath.hxx"

#ifdef _WIN32
#include <fileapi.h>
Expand All @@ -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
Expand All @@ -34,17 +34,15 @@ 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)
CloseHandle(handle);
}
#else
FileReader(FileReader &&other) noexcept
:path(std::move(other.path)),
fd(std::move(other.fd)) {}
:fd(std::move(other.fd)) {}
#endif


Expand Down

0 comments on commit 1ca5d6b

Please sign in to comment.