archive/ArchiveLookup: remove "suffix" output parameter

Let the caller do this.  Our GetSuffix() function was broken anyway.
This commit is contained in:
Max Kellermann 2019-05-31 19:01:22 +02:00
parent 508ba22789
commit 12e75a523a
4 changed files with 15 additions and 31 deletions

View File

@ -36,21 +36,9 @@ FindSlash(char *p, size_t i) noexcept
return nullptr;
}
gcc_pure
static const char *
FindSuffix(const char *p, const char *i) noexcept
{
for (; i > p; --i) {
if (*i == '.')
return i + 1;
}
return nullptr;
}
bool
archive_lookup(char *pathname, const char **archive,
const char **inpath, const char **suffix)
const char **inpath)
{
size_t idx = strlen(pathname);
@ -70,9 +58,6 @@ archive_lookup(char *pathname, const char **archive,
//so the upper should be file
*archive = pathname;
*inpath = slash + 1;
//try to get suffix
*suffix = FindSuffix(pathname, slash - 1);
return true;
} else {
FormatError(archive_domain,

View File

@ -24,24 +24,23 @@
*
* archive_lookup is used to determine if part of pathname refers to an regular
* file (archive). If so then its also used to split pathname into archive file
* and path used to locate file in archive. It also returns suffix of the file.
* and path used to locate file in archive.
* How it works:
* We do stat of the parent of input pathname as long as we find an regular file
* Normally this should never happen. When routine returns true pathname modified
* and split into archive, inpath and suffix. Otherwise nothing happens
* and split into archive and inpath. Otherwise nothing happens
*
* For example:
*
* /music/path/Talco.zip/Talco - Combat Circus/12 - A la pachenka.mp3
* 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,
const char **inpath, const char **suffix);
const char **inpath);
#endif

View File

@ -44,9 +44,9 @@ OpenArchiveInputStream(Path path, Mutex &mutex)
};
// archive_lookup will modify pname when true is returned
const char *archive, *filename, *suffix;
const char *archive, *filename;
try {
if (!archive_lookup(pname, &archive, &filename, &suffix)) {
if (!archive_lookup(pname, &archive, &filename)) {
FormatDebug(archive_domain,
"not an archive, lookup %s failed", pname);
return nullptr;
@ -57,6 +57,8 @@ OpenArchiveInputStream(Path path, Mutex &mutex)
return nullptr;
}
const char *suffix = Path::FromFS(archive).GetSuffix();
//check which archive plugin to use (by ext)
arplug = archive_plugin_from_suffix(suffix);
if (!arplug) {

View File

@ -8,41 +8,39 @@
TEST(ArchiveTest, Lookup)
{
const char *archive, *inpath, *suffix;
const char *archive, *inpath;
char *path = strdup("");
EXPECT_THROW(archive_lookup(path, &archive, &inpath, &suffix),
EXPECT_THROW(archive_lookup(path, &archive, &inpath),
std::system_error);
free(path);
path = strdup(".");
EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix));
EXPECT_FALSE(archive_lookup(path, &archive, &inpath));
free(path);
path = strdup("config.h");
EXPECT_FALSE(archive_lookup(path, &archive, &inpath, &suffix));
EXPECT_FALSE(archive_lookup(path, &archive, &inpath));
free(path);
path = strdup("src/foo/bar");
EXPECT_THROW(archive_lookup(path, &archive, &inpath, &suffix),
EXPECT_THROW(archive_lookup(path, &archive, &inpath),
std::system_error);
free(path);
fclose(fopen("dummy", "w"));
path = strdup("dummy/foo/bar");
EXPECT_TRUE(archive_lookup(path, &archive, &inpath, &suffix));
EXPECT_TRUE(archive_lookup(path, &archive, &inpath));
EXPECT_EQ((const char *)path, archive);
EXPECT_STREQ(archive, "dummy");
EXPECT_STREQ(inpath, "foo/bar");
EXPECT_EQ((const char *)nullptr, suffix);
free(path);
path = strdup("config.h/foo/bar");
EXPECT_TRUE(archive_lookup(path, &archive, &inpath, &suffix));
EXPECT_TRUE(archive_lookup(path, &archive, &inpath));
EXPECT_EQ((const char *)path, archive);
EXPECT_STREQ(archive, "config.h");
EXPECT_STREQ(inpath, "foo/bar");
EXPECT_STREQ(suffix, "h");
free(path);
}