ResourceImplementation.cpp 3.2 KB

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