ResourceImplementation.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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)
  23. {
  24. return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, move(file)));
  25. }
  26. NonnullRefPtr<Resource> ResourceImplementation::make_resource(String full_path, ByteBuffer buffer)
  27. {
  28. return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, move(buffer)));
  29. }
  30. NonnullRefPtr<Resource> ResourceImplementation::make_directory_resource(String full_path)
  31. {
  32. return adopt_ref(*new Resource(move(full_path), Resource::Scheme::Resource, Resource::DirectoryTag {}));
  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. if (is_directory(path))
  44. return adopt_ref(*new Resource(utf8_path, Resource::Scheme::File, Resource::DirectoryTag {}));
  45. auto mapped_file = TRY(MappedFile::map(path));
  46. return adopt_ref(*new Resource(utf8_path, Resource::Scheme::File, move(mapped_file)));
  47. }
  48. dbgln("ResourceImplementation: Unknown scheme for {}", uri);
  49. return Error::from_string_view("Invalid scheme"sv);
  50. }
  51. Vector<String> ResourceImplementation::child_names(Resource const& resource)
  52. {
  53. if (!resource.is_directory())
  54. return {};
  55. if (resource.m_scheme == Resource::Scheme::Resource)
  56. return child_names_for_resource_scheme(resource);
  57. VERIFY(resource.m_scheme == Resource::Scheme::File);
  58. Vector<String> children;
  59. Core::DirIterator it(resource.filesystem_path().to_byte_string(), Core::DirIterator::SkipParentAndBaseDir);
  60. while (it.has_next())
  61. children.append(MUST(String::from_byte_string(it.next_path())));
  62. return children;
  63. }
  64. String ResourceImplementation::filesystem_path(Resource const& resource)
  65. {
  66. if (resource.m_scheme == Resource::Scheme::Resource)
  67. return filesystem_path_for_resource_scheme(resource.m_path);
  68. VERIFY(resource.m_scheme == Resource::Scheme::File);
  69. return resource.m_path;
  70. }
  71. // Note: This is a copy of the impl in LibFilesystem, but we can't link that to LibCore
  72. bool ResourceImplementation::is_directory(StringView filesystem_path)
  73. {
  74. auto st_or_error = System::stat(filesystem_path);
  75. if (st_or_error.is_error())
  76. return false;
  77. auto st = st_or_error.release_value();
  78. return S_ISDIR(st.st_mode);
  79. }
  80. }