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.
This commit is contained in:
parent
aede71b1dc
commit
ca419c84b8
123
src/command.c
123
src/command.c
@ -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
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "database.h"
|
||||
#include "idle.h"
|
||||
#include "conf.h"
|
||||
#include "glib_compat.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/types.h>
|
||||
@ -72,6 +73,67 @@ spl_valid_name(const char *name_utf8)
|
||||
strchr(name_utf8, '\r') == NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
spl_map(GError **error_r)
|
||||
{
|
||||
const char *path_fs = map_spl_path();
|
||||
if (path_fs == NULL)
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_DISABLED,
|
||||
"Stored playlists are disabled");
|
||||
|
||||
return path_fs;
|
||||
}
|
||||
|
||||
static bool
|
||||
spl_check_name(const char *name_utf8, GError **error_r)
|
||||
{
|
||||
if (!spl_valid_name(name_utf8)) {
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_BAD_NAME,
|
||||
"Bad playlist name");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static char *
|
||||
spl_map_to_fs(const char *name_utf8, GError **error_r)
|
||||
{
|
||||
if (spl_map(error_r) == NULL ||
|
||||
!spl_check_name(name_utf8, error_r))
|
||||
return NULL;
|
||||
|
||||
char *path_fs = map_spl_utf8_to_fs(name_utf8);
|
||||
if (path_fs == NULL)
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_BAD_NAME,
|
||||
"Bad playlist name");
|
||||
|
||||
return path_fs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a GError for the current errno.
|
||||
*/
|
||||
static void
|
||||
playlist_errno(GError **error_r)
|
||||
{
|
||||
switch (errno) {
|
||||
case ENOENT:
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_NO_SUCH_LIST,
|
||||
"No such playlist");
|
||||
break;
|
||||
|
||||
default:
|
||||
g_set_error_literal(error_r, g_file_error_quark(), errno,
|
||||
g_strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static struct stored_playlist_info *
|
||||
load_playlist_info(const char *parent_path_fs, const char *name_fs)
|
||||
{
|
||||
@ -108,9 +170,9 @@ load_playlist_info(const char *parent_path_fs, const char *name_fs)
|
||||
}
|
||||
|
||||
GPtrArray *
|
||||
spl_list(void)
|
||||
spl_list(GError **error_r)
|
||||
{
|
||||
const char *parent_path_fs = map_spl_path();
|
||||
const char *parent_path_fs = spl_map(error_r);
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
GPtrArray *list;
|
||||
@ -120,8 +182,11 @@ spl_list(void)
|
||||
return NULL;
|
||||
|
||||
dir = opendir(parent_path_fs);
|
||||
if (dir == NULL)
|
||||
if (dir == NULL) {
|
||||
g_set_error_literal(error_r, g_file_error_quark(), errno,
|
||||
g_strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list = g_ptr_array_new();
|
||||
|
||||
@ -148,25 +213,26 @@ spl_list_free(GPtrArray *list)
|
||||
g_ptr_array_free(list, true);
|
||||
}
|
||||
|
||||
static enum playlist_result
|
||||
spl_save(GPtrArray *list, const char *utf8path)
|
||||
static bool
|
||||
spl_save(GPtrArray *list, const char *utf8path, GError **error_r)
|
||||
{
|
||||
FILE *file;
|
||||
char *path_fs;
|
||||
|
||||
assert(utf8path != NULL);
|
||||
|
||||
if (map_spl_path() == NULL)
|
||||
return PLAYLIST_RESULT_DISABLED;
|
||||
if (spl_map(error_r) == NULL)
|
||||
return false;
|
||||
|
||||
path_fs = map_spl_utf8_to_fs(utf8path);
|
||||
char *path_fs = spl_map_to_fs(utf8path, error_r);
|
||||
if (path_fs == NULL)
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
return false;
|
||||
|
||||
file = fopen(path_fs, "w");
|
||||
g_free(path_fs);
|
||||
if (file == NULL)
|
||||
return PLAYLIST_RESULT_ERRNO;
|
||||
if (file == NULL) {
|
||||
playlist_errno(error_r);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < list->len; ++i) {
|
||||
const char *uri = g_ptr_array_index(list, i);
|
||||
@ -174,27 +240,29 @@ spl_save(GPtrArray *list, const char *utf8path)
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
return PLAYLIST_RESULT_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
GPtrArray *
|
||||
spl_load(const char *utf8path)
|
||||
spl_load(const char *utf8path, GError **error_r)
|
||||
{
|
||||
FILE *file;
|
||||
GPtrArray *list;
|
||||
char *path_fs;
|
||||
|
||||
if (!spl_valid_name(utf8path) || map_spl_path() == NULL)
|
||||
if (spl_map(error_r) == NULL)
|
||||
return NULL;
|
||||
|
||||
path_fs = map_spl_utf8_to_fs(utf8path);
|
||||
path_fs = spl_map_to_fs(utf8path, error_r);
|
||||
if (path_fs == NULL)
|
||||
return NULL;
|
||||
|
||||
file = fopen(path_fs, "r");
|
||||
g_free(path_fs);
|
||||
if (file == NULL)
|
||||
if (file == NULL) {
|
||||
playlist_errno(error_r);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list = g_ptr_array_new();
|
||||
|
||||
@ -260,30 +328,33 @@ spl_insert_index_internal(GPtrArray *list, unsigned idx, char *uri)
|
||||
g_ptr_array_index(list, idx) = uri;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
GPtrArray *list;
|
||||
char *uri;
|
||||
enum playlist_result result;
|
||||
|
||||
if (src == dest)
|
||||
/* this doesn't check whether the playlist exists, but
|
||||
what the hell.. */
|
||||
return PLAYLIST_RESULT_SUCCESS;
|
||||
return true;
|
||||
|
||||
if (!(list = spl_load(utf8path)))
|
||||
return PLAYLIST_RESULT_NO_SUCH_LIST;
|
||||
GPtrArray *list = spl_load(utf8path, error_r);
|
||||
if (list == NULL)
|
||||
return false;
|
||||
|
||||
if (src >= list->len || dest >= list->len) {
|
||||
spl_free(list);
|
||||
return PLAYLIST_RESULT_BAD_RANGE;
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_BAD_RANGE,
|
||||
"Bad range");
|
||||
return false;
|
||||
}
|
||||
|
||||
uri = spl_remove_index_internal(list, src);
|
||||
spl_insert_index_internal(list, dest, uri);
|
||||
|
||||
result = spl_save(list, utf8path);
|
||||
bool result = spl_save(list, utf8path, error_r);
|
||||
|
||||
spl_free(list);
|
||||
|
||||
@ -291,78 +362,72 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest)
|
||||
return result;
|
||||
}
|
||||
|
||||
enum playlist_result
|
||||
spl_clear(const char *utf8path)
|
||||
bool
|
||||
spl_clear(const char *utf8path, GError **error_r)
|
||||
{
|
||||
char *path_fs;
|
||||
FILE *file;
|
||||
|
||||
if (map_spl_path() == NULL)
|
||||
return PLAYLIST_RESULT_DISABLED;
|
||||
if (spl_map(error_r) == NULL)
|
||||
return false;
|
||||
|
||||
if (!spl_valid_name(utf8path))
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
|
||||
path_fs = map_spl_utf8_to_fs(utf8path);
|
||||
char *path_fs = spl_map_to_fs(utf8path, error_r);
|
||||
if (path_fs == NULL)
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
return false;
|
||||
|
||||
file = fopen(path_fs, "w");
|
||||
g_free(path_fs);
|
||||
if (file == NULL)
|
||||
return PLAYLIST_RESULT_ERRNO;
|
||||
if (file == NULL) {
|
||||
playlist_errno(error_r);
|
||||
return false;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
idle_add(IDLE_STORED_PLAYLIST);
|
||||
return PLAYLIST_RESULT_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
enum playlist_result
|
||||
spl_delete(const char *name_utf8)
|
||||
bool
|
||||
spl_delete(const char *name_utf8, GError **error_r)
|
||||
{
|
||||
char *path_fs;
|
||||
int ret;
|
||||
|
||||
if (map_spl_path() == NULL)
|
||||
return PLAYLIST_RESULT_DISABLED;
|
||||
|
||||
if (!spl_valid_name(name_utf8))
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
|
||||
path_fs = map_spl_utf8_to_fs(name_utf8);
|
||||
path_fs = spl_map_to_fs(name_utf8, error_r);
|
||||
if (path_fs == NULL)
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
return false;
|
||||
|
||||
ret = unlink(path_fs);
|
||||
g_free(path_fs);
|
||||
if (ret < 0)
|
||||
return errno == ENOENT
|
||||
? PLAYLIST_RESULT_NO_SUCH_LIST
|
||||
: PLAYLIST_RESULT_ERRNO;
|
||||
if (ret < 0) {
|
||||
playlist_errno(error_r);
|
||||
return false;
|
||||
}
|
||||
|
||||
idle_add(IDLE_STORED_PLAYLIST);
|
||||
return PLAYLIST_RESULT_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
enum playlist_result
|
||||
spl_remove_index(const char *utf8path, unsigned pos)
|
||||
bool
|
||||
spl_remove_index(const char *utf8path, unsigned pos, GError **error_r)
|
||||
{
|
||||
GPtrArray *list;
|
||||
char *uri;
|
||||
enum playlist_result result;
|
||||
|
||||
if (!(list = spl_load(utf8path)))
|
||||
return PLAYLIST_RESULT_NO_SUCH_LIST;
|
||||
GPtrArray *list = spl_load(utf8path, error_r);
|
||||
if (list == NULL)
|
||||
return false;
|
||||
|
||||
if (pos >= list->len) {
|
||||
spl_free(list);
|
||||
return PLAYLIST_RESULT_BAD_RANGE;
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_BAD_RANGE,
|
||||
"Bad range");
|
||||
return false;
|
||||
}
|
||||
|
||||
uri = spl_remove_index_internal(list, pos);
|
||||
g_free(uri);
|
||||
result = spl_save(list, utf8path);
|
||||
bool result = spl_save(list, utf8path, error_r);
|
||||
|
||||
spl_free(list);
|
||||
|
||||
@ -370,38 +435,38 @@ spl_remove_index(const char *utf8path, unsigned pos)
|
||||
return result;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
FILE *file;
|
||||
struct stat st;
|
||||
char *path_fs;
|
||||
|
||||
if (map_spl_path() == NULL)
|
||||
return PLAYLIST_RESULT_DISABLED;
|
||||
if (spl_map(error_r) == NULL)
|
||||
return false;
|
||||
|
||||
if (!spl_valid_name(utf8path))
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
|
||||
path_fs = map_spl_utf8_to_fs(utf8path);
|
||||
char *path_fs = spl_map_to_fs(utf8path, error_r);
|
||||
if (path_fs == NULL)
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
return false;
|
||||
|
||||
file = fopen(path_fs, "a");
|
||||
g_free(path_fs);
|
||||
if (file == NULL)
|
||||
return PLAYLIST_RESULT_ERRNO;
|
||||
if (file == NULL) {
|
||||
playlist_errno(error_r);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fstat(fileno(file), &st) < 0) {
|
||||
int save_errno = errno;
|
||||
playlist_errno(error_r);
|
||||
fclose(file);
|
||||
errno = save_errno;
|
||||
return PLAYLIST_RESULT_ERRNO;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (st.st_size / (MPD_PATH_MAX + 1) >= (off_t)playlist_max_length) {
|
||||
fclose(file);
|
||||
return PLAYLIST_RESULT_TOO_LARGE;
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_TOO_LARGE,
|
||||
"Stored playlist is too large");
|
||||
return false;
|
||||
}
|
||||
|
||||
playlist_print_song(file, song);
|
||||
@ -409,68 +474,79 @@ spl_append_song(const char *utf8path, struct song *song)
|
||||
fclose(file);
|
||||
|
||||
idle_add(IDLE_STORED_PLAYLIST);
|
||||
return PLAYLIST_RESULT_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
enum playlist_result
|
||||
spl_append_uri(const char *url, const char *utf8file)
|
||||
bool
|
||||
spl_append_uri(const char *url, const char *utf8file, GError **error_r)
|
||||
{
|
||||
struct song *song;
|
||||
|
||||
if (uri_has_scheme(url)) {
|
||||
enum playlist_result ret;
|
||||
|
||||
song = song_remote_new(url);
|
||||
ret = spl_append_song(utf8file, song);
|
||||
bool success = spl_append_song(utf8file, song, error_r);
|
||||
song_free(song);
|
||||
return ret;
|
||||
return success;
|
||||
} else {
|
||||
song = db_get_song(url);
|
||||
if (song == NULL)
|
||||
return PLAYLIST_RESULT_NO_SUCH_SONG;
|
||||
if (song == NULL) {
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_NO_SUCH_SONG,
|
||||
"No such song");
|
||||
return false;
|
||||
}
|
||||
|
||||
return spl_append_song(utf8file, song);
|
||||
return spl_append_song(utf8file, song, error_r);
|
||||
}
|
||||
}
|
||||
|
||||
static enum playlist_result
|
||||
spl_rename_internal(const char *from_path_fs, const char *to_path_fs)
|
||||
static bool
|
||||
spl_rename_internal(const char *from_path_fs, const char *to_path_fs,
|
||||
GError **error_r)
|
||||
{
|
||||
if (!g_file_test(from_path_fs, G_FILE_TEST_IS_REGULAR))
|
||||
return PLAYLIST_RESULT_NO_SUCH_LIST;
|
||||
if (!g_file_test(from_path_fs, G_FILE_TEST_IS_REGULAR)) {
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_NO_SUCH_LIST,
|
||||
"No such playlist");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (g_file_test(to_path_fs, G_FILE_TEST_EXISTS))
|
||||
return PLAYLIST_RESULT_LIST_EXISTS;
|
||||
if (g_file_test(to_path_fs, G_FILE_TEST_EXISTS)) {
|
||||
g_set_error_literal(error_r, playlist_quark(),
|
||||
PLAYLIST_RESULT_LIST_EXISTS,
|
||||
"Playlist exists already");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rename(from_path_fs, to_path_fs) < 0)
|
||||
return PLAYLIST_RESULT_ERRNO;
|
||||
if (rename(from_path_fs, to_path_fs) < 0) {
|
||||
playlist_errno(error_r);
|
||||
return false;
|
||||
}
|
||||
|
||||
idle_add(IDLE_STORED_PLAYLIST);
|
||||
return PLAYLIST_RESULT_SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
enum playlist_result
|
||||
spl_rename(const char *utf8from, const char *utf8to)
|
||||
bool
|
||||
spl_rename(const char *utf8from, const char *utf8to, GError **error_r)
|
||||
{
|
||||
char *from_path_fs, *to_path_fs;
|
||||
static enum playlist_result ret;
|
||||
if (spl_map(error_r) == NULL)
|
||||
return false;
|
||||
|
||||
if (map_spl_path() == NULL)
|
||||
return PLAYLIST_RESULT_DISABLED;
|
||||
char *from_path_fs = spl_map_to_fs(utf8from, error_r);
|
||||
if (from_path_fs == NULL)
|
||||
return false;
|
||||
|
||||
if (!spl_valid_name(utf8from) || !spl_valid_name(utf8to))
|
||||
return PLAYLIST_RESULT_BAD_NAME;
|
||||
char *to_path_fs = spl_map_to_fs(utf8to, error_r);
|
||||
if (to_path_fs == NULL) {
|
||||
g_free(from_path_fs);
|
||||
return false;
|
||||
}
|
||||
|
||||
from_path_fs = map_spl_utf8_to_fs(utf8from);
|
||||
to_path_fs = map_spl_utf8_to_fs(utf8to);
|
||||
|
||||
if (from_path_fs != NULL && to_path_fs != NULL)
|
||||
ret = spl_rename_internal(from_path_fs, to_path_fs);
|
||||
else
|
||||
ret = PLAYLIST_RESULT_BAD_NAME;
|
||||
bool success = spl_rename_internal(from_path_fs, to_path_fs, error_r);
|
||||
|
||||
g_free(from_path_fs);
|
||||
g_free(to_path_fs);
|
||||
|
||||
return ret;
|
||||
return success;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user