input/archive: use AtScopeExit() for exception-safety

This commit is contained in:
Max Kellermann 2016-09-09 16:32:11 +02:00
parent 3143dbf3dc
commit 90c8a1b1cf

View File

@ -27,6 +27,7 @@
#include "../InputPlugin.hxx"
#include "fs/Path.hxx"
#include "Log.hxx"
#include "util/ScopeExit.hxx"
#include <stdlib.h>
@ -34,15 +35,17 @@ InputStream *
OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond, Error &error)
{
const ArchivePlugin *arplug;
InputStream *is;
char *pname = strdup(path.c_str());
AtScopeExit(pname) {
free(pname);
};
// 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);
free(pname);
return nullptr;
}
@ -51,22 +54,19 @@ OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond, Error &error)
if (!arplug) {
FormatWarning(archive_domain,
"can't handle archive %s", archive);
free(pname);
return nullptr;
}
auto file = archive_file_open(arplug, Path::FromFS(archive), error);
if (file == nullptr) {
free(pname);
return nullptr;
}
//setup fileops
is = file->OpenStream(filename, mutex, cond, error);
free(pname);
file->Close();
AtScopeExit(file) {
file->Close();
};
return is;
return file->OpenStream(filename, mutex, cond, error);
}
static InputStream *