core/collection: Clean up error handling code

I was assuming that my disk returns -EIO when it enters its failed
state, but it really seems to be return -ENOENT.  Let's have this code
check for any error, rather than one specific one.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-23 09:39:37 -04:00
parent 08bf842767
commit aad90782ba
3 changed files with 16 additions and 29 deletions

View File

@ -5,7 +5,6 @@
#include <core/idle.h>
#include <core/playlist.h>
#include <errno.h>
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
@ -23,28 +22,18 @@ static void __scan_dir(void *);
#ifdef CONFIG_TESTING
bool test_collection_eio = false;
static inline int __g_access(const gchar *path, int mode)
bool test_collection_error = false;
static inline int __g_access(const gchar *path)
{
if (test_collection_eio) {
errno = EIO;
if (test_collection_error)
return -1;
}
return g_access(path, F_OK);
}
#else
#define __g_access g_access
#define __g_access(path) g_access(path, F_OK)
#endif /* CONFIG_TESTING */
static inline int __check_access(const gchar *path)
{
int ret = __g_access(path, F_OK);
if (ret == -1)
return -errno;
return ret;
}
static void __scan_dir_later(struct library *library, const gchar *dir)
{
struct scan_data *data = g_malloc(sizeof(struct scan_data));
@ -111,14 +100,12 @@ static void __validate_library(void *data)
continue;
path = track_path(track);
access = __check_access(path);
access = __g_access(path);
g_free(path);
if (access == -EIO) {
if (collection_check_library(library) == -EIO)
return;
}
if (access < 0) {
if (collection_check_library(library) < 0)
return;
queue_remove_all(&c_queue, track);
track_remove(track);
}
@ -255,7 +242,7 @@ void collection_set_enabled(struct library *library, bool enabled)
if (!library || (library->li_enabled == enabled))
return;
if (enabled && (collection_check_library(library) == -EIO))
if (enabled && (collection_check_library(library) < 0))
return;
library_set_enabled(library, enabled);
@ -278,8 +265,8 @@ void collection_set_enabled(struct library *library, bool enabled)
int collection_check_library(struct library *library)
{
int ret = __check_access(library->li_path);
if (ret == -EIO) {
int ret = __g_access(library->li_path);
if (ret < 0) {
g_warning("Can't access library at %s/\n", library->li_path);
collection_set_enabled(library, false);
}

View File

@ -56,7 +56,7 @@ struct queue *collection_get_queue();
#ifdef CONFIG_TESTING
extern bool test_collection_eio;
extern bool test_collection_error;
#endif /* CONFIG_TESTING */
#endif /* OCARINA_CORE_COLLECTION_H */

View File

@ -236,8 +236,8 @@ static void test_eio()
test_equal(lib->li_enabled, (bool)true);
test_equal(queue_size(q), 48);
test_collection_eio = true;
test_equal(collection_check_library(lib), -EIO);
test_collection_error = true;
test_equal(collection_check_library(lib), -1);
test_equal(lib->li_enabled, (bool)false);
test_equal(queue_size(q), 0);
@ -245,18 +245,18 @@ static void test_eio()
test_equal(lib->li_enabled, (bool)false);
test_equal(queue_size(q), 0);
test_collection_eio = false;
test_collection_error = false;
collection_set_enabled(lib, true);
test_equal(lib->li_enabled, (bool)true);
test_equal(queue_size(q), 48);
test_collection_eio = true;
test_collection_error = true;
collection_update_all();
test_equal(idle_run_task(), (bool)true);
test_equal(lib->li_enabled, (bool)false);
test_equal(queue_size(q), 0);
test_collection_eio = false;
test_collection_error = false;
collection_set_enabled(lib, true);
test_equal(lib->li_enabled, (bool)true);
test_equal(queue_size(q), 48);