Commit 67ae033d authored by Max Kellermann's avatar Max Kellermann

ArchiveLookup: in-place editing, avoid string copy

parent 161f7ced
...@@ -22,8 +22,6 @@ ...@@ -22,8 +22,6 @@
#include "ArchiveDomain.hxx" #include "ArchiveDomain.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
...@@ -42,11 +40,11 @@ FindSlash(char *p, size_t i) ...@@ -42,11 +40,11 @@ FindSlash(char *p, size_t i)
gcc_pure gcc_pure
static const char * static const char *
FindSuffix(const char *p, size_t i) FindSuffix(const char *p, const char *i)
{ {
for (; i > 0; --i) { for (; i > p; --i) {
if (p[i] == '.') if (*i == '.')
return p + i + 1; return i + 1;
} }
return nullptr; return nullptr;
...@@ -56,54 +54,51 @@ bool ...@@ -56,54 +54,51 @@ bool
archive_lookup(char *pathname, const char **archive, archive_lookup(char *pathname, const char **archive,
const char **inpath, const char **suffix) const char **inpath, const char **suffix)
{ {
bool ret = false; size_t idx = strlen(pathname);
char *const pathdupe = g_strdup(pathname); char *slash = nullptr;
const size_t len = strlen(pathname);
size_t idx = len;
while (idx > 0) { while (true) {
//try to stat if its real directory //try to stat if its real directory
struct stat st_info; struct stat st_info;
if (stat(pathdupe, &st_info) == -1) { if (stat(pathname, &st_info) == -1) {
if (errno != ENOTDIR) { if (errno != ENOTDIR) {
FormatErrno(archive_domain, FormatErrno(archive_domain,
"Failed to stat %s", pathdupe); "Failed to stat %s", pathname);
break; 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 (idx == len) { if (slash == nullptr)
break; return false;
}
//its a file ? //its a file ?
if (S_ISREG(st_info.st_mode)) { if (S_ISREG(st_info.st_mode)) {
//so the upper should be file //so the upper should be file
pathname[idx] = 0;
ret = true;
*archive = pathname; *archive = pathname;
*inpath = pathname + idx+1; *inpath = slash + 1;
//try to get suffix //try to get suffix
*suffix = FindSuffix(pathname, idx); *suffix = FindSuffix(pathname, slash - 1);
break; return true;
} else { } else {
FormatError(archive_domain, FormatError(archive_domain,
"Not a regular file: %s", "Not a regular file: %s",
pathdupe); pathname);
break; return false;
} }
} }
//find one dir up //find one dir up
char *slash = FindSlash(pathdupe, idx); if (slash != nullptr)
*slash = '/';
slash = FindSlash(pathname, idx - 1);
if (slash == nullptr) if (slash == nullptr)
break; return false;
*slash = 0; *slash = 0;
idx = slash - pathdupe; idx = slash - pathname;
} }
g_free(pathdupe);
return ret;
} }
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