FileReader.cxx 3.58 KB
Newer Older
1
/*
2
 * Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com>
3
 *
4 5 6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
7
 *
8 9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 29 30
 */

#include "FileReader.hxx"
31
#include "fs/FileInfo.hxx"
32
#include "system/Error.hxx"
33
#include "io/Open.hxx"
34

35
#include <cassert>
36

37
#ifdef _WIN32
38

39
FileReader::FileReader(Path _path)
40 41 42 43 44
	:path(_path),
	 handle(CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
			   nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
			   nullptr))
{
45 46
	if (handle == INVALID_HANDLE_VALUE)
		throw FormatLastError("Failed to open %s", path.ToUTF8().c_str());
47 48
}

49 50
FileInfo
FileReader::GetFileInfo() const
51 52 53
{
	assert(IsDefined());

54
	return FileInfo(path);
55 56
}

57
size_t
58
FileReader::Read(void *data, size_t size)
59 60 61 62
{
	assert(IsDefined());

	DWORD nbytes;
63 64 65
	if (!ReadFile(handle, data, size, &nbytes, nullptr))
		throw FormatLastError("Failed to read from %s",
				      path.ToUTF8().c_str());
66 67 68 69

	return nbytes;
}

70 71
void
FileReader::Seek(off_t offset)
72 73 74 75
{
	assert(IsDefined());

	auto result = SetFilePointer(handle, offset, nullptr, FILE_BEGIN);
76 77
	if (result == INVALID_SET_FILE_POINTER)
		throw MakeLastError("Failed to seek");
78 79
}

80 81 82 83 84 85 86 87 88 89
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");
}

90
void
91
FileReader::Close() noexcept
92 93 94 95 96 97 98 99
{
	assert(IsDefined());

	CloseHandle(handle);
}

#else

100
FileReader::FileReader(Path _path)
101
	:path(_path), fd(OpenReadOnly(path.c_str()))
102 103 104
{
}

105 106
FileInfo
FileReader::GetFileInfo() const
107 108 109
{
	assert(IsDefined());

110
	FileInfo info;
111 112
	const bool success = fstat(fd.Get(), &info.st) == 0;
	if (!success)
113
		throw FormatErrno("Failed to access %s",
114 115
				  path.ToUTF8().c_str());

116
	return info;
117 118
}

119
size_t
120
FileReader::Read(void *data, size_t size)
121 122 123
{
	assert(IsDefined());

124
	ssize_t nbytes = fd.Read(data, size);
125 126
	if (nbytes < 0)
		throw FormatErrno("Failed to read from %s", path.ToUTF8().c_str());
127 128 129 130

	return nbytes;
}

131 132
void
FileReader::Seek(off_t offset)
133 134 135
{
	assert(IsDefined());

136
	auto result = fd.Seek(offset);
137 138
	const bool success = result >= 0;
	if (!success)
139
		throw MakeErrno("Failed to seek");
140 141
}

142 143 144 145 146 147 148 149 150 151 152
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");
}

153
void
154
FileReader::Close() noexcept
155 156 157
{
	assert(IsDefined());

158
	fd.Close();
159 160 161
}

#endif