file_sdmc.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include <sys/stat.h>
  5. #include "common/common_types.h"
  6. #include "common/file_util.h"
  7. #include "core/file_sys/file_sdmc.h"
  8. #include "core/file_sys/archive_sdmc.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // FileSys namespace
  11. namespace FileSys {
  12. File_SDMC::File_SDMC(const Archive_SDMC* archive, const std::string& path, const Mode mode) {
  13. // TODO(Link Mauve): normalize path into an absolute path without "..", it can currently bypass
  14. // the root directory we set while opening the archive.
  15. // For example, opening /../../etc/passwd can give the emulated program your users list.
  16. std::string real_path = archive->GetMountPoint() + path;
  17. if (!mode.create_flag && !FileUtil::Exists(real_path)) {
  18. file = nullptr;
  19. return;
  20. }
  21. std::string mode_string;
  22. if (mode.read_flag)
  23. mode_string += "r";
  24. if (mode.write_flag)
  25. mode_string += "w";
  26. file = new FileUtil::IOFile(real_path, mode_string.c_str());
  27. }
  28. File_SDMC::~File_SDMC() {
  29. Close();
  30. }
  31. size_t File_SDMC::Read(const u64 offset, const u32 length, u8* buffer) const {
  32. file->Seek(offset, SEEK_SET);
  33. return file->ReadBytes(buffer, length);
  34. }
  35. size_t File_SDMC::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const {
  36. file->Seek(offset, SEEK_SET);
  37. size_t written = file->WriteBytes(buffer, length);
  38. if (flush)
  39. file->Flush();
  40. return written;
  41. }
  42. size_t File_SDMC::GetSize() const {
  43. return file->GetSize();
  44. }
  45. bool File_SDMC::Close() const {
  46. return file->Close();
  47. }
  48. } // namespace FileSys