FileReader.cxx 3.07 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 25 26

#ifdef WIN32

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

37 38
FileInfo
FileReader::GetFileInfo() const
39 40 41
{
	assert(IsDefined());

42
	return FileInfo(path);
43 44
}

45
size_t
46
FileReader::Read(void *data, size_t size)
47 48 49 50
{
	assert(IsDefined());

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

	return nbytes;
}

58 59
void
FileReader::Seek(off_t offset)
60 61 62 63
{
	assert(IsDefined());

	auto result = SetFilePointer(handle, offset, nullptr, FILE_BEGIN);
64 65
	if (result == INVALID_SET_FILE_POINTER)
		throw MakeLastError("Failed to seek");
66 67
}

68 69 70 71 72 73 74 75 76 77
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");
}

78 79 80 81 82 83 84 85 86 87
void
FileReader::Close()
{
	assert(IsDefined());

	CloseHandle(handle);
}

#else

88
FileReader::FileReader(Path _path)
89
	:path(_path)
90
{
91 92
	fd.OpenReadOnly(path.c_str());
	if (!fd.IsDefined())
93
		throw FormatErrno("Failed to open %s", path.ToUTF8().c_str());
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 145 146 147 148
void
FileReader::Close()
{
	assert(IsDefined());

149
	fd.Close();
150 151 152
}

#endif