ResourceImplementationFile.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <AK/StringView.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibCore/Resource.h>
  10. #include <LibCore/ResourceImplementationFile.h>
  11. #include <LibCore/System.h>
  12. namespace Core {
  13. ResourceImplementationFile::ResourceImplementationFile(String base_directory)
  14. : m_base_directory(move(base_directory))
  15. {
  16. }
  17. ErrorOr<NonnullRefPtr<Resource>> ResourceImplementationFile::load_from_resource_scheme_uri(StringView uri)
  18. {
  19. StringView const resource_scheme = "resource://"sv;
  20. VERIFY(uri.starts_with(resource_scheme));
  21. auto path = TRY(String::from_utf8(uri.substring_view(resource_scheme.length())));
  22. auto full_path = TRY(String::from_byte_string(LexicalPath::join(m_base_directory, path).string()));
  23. auto st = TRY(System::stat(full_path));
  24. if (S_ISDIR(st.st_mode))
  25. return make_directory_resource(move(path), st.st_mtime);
  26. return make_resource(path, TRY(MappedFile::map(full_path)), st.st_mtime);
  27. }
  28. Vector<String> ResourceImplementationFile::child_names_for_resource_scheme(Resource const& resource)
  29. {
  30. Vector<String> children;
  31. Core::DirIterator it(resource.filesystem_path().to_byte_string(), Core::DirIterator::SkipParentAndBaseDir);
  32. while (it.has_next())
  33. children.append(MUST(String::from_byte_string(it.next_path())));
  34. return children;
  35. }
  36. String ResourceImplementationFile::filesystem_path_for_resource_scheme(String const& relative_path)
  37. {
  38. return MUST(String::from_byte_string(LexicalPath::join(m_base_directory, relative_path).string()));
  39. }
  40. }