summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-16 01:48:51 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-16 01:48:51 +0200
commit1a79de743029449f0728021157b324d8a14bef64 (patch)
tree5952cb4871b3a1a88e280e80ff103372f3cba76c
parent4fcb7baeec6d7c2f9fb9c976a66b78e80fd8622e (diff)
Implemented the get_raw command.
-rw-r--r--commands.c43
-rw-r--r--music.c19
-rw-r--r--music.h1
3 files changed, 63 insertions, 0 deletions
diff --git a/commands.c b/commands.c
index dfdce42..f15c2c6 100644
--- a/commands.c
+++ b/commands.c
@@ -3,6 +3,7 @@
#include <glib.h>
#include <string.h>
+#include <gio/gio.h>
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");
}
}
diff --git a/music.c b/music.c
index 8852835..c64a2ba 100644
--- a/music.c
+++ b/music.c
@@ -100,6 +100,25 @@ struct directory *music_find_dir(const gchar *path) {
return music_find_dir_rec(music_root, path);
}
+struct file *music_find_file(const gchar *path) {
+ gchar *dirname = g_path_get_dirname(path);
+ gchar *filename = g_path_get_basename(path);
+
+ struct directory *directory = music_find_dir(dirname);
+
+ if(directory == NULL) {
+ return NULL;
+ }
+
+ for(struct file *f = directory->files; f; f = f->next) {
+ if(g_strcmp0(filename, f->name) == 0) {
+ return f;
+ }
+ }
+
+ return NULL;
+}
+
static void music_do_free(struct directory *root) {
struct directory *node;
diff --git a/music.h b/music.h
index e93a8c1..b643c8e 100644
--- a/music.h
+++ b/music.h
@@ -24,6 +24,7 @@ gboolean music_init(const gchar *path);
gboolean music_scan(struct directory *directory);
gboolean music_scan_root();
struct directory *music_find_dir(const gchar *path);
+struct file *music_find_file(const gchar *path);
void music_free();
#endif