FileInputPlugin.cxx 3.11 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 25
#include "util/Error.hxx"
#include "util/Domain.hxx"
26
#include "fs/Path.hxx"
27 28
#include "fs/FileInfo.hxx"
#include "fs/io/FileReader.hxx"
29
#include "system/FileDescriptor.hxx"
30 31

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

34 35
static constexpr Domain file_domain("file");

36
class FileInputStream final : public InputStream {
37
	FileReader reader;
38

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

49 50 51 52 53 54 55
	/* virtual methods from InputStream */

	bool IsEOF() override {
		return GetOffset() >= GetSize();
	}

	size_t Read(void *ptr, size_t size, Error &error) override;
56
	bool Seek(offset_type offset, Error &error) override;
57 58
};

59 60 61 62
InputStream *
OpenFileInputStream(Path path,
		    Mutex &mutex, Cond &cond,
		    Error &error)
63 64
try {
	FileReader reader(path);
Warren Dukes's avatar
Warren Dukes committed
65

66
	const FileInfo info = reader.GetFileInfo();
67

68
	if (!info.IsRegular()) {
69 70
		error.Format(file_domain, "Not a regular file: %s",
			     path.c_str());
71
		return nullptr;
72 73
	}

74
#ifdef POSIX_FADV_SEQUENTIAL
75 76
	posix_fadvise(reader.GetFD().Get(), (off_t)0, info.GetSize(),
		      POSIX_FADV_SEQUENTIAL);
77 78
#endif

79
	return new FileInputStream(path.ToUTF8().c_str(),
80
				   std::move(reader), info.GetSize(),
81
				   mutex, cond);
82
} catch (const std::exception &e) {
83
	error.Set(std::current_exception());
84
	return nullptr;
85 86 87
}

static InputStream *
88 89 90
input_file_open(gcc_unused const char *filename,
		gcc_unused Mutex &mutex, gcc_unused Cond &cond,
		gcc_unused Error &error)
91
{
92
	/* dummy method; use OpenFileInputStream() instead */
93

94
	return nullptr;
Warren Dukes's avatar
Warren Dukes committed
95 96
}

97
bool
98
FileInputStream::Seek(offset_type new_offset, Error &error)
99 100
try {
	reader.Seek((off_t)new_offset);
101
	offset = new_offset;
102
	return true;
103
} catch (const std::exception &e) {
104
	error.Set(std::current_exception());
105
	return false;
Warren Dukes's avatar
Warren Dukes committed
106 107
}

108 109
size_t
FileInputStream::Read(void *ptr, size_t read_size, Error &error)
110 111
try {
	size_t nbytes = reader.Read(ptr, read_size);
112
	offset += nbytes;
113 114
	return nbytes;
} catch (const std::exception &e) {
115
	error.Set(std::current_exception());
116
	return 0;
Warren Dukes's avatar
Warren Dukes committed
117 118
}

119
const InputPlugin input_plugin_file = {
120 121 122 123
	"file",
	nullptr,
	nullptr,
	input_file_open,
124
};