From a8b225f947fc2d5e464222ea9b535c49fbce9ac9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 7 Sep 2008 13:44:12 +0200 Subject: playlist: don't pass "fd" to storedPlaylist.c functions Return an "enum playlist_result" value instead of calling commandError() in storedPlaylist.c. --- src/command.c | 31 +++++++--- src/dbUtils.c | 7 +-- src/playlist.c | 50 ++++++---------- src/playlist.h | 8 +-- src/storedPlaylist.c | 159 +++++++++++++++++++++------------------------------ src/storedPlaylist.h | 19 ++++-- 6 files changed, 124 insertions(+), 150 deletions(-) (limited to 'src') diff --git a/src/command.c b/src/command.c index e9dc83ccb..6bdfba58b 100644 --- a/src/command.c +++ b/src/command.c @@ -531,7 +531,10 @@ static int handleSave(int fd, mpd_unused int *permission, static int handleLoad(int fd, mpd_unused int *permission, mpd_unused int argc, char *argv[]) { - return loadPlaylist(fd, argv[1]); + enum playlist_result result; + + result = loadPlaylist(fd, argv[1]); + return print_playlist_result(fd, result); } static int handleListPlaylist(int fd, mpd_unused int *permission, @@ -575,7 +578,10 @@ static int handleRm(int fd, mpd_unused int *permission, static int handleRename(int fd, mpd_unused int *permission, mpd_unused int argc, char *argv[]) { - return renameStoredPlaylist(fd, argv[1], argv[2]); + enum playlist_result result; + + result = renameStoredPlaylist(argv[1], argv[2]); + return print_playlist_result(fd, result); } static int handlePlaylistChanges(int fd, mpd_unused int *permission, @@ -734,11 +740,13 @@ static int handlePlaylistDelete(int fd, mpd_unused int *permission, mpd_unused int argc, char *argv[]) { char *playlist = argv[1]; int from; + enum playlist_result result; if (check_int(fd, &from, argv[2], check_integer, argv[2]) < 0) return -1; - return removeOneSongFromStoredPlaylistByPath(fd, playlist, from); + result = removeOneSongFromStoredPlaylistByPath(playlist, from); + return print_playlist_result(fd, result); } static int handlePlaylistMove(int fd, mpd_unused int *permission, @@ -746,13 +754,15 @@ static int handlePlaylistMove(int fd, mpd_unused int *permission, { char *playlist = argv[1]; int from, to; + enum playlist_result result; if (check_int(fd, &from, argv[2], check_integer, argv[2]) < 0) return -1; if (check_int(fd, &to, argv[3], check_integer, argv[3]) < 0) return -1; - return moveSongInStoredPlaylistByPath(fd, playlist, from, to); + result = moveSongInStoredPlaylistByPath(playlist, from, to); + return print_playlist_result(fd, result); } static int listHandleUpdate(int fd, @@ -1137,7 +1147,10 @@ static int handleNotcommands(int fd, mpd_unused int *permission, static int handlePlaylistClear(int fd, mpd_unused int *permission, mpd_unused int argc, char *argv[]) { - return clearStoredPlaylist(fd, argv[1]); + enum playlist_result result; + + result = clearStoredPlaylist(argv[1]); + return print_playlist_result(fd, result); } static int handlePlaylistAdd(int fd, mpd_unused int *permission, @@ -1145,11 +1158,13 @@ static int handlePlaylistAdd(int fd, mpd_unused int *permission, { char *playlist = argv[1]; char *path = argv[2]; + enum playlist_result result; if (isRemoteUrl(path)) - return addToStoredPlaylist(fd, path, playlist); - - return addAllInToStoredPlaylist(fd, path, playlist); + result = addToStoredPlaylist(path, playlist); + else + result = addAllInToStoredPlaylist(fd, path, playlist); + return print_playlist_result(fd, result); } void initCommands(void) diff --git a/src/dbUtils.c b/src/dbUtils.c index 1dfdf6709..751667b58 100644 --- a/src/dbUtils.c +++ b/src/dbUtils.c @@ -180,11 +180,10 @@ static int directoryAddSongToPlaylist(mpd_unused int fd, Song * song, return addSongToPlaylist(song, NULL); } -static int directoryAddSongToStoredPlaylist(int fd, Song *song, void *data) +static int directoryAddSongToStoredPlaylist(mpd_unused int fd, Song *song, + void *data) { - if (appendSongToStoredPlaylistByPath(fd, (char *)data, song) != 0) - return -1; - return 0; + return appendSongToStoredPlaylistByPath((char *)data, song); } int addAllIn(int fd, const char *name) diff --git a/src/playlist.c b/src/playlist.c index e8257a154..107e67588 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -212,9 +212,9 @@ void clearPlaylist(void) incrPlaylistVersion(); } -int clearStoredPlaylist(int fd, const char *utf8file) +int clearStoredPlaylist(const char *utf8file) { - return removeAllFromStoredPlaylistByPath(fd, utf8file); + return removeAllFromStoredPlaylistByPath(utf8file); } void showPlaylist(int fd) @@ -568,32 +568,27 @@ enum playlist_result addToPlaylist(const char *url, int *added_id) return addSongToPlaylist(song, added_id); } -int addToStoredPlaylist(int fd, const char *url, const char *utf8file) +int addToStoredPlaylist(const char *url, const char *utf8file) { Song *song; DEBUG("add to stored playlist: %s\n", url); song = getSongFromDB(url); - if (song) { - appendSongToStoredPlaylistByPath(fd, utf8file, song); - return 0; - } + if (song) + return appendSongToStoredPlaylistByPath(utf8file, song); if (!isValidRemoteUtf8Url(url)) - goto fail; + return ACK_ERROR_NO_EXIST; song = newSong(url, SONG_TYPE_URL, NULL); if (song) { - appendSongToStoredPlaylistByPath(fd, utf8file, song); + int ret = appendSongToStoredPlaylistByPath(utf8file, song); freeJustSong(song); - return 0; + return ret; } -fail: - commandError(fd, ACK_ERROR_NO_EXIST, "\"%s\" is not in the music db" - "or is not a valid url", url); - return -1; + return ACK_ERROR_NO_EXIST; } enum playlist_result addSongToPlaylist(Song * song, int *added_id) @@ -1347,8 +1342,11 @@ int PlaylistInfo(int fd, const char *utf8file, int detail) ListNode *node; List *list; - if (!(list = loadStoredPlaylist(fd, utf8file))) + if (!(list = loadStoredPlaylist(utf8file))) { + commandError(fd, ACK_ERROR_NO_EXIST, "could not open playlist " + "\"%s\": %s", utf8file, strerror(errno)); return -1; + } node = list->firstNode; while (node != NULL) { @@ -1374,13 +1372,13 @@ int PlaylistInfo(int fd, const char *utf8file, int detail) return 0; } -int loadPlaylist(int fd, const char *utf8file) +enum playlist_result loadPlaylist(int fd, const char *utf8file) { ListNode *node; List *list; - if (!(list = loadStoredPlaylist(fd, utf8file))) - return -1; + if (!(list = loadStoredPlaylist(utf8file))) + return PLAYLIST_RESULT_NO_SUCH_LIST; node = list->firstNode; while (node != NULL) { @@ -1405,7 +1403,7 @@ int loadPlaylist(int fd, const char *utf8file) } freeList(list); - return 0; + return PLAYLIST_RESULT_SUCCESS; } void searchForSongsInPlaylist(int fd, int numItems, LocateTagItem * items) @@ -1456,17 +1454,3 @@ int is_valid_playlist_name(const char *utf8path) strchr(utf8path, '\n') == NULL && strchr(utf8path, '\r') == NULL; } - -int valid_playlist_name(int err_fd, const char *utf8path) -{ - if (!is_valid_playlist_name(utf8path)) { - commandError(err_fd, ACK_ERROR_ARG, "playlist name \"%s\" is " - "invalid: playlist names may not contain slashes," - " newlines or carriage returns", - utf8path); - return 0; - } - return 1; -} - - diff --git a/src/playlist.h b/src/playlist.h index 44800cb72..c013fee9e 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -65,11 +65,11 @@ void savePlaylistState(FILE *); void clearPlaylist(void); -int clearStoredPlaylist(int fd, const char *utf8file); +int clearStoredPlaylist(const char *utf8file); enum playlist_result addToPlaylist(const char *file, int *added_id); -int addToStoredPlaylist(int fd, const char *file, const char *utf8file); +int addToStoredPlaylist(const char *file, const char *utf8file); enum playlist_result addSongToPlaylist(Song * song, int *added_id); @@ -111,7 +111,7 @@ enum playlist_result swapSongsInPlaylist(int song1, int song2); enum playlist_result swapSongsInPlaylistById(int id1, int id2); -int loadPlaylist(int fd, const char *utf8file); +enum playlist_result loadPlaylist(int fd, const char *utf8file); int getPlaylistRepeatStatus(void); @@ -149,6 +149,4 @@ void findSongsInPlaylist(int fd, int numItems, LocateTagItem * items); int is_valid_playlist_name(const char *utf8path); -int valid_playlist_name(int err_fd, const char *utf8path); - #endif diff --git a/src/storedPlaylist.c b/src/storedPlaylist.c index 46a0ce3ca..fef8a70f3 100644 --- a/src/storedPlaylist.c +++ b/src/storedPlaylist.c @@ -19,8 +19,6 @@ #include "storedPlaylist.h" #include "path.h" #include "utils.h" -#include "ack.h" -#include "command.h" #include "ls.h" #include "directory.h" #include "os_compat.h" @@ -60,7 +58,8 @@ static ListNode *nodeOfStoredPlaylist(List *list, int idx) return NULL; } -static int writeStoredPlaylistToPath(int fd, List *list, const char *fspath) +static enum playlist_result +writeStoredPlaylistToPath(List *list, const char *fspath) { ListNode *node; FILE *file; @@ -69,11 +68,8 @@ static int writeStoredPlaylistToPath(int fd, List *list, const char *fspath) assert(fspath != NULL); while (!(file = fopen(fspath, "w")) && errno == EINTR); - if (file == NULL) { - commandError(fd, ACK_ERROR_NO_EXIST, "could not open file " - "\"%s\": %s", fspath, strerror(errno)); - return -1; - } + if (file == NULL) + return PLAYLIST_RESULT_ERRNO; node = list->firstNode; while (node != NULL) { @@ -87,10 +83,10 @@ static int writeStoredPlaylistToPath(int fd, List *list, const char *fspath) } while (fclose(file) != 0 && errno == EINTR); - return 0; + return PLAYLIST_RESULT_SUCCESS; } -List *loadStoredPlaylist(int fd, const char *utf8path) +List *loadStoredPlaylist(const char *utf8path) { List *list; FILE *file; @@ -98,16 +94,13 @@ List *loadStoredPlaylist(int fd, const char *utf8path) char path_max_tmp[MPD_PATH_MAX]; const size_t musicDir_len = strlen(musicDir); - if (!valid_playlist_name(fd, utf8path)) + if (!is_valid_playlist_name(utf8path)) return NULL; utf8_to_fs_playlist_path(path_max_tmp, utf8path); while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR); - if (file == NULL) { - commandError(fd, ACK_ERROR_NO_EXIST, "could not open file " - "\"%s\": %s", path_max_tmp, strerror(errno)); + if (file == NULL) return NULL; - } list = makeList(DEFAULT_FREE_DATA_FUNC, 0); @@ -135,15 +128,13 @@ List *loadStoredPlaylist(int fd, const char *utf8path) return list; } -static int moveSongInStoredPlaylist(int fd, List *list, int src, int dest) +static int moveSongInStoredPlaylist(List *list, int src, int dest) { ListNode *srcNode, *destNode; if (src >= list->numberOfNodes || dest >= list->numberOfNodes || - src < 0 || dest < 0 || src == dest) { - commandError(fd, ACK_ERROR_ARG, "argument out of range"); + src < 0 || dest < 0 || src == dest) return -1; - } srcNode = nodeOfStoredPlaylist(list, src); if (!srcNode) @@ -197,90 +188,78 @@ static int moveSongInStoredPlaylist(int fd, List *list, int src, int dest) return 0; } -int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, - int src, int dest) +enum playlist_result +moveSongInStoredPlaylistByPath(const char *utf8path, int src, int dest) { List *list; + enum playlist_result result; - if (!(list = loadStoredPlaylist(fd, utf8path))) { - commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist"); - return -1; - } + if (!(list = loadStoredPlaylist(utf8path))) + return PLAYLIST_RESULT_NO_SUCH_LIST; - if (moveSongInStoredPlaylist(fd, list, src, dest) != 0) { + if (moveSongInStoredPlaylist(list, src, dest) != 0) { freeList(list); - return -1; + return PLAYLIST_RESULT_BAD_RANGE; } - if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) { - commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist"); - freeList(list); - return -1; - } + result = writeStoredPlaylistToPath(list, utf8path); freeList(list); - return 0; + return result; } -int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path) +enum playlist_result +removeAllFromStoredPlaylistByPath(const char *utf8path) { char filename[MPD_PATH_MAX]; FILE *file; - if (!valid_playlist_name(fd, utf8path)) - return -1; + if (!is_valid_playlist_name(utf8path)) + return PLAYLIST_RESULT_BAD_NAME; + utf8_to_fs_playlist_path(filename, utf8path); while (!(file = fopen(filename, "w")) && errno == EINTR); - if (file == NULL) { - commandError(fd, ACK_ERROR_NO_EXIST, "could not open file " - "\"%s\": %s", filename, strerror(errno)); - return -1; - } + if (file == NULL) + return PLAYLIST_RESULT_ERRNO; while (fclose(file) != 0 && errno == EINTR); - return 0; + return PLAYLIST_RESULT_SUCCESS; } -static int removeOneSongFromStoredPlaylist(int fd, List *list, int pos) +static int removeOneSongFromStoredPlaylist(List *list, int pos) { ListNode *node = nodeOfStoredPlaylist(list, pos); - if (!node) { - commandError(fd, ACK_ERROR_ARG, - "could not find song at position"); + if (!node) return -1; - } deleteNodeFromList(list, node); return 0; } -int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos) +enum playlist_result +removeOneSongFromStoredPlaylistByPath(const char *utf8path, int pos) { List *list; + enum playlist_result result; - if (!(list = loadStoredPlaylist(fd, utf8path))) { - commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist"); - return -1; - } + if (!(list = loadStoredPlaylist(utf8path))) + return PLAYLIST_RESULT_NO_SUCH_LIST; - if (removeOneSongFromStoredPlaylist(fd, list, pos) != 0) { + if (removeOneSongFromStoredPlaylist(list, pos) != 0) { freeList(list); - return -1; + return PLAYLIST_RESULT_BAD_RANGE; } - if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) { - commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist"); - freeList(list); - return -1; - } + result = writeStoredPlaylistToPath(list, utf8path); freeList(list); - return 0; + return result; } -int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song) +enum playlist_result +appendSongToStoredPlaylistByPath(const char *utf8path, Song *song) { FILE *file; char *s; @@ -288,27 +267,28 @@ int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song) char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp2[MPD_PATH_MAX]; - if (!valid_playlist_name(fd, utf8path)) - return -1; + if (!is_valid_playlist_name(utf8path)) + return PLAYLIST_RESULT_BAD_NAME; utf8_to_fs_playlist_path(path_max_tmp, utf8path); while (!(file = fopen(path_max_tmp, "a")) && errno == EINTR); if (file == NULL) { - commandError(fd, ACK_ERROR_NO_EXIST, "could not open file " - "\"%s\": %s", path_max_tmp, strerror(errno)); - return -1; + int save_errno = errno; + while (fclose(file) != 0 && errno == EINTR); + errno = save_errno; + return PLAYLIST_RESULT_ERRNO; } + if (fstat(fileno(file), &st) < 0) { - commandError(fd, ACK_ERROR_NO_EXIST, "could not stat file " - "\"%s\": %s", path_max_tmp, strerror(errno)); + int save_errno = errno; while (fclose(file) != 0 && errno == EINTR); - return -1; + errno = save_errno; + return PLAYLIST_RESULT_ERRNO; } + if (st.st_size >= ((MPD_PATH_MAX+1) * playlist_max_length)) { while (fclose(file) != 0 && errno == EINTR); - commandError(fd, ACK_ERROR_PLAYLIST_MAX, - "playlist is at the max size"); - return -1; + return PLAYLIST_RESULT_TOO_LARGE; } s = utf8_to_fs_charset(path_max_tmp2, get_song_url(path_max_tmp, song)); @@ -319,40 +299,31 @@ int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song) fprintf(file, "%s\n", s); while (fclose(file) != 0 && errno == EINTR); - return 0; + return PLAYLIST_RESULT_SUCCESS; } -int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to) +enum playlist_result +renameStoredPlaylist(const char *utf8from, const char *utf8to) { struct stat st; char from[MPD_PATH_MAX]; char to[MPD_PATH_MAX]; - if (!valid_playlist_name(fd, utf8from) || - !valid_playlist_name(fd, utf8to)) - return -1; + if (!is_valid_playlist_name(utf8from) || + !is_valid_playlist_name(utf8to)) + return PLAYLIST_RESULT_BAD_NAME; utf8_to_fs_playlist_path(from, utf8from); utf8_to_fs_playlist_path(to, utf8to); - if (stat(from, &st) != 0) { - commandError(fd, ACK_ERROR_NO_EXIST, - "no playlist named \"%s\"", utf8from); - return -1; - } + if (stat(from, &st) != 0) + return PLAYLIST_RESULT_NO_SUCH_LIST; - if (stat(to, &st) == 0) { - commandError(fd, ACK_ERROR_EXIST, "a file or directory " - "already exists with the name \"%s\"", utf8to); - return -1; - } + if (stat(to, &st) == 0) + return PLAYLIST_RESULT_LIST_EXISTS; - if (rename(from, to) < 0) { - commandError(fd, ACK_ERROR_UNKNOWN, - "could not rename playlist \"%s\" to \"%s\": %s", - utf8from, utf8to, strerror(errno)); - return -1; - } + if (rename(from, to) < 0) + return PLAYLIST_RESULT_ERRNO; - return 0; + return PLAYLIST_RESULT_SUCCESS; } diff --git a/src/storedPlaylist.h b/src/storedPlaylist.h index a7806386b..964669d35 100644 --- a/src/storedPlaylist.h +++ b/src/storedPlaylist.h @@ -23,14 +23,21 @@ #include "list.h" #include "playlist.h" -List *loadStoredPlaylist(int fd, const char *utf8path); +List *loadStoredPlaylist(const char *utf8path); -int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest); -int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path); -int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos); +enum playlist_result +moveSongInStoredPlaylistByPath(const char *utf8path, int src, int dest); -int appendSongToStoredPlaylistByPath(int fd, const char *utf8path, Song *song); +enum playlist_result +removeAllFromStoredPlaylistByPath(const char *utf8path); -int renameStoredPlaylist(int fd, const char *utf8from, const char *utf8to); +enum playlist_result +removeOneSongFromStoredPlaylistByPath(const char *utf8path, int pos); + +enum playlist_result +appendSongToStoredPlaylistByPath(const char *utf8path, Song *song); + +enum playlist_result +renameStoredPlaylist(const char *utf8from, const char *utf8to); #endif -- cgit v1.2.3