file_sdmc.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /**
  32. * Read data from the file
  33. * @param offset Offset in bytes to start reading data from
  34. * @param length Length in bytes of data to read from file
  35. * @param buffer Buffer to read data into
  36. * @return Number of bytes read
  37. */
  38. size_t File_SDMC::Read(const u64 offset, const u32 length, u8* buffer) const {
  39. file->Seek(offset, SEEK_SET);
  40. return file->ReadBytes(buffer, length);
  41. }
  42. /**
  43. * Write data to the file
  44. * @param offset Offset in bytes to start writing data to
  45. * @param length Length in bytes of data to write to file
  46. * @param flush The flush parameters (0 == do not flush)
  47. * @param buffer Buffer to read data from
  48. * @return Number of bytes written
  49. */
  50. size_t File_SDMC::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const {
  51. file->Seek(offset, SEEK_SET);
  52. size_t written = file->WriteBytes(buffer, length);
  53. if (flush)
  54. file->Flush();
  55. return written;
  56. }
  57. /**
  58. * Get the size of the file in bytes
  59. * @return Size of the file in bytes
  60. */
  61. size_t File_SDMC::GetSize() const {
  62. return static_cast<size_t>(file->GetSize());
  63. }
  64. /**
  65. * Set the size of the file in bytes
  66. * @param size New size of the file
  67. * @return true if successful
  68. */
  69. bool File_SDMC::SetSize(const u64 size) const {
  70. file->Resize(size);
  71. file->Flush();
  72. return true;
  73. }
  74. /**
  75. * Close the file
  76. * @return true if the file closed correctly
  77. */
  78. bool File_SDMC::Close() const {
  79. return file->Close();
  80. }
  81. } // namespace FileSys