mpd/src/archive/ArchiveLookup.cxx

86 lines
2.0 KiB
C++
Raw Normal View History

/*
2018-10-31 17:54:59 +01:00
* Copyright 2003-2018 The Music Player Daemon Project
* 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.
*
* 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"
#include "ArchiveDomain.hxx"
#include "Log.hxx"
#include "fs/FileInfo.hxx"
2019-05-31 18:52:11 +02:00
#include "system/Error.hxx"
2008-12-16 21:42:34 +01:00
#include <string.h>
gcc_pure
static char *
FindSlash(char *p, size_t i) noexcept
{
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,
const char **inpath)
2008-12-16 21:42:34 +01:00
{
size_t idx = strlen(pathname);
2008-12-16 21:42:34 +01:00
char *slash = nullptr;
2008-12-16 21:42:34 +01:00
while (true) {
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)
if (slash == nullptr)
return false;
2008-12-16 21:42:34 +01:00
//its a file ?
if (file_info.IsRegular()) {
2008-12-16 21:42:34 +01:00
//so the upper should be file
*archive = pathname;
*inpath = slash + 1;
return true;
2008-12-16 21:42:34 +01:00
} else {
FormatError(archive_domain,
"Not a regular file: %s",
pathname);
return false;
2008-12-16 21:42:34 +01:00
}
} catch (const std::system_error &e) {
if (!IsPathNotFound(e))
throw;
2008-12-16 21:42:34 +01:00
}
2008-12-16 21:42:34 +01:00
//find one dir up
if (slash != nullptr)
*slash = '/';
slash = FindSlash(pathname, idx - 1);
if (slash == nullptr)
return false;
*slash = 0;
idx = slash - pathname;
2008-12-16 21:42:34 +01:00
}
}