archive_ncch.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <memory>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "common/file_util.h"
  9. #include "common/logging/log.h"
  10. #include "common/string_util.h"
  11. #include "core/file_sys/archive_ncch.h"
  12. #include "core/file_sys/ivfc_archive.h"
  13. #include "core/hle/service/fs/archive.h"
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. // FileSys namespace
  16. namespace FileSys {
  17. static std::string GetNCCHContainerPath(const std::string& nand_directory) {
  18. return Common::StringFromFormat("%s%s/title/", nand_directory.c_str(), SYSTEM_ID);
  19. }
  20. static std::string GetNCCHPath(const std::string& mount_point, u32 high, u32 low) {
  21. return Common::StringFromFormat("%s%08x/%08x/content/00000000.app.romfs", mount_point.c_str(),
  22. high, low);
  23. }
  24. ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory)
  25. : mount_point(GetNCCHContainerPath(nand_directory)) {}
  26. ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) {
  27. auto vec = path.AsBinary();
  28. const u32* data = reinterpret_cast<u32*>(vec.data());
  29. std::string file_path = GetNCCHPath(mount_point, data[1], data[0]);
  30. auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb");
  31. if (!file->IsOpen()) {
  32. return ResultCode(-1); // TODO(Subv): Find the right error code
  33. }
  34. auto size = file->GetSize();
  35. auto archive = std::make_unique<IVFCArchive>(file, 0, size);
  36. return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
  37. }
  38. ResultCode ArchiveFactory_NCCH::Format(const Path& path,
  39. const FileSys::ArchiveFormatInfo& format_info) {
  40. LOG_ERROR(Service_FS, "Attempted to format a NCCH archive.");
  41. // TODO: Verify error code
  42. return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported,
  43. ErrorLevel::Permanent);
  44. }
  45. ResultVal<ArchiveFormatInfo> ArchiveFactory_NCCH::GetFormatInfo(const Path& path) const {
  46. // TODO(Subv): Implement
  47. LOG_ERROR(Service_FS, "Unimplemented GetFormatInfo archive %s", GetName().c_str());
  48. return ResultCode(-1);
  49. }
  50. } // namespace FileSys