summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar wwylele2016-10-17 20:23:34 +0800
committerGravatar wwylele2016-11-19 18:24:37 +0200
commita879984c06baf6c4185e376dd47258bfc108dec5 (patch)
treef8470cd6b72e9b18da75446778263fdf13644f29 /src
parentFileSys: add SaveDataArchive (diff)
downloadyuzu-a879984c06baf6c4185e376dd47258bfc108dec5.tar.gz
yuzu-a879984c06baf6c4185e376dd47258bfc108dec5.tar.xz
yuzu-a879984c06baf6c4185e376dd47258bfc108dec5.zip
FileSys: add ExtSaveDataArchive
ExtSaveData is more similar to SaveData, so let it be a subclass of SaveData
Diffstat (limited to 'src')
-rw-r--r--src/core/file_sys/archive_extsavedata.cpp115
-rw-r--r--src/core/hle/result.h1
2 files changed, 115 insertions, 1 deletions
diff --git a/src/core/file_sys/archive_extsavedata.cpp b/src/core/file_sys/archive_extsavedata.cpp
index e1d29efd3..e1c4931ec 100644
--- a/src/core/file_sys/archive_extsavedata.cpp
+++ b/src/core/file_sys/archive_extsavedata.cpp
@@ -11,6 +11,9 @@
11#include "common/string_util.h" 11#include "common/string_util.h"
12#include "core/file_sys/archive_extsavedata.h" 12#include "core/file_sys/archive_extsavedata.h"
13#include "core/file_sys/disk_archive.h" 13#include "core/file_sys/disk_archive.h"
14#include "core/file_sys/errors.h"
15#include "core/file_sys/path_parser.h"
16#include "core/file_sys/savedata_archive.h"
14#include "core/hle/service/fs/archive.h" 17#include "core/hle/service/fs/archive.h"
15 18
16//////////////////////////////////////////////////////////////////////////////////////////////////// 19////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -18,6 +21,116 @@
18 21
19namespace FileSys { 22namespace FileSys {
20 23
24/**
25 * A modified version of DiskFile for fixed-size file used by ExtSaveData
26 * The file size can't be changed by SetSize or Write.
27 */
28class FixSizeDiskFile : public DiskFile {
29public:
30 FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode) : DiskFile(std::move(file), mode) {
31 size = GetSize();
32 }
33
34 bool SetSize(u64 size) const override {
35 return false;
36 }
37
38 ResultVal<size_t> Write(u64 offset, size_t length, bool flush,
39 const u8* buffer) const override {
40 if (offset > size) {
41 return ResultCode(ErrorDescription::FS_WriteBeyondEnd, ErrorModule::FS,
42 ErrorSummary::InvalidArgument, ErrorLevel::Usage);
43 } else if (offset == size) {
44 return MakeResult<size_t>(0);
45 }
46
47 if (offset + length > size) {
48 length = size - offset;
49 }
50
51 return DiskFile::Write(offset, length, flush, buffer);
52 }
53
54private:
55 u64 size{};
56};
57
58/**
59 * Archive backend for general extsave data archive type.
60 * The behaviour of ExtSaveDataArchive is almost the same as SaveDataArchive, except for
61 * - file size can't be changed once created (thus creating zero-size file and openning with create
62 * flag are prohibited);
63 * - always open a file with read+write permission.
64 */
65class ExtSaveDataArchive : public SaveDataArchive {
66public:
67 ExtSaveDataArchive(const std::string& mount_point) : SaveDataArchive(mount_point) {}
68
69 std::string GetName() const override {
70 return "ExtSaveDataArchive: " + mount_point;
71 }
72
73 ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
74 const Mode& mode) const override {
75 LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
76
77 const PathParser path_parser(path);
78
79 if (!path_parser.IsValid()) {
80 LOG_ERROR(Service_FS, "Invalid path %s", path.DebugStr().c_str());
81 return ERROR_INVALID_PATH;
82 }
83
84 if (mode.hex == 0) {
85 LOG_ERROR(Service_FS, "Empty open mode");
86 return ERROR_UNSUPPORTED_OPEN_FLAGS;
87 }
88
89 if (mode.create_flag) {
90 LOG_ERROR(Service_FS, "Create flag is not supported");
91 return ERROR_UNSUPPORTED_OPEN_FLAGS;
92 }
93
94 const auto full_path = path_parser.BuildHostPath(mount_point);
95
96 switch (path_parser.GetHostStatus(mount_point)) {
97 case PathParser::InvalidMountPoint:
98 LOG_CRITICAL(Service_FS, "(unreachable) Invalid mount point %s", mount_point.c_str());
99 return ERROR_FILE_NOT_FOUND;
100 case PathParser::PathNotFound:
101 LOG_ERROR(Service_FS, "Path not found %s", full_path.c_str());
102 return ERROR_PATH_NOT_FOUND;
103 case PathParser::FileInPath:
104 case PathParser::DirectoryFound:
105 LOG_ERROR(Service_FS, "Unexpected file or directory in %s", full_path.c_str());
106 return ERROR_UNEXPECTED_FILE_OR_DIRECTORY;
107 case PathParser::NotFound:
108 LOG_ERROR(Service_FS, "%s not found", full_path.c_str());
109 return ERROR_FILE_NOT_FOUND;
110 }
111
112 FileUtil::IOFile file(full_path, "r+b");
113 if (!file.IsOpen()) {
114 LOG_CRITICAL(Service_FS, "(unreachable) Unknown error opening %s", full_path.c_str());
115 return ERROR_FILE_NOT_FOUND;
116 }
117
118 Mode rwmode;
119 rwmode.write_flag.Assign(1);
120 rwmode.read_flag.Assign(1);
121 auto disk_file = std::make_unique<FixSizeDiskFile>(std::move(file), rwmode);
122 return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
123 }
124
125 ResultCode CreateFile(const Path& path, u64 size) const override {
126 if (size == 0) {
127 LOG_ERROR(Service_FS, "Zero-size file is not supported");
128 return ERROR_UNSUPPORTED_OPEN_FLAGS;
129 }
130 return SaveDataArchive::CreateFile(path, size);
131 }
132};
133
21std::string GetExtSaveDataPath(const std::string& mount_point, const Path& path) { 134std::string GetExtSaveDataPath(const std::string& mount_point, const Path& path) {
22 std::vector<u8> vec_data = path.AsBinary(); 135 std::vector<u8> vec_data = path.AsBinary();
23 const u32* data = reinterpret_cast<const u32*>(vec_data.data()); 136 const u32* data = reinterpret_cast<const u32*>(vec_data.data());
@@ -84,7 +197,7 @@ ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_ExtSaveData::Open(cons
84 ErrorSummary::InvalidState, ErrorLevel::Status); 197 ErrorSummary::InvalidState, ErrorLevel::Status);
85 } 198 }
86 } 199 }
87 auto archive = std::make_unique<DiskArchive>(fullpath); 200 auto archive = std::make_unique<ExtSaveDataArchive>(fullpath);
88 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); 201 return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive));
89} 202}
90 203
diff --git a/src/core/hle/result.h b/src/core/hle/result.h
index 8330894f2..a355f970a 100644
--- a/src/core/hle/result.h
+++ b/src/core/hle/result.h
@@ -34,6 +34,7 @@ enum class ErrorDescription : u32 {
34 513, // TODO(purpasmart): Check if this name fits its actual usage 34 513, // TODO(purpasmart): Check if this name fits its actual usage
35 GPU_FirstInitialization = 519, 35 GPU_FirstInitialization = 519,
36 FS_InvalidPath = 702, 36 FS_InvalidPath = 702,
37 FS_WriteBeyondEnd = 705,
37 FS_UnsupportedOpenFlags = 760, 38 FS_UnsupportedOpenFlags = 760,
38 FS_UnexpectedFileOrDirectory = 770, 39 FS_UnexpectedFileOrDirectory = 770,
39 InvalidSection = 1000, 40 InvalidSection = 1000,