diff --git a/core/tags/library.c b/core/tags/library.c index 4eede46a..2fe49002 100644 --- a/core/tags/library.c +++ b/core/tags/library.c @@ -1,6 +1,7 @@ /* * Copyright 2014 (c) Anna Schumaker. */ +#include #include #define LIBRARY_DB_MIN 0 /* Ocarina 6.0 */ @@ -82,12 +83,22 @@ const struct database *library_db_get() struct library *library_find(const gchar *path) { - return LIBRARY(db_find(&library_db, path)); + struct library *library = library_lookup(path); + if (library) + return library; + return LIBRARY(db_insert(&library_db, path)); } struct library *library_lookup(const gchar *path) { - return LIBRARY(db_get(&library_db, path)); + struct db_entry *dbe, *next; + + db_for_each(dbe, next, &library_db) { + if (string_is_subdir(path, LIBRARY(dbe)->li_path)) + return LIBRARY(dbe); + } + + return NULL; } struct library *library_get(const unsigned int index) diff --git a/include/core/tags/library.h b/include/core/tags/library.h index d14cf9b4..c38d7505 100644 --- a/include/core/tags/library.h +++ b/include/core/tags/library.h @@ -37,9 +37,11 @@ bool library_db_defrag(); const struct database *library_db_get(); /* - * Called to find a library tag by library path. The difference is that + * Called to find a library tag by path. The difference is that * library_find() will allocate a new library struct if the requested one * doesn't exist yet, but library_lookup() will return NULL in this situation. + * + * Note that path may be a subdirectory of the returned library. */ struct library *library_find(const gchar *); struct library *library_lookup(const gchar *); diff --git a/tests/core/tags/library.c b/tests/core/tags/library.c index eab2dc97..8fed23cd 100644 --- a/tests/core/tags/library.c +++ b/tests/core/tags/library.c @@ -69,14 +69,15 @@ static void test_library_db() g_assert_cmpuint(library_db_get()->db_size, ==, 0); library_db_init(); - library = library_lookup("/home/Zelda/Music"); - g_assert_null(library); + g_assert_null(library_lookup("/home/Zelda/Music")); library = library_find("/home/Zelda/Music"); test_verify_zelda(library); g_assert_cmpuint(library_db_get()->db_size, ==, 1); g_assert(library_lookup("/home/Zelda/Music") == library); + g_assert(library_lookup("/home/Zelda/Music/Ocarina") == library); g_assert(library_find("/home/Zelda/Music") == library); + g_assert(library_find("/home/Zelda/Music/Ocarina") == library); g_assert(library_get(0) == library); g_assert_null(library_get(1));