summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-27 23:40:50 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-27 23:40:50 +0200
commite79ffd8ffa3cfccd0e8f7fb091b483b389aa9c63 (patch)
treebfe61b152c1b8788452b7874d926da152e20e35c
parentf8d1c20542111b9f359f4362638423ab23ed81c5 (diff)
list now accept listr which lists files at a remote host.
-rw-r--r--commands.c90
-rw-r--r--server_communication.c8
-rw-r--r--server_communication.h1
3 files changed, 78 insertions, 21 deletions
diff --git a/commands.c b/commands.c
index 98d775c..4108cff 100644
--- a/commands.c
+++ b/commands.c
@@ -9,30 +9,13 @@ GQuark commands_quark() {
return g_quark_from_static_string("commands");
}
-static void commands_list(GSocketConnection *connection, const gchar *cmd, GError **error) {
- gchar **data = g_strsplit(cmd, " ", 2);
- for(gint i = 0; data[i]; i++) {
- g_debug("\tdata[%d] = %s", i, data[i]);
- }
- g_assert(data[0] != NULL);
-
- gchar *dirname;
- if(data[1] != NULL) {
- dirname = g_strdup(data[1]);
- } else {
- dirname = "/";
- }
-
- g_strfreev(data);
-
+static void list_local(GString *string, gchar *dirname, GError **error) {
struct directory *directory = music_find_dir(dirname);
if(directory == NULL) {
*error = g_error_new(commands_quark(), 0, "error: couldn't find directory %s\n", dirname);
return;
}
- GString *string = g_string_new(NULL);
-
for(GSList *node = directory->sub; node; node = g_slist_next(node)) {
struct directory *d = node->data;
gchar *name = g_path_get_basename(d->path);
@@ -44,13 +27,78 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd, GErro
struct file *f = node->data;
g_string_append_printf(string, "%s\n", f->name);
}
+}
- GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
- if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
- error) == FALSE) {
+static void list_remote(GString *string, gchar *remotehost, gchar *dirname, GError **error) {
+ struct server *server = NULL;
+
+ for(GSList *node = servers; node; node = g_slist_next(node)) {
+ struct server *temp = node->data;
+ if(g_strcasecmp(temp->host, remotehost) == 0) {
+ server = temp;
+ break;
+ }
+ }
+
+ if(server == NULL) {
+ *error = g_error_new(commands_quark(), 0, "couldn't find remote host %s", remotehost);
return;
}
+ gchar **data = server_list(server, dirname);
+ g_debug("got %d results from %s", g_strv_length(data), remotehost);
+
+ for(gint i = 0; i < g_strv_length(data); i++) {
+ if(strlen(data[i]) == 0) {
+ break;
+ }
+ g_string_append_printf(string, "%s:%s\n", server->host, data[i]);
+ }
+
+ g_strfreev(data);
+}
+
+static void commands_list(GSocketConnection *connection, const gchar *cmd, GError **error) {
+ gboolean remote = g_strncasecmp(cmd, "listr", 5) == 0 ? TRUE : FALSE;
+
+ gchar **data = g_strsplit(cmd, " ", remote == TRUE ? 3 : 2);
+
+ if(remote == TRUE && data[1] == NULL) {
+ *error = g_error_new(commands_quark(), 0, "syntax: list[r host] [directory]");
+ g_strfreev(data);
+ return;
+ }
+
+ gchar *dirname, *remotehost;
+ if(remote == TRUE) {
+ remotehost = g_strdup(data[1]);
+ dirname = g_strdup(data[2]);
+ } else {
+ dirname = g_strdup(data[1]);
+ }
+
+ g_strfreev(data);
+
+ if(dirname == NULL) {
+ dirname = "/";
+ }
+
+ GString *string = g_string_new(NULL);
+
+ if(remote == TRUE) {
+ list_remote(string, remotehost, dirname, error);
+ } else {
+ list_local(string, dirname, error);
+ }
+
+ if(*error != NULL) {
+ g_string_free(string, TRUE);
+ return;
+ }
+
+ GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
+ g_output_stream_write_all(os, string->str, string->len, NULL, NULL, error);
+
g_string_free(string, TRUE);
}
diff --git a/server_communication.c b/server_communication.c
index 448ebbf..cc37cde 100644
--- a/server_communication.c
+++ b/server_communication.c
@@ -102,3 +102,11 @@ gchar **server_find(struct server *server, const gchar *type, const gchar *str)
return data;
}
+
+gchar **server_list(struct server *server, const gchar *directory) {
+ gchar *cmd = g_strdup_printf("list %s\nexit\n", directory);
+ gchar **data = server_get_stringlist(server, cmd);
+ g_free(cmd);
+
+ return data;
+}
diff --git a/server_communication.h b/server_communication.h
index b154701..7d7bc7c 100644
--- a/server_communication.h
+++ b/server_communication.h
@@ -7,5 +7,6 @@
gboolean server_ping(struct server *server);
gchar **server_find(struct server *server, const gchar *type, const gchar *str);
+gchar **server_list(struct server *server, const gchar *directory);
#endif