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"
|
2019-05-31 18:51:16 +02:00
|
|
|
#include "fs/FileInfo.hxx"
|
2019-05-31 18:52:11 +02:00
|
|
|
#include "system/Error.hxx"
|
2009-11-11 14:15:34 +01:00
|
|
|
|
2008-12-16 21:42:34 +01:00
|
|
|
#include <string.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:58 +02:00
|
|
|
bool
|
|
|
|
archive_lookup(char *pathname, const char **archive,
|
2019-05-31 19:01:22 +02:00
|
|
|
const char **inpath)
|
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) {
|
2019-05-31 18:51:16 +02:00
|
|
|
try {
|
|
|
|
//try to stat if its real directory
|
|
|
|
const FileInfo file_info(Path::FromFS(pathname));
|
|
|
|
|
2008-12-16 21:42:34 +01:00
|
|
|
//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 ?
|
2019-05-31 18:51:16 +02:00
|
|
|
if (file_info.IsRegular()) {
|
2008-12-16 21:42:34 +01:00
|
|
|
//so the upper should be file
|
|
|
|
*archive = pathname;
|
2013-10-17 00:54:20 +02:00
|
|
|
*inpath = 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
|
|
|
}
|
2019-05-31 18:51:16 +02:00
|
|
|
} catch (const std::system_error &e) {
|
|
|
|
if (!IsPathNotFound(e))
|
|
|
|
throw;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|