FileReader.cxx 3.09 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

25 26
#include <assert.h>

27
#ifdef _WIN32
28

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

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

44
	return FileInfo(path);
45 46
}

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

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

	return nbytes;
}

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

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

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

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

	CloseHandle(handle);
}

#else

90
FileReader::FileReader(Path _path)
91
	:path(_path)
92
{
93 94
	fd.OpenReadOnly(path.c_str());
	if (!fd.IsDefined())
95
		throw FormatErrno("Failed to open %s", path.ToUTF8().c_str());
96 97
}

98 99
FileInfo
FileReader::GetFileInfo() const
100 101 102
{
	assert(IsDefined());

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

109
	return info;
110 111
}

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

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

	return nbytes;
}

124 125
void
FileReader::Seek(off_t offset)
126 127 128
{
	assert(IsDefined());

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

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

146 147 148 149 150
void
FileReader::Close()
{
	assert(IsDefined());

151
	fd.Close();
152 153 154
}

#endif