2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2014-08-07 18:10:23 +02:00
|
|
|
|
|
|
|
#include "FileReader.hxx"
|
2022-11-28 18:49:35 +01:00
|
|
|
#include "lib/fmt/PathFormatter.hxx"
|
2023-10-05 17:50:12 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2015-03-03 20:34:02 +01:00
|
|
|
#include "fs/FileInfo.hxx"
|
2022-11-28 23:05:15 +01:00
|
|
|
#include "lib/fmt/SystemError.hxx"
|
2020-05-05 14:11:13 +02:00
|
|
|
#include "io/Open.hxx"
|
2014-08-07 18:10:23 +02:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2016-08-16 07:58:44 +02:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2014-08-07 18:10:23 +02:00
|
|
|
|
2023-10-05 17:50:12 +02:00
|
|
|
FileReader::FileReader(Path path)
|
|
|
|
:handle(CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
2014-08-07 18:10:23 +02:00
|
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
nullptr))
|
|
|
|
{
|
2015-12-16 11:05:33 +01:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtLastError("Failed to open {}", path);
|
2014-08-07 18:10:23 +02:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
FileInfo
|
|
|
|
FileReader::GetFileInfo() const
|
2015-03-03 20:34:02 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2023-10-07 12:30:46 +02:00
|
|
|
return FileInfo{handle};
|
2015-03-03 20:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 19:39:50 +01:00
|
|
|
std::size_t
|
2023-10-05 10:25:16 +02:00
|
|
|
FileReader::Read(std::span<std::byte> dest)
|
2014-08-07 18:10:23 +02:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
DWORD nbytes;
|
2023-10-05 10:25:16 +02:00
|
|
|
if (!ReadFile(handle, dest.data(), dest.size(), &nbytes, nullptr))
|
2023-10-05 17:50:12 +02:00
|
|
|
throw MakeLastError("Failed to read from file");
|
2014-08-07 18:10:23 +02:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
void
|
|
|
|
FileReader::Seek(off_t offset)
|
2015-03-03 14:29:36 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
auto result = SetFilePointer(handle, offset, nullptr, FILE_BEGIN);
|
2015-12-16 11:05:33 +01:00
|
|
|
if (result == INVALID_SET_FILE_POINTER)
|
|
|
|
throw MakeLastError("Failed to seek");
|
2015-03-03 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2016-02-19 18:18:25 +01:00
|
|
|
void
|
|
|
|
FileReader::Skip(off_t offset)
|
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
auto result = SetFilePointer(handle, offset, nullptr, FILE_CURRENT);
|
|
|
|
if (result == INVALID_SET_FILE_POINTER)
|
|
|
|
throw MakeLastError("Failed to seek");
|
|
|
|
}
|
|
|
|
|
2014-08-07 18:10:23 +02:00
|
|
|
#else
|
|
|
|
|
2023-10-05 17:50:12 +02:00
|
|
|
FileReader::FileReader(Path path)
|
|
|
|
:fd(OpenReadOnly(path.c_str()))
|
2014-08-07 18:10:23 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
FileInfo
|
|
|
|
FileReader::GetFileInfo() const
|
2015-03-03 20:34:02 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2023-10-07 12:37:26 +02:00
|
|
|
return FileInfo{fd};
|
2015-03-03 20:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 19:39:50 +01:00
|
|
|
std::size_t
|
2023-10-05 10:25:16 +02:00
|
|
|
FileReader::Read(std::span<std::byte> dest)
|
2014-08-07 18:10:23 +02:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2023-10-05 10:25:16 +02:00
|
|
|
ssize_t nbytes = fd.Read(dest);
|
2015-12-16 11:05:33 +01:00
|
|
|
if (nbytes < 0)
|
2023-10-05 17:50:12 +02:00
|
|
|
throw MakeErrno("Failed to read from file");
|
2014-08-07 18:10:23 +02:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
void
|
|
|
|
FileReader::Seek(off_t offset)
|
2015-03-03 14:29:36 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2015-03-03 17:03:21 +01:00
|
|
|
auto result = fd.Seek(offset);
|
2015-03-03 14:29:36 +01:00
|
|
|
const bool success = result >= 0;
|
|
|
|
if (!success)
|
2015-12-16 11:05:33 +01:00
|
|
|
throw MakeErrno("Failed to seek");
|
2015-03-03 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2016-02-19 18:18:25 +01:00
|
|
|
void
|
|
|
|
FileReader::Skip(off_t offset)
|
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
auto result = fd.Skip(offset);
|
|
|
|
const bool success = result >= 0;
|
|
|
|
if (!success)
|
|
|
|
throw MakeErrno("Failed to seek");
|
|
|
|
}
|
|
|
|
|
2014-08-07 18:10:23 +02:00
|
|
|
#endif
|