|
|
|
|
@@ -9,7 +9,7 @@
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <haze.h>
|
|
|
|
|
|
|
|
|
|
namespace sphaira::haze {
|
|
|
|
|
namespace sphaira::libhaze {
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
struct InstallSharedData {
|
|
|
|
|
@@ -56,7 +56,7 @@ void on_thing() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct FsProxyBase : ::haze::FileSystemProxyImpl {
|
|
|
|
|
struct FsProxyBase : haze::FileSystemProxyImpl {
|
|
|
|
|
FsProxyBase(const char* name, const char* display_name) : m_name{name}, m_display_name{display_name} {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@@ -65,8 +65,13 @@ struct FsProxyBase : ::haze::FileSystemProxyImpl {
|
|
|
|
|
fs::FsPath buf;
|
|
|
|
|
const auto len = std::strlen(GetName());
|
|
|
|
|
|
|
|
|
|
if (len && !strncasecmp(path + 1, GetName(), len)) {
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%s/%s", base, path + 1 + len);
|
|
|
|
|
// if (!base || !base[0]) {
|
|
|
|
|
// std::strcpy(buf, path);
|
|
|
|
|
// return buf;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (len && !strncasecmp(path, GetName(), len)) {
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%s/%s", base, path + len);
|
|
|
|
|
} else {
|
|
|
|
|
std::snprintf(buf, sizeof(buf), "%s/%s", base, path);
|
|
|
|
|
// std::strcpy(buf, path);
|
|
|
|
|
@@ -89,6 +94,10 @@ protected:
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct FsProxy final : FsProxyBase {
|
|
|
|
|
using File = fs::File;
|
|
|
|
|
using Dir = fs::Dir;
|
|
|
|
|
using DirEntry = FsDirectoryEntry;
|
|
|
|
|
|
|
|
|
|
FsProxy(std::unique_ptr<fs::Fs>&& fs, const char* name, const char* display_name)
|
|
|
|
|
: FsProxyBase{name, display_name}
|
|
|
|
|
, m_fs{std::forward<decltype(fs)>(fs)} {
|
|
|
|
|
@@ -111,131 +120,173 @@ struct FsProxy final : FsProxyBase {
|
|
|
|
|
auto fs = (fs::FsNative*)m_fs.get();
|
|
|
|
|
return fsFsGetTotalSpace(&fs->m_fs, FixPath(path), out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: use statvfs.
|
|
|
|
|
// then fallback to 256gb if not available.
|
|
|
|
|
*out = 1024ULL * 1024ULL * 1024ULL * 256ULL;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result GetFreeSpace(const char *path, s64 *out) override {
|
|
|
|
|
if (m_fs->IsNative()) {
|
|
|
|
|
auto fs = (fs::FsNative*)m_fs.get();
|
|
|
|
|
return fsFsGetFreeSpace(&fs->m_fs, FixPath(path), out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: use statvfs.
|
|
|
|
|
// then fallback to 256gb if not available.
|
|
|
|
|
*out = 1024ULL * 1024ULL * 1024ULL * 256ULL;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result GetEntryType(const char *path, FsDirEntryType *out_entry_type) override {
|
|
|
|
|
const auto rc = m_fs->GetEntryType(FixPath(path), out_entry_type);
|
|
|
|
|
log_write("[HAZE] GetEntryType(%s) 0x%X\n", path, rc);
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
Result GetEntryType(const char *path, haze::FileAttrType *out_entry_type) override {
|
|
|
|
|
FsDirEntryType type;
|
|
|
|
|
R_TRY(m_fs->GetEntryType(FixPath(path), &type));
|
|
|
|
|
*out_entry_type = (type == FsDirEntryType_Dir) ? haze::FileAttrType_DIR : haze::FileAttrType_FILE;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result CreateFile(const char* path, s64 size, u32 option) override {
|
|
|
|
|
|
|
|
|
|
Result GetEntryAttributes(const char *path, haze::FileAttr *out) override {
|
|
|
|
|
FsDirEntryType type;
|
|
|
|
|
R_TRY(m_fs->GetEntryType(FixPath(path), &type));
|
|
|
|
|
|
|
|
|
|
if (type == FsDirEntryType_File) {
|
|
|
|
|
out->type = haze::FileAttrType_FILE;
|
|
|
|
|
|
|
|
|
|
// it doesn't matter if this fails.
|
|
|
|
|
s64 size{};
|
|
|
|
|
FsTimeStampRaw timestamp{};
|
|
|
|
|
R_TRY(m_fs->FileGetSizeAndTimestamp(FixPath(path), ×tamp, &size));
|
|
|
|
|
|
|
|
|
|
out->size = size;
|
|
|
|
|
if (timestamp.is_valid) {
|
|
|
|
|
out->ctime = timestamp.created;
|
|
|
|
|
out->mtime = timestamp.modified;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
out->type = haze::FileAttrType_DIR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsReadOnly()) {
|
|
|
|
|
out->flag |= haze::FileAttrFlag_READ_ONLY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result CreateFile(const char* path, s64 size) override {
|
|
|
|
|
log_write("[HAZE] CreateFile(%s)\n", path);
|
|
|
|
|
return m_fs->CreateFile(FixPath(path), size, option);
|
|
|
|
|
return m_fs->CreateFile(FixPath(path), 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result DeleteFile(const char* path) override {
|
|
|
|
|
log_write("[HAZE] DeleteFile(%s)\n", path);
|
|
|
|
|
return m_fs->DeleteFile(FixPath(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result RenameFile(const char *old_path, const char *new_path) override {
|
|
|
|
|
log_write("[HAZE] RenameFile(%s -> %s)\n", old_path, new_path);
|
|
|
|
|
return m_fs->RenameFile(FixPath(old_path), FixPath(new_path));
|
|
|
|
|
}
|
|
|
|
|
Result OpenFile(const char *path, u32 mode, FsFile *out_file) override {
|
|
|
|
|
log_write("[HAZE] OpenFile(%s)\n", path);
|
|
|
|
|
auto fptr = new fs::File();
|
|
|
|
|
const auto rc = m_fs->OpenFile(FixPath(path), mode, fptr);
|
|
|
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
|
std::memcpy(&out_file->s, &fptr, sizeof(fptr));
|
|
|
|
|
} else {
|
|
|
|
|
delete fptr;
|
|
|
|
|
Result OpenFile(const char *path, haze::FileOpenMode mode, haze::File *out_file) override {
|
|
|
|
|
log_write("[HAZE] OpenFile(%s)\n", path);
|
|
|
|
|
|
|
|
|
|
u32 flags = FsOpenMode_Read;
|
|
|
|
|
if (mode == haze::FileOpenMode_WRITE) {
|
|
|
|
|
flags = FsOpenMode_Write | FsOpenMode_Append;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
auto f = new File();
|
|
|
|
|
const auto rc = m_fs->OpenFile(FixPath(path), flags, f);
|
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
|
log_write("[HAZE] OpenFile(%s) failed: 0x%X\n", path, rc);
|
|
|
|
|
delete f;
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out_file->impl = f;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result GetFileSize(FsFile *file, s64 *out_size) override {
|
|
|
|
|
log_write("[HAZE] GetFileSize()\n");
|
|
|
|
|
fs::File* f;
|
|
|
|
|
std::memcpy(&f, &file->s, sizeof(f));
|
|
|
|
|
|
|
|
|
|
Result GetFileSize(haze::File *file, s64 *out_size) override {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
return f->GetSize(out_size);
|
|
|
|
|
}
|
|
|
|
|
Result SetFileSize(FsFile *file, s64 size) override {
|
|
|
|
|
log_write("[HAZE] SetFileSize(%zd)\n", size);
|
|
|
|
|
fs::File* f;
|
|
|
|
|
std::memcpy(&f, &file->s, sizeof(f));
|
|
|
|
|
|
|
|
|
|
Result SetFileSize(haze::File *file, s64 size) override {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
return f->SetSize(size);
|
|
|
|
|
}
|
|
|
|
|
Result ReadFile(FsFile *file, s64 off, void *buf, u64 read_size, u32 option, u64 *out_bytes_read) override {
|
|
|
|
|
log_write("[HAZE] ReadFile(%zd, %zu)\n", off, read_size);
|
|
|
|
|
fs::File* f;
|
|
|
|
|
std::memcpy(&f, &file->s, sizeof(f));
|
|
|
|
|
return f->Read(off, buf, read_size, option, out_bytes_read);
|
|
|
|
|
|
|
|
|
|
Result ReadFile(haze::File *file, s64 off, void *buf, u64 read_size, u64 *out_bytes_read) override {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
return f->Read(off, buf, read_size, FsReadOption_None, out_bytes_read);
|
|
|
|
|
}
|
|
|
|
|
Result WriteFile(FsFile *file, s64 off, const void *buf, u64 write_size, u32 option) override {
|
|
|
|
|
log_write("[HAZE] WriteFile(%zd, %zu)\n", off, write_size);
|
|
|
|
|
fs::File* f;
|
|
|
|
|
std::memcpy(&f, &file->s, sizeof(f));
|
|
|
|
|
return f->Write(off, buf, write_size, option);
|
|
|
|
|
|
|
|
|
|
Result WriteFile(haze::File *file, s64 off, const void *buf, u64 write_size) override {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
return f->Write(off, buf, write_size, FsWriteOption_None);
|
|
|
|
|
}
|
|
|
|
|
void CloseFile(FsFile *file) override {
|
|
|
|
|
log_write("[HAZE] CloseFile()\n");
|
|
|
|
|
fs::File* f;
|
|
|
|
|
std::memcpy(&f, &file->s, sizeof(f));
|
|
|
|
|
|
|
|
|
|
void CloseFile(haze::File *file) override {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
if (f) {
|
|
|
|
|
delete f;
|
|
|
|
|
file->impl = nullptr;
|
|
|
|
|
}
|
|
|
|
|
std::memset(file, 0, sizeof(*file));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result CreateDirectory(const char* path) override {
|
|
|
|
|
log_write("[HAZE] DeleteFile(%s)\n", path);
|
|
|
|
|
return m_fs->CreateDirectory(FixPath(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result DeleteDirectoryRecursively(const char* path) override {
|
|
|
|
|
log_write("[HAZE] DeleteDirectoryRecursively(%s)\n", path);
|
|
|
|
|
return m_fs->DeleteDirectoryRecursively(FixPath(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result RenameDirectory(const char *old_path, const char *new_path) override {
|
|
|
|
|
log_write("[HAZE] RenameDirectory(%s -> %s)\n", old_path, new_path);
|
|
|
|
|
return m_fs->RenameDirectory(FixPath(old_path), FixPath(new_path));
|
|
|
|
|
}
|
|
|
|
|
Result OpenDirectory(const char *path, u32 mode, FsDir *out_dir) override {
|
|
|
|
|
auto fptr = new fs::Dir();
|
|
|
|
|
const auto rc = m_fs->OpenDirectory(FixPath(path), mode, fptr);
|
|
|
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
|
std::memcpy(&out_dir->s, &fptr, sizeof(fptr));
|
|
|
|
|
} else {
|
|
|
|
|
delete fptr;
|
|
|
|
|
Result OpenDirectory(const char *path, haze::Dir *out_dir) override {
|
|
|
|
|
auto dir = new Dir();
|
|
|
|
|
const auto rc = m_fs->OpenDirectory(FixPath(path), FsDirOpenMode_ReadDirs | FsDirOpenMode_ReadFiles | FsDirOpenMode_NoFileSize, dir);
|
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
|
log_write("[HAZE] OpenDirectory(%s) failed: 0x%X\n", path, rc);
|
|
|
|
|
delete dir;
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_write("[HAZE] OpenDirectory(%s) 0x%X\n", path, rc);
|
|
|
|
|
return rc;
|
|
|
|
|
out_dir->impl = dir;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result ReadDirectory(FsDir *d, s64 *out_total_entries, size_t max_entries, FsDirectoryEntry *buf) override {
|
|
|
|
|
fs::Dir* f;
|
|
|
|
|
std::memcpy(&f, &d->s, sizeof(f));
|
|
|
|
|
const auto rc = f->Read(out_total_entries, max_entries, buf);
|
|
|
|
|
log_write("[HAZE] ReadDirectory(%zd) 0x%X\n", *out_total_entries, rc);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
Result GetDirectoryEntryCount(FsDir *d, s64 *out_count) override {
|
|
|
|
|
fs::Dir* f;
|
|
|
|
|
std::memcpy(&f, &d->s, sizeof(f));
|
|
|
|
|
const auto rc = f->GetEntryCount(out_count);
|
|
|
|
|
log_write("[HAZE] GetDirectoryEntryCount(%zd) 0x%X\n", *out_count, rc);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
void CloseDirectory(FsDir *d) override {
|
|
|
|
|
log_write("[HAZE] CloseDirectory()\n");
|
|
|
|
|
fs::Dir* f;
|
|
|
|
|
std::memcpy(&f, &d->s, sizeof(f));
|
|
|
|
|
if (f) {
|
|
|
|
|
delete f;
|
|
|
|
|
|
|
|
|
|
Result ReadDirectory(haze::Dir *d, s64 *out_total_entries, size_t max_entries, haze::DirEntry *buf) override {
|
|
|
|
|
auto dir = static_cast<Dir*>(d->impl);
|
|
|
|
|
|
|
|
|
|
std::vector<FsDirectoryEntry> entries(max_entries);
|
|
|
|
|
R_TRY(dir->Read(out_total_entries, entries.size(), entries.data()));
|
|
|
|
|
|
|
|
|
|
for (s64 i = 0; i < *out_total_entries; i++) {
|
|
|
|
|
std::strcpy(buf[i].name, entries[i].name);
|
|
|
|
|
}
|
|
|
|
|
std::memset(d, 0, sizeof(*d));
|
|
|
|
|
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
virtual bool MultiThreadTransfer(s64 size, bool read) override {
|
|
|
|
|
return !App::IsFileBaseEmummc();
|
|
|
|
|
|
|
|
|
|
Result GetDirectoryEntryCount(haze::Dir *d, s64 *out_count) override {
|
|
|
|
|
auto dir = static_cast<Dir*>(d->impl);
|
|
|
|
|
return dir->GetEntryCount(out_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CloseDirectory(haze::Dir *d) override {
|
|
|
|
|
auto dir = static_cast<Dir*>(d->impl);
|
|
|
|
|
if (dir) {
|
|
|
|
|
delete dir;
|
|
|
|
|
d->impl = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
@@ -245,6 +296,15 @@ private:
|
|
|
|
|
// fake fs that allows for files to create r/w on the root.
|
|
|
|
|
// folders are not yet supported.
|
|
|
|
|
struct FsProxyVfs : FsProxyBase {
|
|
|
|
|
struct File {
|
|
|
|
|
u64 index{};
|
|
|
|
|
haze::FileOpenMode mode{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Dir {
|
|
|
|
|
u64 pos{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using FsProxyBase::FsProxyBase;
|
|
|
|
|
virtual ~FsProxyVfs() = default;
|
|
|
|
|
|
|
|
|
|
@@ -260,9 +320,9 @@ struct FsProxyVfs : FsProxyBase {
|
|
|
|
|
return file_name + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual Result GetEntryType(const char *path, FsDirEntryType *out_entry_type) {
|
|
|
|
|
virtual Result GetEntryType(const char *path, haze::FileAttrType *out_entry_type) {
|
|
|
|
|
if (FixPath(path) == "/") {
|
|
|
|
|
*out_entry_type = FsDirEntryType_Dir;
|
|
|
|
|
*out_entry_type = haze::FileAttrType_DIR;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
} else {
|
|
|
|
|
const auto file_name = GetFileName(path);
|
|
|
|
|
@@ -273,11 +333,12 @@ struct FsProxyVfs : FsProxyBase {
|
|
|
|
|
});
|
|
|
|
|
R_UNLESS(it != m_entries.end(), FsError_PathNotFound);
|
|
|
|
|
|
|
|
|
|
*out_entry_type = FsDirEntryType_File;
|
|
|
|
|
*out_entry_type = haze::FileAttrType_FILE;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual Result CreateFile(const char* path, s64 size, u32 option) {
|
|
|
|
|
|
|
|
|
|
virtual Result CreateFile(const char* path, s64 size) {
|
|
|
|
|
const auto file_name = GetFileName(path);
|
|
|
|
|
R_UNLESS(file_name, FsError_PathNotFound);
|
|
|
|
|
|
|
|
|
|
@@ -294,6 +355,7 @@ struct FsProxyVfs : FsProxyBase {
|
|
|
|
|
m_entries.emplace_back(entry);
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual Result DeleteFile(const char* path) {
|
|
|
|
|
const auto file_name = GetFileName(path);
|
|
|
|
|
R_UNLESS(file_name, FsError_PathNotFound);
|
|
|
|
|
@@ -306,6 +368,7 @@ struct FsProxyVfs : FsProxyBase {
|
|
|
|
|
m_entries.erase(it);
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual Result RenameFile(const char *old_path, const char *new_path) {
|
|
|
|
|
const auto file_name = GetFileName(old_path);
|
|
|
|
|
R_UNLESS(file_name, FsError_PathNotFound);
|
|
|
|
|
@@ -326,7 +389,8 @@ struct FsProxyVfs : FsProxyBase {
|
|
|
|
|
std::strcpy(it->name, file_name_new);
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
virtual Result OpenFile(const char *path, u32 mode, FsFile *out_file) {
|
|
|
|
|
|
|
|
|
|
virtual Result OpenFile(const char *path, haze::FileOpenMode mode, haze::File *out_file) {
|
|
|
|
|
const auto file_name = GetFileName(path);
|
|
|
|
|
R_UNLESS(file_name, FsError_PathNotFound);
|
|
|
|
|
|
|
|
|
|
@@ -335,65 +399,89 @@ struct FsProxyVfs : FsProxyBase {
|
|
|
|
|
});
|
|
|
|
|
R_UNLESS(it != m_entries.end(), FsError_PathNotFound);
|
|
|
|
|
|
|
|
|
|
out_file->s.object_id = std::distance(m_entries.begin(), it);
|
|
|
|
|
out_file->s.own_handle = mode;
|
|
|
|
|
auto f = new File();
|
|
|
|
|
f->index = std::distance(m_entries.begin(), it);
|
|
|
|
|
f->mode = mode;
|
|
|
|
|
out_file->impl = f;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
virtual Result GetFileSize(FsFile *file, s64 *out_size) {
|
|
|
|
|
auto& e = m_entries[file->s.object_id];
|
|
|
|
|
*out_size = e.file_size;
|
|
|
|
|
|
|
|
|
|
virtual Result GetFileSize(haze::File *file, s64 *out_size) {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
*out_size = m_entries[f->index].file_size;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
virtual Result SetFileSize(FsFile *file, s64 size) {
|
|
|
|
|
auto& e = m_entries[file->s.object_id];
|
|
|
|
|
e.file_size = size;
|
|
|
|
|
|
|
|
|
|
virtual Result SetFileSize(haze::File *file, s64 size) {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
m_entries[f->index].file_size = size;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
virtual Result ReadFile(FsFile *file, s64 off, void *buf, u64 read_size, u32 option, u64 *out_bytes_read) {
|
|
|
|
|
|
|
|
|
|
virtual Result ReadFile(haze::File *file, s64 off, void *buf, u64 read_size, u64 *out_bytes_read) {
|
|
|
|
|
// stub for now as it may confuse users who think that the returned file is valid.
|
|
|
|
|
// the code below can be used to benchmark mtp reads.
|
|
|
|
|
R_THROW(FsError_NotImplemented);
|
|
|
|
|
// auto& e = m_entries[file->s.object_id];
|
|
|
|
|
// read_size = std::min<s64>(e.file_size - off, read_size);
|
|
|
|
|
// std::memset(buf, 0, read_size);
|
|
|
|
|
// *out_bytes_read = read_size;
|
|
|
|
|
// R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
virtual Result WriteFile(FsFile *file, s64 off, const void *buf, u64 write_size, u32 option) {
|
|
|
|
|
auto& e = m_entries[file->s.object_id];
|
|
|
|
|
|
|
|
|
|
virtual Result WriteFile(haze::File *file, s64 off, const void *buf, u64 write_size) {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
auto& e = m_entries[f->index];
|
|
|
|
|
e.file_size = std::max<s64>(e.file_size, off + write_size);
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
virtual void CloseFile(FsFile *file) {
|
|
|
|
|
std::memset(file, 0, sizeof(*file));
|
|
|
|
|
|
|
|
|
|
virtual void CloseFile(haze::File *file) {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
if (f) {
|
|
|
|
|
delete f;
|
|
|
|
|
file->impl = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result CreateDirectory(const char* path) override {
|
|
|
|
|
R_THROW(FsError_NotImplemented);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result DeleteDirectoryRecursively(const char* path) override {
|
|
|
|
|
R_THROW(FsError_NotImplemented);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result RenameDirectory(const char *old_path, const char *new_path) override {
|
|
|
|
|
R_THROW(FsError_NotImplemented);
|
|
|
|
|
}
|
|
|
|
|
Result OpenDirectory(const char *path, u32 mode, FsDir *out_dir) override {
|
|
|
|
|
std::memset(out_dir, 0, sizeof(*out_dir));
|
|
|
|
|
|
|
|
|
|
Result OpenDirectory(const char *path, haze::Dir *out_dir) override {
|
|
|
|
|
auto dir = new Dir();
|
|
|
|
|
out_dir->impl = dir;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result ReadDirectory(FsDir *d, s64 *out_total_entries, size_t max_entries, FsDirectoryEntry *buf) override {
|
|
|
|
|
max_entries = std::min<s64>(m_entries.size()- d->s.object_id, max_entries);
|
|
|
|
|
std::memcpy(buf, m_entries.data() + d->s.object_id, max_entries * sizeof(*buf));
|
|
|
|
|
d->s.object_id += max_entries;
|
|
|
|
|
|
|
|
|
|
Result ReadDirectory(haze::Dir *d, s64 *out_total_entries, size_t max_entries, haze::DirEntry *buf) override {
|
|
|
|
|
auto dir = static_cast<Dir*>(d->impl);
|
|
|
|
|
|
|
|
|
|
max_entries = std::min<s64>(m_entries.size() - dir->pos, max_entries);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < max_entries; i++) {
|
|
|
|
|
std::strcpy(buf[i].name, m_entries[dir->pos + i].name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir->pos += max_entries;
|
|
|
|
|
*out_total_entries = max_entries;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result GetDirectoryEntryCount(FsDir *d, s64 *out_count) override {
|
|
|
|
|
|
|
|
|
|
Result GetDirectoryEntryCount(haze::Dir *d, s64 *out_count) override {
|
|
|
|
|
*out_count = m_entries.size();
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
void CloseDirectory(FsDir *d) override {
|
|
|
|
|
std::memset(d, 0, sizeof(*d));
|
|
|
|
|
|
|
|
|
|
void CloseDirectory(haze::Dir *d) override {
|
|
|
|
|
auto dir = static_cast<Dir*>(d->impl);
|
|
|
|
|
if (dir) {
|
|
|
|
|
delete dir;
|
|
|
|
|
d->impl = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
@@ -407,13 +495,11 @@ struct FsDevNullProxy final : FsProxyVfs {
|
|
|
|
|
*out = 1024ULL * 1024ULL * 1024ULL * 256ULL;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result GetFreeSpace(const char *path, s64 *out) override {
|
|
|
|
|
*out = 1024ULL * 1024ULL * 1024ULL * 256ULL;
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
bool MultiThreadTransfer(s64 size, bool read) override {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct FsInstallProxy final : FsProxyVfs {
|
|
|
|
|
@@ -456,6 +542,7 @@ struct FsInstallProxy final : FsProxyVfs {
|
|
|
|
|
return fs::FsNativeContentStorage(FsContentStorageId_User).GetTotalSpace("/", out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result GetFreeSpace(const char *path, s64 *out) override {
|
|
|
|
|
if (App::GetApp()->m_install_sd.Get()) {
|
|
|
|
|
return fs::FsNativeContentStorage(FsContentStorageId_SdCard).GetFreeSpace("/", out);
|
|
|
|
|
@@ -464,27 +551,30 @@ struct FsInstallProxy final : FsProxyVfs {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result GetEntryType(const char *path, FsDirEntryType *out_entry_type) override {
|
|
|
|
|
Result GetEntryType(const char *path, haze::FileAttrType *out_entry_type) override {
|
|
|
|
|
R_TRY(FsProxyVfs::GetEntryType(path, out_entry_type));
|
|
|
|
|
if (*out_entry_type == FsDirEntryType_File) {
|
|
|
|
|
if (*out_entry_type == haze::FileAttrType_FILE) {
|
|
|
|
|
R_TRY(FailedIfNotEnabled());
|
|
|
|
|
}
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result CreateFile(const char* path, s64 size, u32 option) override {
|
|
|
|
|
|
|
|
|
|
Result CreateFile(const char* path, s64 size) override {
|
|
|
|
|
R_TRY(FailedIfNotEnabled());
|
|
|
|
|
R_TRY(IsValidFileType(path));
|
|
|
|
|
R_TRY(FsProxyVfs::CreateFile(path, size, option));
|
|
|
|
|
R_TRY(FsProxyVfs::CreateFile(path, size));
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result OpenFile(const char *path, u32 mode, FsFile *out_file) override {
|
|
|
|
|
|
|
|
|
|
Result OpenFile(const char *path, haze::FileOpenMode mode, haze::File *out_file) override {
|
|
|
|
|
R_TRY(FailedIfNotEnabled());
|
|
|
|
|
R_TRY(IsValidFileType(path));
|
|
|
|
|
R_TRY(FsProxyVfs::OpenFile(path, mode, out_file));
|
|
|
|
|
log_write("[MTP] done file open: %s mode: 0x%X\n", path, mode);
|
|
|
|
|
|
|
|
|
|
if (mode & FsOpenMode_Write) {
|
|
|
|
|
const auto& e = m_entries[out_file->s.object_id];
|
|
|
|
|
if (mode == haze::FileOpenMode_WRITE) {
|
|
|
|
|
auto f = static_cast<File*>(out_file->impl);
|
|
|
|
|
const auto& e = m_entries[f->index];
|
|
|
|
|
|
|
|
|
|
// check if we already have this file queued.
|
|
|
|
|
log_write("[MTP] checking if empty\n");
|
|
|
|
|
@@ -497,7 +587,8 @@ struct FsInstallProxy final : FsProxyVfs {
|
|
|
|
|
log_write("[MTP] got file: %s\n", path);
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
Result WriteFile(FsFile *file, s64 off, const void *buf, u64 write_size, u32 option) override {
|
|
|
|
|
|
|
|
|
|
Result WriteFile(haze::File *file, s64 off, const void *buf, u64 write_size) override {
|
|
|
|
|
SCOPED_MUTEX(&g_shared_data.mutex);
|
|
|
|
|
if (!g_shared_data.enabled) {
|
|
|
|
|
log_write("[MTP] failing as not enabled\n");
|
|
|
|
|
@@ -509,14 +600,20 @@ struct FsInstallProxy final : FsProxyVfs {
|
|
|
|
|
R_THROW(FsError_NotImplemented);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
R_TRY(FsProxyVfs::WriteFile(file, off, buf, write_size, option));
|
|
|
|
|
R_TRY(FsProxyVfs::WriteFile(file, off, buf, write_size));
|
|
|
|
|
R_SUCCEED();
|
|
|
|
|
}
|
|
|
|
|
void CloseFile(FsFile *file) override {
|
|
|
|
|
|
|
|
|
|
void CloseFile(haze::File *file) override {
|
|
|
|
|
auto f = static_cast<File*>(file->impl);
|
|
|
|
|
if (!f) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool update{};
|
|
|
|
|
{
|
|
|
|
|
SCOPED_MUTEX(&g_shared_data.mutex);
|
|
|
|
|
if (file->s.own_handle & FsOpenMode_Write) {
|
|
|
|
|
if (f->mode == haze::FileOpenMode_WRITE) {
|
|
|
|
|
log_write("[MTP] closing current file\n");
|
|
|
|
|
if (g_shared_data.on_close) {
|
|
|
|
|
g_shared_data.on_close();
|
|
|
|
|
@@ -534,40 +631,36 @@ struct FsInstallProxy final : FsProxyVfs {
|
|
|
|
|
|
|
|
|
|
FsProxyVfs::CloseFile(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// installs are already multi-threaded via yati.
|
|
|
|
|
bool MultiThreadTransfer(s64 size, bool read) override {
|
|
|
|
|
App::IsFileBaseEmummc();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
::haze::FsEntries g_fs_entries{};
|
|
|
|
|
haze::FsEntries g_fs_entries{};
|
|
|
|
|
|
|
|
|
|
void haze_callback(const ::haze::CallbackData *data) {
|
|
|
|
|
void haze_callback(const haze::CallbackData *data) {
|
|
|
|
|
auto& e = *data;
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
switch (e.type) {
|
|
|
|
|
case ::haze::CallbackType_OpenSession: log_write("[LIBHAZE] Opening Session\n"); break;
|
|
|
|
|
case ::haze::CallbackType_CloseSession: log_write("[LIBHAZE] Closing Session\n"); break;
|
|
|
|
|
case haze::CallbackType_OpenSession: log_write("[LIBHAZE] Opening Session\n"); break;
|
|
|
|
|
case haze::CallbackType_CloseSession: log_write("[LIBHAZE] Closing Session\n"); break;
|
|
|
|
|
|
|
|
|
|
case ::haze::CallbackType_CreateFile: log_write("[LIBHAZE] Creating File: %s\n", e.file.filename); break;
|
|
|
|
|
case ::haze::CallbackType_DeleteFile: log_write("[LIBHAZE] Deleting File: %s\n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_CreateFile: log_write("[LIBHAZE] Creating File: %s\n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_DeleteFile: log_write("[LIBHAZE] Deleting File: %s\n", e.file.filename); break;
|
|
|
|
|
|
|
|
|
|
case ::haze::CallbackType_RenameFile: log_write("[LIBHAZE] Rename File: %s -> %s\n", e.rename.filename, e.rename.newname); break;
|
|
|
|
|
case ::haze::CallbackType_RenameFolder: log_write("[LIBHAZE] Rename Folder: %s -> %s\n", e.rename.filename, e.rename.newname); break;
|
|
|
|
|
case haze::CallbackType_RenameFile: log_write("[LIBHAZE] Rename File: %s -> %s\n", e.rename.filename, e.rename.newname); break;
|
|
|
|
|
case haze::CallbackType_RenameFolder: log_write("[LIBHAZE] Rename Folder: %s -> %s\n", e.rename.filename, e.rename.newname); break;
|
|
|
|
|
|
|
|
|
|
case ::haze::CallbackType_CreateFolder: log_write("[LIBHAZE] Creating Folder: %s\n", e.file.filename); break;
|
|
|
|
|
case ::haze::CallbackType_DeleteFolder: log_write("[LIBHAZE] Deleting Folder: %s\n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_CreateFolder: log_write("[LIBHAZE] Creating Folder: %s\n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_DeleteFolder: log_write("[LIBHAZE] Deleting Folder: %s\n", e.file.filename); break;
|
|
|
|
|
|
|
|
|
|
case ::haze::CallbackType_ReadBegin: log_write("[LIBHAZE] Reading File Begin: %s \n", e.file.filename); break;
|
|
|
|
|
case ::haze::CallbackType_ReadProgress: log_write("\t[LIBHAZE] Reading File: offset: %lld size: %lld\n", e.progress.offset, e.progress.size); break;
|
|
|
|
|
case ::haze::CallbackType_ReadEnd: log_write("[LIBHAZE] Reading File Finished: %s\n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_ReadBegin: log_write("[LIBHAZE] Reading File Begin: %s \n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_ReadProgress: log_write("\t[LIBHAZE] Reading File: offset: %lld size: %lld\n", e.progress.offset, e.progress.size); break;
|
|
|
|
|
case haze::CallbackType_ReadEnd: log_write("[LIBHAZE] Reading File Finished: %s\n", e.file.filename); break;
|
|
|
|
|
|
|
|
|
|
case ::haze::CallbackType_WriteBegin: log_write("[LIBHAZE] Writing File Begin: %s \n", e.file.filename); break;
|
|
|
|
|
case ::haze::CallbackType_WriteProgress: log_write("\t[LIBHAZE] Writing File: offset: %lld size: %lld\n", e.progress.offset, e.progress.size); break;
|
|
|
|
|
case ::haze::CallbackType_WriteEnd: log_write("[LIBHAZE] Writing File Finished: %s\n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_WriteBegin: log_write("[LIBHAZE] Writing File Begin: %s \n", e.file.filename); break;
|
|
|
|
|
case haze::CallbackType_WriteProgress: log_write("\t[LIBHAZE] Writing File: offset: %lld size: %lld\n", e.progress.offset, e.progress.size); break;
|
|
|
|
|
case haze::CallbackType_WriteEnd: log_write("[LIBHAZE] Writing File Finished: %s\n", e.file.filename); break;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
App::NotifyFlashLed();
|
|
|
|
|
}
|
|
|
|
|
@@ -588,7 +681,7 @@ bool Init() {
|
|
|
|
|
g_fs_entries.emplace_back(std::make_shared<FsInstallProxy>("install", "Install (NSP, XCI, NSZ, XCZ)"));
|
|
|
|
|
|
|
|
|
|
g_should_exit = false;
|
|
|
|
|
if (!::haze::Initialize(haze_callback, THREAD_PRIO, THREAD_CORE, g_fs_entries, App::GetApp()->m_mtp_vid.Get(), App::GetApp()->m_mtp_pid.Get())) {
|
|
|
|
|
if (!haze::Initialize(haze_callback, g_fs_entries, App::GetApp()->m_mtp_vid.Get(), App::GetApp()->m_mtp_pid.Get())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -607,7 +700,7 @@ void Exit() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
::haze::Exit();
|
|
|
|
|
haze::Exit();
|
|
|
|
|
g_is_running = false;
|
|
|
|
|
g_should_exit = true;
|
|
|
|
|
g_fs_entries.clear();
|
|
|
|
|
@@ -628,4 +721,4 @@ void DisableInstallMode() {
|
|
|
|
|
g_shared_data.enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace sphaira::haze
|
|
|
|
|
} // namespace sphaira::libhaze
|
|
|
|
|
|