From 1a79de743029449f0728021157b324d8a14bef64 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 16 Aug 2010 01:48:51 +0200 Subject: Implemented the get_raw command. --- commands.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'commands.c') diff --git a/commands.c b/commands.c index dfdce42..f15c2c6 100644 --- a/commands.c +++ b/commands.c @@ -3,6 +3,7 @@ #include #include +#include static void commands_list(GSocketConnection *connection, const gchar *cmd) { gchar **data = g_strsplit(cmd, " ", 2); @@ -12,6 +13,7 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) { g_assert(data[0] != NULL && data[1] != NULL); gchar *dirname = g_strdup(data[1]); + g_strfreev(data); struct directory *directory = music_find_dir(dirname); g_assert(directory != NULL); @@ -31,9 +33,50 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) { } } +static void commands_get_raw(GSocketConnection *connection, const gchar *cmd) { + GError *error = NULL; + gchar **data = g_strsplit(cmd, " ", 2); + g_assert(data[0] != NULL && data[1] != NULL); + + gchar *path = g_strdup(data[1]); + g_strfreev(data); + + struct file *f = music_find_file(path); + + if(f == NULL) { + g_error("couldn't find %s", path); + } + + GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection); + GFile *file = g_file_new_for_path(path); + GFileInputStream *is = g_file_read(file, NULL, &error); + + if(is == NULL) { + g_error(error->message); + } + + gssize size = g_output_stream_splice(os, (GInputStream*)is, + G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE || + G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, + NULL, &error); + if(size == -1) { + g_error(error->message); + } + g_debug("wrote %lu bytes of file data", size); + + g_object_unref(is); + g_object_unref(file); + + g_free(path); +} + void commands_handle(GSocketConnection *connection, const gchar *cmd) { g_debug("handling command string %s", cmd); if(g_ascii_strncasecmp(cmd, "/list", 5) == 0) { commands_list(connection, cmd); + } else if(g_ascii_strncasecmp(cmd, "/get_raw", 8) == 0) { + commands_get_raw(connection, cmd); + } else { + g_warning("no command handlers found"); } } -- cgit v1.2.3