1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: fs_FileBase.h 4 5 Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Rev: 29702 $ 14 *---------------------------------------------------------------------------*/ 15 16 #ifndef NN_FS_CTR_MPCORE_FS_FILEBASE_H_ 17 #define NN_FS_CTR_MPCORE_FS_FILEBASE_H_ 18 19 #include <nn/Handle.h> 20 #include <nn/Result.h> 21 #include <nn/types.h> 22 #include <nn/assert.h> 23 #include <nn/util/util_Result.h> 24 #include <nn/os/os_HandleObject.h> 25 #include <nn/fs/fs_Parameters.h> 26 #include <nn/fs/fs_Result.h> 27 #include <nn/util/util_NonCopyable.h> 28 29 #include <nn/fs/CTR/MPCore/fs_UserFileSystem.h> 30 31 namespace nn { namespace fs { namespace detail { 32 33 class FileBaseImpl : private nn::util::NonCopyable<FileBaseImpl>, private nn::fs::CTR::MPCore::detail::UserFileSystem 34 { 35 protected: 36 FileBaseImpl()37 FileBaseImpl() : m_P(0) {} 38 TryOpenImpl(const wchar_t * path,bit32 mode)39 Result TryOpenImpl(const wchar_t* path, bit32 mode) 40 { 41 return TryOpenFile(&m_P, path, mode); 42 } 43 TryOpenImpl(const wchar_t * path,bit32 mode,nn::fs::PathMark pathMark)44 Result TryOpenImpl(const wchar_t* path, bit32 mode, nn::fs::PathMark pathMark) 45 { 46 NN_UNUSED_VAR( pathMark); 47 return TryOpenFile(&m_P, path, mode); 48 } 49 Finalize()50 void Finalize() 51 { 52 if (GetPtr()) 53 { 54 // 今は、Write したら必ず Flush を行なうこと 55 NN_TASSERT_(!IsNotFlushed()); 56 57 UserFileSystem::CloseFile(GetPtr()); 58 this->m_P = 0; 59 } 60 } ~FileBaseImpl()61 ~FileBaseImpl() { Finalize(); } 62 TryRead(s32 * pOut,s64 offset,void * buffer,size_t size)63 Result TryRead(s32* pOut, s64 offset, void* buffer, size_t size) { return UserFileSystem::TryReadFile(pOut, GetPtr(), offset, buffer, size); } 64 Result TryWrite(s32* pOut, s64 offset, const void* buffer, size_t size, bool flush=true) 65 { 66 if(!flush) 67 { 68 NeedFlush(); 69 } 70 return UserFileSystem::TryWriteFile(pOut, GetPtr(), offset, buffer, size, flush); 71 } TryGetSize(s64 * pOut)72 Result TryGetSize(s64* pOut) const { return UserFileSystem::TryGetFileSize(pOut, GetPtr()); } TrySetSize(s64 size)73 Result TrySetSize(s64 size) { return UserFileSystem::TrySetFileSize(GetPtr(), size); } TryFlush()74 Result TryFlush() 75 { 76 DoneFlush(); 77 return UserFileSystem::TryFlush(GetPtr()); 78 } 79 80 public: 81 DuplicateHandle(Handle * pOut,s64 offset,s64 length)82 Result DuplicateHandle(Handle* pOut, s64 offset, s64 length) 83 { 84 return UserFileSystem::DuplicateHandleForFile(pOut, GetPtr(), offset, length); 85 } 86 OpenDirect(Handle handle)87 void OpenDirect(Handle handle) 88 { 89 UserFileSystem::OpenDirect(&m_P, handle); 90 } 91 CloseDirect()92 void CloseDirect() 93 { 94 if (GetPtr()) 95 { 96 UserFileSystem::CloseDirect(GetPtr()); 97 this->m_P = 0; 98 } 99 } 100 GetHandle()101 Handle GetHandle() 102 { 103 return UserFileSystem::GetFileHandle(GetPtr()); 104 } 105 DetachHandle()106 void DetachHandle() 107 { 108 return UserFileSystem::DetachHandle(GetPtr()); 109 } 110 TryInitializeRaw(ArchiveHandle handle,const nn::fs::CTR::MPCore::Path & path,bit32 mode)111 Result TryInitializeRaw(ArchiveHandle handle, const nn::fs::CTR::MPCore::Path& path, bit32 mode) 112 { 113 return UserFileSystem::TryOpenFileRaw(&m_P, handle, path, mode); 114 } 115 TryInitializeRawDirectly(bit32 archiveType,const nn::fs::CTR::MPCore::Path & archivePath,const nn::fs::CTR::MPCore::Path & path,bit32 mode)116 Result TryInitializeRawDirectly(bit32 archiveType, const nn::fs::CTR::MPCore::Path& archivePath, const nn::fs::CTR::MPCore::Path& path, bit32 mode) 117 { 118 return UserFileSystem::TryOpenFileRawDirectly(&m_P, archiveType, archivePath, path, mode); 119 } 120 IsValid()121 bool IsValid() { return GetHandle().IsValid(); } 122 123 private: 124 125 // 4バイトアラインの値が入るはずなので、下位1ビットをフラグとして使う 126 void* m_P; 127 IsInitialized()128 bool IsInitialized() const { return GetPtr() != 0; } 129 130 // アドレスの下位1ビットをフラグとして使用するための関数 GetPtr()131 void* GetPtr() const { return reinterpret_cast<void*>(reinterpret_cast<uptr>(m_P) & ~static_cast<uptr>(0x1)); } NeedFlush()132 void NeedFlush() { m_P = reinterpret_cast<void*>(reinterpret_cast<uptr>(m_P) | 0x1); } DoneFlush()133 void DoneFlush() { m_P = reinterpret_cast<void*>(reinterpret_cast<uptr>(m_P) & ~static_cast<uptr>(0x1)); } IsNotFlushed()134 bool IsNotFlushed() { return (reinterpret_cast<uptr>(m_P) & 0x1) != 0; } 135 }; 136 137 }}} 138 139 #endif 140