FileReader.cxx 3.05 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "FileReader.hxx"
22
#include "fs/FileInfo.hxx"
23
#include "system/Error.hxx"
24
#include "system/Open.hxx"
25

26 27
#include <assert.h>

28
#ifdef _WIN32
29

30
FileReader::FileReader(Path _path)
31 32 33 34 35
	:path(_path),
	 handle(CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
			   nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
			   nullptr))
{
36 37
	if (handle == INVALID_HANDLE_VALUE)
		throw FormatLastError("Failed to open %s", path.ToUTF8().c_str());
38 39
}

40 41
FileInfo
FileReader::GetFileInfo() const
42 43 44
{
	assert(IsDefined());

45
	return FileInfo(path);
46 47
}

48
size_t
49
FileReader::Read(void *data, size_t size)
50 51 52 53
{
	assert(IsDefined());

	DWORD nbytes;
54 55 56
	if (!ReadFile(handle, data, size, &nbytes, nullptr))
		throw FormatLastError("Failed to read from %s",
				      path.ToUTF8().c_str());
57 58 59 60

	return nbytes;
}

61 62
void
FileReader::Seek(off_t offset)
63 64 65 66
{
	assert(IsDefined());

	auto result = SetFilePointer(handle, offset, nullptr, FILE_BEGIN);
67 68
	if (result == INVALID_SET_FILE_POINTER)
		throw MakeLastError("Failed to seek");
69 70
}

71 72 73 74 75 76 77 78 79 80
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");
}

81
void
82
FileReader::Close() noexcept
83 84 85 86 87 88 89 90
{
	assert(IsDefined());

	CloseHandle(handle);
}

#else

91
FileReader::FileReader(Path _path)
92
	:path(_path), fd(OpenReadOnly(path.c_str()))
93 94 95
{
}

96 97
FileInfo
FileReader::GetFileInfo() const
98 99 100
{
	assert(IsDefined());

101
	FileInfo info;
102 103
	const bool success = fstat(fd.Get(), &info.st) == 0;
	if (!success)
104
		throw FormatErrno("Failed to access %s",
105 106
				  path.ToUTF8().c_str());

107
	return info;
108 109
}

110
size_t
111
FileReader::Read(void *data, size_t size)
112 113 114
{
	assert(IsDefined());

115
	ssize_t nbytes = fd.Read(data, size);
116 117
	if (nbytes < 0)
		throw FormatErrno("Failed to read from %s", path.ToUTF8().c_str());
118 119 120 121

	return nbytes;
}

122 123
void
FileReader::Seek(off_t offset)
124 125 126
{
	assert(IsDefined());

127
	auto result = fd.Seek(offset);
128 129
	const bool success = result >= 0;
	if (!success)
130
		throw MakeErrno("Failed to seek");
131 132
}

133 134 135 136 137 138 139 140 141 142 143
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");
}

144
void
145
FileReader::Close() noexcept
146 147 148
{
	assert(IsDefined());

149
	fd.Close();
150 151 152
}

#endif