util/Error: new error passing library

Replaces GLib's GError.
This commit is contained in:
Max Kellermann
2013-08-10 18:02:44 +02:00
parent c9fcc7f148
commit 29030b54c9
256 changed files with 3269 additions and 3371 deletions

View File

@@ -25,7 +25,6 @@
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "Song.hxx"
#include "io_error.h"
#include "Mapper.hxx"
#include "TextFile.hxx"
#include "conf.h"
@@ -34,6 +33,7 @@
#include "fs/FileSystem.hxx"
#include "fs/DirectoryReader.hxx"
#include "util/UriUtil.hxx"
#include "util/Error.hxx"
#include <assert.h>
#include <sys/types.h>
@@ -79,22 +79,20 @@ spl_valid_name(const char *name_utf8)
}
static const Path &
spl_map(GError **error_r)
spl_map(Error &error)
{
const Path &path_fs = map_spl_path();
if (path_fs.IsNull())
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_DISABLED,
"Stored playlists are disabled");
error.Set(playlist_domain, PLAYLIST_RESULT_DISABLED,
"Stored playlists are disabled");
return path_fs;
}
static bool
spl_check_name(const char *name_utf8, GError **error_r)
spl_check_name(const char *name_utf8, Error &error)
{
if (!spl_valid_name(name_utf8)) {
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_BAD_NAME,
error.Set(playlist_domain, PLAYLIST_RESULT_BAD_NAME,
"Bad playlist name");
return false;
}
@@ -103,35 +101,33 @@ spl_check_name(const char *name_utf8, GError **error_r)
}
static Path
spl_map_to_fs(const char *name_utf8, GError **error_r)
spl_map_to_fs(const char *name_utf8, Error &error)
{
if (spl_map(error_r).IsNull() || !spl_check_name(name_utf8, error_r))
if (spl_map(error).IsNull() || !spl_check_name(name_utf8, error))
return Path::Null();
Path path_fs = map_spl_utf8_to_fs(name_utf8);
if (path_fs.IsNull())
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_BAD_NAME,
"Bad playlist name");
error.Set(playlist_domain, PLAYLIST_RESULT_BAD_NAME,
"Bad playlist name");
return path_fs;
}
/**
* Create a GError for the current errno.
* Create an #Error for the current errno.
*/
static void
playlist_errno(GError **error_r)
playlist_errno(Error &error)
{
switch (errno) {
case ENOENT:
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_NO_SUCH_LIST,
"No such playlist");
error.Set(playlist_domain, PLAYLIST_RESULT_NO_SUCH_LIST,
"No such playlist");
break;
default:
set_error_errno(error_r);
error.SetErrno();
break;
}
}
@@ -168,17 +164,17 @@ LoadPlaylistFileInfo(PlaylistInfo &info,
}
PlaylistVector
ListPlaylistFiles(GError **error_r)
ListPlaylistFiles(Error &error)
{
PlaylistVector list;
const Path &parent_path_fs = spl_map(error_r);
const Path &parent_path_fs = spl_map(error);
if (parent_path_fs.IsNull())
return list;
DirectoryReader reader(parent_path_fs);
if (reader.HasFailed()) {
set_error_errno(error_r);
error.SetErrno();
return list;
}
@@ -194,20 +190,20 @@ ListPlaylistFiles(GError **error_r)
static bool
SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path,
GError **error_r)
Error &error)
{
assert(utf8path != NULL);
if (spl_map(error_r).IsNull())
if (spl_map(error).IsNull())
return false;
const Path path_fs = spl_map_to_fs(utf8path, error_r);
const Path path_fs = spl_map_to_fs(utf8path, error);
if (path_fs.IsNull())
return false;
FILE *file = FOpen(path_fs, FOpenMode::WriteText);
if (file == NULL) {
playlist_errno(error_r);
playlist_errno(error);
return false;
}
@@ -219,20 +215,20 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path,
}
PlaylistFileContents
LoadPlaylistFile(const char *utf8path, GError **error_r)
LoadPlaylistFile(const char *utf8path, Error &error)
{
PlaylistFileContents contents;
if (spl_map(error_r).IsNull())
if (spl_map(error).IsNull())
return contents;
const Path path_fs = spl_map_to_fs(utf8path, error_r);
const Path path_fs = spl_map_to_fs(utf8path, error);
if (path_fs.IsNull())
return contents;
TextFile file(path_fs);
if (file.HasFailed()) {
playlist_errno(error_r);
playlist_errno(error);
return contents;
}
@@ -262,24 +258,20 @@ LoadPlaylistFile(const char *utf8path, GError **error_r)
bool
spl_move_index(const char *utf8path, unsigned src, unsigned dest,
GError **error_r)
Error &error)
{
if (src == dest)
/* this doesn't check whether the playlist exists, but
what the hell.. */
return true;
GError *error = nullptr;
auto contents = LoadPlaylistFile(utf8path, &error);
if (contents.empty() && error != nullptr) {
g_propagate_error(error_r, error);
auto contents = LoadPlaylistFile(utf8path, error);
if (contents.empty() && error.IsDefined())
return false;
}
if (src >= contents.size() || dest >= contents.size()) {
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_BAD_RANGE,
"Bad range");
error.Set(playlist_domain, PLAYLIST_RESULT_BAD_RANGE,
"Bad range");
return false;
}
@@ -290,25 +282,25 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest,
const auto dest_i = std::next(contents.begin(), dest);
contents.insert(dest_i, std::move(value));
bool result = SavePlaylistFile(contents, utf8path, error_r);
bool result = SavePlaylistFile(contents, utf8path, error);
idle_add(IDLE_STORED_PLAYLIST);
return result;
}
bool
spl_clear(const char *utf8path, GError **error_r)
spl_clear(const char *utf8path, Error &error)
{
if (spl_map(error_r).IsNull())
if (spl_map(error).IsNull())
return false;
const Path path_fs = spl_map_to_fs(utf8path, error_r);
const Path path_fs = spl_map_to_fs(utf8path, error);
if (path_fs.IsNull())
return false;
FILE *file = FOpen(path_fs, FOpenMode::WriteText);
if (file == NULL) {
playlist_errno(error_r);
playlist_errno(error);
return false;
}
@@ -319,14 +311,14 @@ spl_clear(const char *utf8path, GError **error_r)
}
bool
spl_delete(const char *name_utf8, GError **error_r)
spl_delete(const char *name_utf8, Error &error)
{
const Path path_fs = spl_map_to_fs(name_utf8, error_r);
const Path path_fs = spl_map_to_fs(name_utf8, error);
if (path_fs.IsNull())
return false;
if (!RemoveFile(path_fs)) {
playlist_errno(error_r);
playlist_errno(error);
return false;
}
@@ -335,58 +327,53 @@ spl_delete(const char *name_utf8, GError **error_r)
}
bool
spl_remove_index(const char *utf8path, unsigned pos, GError **error_r)
spl_remove_index(const char *utf8path, unsigned pos, Error &error)
{
GError *error = nullptr;
auto contents = LoadPlaylistFile(utf8path, &error);
if (contents.empty() && error != nullptr) {
g_propagate_error(error_r, error);
auto contents = LoadPlaylistFile(utf8path, error);
if (contents.empty() && error.IsDefined())
return false;
}
if (pos >= contents.size()) {
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_BAD_RANGE,
"Bad range");
error.Set(playlist_domain, PLAYLIST_RESULT_BAD_RANGE,
"Bad range");
return false;
}
contents.erase(std::next(contents.begin(), pos));
bool result = SavePlaylistFile(contents, utf8path, error_r);
bool result = SavePlaylistFile(contents, utf8path, error);
idle_add(IDLE_STORED_PLAYLIST);
return result;
}
bool
spl_append_song(const char *utf8path, Song *song, GError **error_r)
spl_append_song(const char *utf8path, Song *song, Error &error)
{
if (spl_map(error_r).IsNull())
if (spl_map(error).IsNull())
return false;
const Path path_fs = spl_map_to_fs(utf8path, error_r);
const Path path_fs = spl_map_to_fs(utf8path, error);
if (path_fs.IsNull())
return false;
FILE *file = FOpen(path_fs, FOpenMode::AppendText);
if (file == NULL) {
playlist_errno(error_r);
playlist_errno(error);
return false;
}
struct stat st;
if (fstat(fileno(file), &st) < 0) {
playlist_errno(error_r);
playlist_errno(error);
fclose(file);
return false;
}
if (st.st_size / (MPD_PATH_MAX + 1) >= (off_t)playlist_max_length) {
fclose(file);
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_TOO_LARGE,
"Stored playlist is too large");
error.Set(playlist_domain, PLAYLIST_RESULT_TOO_LARGE,
"Stored playlist is too large");
return false;
}
@@ -399,23 +386,23 @@ spl_append_song(const char *utf8path, Song *song, GError **error_r)
}
bool
spl_append_uri(const char *url, const char *utf8file, GError **error_r)
spl_append_uri(const char *url, const char *utf8file, Error &error)
{
if (uri_has_scheme(url)) {
Song *song = Song::NewRemote(url);
bool success = spl_append_song(utf8file, song, error_r);
bool success = spl_append_song(utf8file, song, error);
song->Free();
return success;
} else {
const Database *db = GetDatabase(error_r);
const Database *db = GetDatabase(error);
if (db == nullptr)
return false;
Song *song = db->GetSong(url, error_r);
Song *song = db->GetSong(url, error);
if (song == nullptr)
return false;
bool success = spl_append_song(utf8file, song, error_r);
bool success = spl_append_song(utf8file, song, error);
db->ReturnSong(song);
return success;
}
@@ -423,24 +410,22 @@ spl_append_uri(const char *url, const char *utf8file, GError **error_r)
static bool
spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs,
GError **error_r)
Error &error)
{
if (!FileExists(from_path_fs)) {
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_NO_SUCH_LIST,
"No such playlist");
error.Set(playlist_domain, PLAYLIST_RESULT_NO_SUCH_LIST,
"No such playlist");
return false;
}
if (FileExists(to_path_fs)) {
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_LIST_EXISTS,
"Playlist exists already");
error.Set(playlist_domain, PLAYLIST_RESULT_LIST_EXISTS,
"Playlist exists already");
return false;
}
if (!RenameFile(from_path_fs, to_path_fs)) {
playlist_errno(error_r);
playlist_errno(error);
return false;
}
@@ -449,18 +434,18 @@ spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs,
}
bool
spl_rename(const char *utf8from, const char *utf8to, GError **error_r)
spl_rename(const char *utf8from, const char *utf8to, Error &error)
{
if (spl_map(error_r).IsNull())
if (spl_map(error).IsNull())
return false;
Path from_path_fs = spl_map_to_fs(utf8from, error_r);
Path from_path_fs = spl_map_to_fs(utf8from, error);
if (from_path_fs.IsNull())
return false;
Path to_path_fs = spl_map_to_fs(utf8to, error_r);
Path to_path_fs = spl_map_to_fs(utf8to, error);
if (to_path_fs.IsNull())
return false;
return spl_rename_internal(from_path_fs, to_path_fs, error_r);
return spl_rename_internal(from_path_fs, to_path_fs, error);
}