FileInputPlugin.cxx 2.67 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h" /* must be first for large file support */
21
#include "FileInputPlugin.hxx"
Max Kellermann's avatar
Max Kellermann committed
22 23
#include "../InputStream.hxx"
#include "../InputPlugin.hxx"
24
#include "fs/Path.hxx"
25 26
#include "fs/FileInfo.hxx"
#include "fs/io/FileReader.hxx"
27
#include "system/FileDescriptor.hxx"
28
#include "util/RuntimeError.hxx"
29 30

#include <sys/stat.h>
31
#include <fcntl.h>
Warren Dukes's avatar
Warren Dukes committed
32

33
class FileInputStream final : public InputStream {
34
	FileReader reader;
35

36
public:
37
	FileInputStream(const char *path, FileReader &&_reader, off_t _size,
38
			Mutex &_mutex, Cond &_cond)
39
		:InputStream(path, _mutex, _cond),
40
		 reader(std::move(_reader)) {
41 42 43
		size = _size;
		seekable = true;
		SetReady();
44 45
	}

46 47
	/* virtual methods from InputStream */

48
	bool IsEOF() noexcept override {
49 50 51
		return GetOffset() >= GetSize();
	}

52 53
	size_t Read(void *ptr, size_t size) override;
	void Seek(offset_type offset) override;
54 55
};

56
InputStreamPtr
57
OpenFileInputStream(Path path,
58 59
		    Mutex &mutex, Cond &cond)
{
60
	FileReader reader(path);
Warren Dukes's avatar
Warren Dukes committed
61

62
	const FileInfo info = reader.GetFileInfo();
63

64 65 66
	if (!info.IsRegular())
		throw FormatRuntimeError("Not a regular file: %s",
					 path.c_str());
67

68
#ifdef POSIX_FADV_SEQUENTIAL
69 70
	posix_fadvise(reader.GetFD().Get(), (off_t)0, info.GetSize(),
		      POSIX_FADV_SEQUENTIAL);
71 72
#endif

73 74 75
	return InputStreamPtr(new FileInputStream(path.ToUTF8().c_str(),
						  std::move(reader), info.GetSize(),
						  mutex, cond));
76 77 78
}

static InputStream *
79
input_file_open(gcc_unused const char *filename,
80
		gcc_unused Mutex &mutex, gcc_unused Cond &cond)
81
{
82
	/* dummy method; use OpenFileInputStream() instead */
83

84
	return nullptr;
Warren Dukes's avatar
Warren Dukes committed
85 86
}

87 88 89
void
FileInputStream::Seek(offset_type new_offset)
{
90
	reader.Seek((off_t)new_offset);
91
	offset = new_offset;
Warren Dukes's avatar
Warren Dukes committed
92 93
}

94
size_t
95 96
FileInputStream::Read(void *ptr, size_t read_size)
{
97
	size_t nbytes = reader.Read(ptr, read_size);
98
	offset += nbytes;
99
	return nbytes;
Warren Dukes's avatar
Warren Dukes committed
100 101
}

102
const InputPlugin input_plugin_file = {
103 104 105 106
	"file",
	nullptr,
	nullptr,
	input_file_open,
107
};