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 @@
#include "playlist_print.h"
#include "playlist_save.h"
#include "playlist_queue.h"
#include "playlist_error.h"
#include "queue_print.h"
#include "ls.h"
#include "uri.h"
......@@ -376,6 +377,33 @@ print_playlist_result(struct client *client,
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
print_spl_list(struct client *client, GPtrArray *list)
{
......@@ -766,9 +794,11 @@ handle_load(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
if (result != PLAYLIST_RESULT_NO_SUCH_LIST)
return print_playlist_result(client, result);
result = playlist_load_spl(&g_playlist, client->player_control,
argv[1]);
return print_playlist_result(client, result);
GError *error = NULL;
return playlist_load_spl(&g_playlist, client->player_control,
argv[1], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
......@@ -777,15 +807,10 @@ handle_listplaylist(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
if (playlist_file_print(client, argv[1], false))
return COMMAND_RETURN_OK;
bool ret;
ret = spl_print(client, argv[1], false);
if (!ret) {
command_error(client, ACK_ERROR_NO_EXIST, "No such playlist");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
GError *error = NULL;
return spl_print(client, argv[1], false, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
......@@ -795,15 +820,10 @@ handle_listplaylistinfo(struct client *client,
if (playlist_file_print(client, argv[1], true))
return COMMAND_RETURN_OK;
bool ret;
ret = spl_print(client, argv[1], true);
if (!ret) {
command_error(client, ACK_ERROR_NO_EXIST, "No such playlist");
return COMMAND_RETURN_ERROR;
}
return COMMAND_RETURN_OK;
GError *error = NULL;
return spl_print(client, argv[1], true, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
......@@ -828,7 +848,7 @@ handle_lsinfo(struct client *client, int argc, char *argv[])
directory_print(client, directory);
if (isRootDirectory(uri)) {
GPtrArray *list = spl_list();
GPtrArray *list = spl_list(NULL);
if (list != NULL) {
print_spl_list(client, list);
spl_list_free(list);
......@@ -841,19 +861,19 @@ handle_lsinfo(struct client *client, int argc, char *argv[])
static enum command_return
handle_rm(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
enum playlist_result result;
result = spl_delete(argv[1]);
return print_playlist_result(client, result);
GError *error = NULL;
return spl_delete(argv[1], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
handle_rename(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
enum playlist_result result;
result = spl_rename(argv[1], argv[2]);
return print_playlist_result(client, result);
GError *error = NULL;
return spl_rename(argv[1], argv[2], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
......@@ -1064,13 +1084,14 @@ handle_playlistdelete(struct client *client,
G_GNUC_UNUSED int argc, char *argv[]) {
char *playlist = argv[1];
int from;
enum playlist_result result;
if (!check_int(client, &from, argv[2], check_integer, argv[2]))
return COMMAND_RETURN_ERROR;
result = spl_remove_index(playlist, from);
return print_playlist_result(client, result);
GError *error = NULL;
return spl_remove_index(playlist, from, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
......@@ -1078,15 +1099,16 @@ handle_playlistmove(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
char *playlist = argv[1];
int from, to;
enum playlist_result result;
if (!check_int(client, &from, argv[2], check_integer, argv[2]))
return COMMAND_RETURN_ERROR;
if (!check_int(client, &to, argv[3], check_integer, argv[3]))
return COMMAND_RETURN_ERROR;
result = spl_move_index(playlist, from, to);
return print_playlist_result(client, result);
GError *error = NULL;
return spl_move_index(playlist, from, to, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
......@@ -1642,10 +1664,10 @@ handle_not_commands(struct client *client,
static enum command_return
handle_playlistclear(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
enum playlist_result result;
result = spl_clear(argv[1]);
return print_playlist_result(client, result);
GError *error = NULL;
return spl_clear(argv[1], &error)
? COMMAND_RETURN_OK
: print_error(client, error);
}
static enum command_return
......@@ -1653,8 +1675,9 @@ handle_playlistadd(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
{
char *playlist = argv[1];
char *uri = argv[2];
enum playlist_result result;
bool success;
GError *error = NULL;
if (uri_has_scheme(uri)) {
if (!uri_supported_scheme(uri)) {
command_error(client, ACK_ERROR_NO_EXIST,
......@@ -1662,29 +1685,27 @@ handle_playlistadd(struct client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR;
}
result = spl_append_uri(uri, playlist);
success = spl_append_uri(argv[1], playlist, &error);
} 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,
"directory or file not found");
return COMMAND_RETURN_ERROR;
}
return print_playlist_result(client, result);
return success ? COMMAND_RETURN_OK : print_error(client, error);
}
static enum command_return
handle_listplaylists(struct client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
GPtrArray *list = spl_list();
if (list == NULL) {
command_error(client, ACK_ERROR_SYSTEM,
"failed to get list of stored playlists");
return COMMAND_RETURN_ERROR;
}
GError *error = NULL;
GPtrArray *list = spl_list(&error);
if (list == NULL)
return print_error(client, error);
print_spl_list(client, list);
spl_list_free(list);
......
......@@ -36,6 +36,7 @@ directoryAddSongToPlaylist(struct song *song, void *data)
struct add_data {
const char *path;
GError **error_r;
};
static int
......@@ -43,8 +44,10 @@ directoryAddSongToStoredPlaylist(struct song *song, void *_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 0;
}
......@@ -54,13 +57,16 @@ addAllIn(struct player_control *pc, const char *name)
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 = {
.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 {
......
......@@ -20,13 +20,18 @@
#ifndef MPD_DB_UTILS_H
#define MPD_DB_UTILS_H
#include <glib.h>
#include <stdbool.h>
struct locate_item_list;
struct player_control;
int
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
findAddIn(struct player_control *pc, const char *name,
......
......@@ -20,6 +20,8 @@
#ifndef MPD_PLAYLIST_ERROR_H
#define MPD_PLAYLIST_ERROR_H
#include <glib.h>
enum playlist_result {
PLAYLIST_RESULT_SUCCESS,
PLAYLIST_RESULT_ERRNO,
......@@ -34,4 +36,14 @@ enum playlist_result {
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
......@@ -117,11 +117,12 @@ playlist_print_changes_position(struct client *client,
}
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;
list = spl_load(name_utf8);
list = spl_load(name_utf8, error_r);
if (list == NULL)
return false;
......
......@@ -20,6 +20,7 @@
#ifndef PLAYLIST_PRINT_H
#define PLAYLIST_PRINT_H
#include <glib.h>
#include <stdbool.h>
#include <stdint.h>
......@@ -99,7 +100,8 @@ playlist_print_changes_position(struct client *client,
* @return true on success, false if the playlist does not exist
*/
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.
......
......@@ -110,15 +110,15 @@ spl_save_playlist(const char *name_utf8, const struct playlist *playlist)
return spl_save_queue(name_utf8, &playlist->queue);
}
enum playlist_result
bool
playlist_load_spl(struct playlist *playlist, struct player_control *pc,
const char *name_utf8)
const char *name_utf8, GError **error_r)
{
GPtrArray *list;
list = spl_load(name_utf8);
list = spl_load(name_utf8, error_r);
if (list == NULL)
return PLAYLIST_RESULT_NO_SUCH_LIST;
return false;
for (unsigned i = 0; i < list->len; ++i) {
const char *temp = g_ptr_array_index(list, i);
......@@ -139,5 +139,5 @@ playlist_load_spl(struct playlist *playlist, struct player_control *pc,
}
spl_free(list);
return PLAYLIST_RESULT_SUCCESS;
return true;
}
......@@ -22,6 +22,7 @@
#include "playlist_error.h"
#include <stdbool.h>
#include <stdio.h>
struct song;
......@@ -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
* playlist.
*/
enum playlist_result
bool
playlist_load_spl(struct playlist *playlist, struct player_control *pc,
const char *name_utf8);
const char *name_utf8, GError **error_r);
#endif
......@@ -20,8 +20,6 @@
#ifndef MPD_STORED_PLAYLIST_H
#define MPD_STORED_PLAYLIST_H
#include "playlist_error.h"
#include <glib.h>
#include <stdbool.h>
#include <time.h>
......@@ -54,36 +52,37 @@ spl_valid_name(const char *name_utf8);
* NULL if an error occurred.
*/
GPtrArray *
spl_list(void);
spl_list(GError **error_r);
void
spl_list_free(GPtrArray *list);
GPtrArray *
spl_load(const char *utf8path);
spl_load(const char *utf8path, GError **error_r);
void
spl_free(GPtrArray *list);
enum playlist_result
spl_move_index(const char *utf8path, unsigned src, unsigned dest);
bool
spl_move_index(const char *utf8path, unsigned src, unsigned dest,
GError **error_r);
enum playlist_result
spl_clear(const char *utf8path);
bool
spl_clear(const char *utf8path, GError **error_r);
enum playlist_result
spl_delete(const char *name_utf8);
bool
spl_delete(const char *name_utf8, GError **error_r);
enum playlist_result
spl_remove_index(const char *utf8path, unsigned pos);
bool
spl_remove_index(const char *utf8path, unsigned pos, GError **error_r);
enum playlist_result
spl_append_song(const char *utf8path, struct song *song);
bool
spl_append_song(const char *utf8path, struct song *song, GError **error_r);
enum playlist_result
spl_append_uri(const char *file, const char *utf8file);
bool
spl_append_uri(const char *file, const char *utf8file, GError **error_r);
enum playlist_result
spl_rename(const char *utf8from, const char *utf8to);
bool
spl_rename(const char *utf8from, const char *utf8to, GError **error_r);
#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