2023-10-02 20:36:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
|
|
|
#include <LibCore/Resource.h>
|
|
|
|
#include <LibCore/ResourceImplementationFile.h>
|
2024-04-01 18:17:50 +00:00
|
|
|
#include <LibCore/System.h>
|
2023-10-02 20:36:53 +00:00
|
|
|
|
|
|
|
namespace Core {
|
2023-11-05 13:05:22 +00:00
|
|
|
|
2023-10-02 20:36:53 +00:00
|
|
|
ResourceImplementationFile::ResourceImplementationFile(String base_directory)
|
|
|
|
: m_base_directory(move(base_directory))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<NonnullRefPtr<Resource>> ResourceImplementationFile::load_from_resource_scheme_uri(StringView uri)
|
|
|
|
{
|
|
|
|
StringView const resource_scheme = "resource://"sv;
|
|
|
|
|
|
|
|
VERIFY(uri.starts_with(resource_scheme));
|
|
|
|
|
|
|
|
auto path = TRY(String::from_utf8(uri.substring_view(resource_scheme.length())));
|
2023-12-16 14:19:34 +00:00
|
|
|
auto full_path = TRY(String::from_byte_string(LexicalPath::join(m_base_directory, path).string()));
|
2023-10-02 20:36:53 +00:00
|
|
|
|
2024-04-01 18:17:50 +00:00
|
|
|
auto st = TRY(System::stat(full_path));
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
return make_directory_resource(move(path), st.st_mtime);
|
|
|
|
|
|
|
|
return make_resource(path, TRY(MappedFile::map(full_path)), st.st_mtime);
|
2023-10-02 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> ResourceImplementationFile::child_names_for_resource_scheme(Resource const& resource)
|
|
|
|
{
|
|
|
|
Vector<String> children;
|
2023-12-16 14:19:34 +00:00
|
|
|
Core::DirIterator it(resource.filesystem_path().to_byte_string(), Core::DirIterator::SkipParentAndBaseDir);
|
2023-10-02 20:36:53 +00:00
|
|
|
while (it.has_next())
|
2023-12-16 14:19:34 +00:00
|
|
|
children.append(MUST(String::from_byte_string(it.next_path())));
|
2023-10-02 20:36:53 +00:00
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2023-11-05 13:05:22 +00:00
|
|
|
String ResourceImplementationFile::filesystem_path_for_resource_scheme(String const& relative_path)
|
2023-10-02 20:36:53 +00:00
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
return MUST(String::from_byte_string(LexicalPath::join(m_base_directory, relative_path).string()));
|
2023-10-02 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|