From fa13648f2cf9269187f89a78b2274cab9c78d457 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 31 May 2019 18:52:11 +0200 Subject: [PATCH] archive/ArchiveLookup: throw on error --- src/archive/ArchiveLookup.cxx | 9 ++++----- src/archive/ArchiveLookup.hxx | 2 ++ src/input/plugins/ArchiveInputPlugin.cxx | 12 +++++++++--- test/test_archive.cxx | 6 ++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/archive/ArchiveLookup.cxx b/src/archive/ArchiveLookup.cxx index 705ac28f8..fac171f02 100644 --- a/src/archive/ArchiveLookup.cxx +++ b/src/archive/ArchiveLookup.cxx @@ -20,6 +20,7 @@ #include "ArchiveLookup.hxx" #include "ArchiveDomain.hxx" #include "Log.hxx" +#include "system/Error.hxx" #include #include @@ -60,11 +61,9 @@ archive_lookup(char *pathname, const char **archive, //try to stat if its real directory struct stat st_info; if (stat(pathname, &st_info) == -1) { - if (errno != ENOTDIR) { - FormatErrno(archive_domain, - "Failed to stat %s", pathname); - return false; - } + int e = errno; + if (e != ENOTDIR) + throw FormatErrno(e, "Failed to stat %s", pathname); } else { //is something found ins original path (is not an archive) if (slash == nullptr) diff --git a/src/archive/ArchiveLookup.hxx b/src/archive/ArchiveLookup.hxx index ba7251f1b..c217d56f2 100644 --- a/src/archive/ArchiveLookup.hxx +++ b/src/archive/ArchiveLookup.hxx @@ -36,6 +36,8 @@ * is split into archive: /music/path/Talco.zip * inarchive pathname: Talco - Combat Circus/12 - A la pachenka.mp3 * and suffix: zip + * + * Throws on error. */ bool archive_lookup(char *pathname, const char **archive, diff --git a/src/input/plugins/ArchiveInputPlugin.cxx b/src/input/plugins/ArchiveInputPlugin.cxx index b7f12f74b..0f3954755 100644 --- a/src/input/plugins/ArchiveInputPlugin.cxx +++ b/src/input/plugins/ArchiveInputPlugin.cxx @@ -45,9 +45,15 @@ OpenArchiveInputStream(Path path, Mutex &mutex) // archive_lookup will modify pname when true is returned const char *archive, *filename, *suffix; - if (!archive_lookup(pname, &archive, &filename, &suffix)) { - FormatDebug(archive_domain, - "not an archive, lookup %s failed", pname); + try { + if (!archive_lookup(pname, &archive, &filename, &suffix)) { + FormatDebug(archive_domain, + "not an archive, lookup %s failed", pname); + return nullptr; + } + } catch (...) { + LogFormat(LogLevel::DEBUG, std::current_exception(), + "not an archive, lookup %s failed", pname); return nullptr; } diff --git a/test/test_archive.cxx b/test/test_archive.cxx index a5f3dd663..7b92cea4b 100644 --- a/test/test_archive.cxx +++ b/test/test_archive.cxx @@ -11,7 +11,8 @@ TEST(ArchiveTest, Lookup) const char *archive, *inpath, *suffix; char *path = strdup(""); - EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_THROW(archive_lookup(path, &archive, &inpath, &suffix), + std::system_error); free(path); path = strdup("."); @@ -23,7 +24,8 @@ TEST(ArchiveTest, Lookup) free(path); path = strdup("src/foo/bar"); - EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix)); + EXPECT_THROW(archive_lookup(path, &archive, &inpath, &suffix), + std::system_error); free(path); fclose(fopen("dummy", "w"));