FileInputPlugin.cxx 3.35 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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"
22
#include "InputStream.hxx"
23
#include "InputPlugin.hxx"
24 25
#include "util/Error.hxx"
#include "util/Domain.hxx"
26
#include "fs/Traits.hxx"
27
#include "system/fd_util.h"
28
#include "open.h"
29 30 31 32

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

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

36
struct FileInputStream {
37
	InputStream base;
38 39

	int fd;
40 41

	FileInputStream(const char *path, int _fd, off_t size,
42
			Mutex &mutex, Cond &cond)
43 44
		:base(input_plugin_file, path, mutex, cond),
		 fd(_fd) {
45 46 47 48 49 50 51 52
		base.size = size;
		base.seekable = true;
		base.ready = true;
	}

	~FileInputStream() {
		close(fd);
	}
53 54
};

55
static InputStream *
56
input_file_open(const char *filename,
57
		Mutex &mutex, Cond &cond,
58
		Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
59
{
60 61
	int fd, ret;
	struct stat st;
Warren Dukes's avatar
Warren Dukes committed
62

63
	if (!PathTraitsFS::IsAbsolute(filename))
64
		return nullptr;
65

66
	fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0);
67
	if (fd < 0) {
68
		if (errno != ENOENT && errno != ENOTDIR)
69 70
			error.FormatErrno("Failed to open \"%s\"",
					  filename);
71
		return nullptr;
Warren Dukes's avatar
Warren Dukes committed
72 73
	}

74
	ret = fstat(fd, &st);
75
	if (ret < 0) {
76
		error.FormatErrno("Failed to stat \"%s\"", filename);
77
		close(fd);
78
		return nullptr;
79 80
	}

81
	if (!S_ISREG(st.st_mode)) {
82
		error.Format(file_domain, "Not a regular file: %s", filename);
83
		close(fd);
84
		return nullptr;
85 86
	}

87
#ifdef POSIX_FADV_SEQUENTIAL
88
	posix_fadvise(fd, (off_t)0, st.st_size, POSIX_FADV_SEQUENTIAL);
89 90
#endif

91 92
	FileInputStream *fis = new FileInputStream(filename, fd, st.st_size,
						   mutex, cond);
93
	return &fis->base;
Warren Dukes's avatar
Warren Dukes committed
94 95
}

96
static bool
97
input_file_seek(InputStream *is, InputPlugin::offset_type offset,
98
		int whence,
99
		Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
100
{
101
	FileInputStream *fis = (FileInputStream *)is;
102

103
	offset = (InputPlugin::offset_type)lseek(fis->fd, (off_t)offset, whence);
104
	if (offset < 0) {
105
		error.SetErrno("Failed to seek");
106
		return false;
Warren Dukes's avatar
Warren Dukes committed
107 108
	}

109
	is->offset = offset;
110
	return true;
Warren Dukes's avatar
Warren Dukes committed
111 112
}

113
static size_t
114
input_file_read(InputStream *is, void *ptr, size_t size,
115
		Error &error)
Warren Dukes's avatar
Warren Dukes committed
116
{
117
	FileInputStream *fis = (FileInputStream *)is;
118
	ssize_t nbytes;
Warren Dukes's avatar
Warren Dukes committed
119

120
	nbytes = read(fis->fd, ptr, size);
121
	if (nbytes < 0) {
122
		error.SetErrno("Failed to read");
123
		return 0;
Avuton Olrich's avatar
Avuton Olrich committed
124
	}
Warren Dukes's avatar
Warren Dukes committed
125

126 127
	is->offset += nbytes;
	return (size_t)nbytes;
Warren Dukes's avatar
Warren Dukes committed
128 129
}

130
static void
131
input_file_close(InputStream *is)
Avuton Olrich's avatar
Avuton Olrich committed
132
{
133
	FileInputStream *fis = (FileInputStream *)is;
134

135
	delete fis;
Warren Dukes's avatar
Warren Dukes committed
136 137
}

138
static bool
139
input_file_eof(InputStream *is)
Avuton Olrich's avatar
Avuton Olrich committed
140
{
141
	return is->offset >= is->size;
Warren Dukes's avatar
Warren Dukes committed
142
}
143

144
const InputPlugin input_plugin_file = {
145 146 147 148 149 150 151 152 153 154 155 156
	"file",
	nullptr,
	nullptr,
	input_file_open,
	input_file_close,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	input_file_read,
	input_file_eof,
	input_file_seek,
157
};