diff options
| -rw-r--r-- | commands.c | 43 | ||||
| -rw-r--r-- | music.c | 19 | ||||
| -rw-r--r-- | music.h | 1 | 
3 files changed, 63 insertions, 0 deletions
| @@ -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");  	}  } @@ -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; @@ -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 | 
