FileInputPlugin.cxx 3.39 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 33 34

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

36 37
static constexpr Domain file_domain("file");

38
struct FileInputStream {
39
	InputStream base;
40 41

	int fd;
42 43

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

	~FileInputStream() {
		close(fd);
	}
55 56
};

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

65
	if (!PathTraits::IsAbsoluteFS(filename))
66
		return nullptr;
67

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

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

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

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

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

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

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

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

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

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

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

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

137
	delete fis;
Warren Dukes's avatar
Warren Dukes committed
138 139
}

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

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