mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-07-17 14:22:13 +02:00
fs: implement FileHandleStorage
This commit is contained in:
parent
3d518759da
commit
b3077c4a1a
@ -28,6 +28,7 @@
|
|||||||
#include <stratosphere/fs/fs_memory_storage.hpp>
|
#include <stratosphere/fs/fs_memory_storage.hpp>
|
||||||
#include <stratosphere/fs/fs_remote_storage.hpp>
|
#include <stratosphere/fs/fs_remote_storage.hpp>
|
||||||
#include <stratosphere/fs/fs_file_storage.hpp>
|
#include <stratosphere/fs/fs_file_storage.hpp>
|
||||||
|
#include <stratosphere/fs/fs_file_handle_storage.hpp>
|
||||||
#include <stratosphere/fs/fs_query_range.hpp>
|
#include <stratosphere/fs/fs_query_range.hpp>
|
||||||
#include <stratosphere/fs/impl/fs_common_mount_name.hpp>
|
#include <stratosphere/fs/impl/fs_common_mount_name.hpp>
|
||||||
#include <stratosphere/fs/fs_mount.hpp>
|
#include <stratosphere/fs/fs_mount.hpp>
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018-2020 Adubbz, Atmosphère-NX
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms and conditions of the GNU General Public License,
|
||||||
|
* version 2, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include "fs_common.hpp"
|
||||||
|
#include "fs_istorage.hpp"
|
||||||
|
|
||||||
|
namespace ams::fs {
|
||||||
|
|
||||||
|
class FileHandleStorage : public IStorage {
|
||||||
|
private:
|
||||||
|
static constexpr s64 InvalidSize = -1;
|
||||||
|
private:
|
||||||
|
FileHandle handle;
|
||||||
|
bool close_file;
|
||||||
|
s64 size;
|
||||||
|
os::Mutex mutex;
|
||||||
|
public:
|
||||||
|
FileHandleStorage(FileHandle handle) : FileHandleStorage(handle, false) { /* ... */ }
|
||||||
|
|
||||||
|
FileHandleStorage(FileHandle handle, bool close_file) : handle(handle), close_file(close_file), size(InvalidSize) {
|
||||||
|
/* ... */
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~FileHandleStorage() {
|
||||||
|
if (this->close_file) {
|
||||||
|
CloseFile(this->handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
Result UpdateSize();
|
||||||
|
public:
|
||||||
|
virtual Result Read(s64 offset, void *buffer, size_t size) override;
|
||||||
|
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
|
||||||
|
virtual Result Flush() override;
|
||||||
|
virtual Result GetSize(s64 *out_size) override;
|
||||||
|
virtual Result SetSize(s64 size) override;
|
||||||
|
virtual Result OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018-2020 Adubbz, Atmosphère-NX
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms and conditions of the GNU General Public License,
|
||||||
|
* version 2, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
namespace ams::fs {
|
||||||
|
|
||||||
|
Result FileHandleStorage::UpdateSize() {
|
||||||
|
R_SUCCEED_IF(this->size != InvalidSize);
|
||||||
|
return GetFileSize(std::addressof(this->size), this->handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result FileHandleStorage::Read(s64 offset, void *buffer, size_t size) {
|
||||||
|
/* Lock the mutex. */
|
||||||
|
std::scoped_lock lk(this->mutex);
|
||||||
|
|
||||||
|
/* Immediately succeed if there's nothing to read. */
|
||||||
|
R_SUCCEED_IF(size == 0);
|
||||||
|
|
||||||
|
/* Validate buffer. */
|
||||||
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
||||||
|
|
||||||
|
/* Ensure our size is valid. */
|
||||||
|
R_TRY(this->UpdateSize());
|
||||||
|
|
||||||
|
/* Ensure our access is valid. */
|
||||||
|
R_UNLESS(IStorage::IsRangeValid(offset, size, this->size), fs::ResultOutOfRange());
|
||||||
|
|
||||||
|
return ReadFile(this->handle, offset, buffer, size, fs::ReadOption());
|
||||||
|
}
|
||||||
|
|
||||||
|
Result FileHandleStorage::Write(s64 offset, const void *buffer, size_t size) {
|
||||||
|
/* Lock the mutex. */
|
||||||
|
std::scoped_lock lk(this->mutex);
|
||||||
|
|
||||||
|
/* Immediately succeed if there's nothing to write. */
|
||||||
|
R_SUCCEED_IF(size == 0);
|
||||||
|
|
||||||
|
/* Validate buffer. */
|
||||||
|
R_UNLESS(buffer != nullptr, fs::ResultNullptrArgument());
|
||||||
|
|
||||||
|
/* Ensure our size is valid. */
|
||||||
|
R_TRY(this->UpdateSize());
|
||||||
|
|
||||||
|
/* Ensure our access is valid. */
|
||||||
|
R_UNLESS(IStorage::IsRangeValid(offset, size, this->size), fs::ResultOutOfRange());
|
||||||
|
|
||||||
|
return WriteFile(this->handle, offset, buffer, size, fs::WriteOption());
|
||||||
|
}
|
||||||
|
|
||||||
|
Result FileHandleStorage::Flush() {
|
||||||
|
return FlushFile(this->handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result FileHandleStorage::GetSize(s64 *out_size) {
|
||||||
|
R_TRY(this->UpdateSize());
|
||||||
|
*out_size = this->size;
|
||||||
|
return ResultSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
|
Result FileHandleStorage::SetSize(s64 size) {
|
||||||
|
this->size = InvalidSize;
|
||||||
|
return SetFileSize(this->handle, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result FileHandleStorage::OperateRange(void *dst, size_t dst_size, OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) {
|
||||||
|
switch (op_id) {
|
||||||
|
case OperationId::QueryRange:
|
||||||
|
/* Validate buffer and size. */
|
||||||
|
R_UNLESS(dst != nullptr, fs::ResultNullptrArgument());
|
||||||
|
R_UNLESS(dst_size == sizeof(QueryRangeInfo), fs::ResultInvalidSize());
|
||||||
|
|
||||||
|
return QueryRange(static_cast<QueryRangeInfo *>(dst), this->handle, offset, size);
|
||||||
|
default:
|
||||||
|
return fs::ResultUnsupportedOperationInFileStorageB();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -254,6 +254,7 @@ namespace ams::fs {
|
|||||||
R_DEFINE_ERROR_RESULT(UnsupportedOperationInMemoryStorageA, 6304);
|
R_DEFINE_ERROR_RESULT(UnsupportedOperationInMemoryStorageA, 6304);
|
||||||
R_DEFINE_ERROR_RESULT(UnsupportedOperationInMemoryStorageB, 6305);
|
R_DEFINE_ERROR_RESULT(UnsupportedOperationInMemoryStorageB, 6305);
|
||||||
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileStorageA, 6306);
|
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileStorageA, 6306);
|
||||||
|
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileStorageB, 6307);
|
||||||
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileServiceObjectAdapterA, 6362);
|
R_DEFINE_ERROR_RESULT(UnsupportedOperationInFileServiceObjectAdapterA, 6362);
|
||||||
R_DEFINE_ERROR_RESULT(UnsupportedOperationInRomFsFileSystemA, 6364);
|
R_DEFINE_ERROR_RESULT(UnsupportedOperationInRomFsFileSystemA, 6364);
|
||||||
R_DEFINE_ERROR_RESULT(UnsupportedOperationInRomFsFileSystemB, 6365);
|
R_DEFINE_ERROR_RESULT(UnsupportedOperationInRomFsFileSystemB, 6365);
|
||||||
|
Loading…
Reference in New Issue
Block a user