diff options
Diffstat (limited to 'src/input/file_input_plugin.c')
-rw-r--r-- | src/input/file_input_plugin.c | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/src/input/file_input_plugin.c b/src/input/file_input_plugin.c index 92da42290..1c5813f88 100644 --- a/src/input/file_input_plugin.c +++ b/src/input/file_input_plugin.c @@ -32,18 +32,24 @@ #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "input_file" +struct file_input_stream { + struct input_stream base; + + int fd; +}; + static inline GQuark file_quark(void) { return g_quark_from_static_string("file"); } -static bool -input_file_open(struct input_stream *is, const char *filename, - GError **error_r) +static struct input_stream * +input_file_open(const char *filename, GError **error_r) { int fd, ret; struct stat st; + struct file_input_stream *fis; if (!g_path_is_absolute(filename)) return false; @@ -57,8 +63,6 @@ input_file_open(struct input_stream *is, const char *filename, return false; } - is->seekable = true; - ret = fstat(fd, &st); if (ret < 0) { g_set_error(error_r, file_quark(), errno, @@ -75,26 +79,29 @@ input_file_open(struct input_stream *is, const char *filename, return false; } - is->size = st.st_size; - #ifdef POSIX_FADV_SEQUENTIAL - posix_fadvise(fd, (off_t)0, is->size, POSIX_FADV_SEQUENTIAL); + posix_fadvise(fd, (off_t)0, st.st_size, POSIX_FADV_SEQUENTIAL); #endif - is->plugin = &input_plugin_file; - is->data = GINT_TO_POINTER(fd); - is->ready = true; + fis = g_new(struct file_input_stream, 1); + input_stream_init(&fis->base, &input_plugin_file); - return true; + fis->base.size = st.st_size; + fis->base.seekable = true; + fis->base.ready = true; + + fis->fd = fd; + + return &fis->base; } static bool input_file_seek(struct input_stream *is, goffset offset, int whence, GError **error_r) { - int fd = GPOINTER_TO_INT(is->data); + struct file_input_stream *fis = (struct file_input_stream *)is; - offset = (goffset)lseek(fd, (off_t)offset, whence); + offset = (goffset)lseek(fis->fd, (off_t)offset, whence); if (offset < 0) { g_set_error(error_r, file_quark(), errno, "Failed to seek: %s", g_strerror(errno)); @@ -109,10 +116,10 @@ static size_t input_file_read(struct input_stream *is, void *ptr, size_t size, GError **error_r) { - int fd = GPOINTER_TO_INT(is->data); + struct file_input_stream *fis = (struct file_input_stream *)is; ssize_t nbytes; - nbytes = read(fd, ptr, size); + nbytes = read(fis->fd, ptr, size); if (nbytes < 0) { g_set_error(error_r, file_quark(), errno, "Failed to read: %s", g_strerror(errno)); @@ -126,9 +133,10 @@ input_file_read(struct input_stream *is, void *ptr, size_t size, static void input_file_close(struct input_stream *is) { - int fd = GPOINTER_TO_INT(is->data); + struct file_input_stream *fis = (struct file_input_stream *)is; - close(fd); + close(fis->fd); + g_free(fis); } static bool |