Commit ca419c84 authored by Max Kellermann's avatar Max Kellermann

stored_playlist: return GError, code is playlist_result

Improve error reporting and handling. command.c gets the new function print_error(), which sends a GError to the client.
parent aede71b1
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "playlist_print.h" #include "playlist_print.h"
#include "playlist_save.h" #include "playlist_save.h"
#include "playlist_queue.h" #include "playlist_queue.h"
#include "playlist_error.h"
#include "queue_print.h" #include "queue_print.h"
#include "ls.h" #include "ls.h"
#include "uri.h" #include "uri.h"
...@@ -376,6 +377,33 @@ print_playlist_result(struct client *client, ...@@ -376,6 +377,33 @@ print_playlist_result(struct client *client,
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
/**
* Send the GError to the client and free the GError.
*/
static enum command_return
print_error(struct client *client, GError *error)
{
assert(client != NULL);
assert(error != NULL);
g_warning("%s", error->message);
if (error->domain == playlist_quark()) {
enum playlist_result result = error->code;
g_error_free(error);
return print_playlist_result(client, result);
} else if (error->domain == g_file_error_quark()) {
command_error(client, ACK_ERROR_SYSTEM, "%s",
g_strerror(error->code));
g_error_free(error);
return COMMAND_RETURN_ERROR;
}
g_error_free(error);
command_error(client, ACK_ERROR_UNKNOWN, "error");
return COMMAND_RETURN_ERROR;
}
static void static void
print_spl_list(struct client *client, GPtrArray *list) print_spl_list(struct client *client, GPtrArray *list)
{ {
...@@ -766,9 +794,11 @@ handle_load(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) ...@@ -766,9 +794,11 @@ handle_load(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
if (result != PLAYLIST_RESULT_NO_SUCH_LIST) if (result != PLAYLIST_RESULT_NO_SUCH_LIST)
return print_playlist_result(client, result); return print_playlist_result(client, result);
result = playlist_load_spl(&g_playlist, client->player_control, GError *error = NULL;
argv[1]); return playlist_load_spl(&g_playlist, client->player_control,
return print_playlist_result(client, result); argv[1], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
} }
static enum command_return static enum command_return
...@@ -777,15 +807,10 @@ handle_listplaylist(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) ...@@ -777,15 +807,10 @@ handle_listplaylist(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
if (playlist_file_print(client, argv[1], false)) if (playlist_file_print(client, argv[1], false))
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;
bool ret; GError *error = NULL;
return spl_print(client, argv[1], false, &error)
ret = spl_print(client, argv[1], false); ? COMMAND_RETURN_OK
if (!ret) { : print_error(client, error);
command_error(client, ACK_ERROR_NO_EXIST, "No such playlist");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
} }
static enum command_return static enum command_return
...@@ -795,15 +820,10 @@ handle_listplaylistinfo(struct client *client, ...@@ -795,15 +820,10 @@ handle_listplaylistinfo(struct client *client,
if (playlist_file_print(client, argv[1], true)) if (playlist_file_print(client, argv[1], true))
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;
bool ret; GError *error = NULL;
return spl_print(client, argv[1], true, &error)
ret = spl_print(client, argv[1], true); ? COMMAND_RETURN_OK
if (!ret) { : print_error(client, error);
command_error(client, ACK_ERROR_NO_EXIST, "No such playlist");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
} }
static enum command_return static enum command_return
...@@ -828,7 +848,7 @@ handle_lsinfo(struct client *client, int argc, char *argv[]) ...@@ -828,7 +848,7 @@ handle_lsinfo(struct client *client, int argc, char *argv[])
directory_print(client, directory); directory_print(client, directory);
if (isRootDirectory(uri)) { if (isRootDirectory(uri)) {
GPtrArray *list = spl_list(); GPtrArray *list = spl_list(NULL);
if (list != NULL) { if (list != NULL) {
print_spl_list(client, list); print_spl_list(client, list);
spl_list_free(list); spl_list_free(list);
...@@ -841,19 +861,19 @@ handle_lsinfo(struct client *client, int argc, char *argv[]) ...@@ -841,19 +861,19 @@ handle_lsinfo(struct client *client, int argc, char *argv[])
static enum command_return static enum command_return
handle_rm(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) handle_rm(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{ {
enum playlist_result result; GError *error = NULL;
return spl_delete(argv[1], &error)
result = spl_delete(argv[1]); ? COMMAND_RETURN_OK
return print_playlist_result(client, result); : print_error(client, error);
} }
static enum command_return static enum command_return
handle_rename(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) handle_rename(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{ {
enum playlist_result result; GError *error = NULL;
return spl_rename(argv[1], argv[2], &error)
result = spl_rename(argv[1], argv[2]); ? COMMAND_RETURN_OK
return print_playlist_result(client, result); : print_error(client, error);
} }
static enum command_return static enum command_return
...@@ -1064,13 +1084,14 @@ handle_playlistdelete(struct client *client, ...@@ -1064,13 +1084,14 @@ handle_playlistdelete(struct client *client,
G_GNUC_UNUSED int argc, char *argv[]) { G_GNUC_UNUSED int argc, char *argv[]) {
char *playlist = argv[1]; char *playlist = argv[1];
int from; int from;
enum playlist_result result;
if (!check_int(client, &from, argv[2], check_integer, argv[2])) if (!check_int(client, &from, argv[2], check_integer, argv[2]))
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
result = spl_remove_index(playlist, from); GError *error = NULL;
return print_playlist_result(client, result); return spl_remove_index(playlist, from, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
} }
static enum command_return static enum command_return
...@@ -1078,15 +1099,16 @@ handle_playlistmove(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) ...@@ -1078,15 +1099,16 @@ handle_playlistmove(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{ {
char *playlist = argv[1]; char *playlist = argv[1];
int from, to; int from, to;
enum playlist_result result;
if (!check_int(client, &from, argv[2], check_integer, argv[2])) if (!check_int(client, &from, argv[2], check_integer, argv[2]))
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
if (!check_int(client, &to, argv[3], check_integer, argv[3])) if (!check_int(client, &to, argv[3], check_integer, argv[3]))
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
result = spl_move_index(playlist, from, to); GError *error = NULL;
return print_playlist_result(client, result); return spl_move_index(playlist, from, to, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
} }
static enum command_return static enum command_return
...@@ -1642,10 +1664,10 @@ handle_not_commands(struct client *client, ...@@ -1642,10 +1664,10 @@ handle_not_commands(struct client *client,
static enum command_return static enum command_return
handle_playlistclear(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) handle_playlistclear(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{ {
enum playlist_result result; GError *error = NULL;
return spl_clear(argv[1], &error)
result = spl_clear(argv[1]); ? COMMAND_RETURN_OK
return print_playlist_result(client, result); : print_error(client, error);
} }
static enum command_return static enum command_return
...@@ -1653,8 +1675,9 @@ handle_playlistadd(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) ...@@ -1653,8 +1675,9 @@ handle_playlistadd(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{ {
char *playlist = argv[1]; char *playlist = argv[1];
char *uri = argv[2]; char *uri = argv[2];
enum playlist_result result;
bool success;
GError *error = NULL;
if (uri_has_scheme(uri)) { if (uri_has_scheme(uri)) {
if (!uri_supported_scheme(uri)) { if (!uri_supported_scheme(uri)) {
command_error(client, ACK_ERROR_NO_EXIST, command_error(client, ACK_ERROR_NO_EXIST,
...@@ -1662,29 +1685,27 @@ handle_playlistadd(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) ...@@ -1662,29 +1685,27 @@ handle_playlistadd(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
result = spl_append_uri(uri, playlist); success = spl_append_uri(argv[1], playlist, &error);
} else } else
result = addAllInToStoredPlaylist(uri, playlist); success = addAllInToStoredPlaylist(uri, playlist, &error);
if (result == (enum playlist_result)-1) { if (!success && error == NULL) {
command_error(client, ACK_ERROR_NO_EXIST, command_error(client, ACK_ERROR_NO_EXIST,
"directory or file not found"); "directory or file not found");
return COMMAND_RETURN_ERROR; return COMMAND_RETURN_ERROR;
} }
return print_playlist_result(client, result); return success ? COMMAND_RETURN_OK : print_error(client, error);
} }
static enum command_return static enum command_return
handle_listplaylists(struct client *client, handle_listplaylists(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]) G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{ {
GPtrArray *list = spl_list(); GError *error = NULL;
if (list == NULL) { GPtrArray *list = spl_list(&error);
command_error(client, ACK_ERROR_SYSTEM, if (list == NULL)
"failed to get list of stored playlists"); return print_error(client, error);
return COMMAND_RETURN_ERROR;
}
print_spl_list(client, list); print_spl_list(client, list);
spl_list_free(list); spl_list_free(list);
......
...@@ -36,6 +36,7 @@ directoryAddSongToPlaylist(struct song *song, void *data) ...@@ -36,6 +36,7 @@ directoryAddSongToPlaylist(struct song *song, void *data)
struct add_data { struct add_data {
const char *path; const char *path;
GError **error_r;
}; };
static int static int
...@@ -43,8 +44,10 @@ directoryAddSongToStoredPlaylist(struct song *song, void *_data) ...@@ -43,8 +44,10 @@ directoryAddSongToStoredPlaylist(struct song *song, void *_data)
{ {
struct add_data *data = _data; struct add_data *data = _data;
if (spl_append_song(data->path, song) != 0) if (!spl_append_song(data->path, song, data->error_r)) {
return -1; return -1;
}
return 0; return 0;
} }
...@@ -54,13 +57,16 @@ addAllIn(struct player_control *pc, const char *name) ...@@ -54,13 +57,16 @@ addAllIn(struct player_control *pc, const char *name)
return db_walk(name, directoryAddSongToPlaylist, NULL, pc); return db_walk(name, directoryAddSongToPlaylist, NULL, pc);
} }
int addAllInToStoredPlaylist(const char *name, const char *utf8file) bool
addAllInToStoredPlaylist(const char *name, const char *utf8file,
GError **error_r)
{ {
struct add_data data = { struct add_data data = {
.path = utf8file, .path = utf8file,
.error_r = error_r,
}; };
return db_walk(name, directoryAddSongToStoredPlaylist, NULL, &data); return db_walk(name, directoryAddSongToStoredPlaylist, NULL, &data) == 0;
} }
struct find_add_data { struct find_add_data {
......
...@@ -20,13 +20,18 @@ ...@@ -20,13 +20,18 @@
#ifndef MPD_DB_UTILS_H #ifndef MPD_DB_UTILS_H
#define MPD_DB_UTILS_H #define MPD_DB_UTILS_H
#include <glib.h>
#include <stdbool.h>
struct locate_item_list; struct locate_item_list;
struct player_control; struct player_control;
int int
addAllIn(struct player_control *pc, const char *name); addAllIn(struct player_control *pc, const char *name);
int addAllInToStoredPlaylist(const char *name, const char *utf8file); bool
addAllInToStoredPlaylist(const char *uri_utf8, const char *path_utf8,
GError **error_r);
int int
findAddIn(struct player_control *pc, const char *name, findAddIn(struct player_control *pc, const char *name,
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#ifndef MPD_PLAYLIST_ERROR_H #ifndef MPD_PLAYLIST_ERROR_H
#define MPD_PLAYLIST_ERROR_H #define MPD_PLAYLIST_ERROR_H
#include <glib.h>
enum playlist_result { enum playlist_result {
PLAYLIST_RESULT_SUCCESS, PLAYLIST_RESULT_SUCCESS,
PLAYLIST_RESULT_ERRNO, PLAYLIST_RESULT_ERRNO,
...@@ -34,4 +36,14 @@ enum playlist_result { ...@@ -34,4 +36,14 @@ enum playlist_result {
PLAYLIST_RESULT_DISABLED, PLAYLIST_RESULT_DISABLED,
}; };
/**
* Quark for GError.domain; the code is an enum #playlist_result.
*/
G_GNUC_CONST
static inline GQuark
playlist_quark(void)
{
return g_quark_from_static_string("playlist");
}
#endif #endif
...@@ -117,11 +117,12 @@ playlist_print_changes_position(struct client *client, ...@@ -117,11 +117,12 @@ playlist_print_changes_position(struct client *client,
} }
bool bool
spl_print(struct client *client, const char *name_utf8, bool detail) spl_print(struct client *client, const char *name_utf8, bool detail,
GError **error_r)
{ {
GPtrArray *list; GPtrArray *list;
list = spl_load(name_utf8); list = spl_load(name_utf8, error_r);
if (list == NULL) if (list == NULL)
return false; return false;
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#ifndef PLAYLIST_PRINT_H #ifndef PLAYLIST_PRINT_H
#define PLAYLIST_PRINT_H #define PLAYLIST_PRINT_H
#include <glib.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
...@@ -99,7 +100,8 @@ playlist_print_changes_position(struct client *client, ...@@ -99,7 +100,8 @@ playlist_print_changes_position(struct client *client,
* @return true on success, false if the playlist does not exist * @return true on success, false if the playlist does not exist
*/ */
bool bool
spl_print(struct client *client, const char *name_utf8, bool detail); spl_print(struct client *client, const char *name_utf8, bool detail,
GError **error_r);
/** /**
* Send the playlist file to the client. * Send the playlist file to the client.
......
...@@ -110,15 +110,15 @@ spl_save_playlist(const char *name_utf8, const struct playlist *playlist) ...@@ -110,15 +110,15 @@ spl_save_playlist(const char *name_utf8, const struct playlist *playlist)
return spl_save_queue(name_utf8, &playlist->queue); return spl_save_queue(name_utf8, &playlist->queue);
} }
enum playlist_result bool
playlist_load_spl(struct playlist *playlist, struct player_control *pc, playlist_load_spl(struct playlist *playlist, struct player_control *pc,
const char *name_utf8) const char *name_utf8, GError **error_r)
{ {
GPtrArray *list; GPtrArray *list;
list = spl_load(name_utf8); list = spl_load(name_utf8, error_r);
if (list == NULL) if (list == NULL)
return PLAYLIST_RESULT_NO_SUCH_LIST; return false;
for (unsigned i = 0; i < list->len; ++i) { for (unsigned i = 0; i < list->len; ++i) {
const char *temp = g_ptr_array_index(list, i); const char *temp = g_ptr_array_index(list, i);
...@@ -139,5 +139,5 @@ playlist_load_spl(struct playlist *playlist, struct player_control *pc, ...@@ -139,5 +139,5 @@ playlist_load_spl(struct playlist *playlist, struct player_control *pc,
} }
spl_free(list); spl_free(list);
return PLAYLIST_RESULT_SUCCESS; return true;
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "playlist_error.h" #include "playlist_error.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
struct song; struct song;
...@@ -51,8 +52,8 @@ spl_save_playlist(const char *name_utf8, const struct playlist *playlist); ...@@ -51,8 +52,8 @@ spl_save_playlist(const char *name_utf8, const struct playlist *playlist);
* Loads a stored playlist file, and append all songs to the global * Loads a stored playlist file, and append all songs to the global
* playlist. * playlist.
*/ */
enum playlist_result bool
playlist_load_spl(struct playlist *playlist, struct player_control *pc, playlist_load_spl(struct playlist *playlist, struct player_control *pc,
const char *name_utf8); const char *name_utf8, GError **error_r);
#endif #endif
...@@ -20,8 +20,6 @@ ...@@ -20,8 +20,6 @@
#ifndef MPD_STORED_PLAYLIST_H #ifndef MPD_STORED_PLAYLIST_H
#define MPD_STORED_PLAYLIST_H #define MPD_STORED_PLAYLIST_H
#include "playlist_error.h"
#include <glib.h> #include <glib.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
...@@ -54,36 +52,37 @@ spl_valid_name(const char *name_utf8); ...@@ -54,36 +52,37 @@ spl_valid_name(const char *name_utf8);
* NULL if an error occurred. * NULL if an error occurred.
*/ */
GPtrArray * GPtrArray *
spl_list(void); spl_list(GError **error_r);
void void
spl_list_free(GPtrArray *list); spl_list_free(GPtrArray *list);
GPtrArray * GPtrArray *
spl_load(const char *utf8path); spl_load(const char *utf8path, GError **error_r);
void void
spl_free(GPtrArray *list); spl_free(GPtrArray *list);
enum playlist_result bool
spl_move_index(const char *utf8path, unsigned src, unsigned dest); spl_move_index(const char *utf8path, unsigned src, unsigned dest,
GError **error_r);
enum playlist_result bool
spl_clear(const char *utf8path); spl_clear(const char *utf8path, GError **error_r);
enum playlist_result bool
spl_delete(const char *name_utf8); spl_delete(const char *name_utf8, GError **error_r);
enum playlist_result bool
spl_remove_index(const char *utf8path, unsigned pos); spl_remove_index(const char *utf8path, unsigned pos, GError **error_r);
enum playlist_result bool
spl_append_song(const char *utf8path, struct song *song); spl_append_song(const char *utf8path, struct song *song, GError **error_r);
enum playlist_result bool
spl_append_uri(const char *file, const char *utf8file); spl_append_uri(const char *file, const char *utf8file, GError **error_r);
enum playlist_result bool
spl_rename(const char *utf8from, const char *utf8to); spl_rename(const char *utf8from, const char *utf8to, GError **error_r);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment