Commit be79b44d authored by Max Kellermann's avatar Max Kellermann

archive/Lookup: pass const pointer

parent 17f207ff
...@@ -35,16 +35,17 @@ FindSlash(PathTraitsFS::pointer_type p, size_t i) noexcept ...@@ -35,16 +35,17 @@ FindSlash(PathTraitsFS::pointer_type p, size_t i) noexcept
} }
ArchiveLookupResult ArchiveLookupResult
archive_lookup(PathTraitsFS::pointer_type pathname) archive_lookup(PathTraitsFS::const_pointer_type pathname)
{ {
size_t idx = strlen(pathname); PathTraitsFS::string buffer(pathname);
size_t idx = buffer.size();
PathTraitsFS::pointer_type slash = nullptr; PathTraitsFS::pointer_type slash = nullptr;
while (true) { while (true) {
try { try {
//try to stat if its real directory //try to stat if its real directory
const FileInfo file_info(Path::FromFS(pathname)); const FileInfo file_info(Path::FromFS(buffer.c_str()));
//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)
...@@ -53,7 +54,7 @@ archive_lookup(PathTraitsFS::pointer_type pathname) ...@@ -53,7 +54,7 @@ archive_lookup(PathTraitsFS::pointer_type pathname)
//its a file ? //its a file ?
if (file_info.IsRegular()) { if (file_info.IsRegular()) {
//so the upper should be file //so the upper should be file
return {AllocatedPath::FromFS(pathname), AllocatedPath::FromFS(slash + 1)}; return {AllocatedPath::FromFS(buffer.c_str()), AllocatedPath::FromFS(slash + 1)};
} else { } else {
return {}; return {};
} }
...@@ -66,12 +67,12 @@ archive_lookup(PathTraitsFS::pointer_type pathname) ...@@ -66,12 +67,12 @@ archive_lookup(PathTraitsFS::pointer_type pathname)
if (slash != nullptr) if (slash != nullptr)
*slash = '/'; *slash = '/';
slash = FindSlash(pathname, idx - 1); slash = FindSlash(&buffer.front(), idx - 1);
if (slash == nullptr) if (slash == nullptr)
return {}; return {};
*slash = 0; *slash = 0;
idx = slash - pathname; idx = slash - buffer.c_str();
} }
} }
...@@ -50,7 +50,7 @@ struct ArchiveLookupResult { ...@@ -50,7 +50,7 @@ struct ArchiveLookupResult {
* Throws on error. * Throws on error.
*/ */
ArchiveLookupResult ArchiveLookupResult
archive_lookup(PathTraitsFS::pointer_type pathname); archive_lookup(PathTraitsFS::const_pointer_type pathname);
#endif #endif
...@@ -25,30 +25,22 @@ ...@@ -25,30 +25,22 @@
#include "../InputStream.hxx" #include "../InputStream.hxx"
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "Log.hxx" #include "Log.hxx"
#include "util/ScopeExit.hxx"
#include <string.h>
InputStreamPtr InputStreamPtr
OpenArchiveInputStream(Path path, Mutex &mutex) OpenArchiveInputStream(Path path, Mutex &mutex)
{ {
const ArchivePlugin *arplug; const ArchivePlugin *arplug;
char *pname = strdup(path.c_str());
AtScopeExit(pname) {
free(pname);
};
// archive_lookup will modify pname when true is returned // archive_lookup will modify pname when true is returned
ArchiveLookupResult l; ArchiveLookupResult l;
try { try {
l = archive_lookup(pname); l = archive_lookup(path.c_str());
if (l.archive.IsNull()) { if (l.archive.IsNull()) {
return nullptr; return nullptr;
} }
} catch (...) { } catch (...) {
LogFormat(LogLevel::DEBUG, std::current_exception(), LogFormat(LogLevel::DEBUG, std::current_exception(),
"not an archive, lookup %s failed", pname); "not an archive, lookup %s failed", path.c_str());
return nullptr; return nullptr;
} }
......
...@@ -8,35 +8,23 @@ ...@@ -8,35 +8,23 @@
TEST(ArchiveTest, Lookup) TEST(ArchiveTest, Lookup)
{ {
char *path = strdup(""); EXPECT_THROW(archive_lookup(""), std::system_error);
EXPECT_THROW(archive_lookup(path), std::system_error);
free(path);
path = strdup("."); EXPECT_FALSE(archive_lookup("."));
EXPECT_FALSE(archive_lookup(path));
free(path);
path = strdup("config.h"); EXPECT_FALSE(archive_lookup("config.h"));
EXPECT_FALSE(archive_lookup(path));
free(path);
path = strdup("src/foo/bar"); EXPECT_THROW(archive_lookup("src/foo/bar"), std::system_error);
EXPECT_THROW(archive_lookup(path), std::system_error);
free(path);
fclose(fopen("dummy", "w")); fclose(fopen("dummy", "w"));
path = strdup("dummy/foo/bar"); auto result = archive_lookup("dummy/foo/bar");
auto result = archive_lookup(path);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_STREQ(result.archive.c_str(), "dummy"); EXPECT_STREQ(result.archive.c_str(), "dummy");
EXPECT_STREQ(result.inside.c_str(), "foo/bar"); EXPECT_STREQ(result.inside.c_str(), "foo/bar");
free(path);
path = strdup("config.h/foo/bar"); result = archive_lookup("config.h/foo/bar");
result = archive_lookup(path);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_STREQ(result.archive.c_str(), "config.h"); EXPECT_STREQ(result.archive.c_str(), "config.h");
EXPECT_STREQ(result.inside.c_str(), "foo/bar"); EXPECT_STREQ(result.inside.c_str(), "foo/bar");
free(path);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment