file_input_plugin.c 3.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 "input/file_input_plugin.h"
22
#include "input_plugin.h"
23
#include "fd_util.h"
24
#include "open.h"
25 26 27 28 29 30

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

32 33 34
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "input_file"

35 36 37 38 39 40
struct file_input_stream {
	struct input_stream base;

	int fd;
};

41 42 43 44 45 46
static inline GQuark
file_quark(void)
{
	return g_quark_from_static_string("file");
}

47 48
static struct input_stream *
input_file_open(const char *filename, GError **error_r)
Avuton Olrich's avatar
Avuton Olrich committed
49
{
50 51
	int fd, ret;
	struct stat st;
52
	struct file_input_stream *fis;
Warren Dukes's avatar
Warren Dukes committed
53

54
	if (!g_path_is_absolute(filename))
55 56
		return false;

57
	fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0);
58
	if (fd < 0) {
59 60 61 62
		if (errno != ENOENT && errno != ENOTDIR)
			g_set_error(error_r, file_quark(), errno,
				    "Failed to open \"%s\": %s",
				    filename, g_strerror(errno));
63
		return false;
Warren Dukes's avatar
Warren Dukes committed
64 65
	}

66
	ret = fstat(fd, &st);
67
	if (ret < 0) {
68 69 70
		g_set_error(error_r, file_quark(), errno,
			    "Failed to stat \"%s\": %s",
			    filename, g_strerror(errno));
71 72 73 74
		close(fd);
		return false;
	}

75
	if (!S_ISREG(st.st_mode)) {
76 77
		g_set_error(error_r, file_quark(), 0,
			    "Not a regular file: %s", filename);
78 79 80 81
		close(fd);
		return false;
	}

82
#ifdef POSIX_FADV_SEQUENTIAL
83
	posix_fadvise(fd, (off_t)0, st.st_size, POSIX_FADV_SEQUENTIAL);
84 85
#endif

86
	fis = g_new(struct file_input_stream, 1);
87
	input_stream_init(&fis->base, &input_plugin_file, filename);
Max Kellermann's avatar
Max Kellermann committed
88

89 90 91 92 93 94 95
	fis->base.size = st.st_size;
	fis->base.seekable = true;
	fis->base.ready = true;

	fis->fd = fd;

	return &fis->base;
Warren Dukes's avatar
Warren Dukes committed
96 97
}

98
static bool
99 100
input_file_seek(struct input_stream *is, goffset offset, int whence,
		GError **error_r)
Avuton Olrich's avatar
Avuton Olrich committed
101
{
102
	struct file_input_stream *fis = (struct file_input_stream *)is;
103

104
	offset = (goffset)lseek(fis->fd, (off_t)offset, whence);
105
	if (offset < 0) {
106 107
		g_set_error(error_r, file_quark(), errno,
			    "Failed to seek: %s", g_strerror(errno));
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 117
input_file_read(struct input_stream *is, void *ptr, size_t size,
		GError **error_r)
Warren Dukes's avatar
Warren Dukes committed
118
{
119
	struct file_input_stream *fis = (struct file_input_stream *)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 125
		g_set_error(error_r, file_quark(), errno,
			    "Failed to read: %s", g_strerror(errno));
126
		return 0;
Avuton Olrich's avatar
Avuton Olrich committed
127
	}
Warren Dukes's avatar
Warren Dukes committed
128

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

133
static void
134
input_file_close(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
135
{
136
	struct file_input_stream *fis = (struct file_input_stream *)is;
137

138
	close(fis->fd);
139
	input_stream_deinit(&fis->base);
140
	g_free(fis);
Warren Dukes's avatar
Warren Dukes committed
141 142
}

143
static bool
144
input_file_eof(struct input_stream *is)
Avuton Olrich's avatar
Avuton Olrich committed
145
{
146
	return is->offset >= is->size;
Warren Dukes's avatar
Warren Dukes committed
147
}
148

149
const struct input_plugin input_plugin_file = {
150
	.name = "file",
151 152 153 154 155 156
	.open = input_file_open,
	.close = input_file_close,
	.read = input_file_read,
	.eof = input_file_eof,
	.seek = input_file_seek,
};