ResourceImplementation.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/DirIterator.h>
  7. #include <LibCore/ResourceImplementation.h>
  8. #include <LibCore/ResourceImplementationFile.h>
  9. #include <LibCore/System.h>
  10. namespace Core {
  11. static OwnPtr<ResourceImplementation> s_the;
  12. void ResourceImplementation::install(OwnPtr<ResourceImplementation> the)
  13. {
  14. s_the = move(the);
  15. }
  16. ResourceImplementation& ResourceImplementation::the()
  17. {
  18. if (!s_the)
  19. install(make<ResourceImplementationFile>("/res"_string));
  20. return *s_the;
  21. }
  22. NonnullRefPtr<Resource> ResourceImplementation::make_resource(String full_path, NonnullOwnPtr<Core::MappedFile> file, time_t modified_time)
  23. {
  24. return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, move(file), modified_time));
  25. }
  26. NonnullRefPtr<Resource> ResourceImplementation::make_resource(String full_path, ByteBuffer buffer, time_t modified_time)
  27. {
  28. return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, move(buffer), modified_time));
  29. }
  30. NonnullRefPtr<Resource> ResourceImplementation::make_directory_resource(String full_path, time_t modified_time)
  31. {
  32. return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, Resource::DirectoryTag {}, modified_time));
  33. }
  34. ErrorOr<NonnullRefPtr<Resource>> ResourceImplementation::load_from_uri(StringView uri)
  35. {
  36. StringView const file_scheme = "file://"sv;
  37. StringView const resource_scheme = "resource://"sv;
  38. if (uri.starts_with(resource_scheme))
  39. return load_from_resource_scheme_uri(uri);
  40. if (uri.starts_with(file_scheme)) {
  41. auto path = uri.substring_view(file_scheme.length());
  42. auto utf8_path = TRY(String::from_utf8(path));
  43. auto st = TRY(System::stat(utf8_path));
  44. if (S_ISDIR(st.st_mode))
  45. return adopt_ref(*new Resource(utf8_path, Resource::Scheme::File, Resource::DirectoryTag {}, st.st_mtime));
  46. auto mapped_file = TRY(MappedFile::map(path));
  47. return adopt_ref(*new Resource(utf8_path, Resource::Scheme::File, move(mapped_file), st.st_mtime));
  48. }
  49. dbgln("ResourceImplementation: Unknown scheme for {}", uri);
  50. return Error::from_string_literal("Invalid scheme");
  51. }
  52. Vector<String> ResourceImplementation::child_names(Resource const& resource)
  53. {
  54. if (!resource.is_directory())
  55. return {};
  56. if (resource.m_scheme == Resource::Scheme::Resource)
  57. return child_names_for_resource_scheme(resource);
  58. VERIFY(resource.m_scheme == Resource::Scheme::File);
  59. Vector<String> children;
  60. Core::DirIterator it(resource.filesystem_path().to_byte_string(), Core::DirIterator::SkipParentAndBaseDir);
  61. while (it.has_next())
  62. children.append(MUST(String::from_byte_string(it.next_path())));
  63. return children;
  64. }
  65. String ResourceImplementation::filesystem_path(Resource const& resource)
  66. {
  67. if (resource.m_scheme == Resource::Scheme::Resource)
  68. return filesystem_path_for_resource_scheme(resource.m_path);
  69. VERIFY(resource.m_scheme == Resource::Scheme::File);
  70. return resource.m_path;
  71. }
  72. }