From 887f97b868886a66d7bb2fcd0e1fd6810297299d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 28 Sep 2008 20:29:40 -0700 Subject: clean up updateInit calling and error handling Move error reporting to command.c so directory.c does not deal with client error handling any more. --- src/command.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/command.c') diff --git a/src/command.c b/src/command.c index 616132bea..ce41abb64 100644 --- a/src/command.c +++ b/src/command.c @@ -799,6 +799,16 @@ static int handlePlaylistMove(int fd, mpd_unused int *permission, return print_playlist_result(fd, result); } +static int print_update_result(int fd, int ret) +{ + if (ret >= 0) { + fdprintf(fd, "updating_db: %i\n", ret); + return 0; + } + commandError(fd, ACK_ERROR_UPDATE_ALREADY, "already updating"); + return -1; +} + static int listHandleUpdate(int fd, mpd_unused int *permission, mpd_unused int argc, @@ -818,7 +828,7 @@ static int listHandleUpdate(int fd, nextCmd = getCommandEntryFromString(next->data, permission); if (cmd != nextCmd) - return updateInit(fd, pathList); + return print_update_result(fd, updateInit(pathList)); return 0; } @@ -826,12 +836,14 @@ static int listHandleUpdate(int fd, static int handleUpdate(int fd, mpd_unused int *permission, mpd_unused int argc, char *argv[]) { + List *pathList = NULL; + if (argc == 2) { - List *pathList = makeList(NULL, 1); + pathList = makeList(NULL, 1); insertInList(pathList, argv[1], NULL); - return updateInit(fd, pathList); } - return updateInit(fd, NULL); + + return print_update_result(fd, updateInit(pathList)); } static int handleNext(mpd_unused int fd, mpd_unused int *permission, -- cgit v1.2.3 From 659a543da853bcb28c22df300a93bd22dc9ae877 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 29 Sep 2008 02:48:09 -0700 Subject: update: move path sanitation up the stack to avoid extra copies Remove yet another use of our old malloc-happy linked list implementation and replace it with a simple array of strings. This also implements more eager error handling of invalid paths (still centralized in updateInit) so we can filter out bad paths before we spawn a thread. This also does its part to fix the "update" command inside list mode which lost its static variable in ada24f9a921ff95d874195acf253b5a9dd12213d (although it was broken and requires the fix in 769939b62f7557f8e7c483223d68a8b39af43e37, too). --- src/command.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'src/command.c') diff --git a/src/command.c b/src/command.c index ce41abb64..70bfa78b5 100644 --- a/src/command.c +++ b/src/command.c @@ -36,6 +36,7 @@ #include "os_compat.h" #include "player_error.h" #include "outputBuffer.h" +#include "path.h" #define COMMAND_PLAY "play" #define COMMAND_PLAYID "playid" @@ -805,7 +806,10 @@ static int print_update_result(int fd, int ret) fdprintf(fd, "updating_db: %i\n", ret); return 0; } - commandError(fd, ACK_ERROR_UPDATE_ALREADY, "already updating"); + if (ret == -2) + commandError(fd, ACK_ERROR_ARG, "invalid path"); + else + commandError(fd, ACK_ERROR_UPDATE_ALREADY, "already updating"); return -1; } @@ -815,20 +819,28 @@ static int listHandleUpdate(int fd, char *argv[], struct strnode *cmdnode, CommandEntry * cmd) { - List *pathList = makeList(NULL, 1); + static char **pathv; + static int pathc; CommandEntry *nextCmd = NULL; struct strnode *next = cmdnode->next; + int last = pathc++; - if (argc == 2) - insertInList(pathList, argv[1], NULL); - else - insertInList(pathList, "", NULL); + pathv = xrealloc(pathv, pathc * sizeof(char *)); + pathv[last] = sanitizePathDup(argc == 2 ? argv[1] : ""); if (next) nextCmd = getCommandEntryFromString(next->data, permission); - if (cmd != nextCmd) - return print_update_result(fd, updateInit(pathList)); + if (cmd != nextCmd) { + int ret = print_update_result(fd, updateInit(pathc, pathv)); + if (pathc) { + assert(pathv); + free(pathv); + pathv = NULL; + pathc = 0; + } + return ret; + } return 0; } @@ -836,14 +848,12 @@ static int listHandleUpdate(int fd, static int handleUpdate(int fd, mpd_unused int *permission, mpd_unused int argc, char *argv[]) { - List *pathList = NULL; + char *pathv[1]; - if (argc == 2) { - pathList = makeList(NULL, 1); - insertInList(pathList, argv[1], NULL); - } - - return print_update_result(fd, updateInit(pathList)); + assert(argc <= 2); + if (argc == 2) + pathv[0] = sanitizePathDup(argv[1]); + return print_update_result(fd, updateInit(argc - 1, pathv)); } static int handleNext(mpd_unused int fd, mpd_unused int *permission, -- cgit v1.2.3