mpd/src/archive/ArchiveLookup.cxx

79 lines
2.0 KiB
C++
Raw Normal View History

/*
* Copyright 2003-2019 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 "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 PathTraitsFS::pointer_type
FindSlash(PathTraitsFS::pointer_type p, size_t i) noexcept
{
for (; i > 0; --i)
if (p[i] == '/')
return p + i;
return nullptr;
}
ArchiveLookupResult
2019-06-15 13:59:22 +02:00
archive_lookup(PathTraitsFS::const_pointer_type pathname)
2008-12-16 21:42:34 +01:00
{
2019-06-15 13:59:22 +02:00
PathTraitsFS::string buffer(pathname);
size_t idx = buffer.size();
2008-12-16 21:42:34 +01:00
PathTraitsFS::pointer_type slash = nullptr;
2008-12-16 21:42:34 +01:00
while (true) {
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()));
2008-12-16 21:42:34 +01:00
//is something found ins original path (is not an archive)
if (slash == nullptr)
return {};
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
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 {
return {};
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 = '/';
2019-06-15 13:59:22 +02:00
slash = FindSlash(&buffer.front(), idx - 1);
if (slash == nullptr)
return {};
*slash = 0;
2019-06-15 13:59:22 +02:00
idx = slash - buffer.c_str();
2008-12-16 21:42:34 +01:00
}
}