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: 31889 $
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         else
71         {
72             DoneFlush();
73         }
74         return UserFileSystem::TryWriteFile(pOut, GetPtr(), offset, buffer, size, flush);
75     }
TryGetSize(s64 * pOut)76     Result TryGetSize(s64* pOut) const { return UserFileSystem::TryGetFileSize(pOut, GetPtr()); }
TrySetSize(s64 size)77     Result TrySetSize(s64 size) { return UserFileSystem::TrySetFileSize(GetPtr(), size); }
TryFlush()78     Result TryFlush()
79     {
80         DoneFlush();
81         return UserFileSystem::TryFlush(GetPtr());
82     }
83 
84 public:
85 
DuplicateHandle(Handle * pOut,s64 offset,s64 length)86     Result DuplicateHandle(Handle* pOut, s64 offset, s64 length)
87     {
88         return UserFileSystem::DuplicateHandleForFile(pOut, GetPtr(), offset, length);
89     }
90 
OpenDirect(Handle handle)91     void OpenDirect(Handle handle)
92     {
93         UserFileSystem::OpenDirect(&m_P, handle);
94     }
95 
CloseDirect()96     void CloseDirect()
97     {
98         if (GetPtr())
99         {
100             UserFileSystem::CloseDirect(GetPtr());
101             this->m_P = 0;
102         }
103     }
104 
GetHandle()105     Handle GetHandle()
106     {
107         return UserFileSystem::GetFileHandle(GetPtr());
108     }
109 
DetachHandle()110     void DetachHandle()
111     {
112         return UserFileSystem::DetachHandle(GetPtr());
113     }
114 
TryInitializeRaw(ArchiveHandle handle,const nn::fs::CTR::MPCore::Path & path,bit32 mode)115     Result TryInitializeRaw(ArchiveHandle handle, const nn::fs::CTR::MPCore::Path& path, bit32 mode)
116     {
117         return UserFileSystem::TryOpenFileRaw(&m_P, handle, path, mode);
118     }
119 
TryInitializeRawDirectly(bit32 archiveType,const nn::fs::CTR::MPCore::Path & archivePath,const nn::fs::CTR::MPCore::Path & path,bit32 mode)120     Result TryInitializeRawDirectly(bit32 archiveType, const nn::fs::CTR::MPCore::Path& archivePath, const nn::fs::CTR::MPCore::Path& path, bit32 mode)
121     {
122         return UserFileSystem::TryOpenFileRawDirectly(&m_P, archiveType, archivePath, path, mode);
123     }
124 
IsValid()125     bool IsValid() { return GetHandle().IsValid(); }
126 
127 private:
128 
129     // 4バイトアラインの値が入るはずなので、下位1ビットをフラグとして使う
130     void* m_P;
131 
IsInitialized()132     bool IsInitialized() const { return GetPtr() != 0; }
133 
134     // アドレスの下位1ビットをフラグとして使用するための関数
GetPtr()135     void*   GetPtr() const  { return reinterpret_cast<void*>(reinterpret_cast<uptr>(m_P) & ~static_cast<uptr>(0x1)); }
NeedFlush()136     void    NeedFlush()     { m_P = reinterpret_cast<void*>(reinterpret_cast<uptr>(m_P) | 0x1); }
DoneFlush()137     void    DoneFlush()     { m_P = reinterpret_cast<void*>(reinterpret_cast<uptr>(m_P) & ~static_cast<uptr>(0x1)); }
IsNotFlushed()138     bool    IsNotFlushed()  { return (reinterpret_cast<uptr>(m_P) & 0x1) != 0; }
139 };
140 
141 }}}
142 
143 #endif
144