2023-10-02 20:36:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/DirIterator.h>
|
|
|
|
#include <LibCore/ResourceImplementation.h>
|
|
|
|
#include <LibCore/ResourceImplementationFile.h>
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
static OwnPtr<ResourceImplementation> s_the;
|
|
|
|
|
|
|
|
void ResourceImplementation::install(OwnPtr<ResourceImplementation> the)
|
|
|
|
{
|
|
|
|
s_the = move(the);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceImplementation& ResourceImplementation::the()
|
|
|
|
{
|
|
|
|
if (!s_the)
|
|
|
|
install(make<ResourceImplementationFile>("/res"_string));
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2024-04-01 18:17:50 +00:00
|
|
|
NonnullRefPtr<Resource> ResourceImplementation::make_resource(String full_path, NonnullOwnPtr<Core::MappedFile> file, time_t modified_time)
|
2023-10-02 20:36:53 +00:00
|
|
|
{
|
2024-04-01 18:17:50 +00:00
|
|
|
return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, move(file), modified_time));
|
2023-10-02 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 18:17:50 +00:00
|
|
|
NonnullRefPtr<Resource> ResourceImplementation::make_resource(String full_path, ByteBuffer buffer, time_t modified_time)
|
2023-10-02 20:36:53 +00:00
|
|
|
{
|
2024-04-01 18:17:50 +00:00
|
|
|
return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, move(buffer), modified_time));
|
2023-10-02 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 18:17:50 +00:00
|
|
|
NonnullRefPtr<Resource> ResourceImplementation::make_directory_resource(String full_path, time_t modified_time)
|
2023-10-02 20:36:53 +00:00
|
|
|
{
|
2024-04-01 18:17:50 +00:00
|
|
|
return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, Resource::DirectoryTag {}, modified_time));
|
2023-10-02 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<NonnullRefPtr<Resource>> ResourceImplementation::load_from_uri(StringView uri)
|
|
|
|
{
|
|
|
|
StringView const file_scheme = "file://"sv;
|
|
|
|
StringView const resource_scheme = "resource://"sv;
|
|
|
|
|
|
|
|
if (uri.starts_with(resource_scheme))
|
|
|
|
return load_from_resource_scheme_uri(uri);
|
|
|
|
|
|
|
|
if (uri.starts_with(file_scheme)) {
|
|
|
|
auto path = uri.substring_view(file_scheme.length());
|
2023-12-13 19:10:40 +00:00
|
|
|
auto utf8_path = TRY(String::from_utf8(path));
|
2024-04-01 18:17:50 +00:00
|
|
|
auto st = TRY(System::stat(utf8_path));
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
return adopt_ref(*new Resource(utf8_path, Resource::Scheme::File, Resource::DirectoryTag {}, st.st_mtime));
|
2023-12-13 19:10:40 +00:00
|
|
|
auto mapped_file = TRY(MappedFile::map(path));
|
2024-04-01 18:17:50 +00:00
|
|
|
return adopt_ref(*new Resource(utf8_path, Resource::Scheme::File, move(mapped_file), st.st_mtime));
|
2023-10-02 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbgln("ResourceImplementation: Unknown scheme for {}", uri);
|
2024-09-05 11:06:15 +00:00
|
|
|
return Error::from_string_literal("Invalid scheme");
|
2023-10-02 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> ResourceImplementation::child_names(Resource const& resource)
|
|
|
|
{
|
|
|
|
if (!resource.is_directory())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (resource.m_scheme == Resource::Scheme::Resource)
|
|
|
|
return child_names_for_resource_scheme(resource);
|
|
|
|
|
|
|
|
VERIFY(resource.m_scheme == Resource::Scheme::File);
|
|
|
|
|
|
|
|
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 ResourceImplementation::filesystem_path(Resource const& resource)
|
2023-10-02 20:36:53 +00:00
|
|
|
{
|
|
|
|
if (resource.m_scheme == Resource::Scheme::Resource)
|
|
|
|
return filesystem_path_for_resource_scheme(resource.m_path);
|
|
|
|
|
|
|
|
VERIFY(resource.m_scheme == Resource::Scheme::File);
|
|
|
|
|
|
|
|
return resource.m_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|