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"
|
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
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
FileReader::FileReader(Path _path)
|
2014-08-07 18:10:23 +02:00
|
|
|
:path(_path),
|
|
|
|
handle(CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
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());
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
return FileInfo(path);
|
2015-03-03 20:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 19:39:50 +01:00
|
|
|
std::size_t
|
|
|
|
FileReader::Read(void *data, std::size_t size)
|
2014-08-07 18:10:23 +02:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
DWORD nbytes;
|
2015-12-16 11:05:33 +01:00
|
|
|
if (!ReadFile(handle, data, size, &nbytes, nullptr))
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtLastError("Failed to read from {}", path);
|
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
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
FileReader::FileReader(Path _path)
|
2018-08-21 18:16:52 +02:00
|
|
|
: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());
|
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
FileInfo info;
|
2015-03-03 20:34:02 +01:00
|
|
|
const bool success = fstat(fd.Get(), &info.st) == 0;
|
|
|
|
if (!success)
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to access {}", path);
|
2015-03-03 20:34:02 +01:00
|
|
|
|
2015-12-16 11:05:33 +01:00
|
|
|
return info;
|
2015-03-03 20:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 19:39:50 +01:00
|
|
|
std::size_t
|
|
|
|
FileReader::Read(void *data, std::size_t size)
|
2014-08-07 18:10:23 +02:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2015-03-03 17:03:21 +01:00
|
|
|
ssize_t nbytes = fd.Read(data, size);
|
2015-12-16 11:05:33 +01:00
|
|
|
if (nbytes < 0)
|
2022-11-28 18:49:35 +01:00
|
|
|
throw FmtErrno("Failed to read from {}", path);
|
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
|