2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2019-05-31 19:29:13 +02:00
|
|
|
* Copyright 2003-2019 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
|
|
|
*/
|
|
|
|
|
2019-06-15 14:26:26 +02:00
|
|
|
#include "LookupFile.hxx"
|
|
|
|
#include "FileInfo.hxx"
|
2019-05-31 18:52:11 +02:00
|
|
|
#include "system/Error.hxx"
|
2009-11-11 14:15:34 +01:00
|
|
|
|
2013-10-17 00:44:57 +02:00
|
|
|
gcc_pure
|
2019-06-11 18:34:28 +02:00
|
|
|
static PathTraitsFS::pointer_type
|
|
|
|
FindSlash(PathTraitsFS::pointer_type 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;
|
|
|
|
}
|
|
|
|
|
2019-05-31 19:29:13 +02:00
|
|
|
ArchiveLookupResult
|
2019-06-15 14:26:26 +02:00
|
|
|
LookupFile(Path pathname)
|
2008-12-16 21:42:34 +01:00
|
|
|
{
|
2019-06-15 14:06:50 +02:00
|
|
|
PathTraitsFS::string buffer(pathname.c_str());
|
2019-06-15 13:59:22 +02:00
|
|
|
size_t idx = buffer.size();
|
2008-12-16 21:42:34 +01:00
|
|
|
|
2019-06-11 18:34:28 +02:00
|
|
|
PathTraitsFS::pointer_type 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
|
2019-06-15 13:59:22 +02:00
|
|
|
const FileInfo file_info(Path::FromFS(buffer.c_str()));
|
2019-05-31 18:51:16 +02:00
|
|
|
|
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)
|
2019-05-31 19:29:13 +02:00
|
|
|
return {};
|
2013-10-17 00:54:20 +02:00
|
|
|
|
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
|
2019-06-15 13:59:22 +02:00
|
|
|
return {AllocatedPath::FromFS(buffer.c_str()), AllocatedPath::FromFS(slash + 1)};
|
2008-12-16 21:42:34 +01:00
|
|
|
} else {
|
2019-05-31 19:29:13 +02:00
|
|
|
return {};
|
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 = '/';
|
|
|
|
|
2019-06-15 13:59:22 +02:00
|
|
|
slash = FindSlash(&buffer.front(), idx - 1);
|
2013-10-17 00:44:57 +02:00
|
|
|
if (slash == nullptr)
|
2019-05-31 19:29:13 +02:00
|
|
|
return {};
|
2013-10-17 00:44:57 +02:00
|
|
|
|
|
|
|
*slash = 0;
|
2019-06-15 13:59:22 +02:00
|
|
|
idx = slash - buffer.c_str();
|
2008-12-16 21:42:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|