2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-12-16 21:42:34 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-12-16 21:42:34 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-24 19:18:58 +01:00
|
|
|
#include "ArchiveLookup.hxx"
|
2013-10-15 23:24:54 +02:00
|
|
|
#include "ArchiveDomain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-11-11 14:15:34 +01:00
|
|
|
|
2008-12-16 21:42:34 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2013-09-27 22:31:24 +02:00
|
|
|
|
2013-10-17 00:44:57 +02:00
|
|
|
gcc_pure
|
|
|
|
static char *
|
2017-05-08 14:44:49 +02:00
|
|
|
FindSlash(char *p, size_t i) noexcept
|
2013-10-17 00:44:57 +02:00
|
|
|
{
|
|
|
|
for (; i > 0; --i)
|
|
|
|
if (p[i] == '/')
|
|
|
|
return p + i;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-17 00:35:46 +02:00
|
|
|
gcc_pure
|
|
|
|
static const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
FindSuffix(const char *p, const char *i) noexcept
|
2013-10-17 00:35:46 +02:00
|
|
|
{
|
2013-10-17 00:54:20 +02:00
|
|
|
for (; i > p; --i) {
|
|
|
|
if (*i == '.')
|
|
|
|
return i + 1;
|
2013-10-17 00:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-17 00:35:58 +02:00
|
|
|
bool
|
|
|
|
archive_lookup(char *pathname, const char **archive,
|
|
|
|
const char **inpath, const char **suffix)
|
2008-12-16 21:42:34 +01:00
|
|
|
{
|
2013-10-17 00:54:20 +02:00
|
|
|
size_t idx = strlen(pathname);
|
2008-12-16 21:42:34 +01:00
|
|
|
|
2013-10-17 00:54:20 +02:00
|
|
|
char *slash = nullptr;
|
2008-12-16 21:42:34 +01:00
|
|
|
|
2013-10-17 00:54:20 +02:00
|
|
|
while (true) {
|
2008-12-16 21:42:34 +01:00
|
|
|
//try to stat if its real directory
|
2013-10-17 00:48:58 +02:00
|
|
|
struct stat st_info;
|
2013-10-17 00:54:20 +02:00
|
|
|
if (stat(pathname, &st_info) == -1) {
|
2008-12-16 21:42:34 +01:00
|
|
|
if (errno != ENOTDIR) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatErrno(archive_domain,
|
2013-10-17 00:54:20 +02:00
|
|
|
"Failed to stat %s", pathname);
|
|
|
|
return false;
|
2008-12-16 21:42:34 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//is something found ins original path (is not an archive)
|
2013-10-17 00:54:20 +02:00
|
|
|
if (slash == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2008-12-16 21:42:34 +01:00
|
|
|
//its a file ?
|
|
|
|
if (S_ISREG(st_info.st_mode)) {
|
|
|
|
//so the upper should be file
|
|
|
|
*archive = pathname;
|
2013-10-17 00:54:20 +02:00
|
|
|
*inpath = slash + 1;
|
2008-12-16 21:42:34 +01:00
|
|
|
|
|
|
|
//try to get suffix
|
2013-10-17 00:54:20 +02:00
|
|
|
*suffix = FindSuffix(pathname, slash - 1);
|
|
|
|
return true;
|
2008-12-16 21:42:34 +01:00
|
|
|
} else {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(archive_domain,
|
|
|
|
"Not a regular file: %s",
|
2013-10-17 00:54:20 +02:00
|
|
|
pathname);
|
|
|
|
return false;
|
2008-12-16 21:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 00:44:57 +02:00
|
|
|
|
2008-12-16 21:42:34 +01:00
|
|
|
//find one dir up
|
2013-10-17 00:54:20 +02:00
|
|
|
if (slash != nullptr)
|
|
|
|
*slash = '/';
|
|
|
|
|
|
|
|
slash = FindSlash(pathname, idx - 1);
|
2013-10-17 00:44:57 +02:00
|
|
|
if (slash == nullptr)
|
2013-10-17 00:54:20 +02:00
|
|
|
return false;
|
2013-10-17 00:44:57 +02:00
|
|
|
|
|
|
|
*slash = 0;
|
2013-10-17 00:54:20 +02:00
|
|
|
idx = slash - pathname;
|
2008-12-16 21:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|