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: 25872 $
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/util/util_Result.h>
23 #include <nn/os/os_HandleObject.h>
24 #include <nn/fs/fs_IpcFile.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 "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() { UserFileSystem::CloseFile(m_P); this->m_P = 0; }
~FileBaseImpl()51     ~FileBaseImpl() { Finalize(); }
52 
TryRead(s32 * pOut,s64 offset,void * buffer,size_t size)53     Result TryRead(s32* pOut, s64 offset, void* buffer, size_t size) { return UserFileSystem::TryReadFile(pOut, m_P, offset, buffer, size); }
TryWrite(s32 * pOut,s64 offset,const void * buffer,size_t size)54     Result TryWrite(s32* pOut, s64 offset, const void* buffer, size_t size) { return UserFileSystem::TryWriteFile(pOut, m_P, offset, buffer, size); }
TryGetSize(s64 * pOut)55     Result TryGetSize(s64* pOut) const { return UserFileSystem::TryGetFileSize(pOut, m_P); }
TrySetSize(s64 size)56     Result TrySetSize(s64 size) { return UserFileSystem::TrySetFileSize(m_P, size); }
57 
58 public:
59 
DuplicateHandle(Handle * pOut,s64 offset,s64 length)60     Result DuplicateHandle(Handle* pOut, s64 offset, s64 length)
61     {
62         return UserFileSystem::DuplicateHandleForFile(pOut, m_P, offset, length);
63     }
64 
OpenDirect(Handle handle)65     void OpenDirect(Handle handle)
66     {
67         UserFileSystem::OpenDirect(&m_P, handle);
68     }
69 
CloseDirect()70     void CloseDirect()
71     {
72         UserFileSystem::CloseDirect(m_P);
73     }
74 
GetHandle()75     Handle GetHandle()
76     {
77         return UserFileSystem::GetFileHandle(m_P);
78     }
79 
DetachHandle()80     void DetachHandle()
81     {
82         return UserFileSystem::DetachHandle(m_P);
83     }
84 
TryInitializeRaw(ArchiveHandle handle,const nn::fs::CTR::MPCore::Path & path,bit32 mode)85     Result TryInitializeRaw(ArchiveHandle handle, const nn::fs::CTR::MPCore::Path& path, bit32 mode)
86     {
87         return UserFileSystem::TryOpenFileRaw(&m_P, handle, path, mode);
88     }
89 
TryInitializeRawDirectly(bit32 archiveType,const nn::fs::CTR::MPCore::Path & archivePath,const nn::fs::CTR::MPCore::Path & path,bit32 mode)90     Result TryInitializeRawDirectly(bit32 archiveType, const nn::fs::CTR::MPCore::Path& archivePath, const nn::fs::CTR::MPCore::Path& path, bit32 mode)
91     {
92         return UserFileSystem::TryOpenFileRawDirectly(&m_P, archiveType, archivePath, path, mode);
93     }
94 
IsValid()95     bool IsValid() { return GetHandle().IsValid(); }
96 
97 private:
98 
99     void* m_P;
100 
IsInitialized()101     bool IsInitialized() const { return m_P != 0; }
102 
103 };
104 
105 }}}
106 
107 #endif
108