archive/ArchiveLookup: throw on error

This commit is contained in:
Max Kellermann 2019-05-31 18:52:11 +02:00
parent 2f83ed90d0
commit fa13648f2c
4 changed files with 19 additions and 10 deletions

View File

@ -20,6 +20,7 @@
#include "ArchiveLookup.hxx" #include "ArchiveLookup.hxx"
#include "ArchiveDomain.hxx" #include "ArchiveDomain.hxx"
#include "Log.hxx" #include "Log.hxx"
#include "system/Error.hxx"
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -60,11 +61,9 @@ archive_lookup(char *pathname, const char **archive,
//try to stat if its real directory //try to stat if its real directory
struct stat st_info; struct stat st_info;
if (stat(pathname, &st_info) == -1) { if (stat(pathname, &st_info) == -1) {
if (errno != ENOTDIR) { int e = errno;
FormatErrno(archive_domain, if (e != ENOTDIR)
"Failed to stat %s", pathname); throw FormatErrno(e, "Failed to stat %s", pathname);
return false;
}
} else { } else {
//is something found ins original path (is not an archive) //is something found ins original path (is not an archive)
if (slash == nullptr) if (slash == nullptr)

View File

@ -36,6 +36,8 @@
* is split into archive: /music/path/Talco.zip * is split into archive: /music/path/Talco.zip
* inarchive pathname: Talco - Combat Circus/12 - A la pachenka.mp3 * inarchive pathname: Talco - Combat Circus/12 - A la pachenka.mp3
* and suffix: zip * and suffix: zip
*
* Throws on error.
*/ */
bool bool
archive_lookup(char *pathname, const char **archive, archive_lookup(char *pathname, const char **archive,

View File

@ -45,11 +45,17 @@ OpenArchiveInputStream(Path path, Mutex &mutex)
// archive_lookup will modify pname when true is returned // archive_lookup will modify pname when true is returned
const char *archive, *filename, *suffix; const char *archive, *filename, *suffix;
try {
if (!archive_lookup(pname, &archive, &filename, &suffix)) { if (!archive_lookup(pname, &archive, &filename, &suffix)) {
FormatDebug(archive_domain, FormatDebug(archive_domain,
"not an archive, lookup %s failed", pname); "not an archive, lookup %s failed", pname);
return nullptr; return nullptr;
} }
} catch (...) {
LogFormat(LogLevel::DEBUG, std::current_exception(),
"not an archive, lookup %s failed", pname);
return nullptr;
}
//check which archive plugin to use (by ext) //check which archive plugin to use (by ext)
arplug = archive_plugin_from_suffix(suffix); arplug = archive_plugin_from_suffix(suffix);

View File

@ -11,7 +11,8 @@ TEST(ArchiveTest, Lookup)
const char *archive, *inpath, *suffix; const char *archive, *inpath, *suffix;
char *path = strdup(""); char *path = strdup("");
EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix)); EXPECT_THROW(archive_lookup(path, &archive, &inpath, &suffix),
std::system_error);
free(path); free(path);
path = strdup("."); path = strdup(".");
@ -23,7 +24,8 @@ TEST(ArchiveTest, Lookup)
free(path); free(path);
path = strdup("src/foo/bar"); 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); free(path);
fclose(fopen("dummy", "w")); fclose(fopen("dummy", "w"));