YSTest: NativeAPI.cpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

NativeAPI.cpp
浏览该文件的文档.
1 /*
2 © 2012-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YCLib/YModules.h"
29 #include YFM_YCLib_NativeAPI
30 #if YCL_DS
31 # include YFM_DS_YCLib_DSIO // for ::DISC_INTERFACE, Disc,
32 // platform_ex::FAT::Mount;
33 # include <arm9/dldi.h> // for ::_io_dldi_stub;
34
35
37 extern "C" ::DLDI_INTERFACE _io_dldi_stub;
38 #elif YCL_MinGW
39 # include <ctime> // for std::gmtime;
40 #elif YCL_Android
41 # include <sys/syscall.h> // for ::syscall, __NR_utimensat;
42 #endif
43
44 namespace
45 {
46
47 }
48
49 #if YCL_DS
50 namespace platform_ex
51 {
52
54 using namespace platform::Descriptions;
55
56 FileSystem::FileSystem(size_t pages)
57 : root([pages]() ynothrow -> RootKind{
58 const auto init([=](const char* name, const ::DISC_INTERFACE& disc_io)
59 ynothrow -> bool{
60 // NOTE: %DEFAULT_SECTORS_PAGE is 8 in "common.h" in libfat source.
61 // This is also the minimal value used by the cache. So the shift
62 // is 3.
63 TryRet(platform_ex::FAT::Mount(name, disc_io, 0, pages, 3))
64 CatchExpr(std::exception& e, YCL_TraceRaw(Warning,
65 "FATMount failure[%s]: %s", typeid(e).name(), e.what()))
66 CatchExpr(..., YCL_TraceRaw(Emergent,
67 "Unknown exception found @ InitializeFileSystem."))
68 return {};
69 });
70
71 if(init("sd", ::__io_dsisd))
72 return RootKind::SD;
73 // NOTE: As %::dldiGetInternal.
74 if((::_io_dldi_stub.ioInterface.features
75 & (FEATURE_SLOT_GBA | FEATURE_SLOT_NDS)) != 0)
76 // NOTE: As %::sysSetCardOwner(BUS_OWNER_ARM9), with one less access
77 // to the volatile lvalue.
78 REG_EXMEMCNT &= ~ARM7_OWNS_CARD;
79 if(init("fat", ::_io_dldi_stub.ioInterface))
80 return RootKind::FAT;
81 return RootKind::Null;
82 }()), init_dev([this]{
83 if(root != RootKind::Null)
84 return root == RootKind::FAT ? "fat" : "sd";
85 // TODO: More descriptive message with underlying reason?
86 throw std::runtime_error("Failed initializing file system.");
87 }())
88 {
89 // NOTE: No %ARGV_MAGIC here as libnds does.
90 ::chdir(root == RootKind::FAT ? "fat:/" : "sd:/");
91 }
92
93 FileSystem::~FileSystem()
94 {
95 if(!YB_UNLIKELY(FAT::Unmount(init_dev)))
96 YCL_TraceRaw(Err, "Failed uninitializing file system.");
97 }
98
99 } // namespace platform_ex;
100 #elif YCL_MinGW
101 extern "C"
102 {
103
104 # if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
105 // See FileSystem.cpp.
107 struct ::tm* __cdecl __MINGW_NOTHROW
108 _gmtime32(const ::__time32_t* p)
109 {
110 return std::gmtime(p);
111 }
112 # endif
113
114 } // extern "C";
115 #elif YCL_Android
116 // NOTE: For versions where syscalls available, see http://man7.org/linux/man-pages/man2/syscalls.2.html.
117 // NOTE: Linux kernel version since 2.6.23 should be all OK. For early kernel
118 // versions Android used, see http://elinux.org/Android_Kernel_Versions.
119 // Also https://en.wikipedia.org/wiki/Android_version_history.
120 int
121 linkat(int fd1, const char* path1, int fd2, const char* path2, int flag)
122 ynothrow
123 {
124 // NOTE: The 'linkat' syscall was introduced in Linux 2.6.16.
125 return ::syscall(__NR_linkat, fd1, path1, fd2, path2, flag);
126 }
127
128 int
129 futimens(int fd, const ::timespec times[2]) ynothrow
130 {
131 // NOTE: See https://android.googlesource.com/platform/bionic/+/840a114eb12773c5af39c0c97675b27aa6dee78c/libc/bionic/futimens.cpp.
132 return ::utimensat(fd, {}, times, 0);
133 }
134
135 int
136 utimensat(int fd, const char* path, const ::timespec times[2], int flags)
137 ynothrow
138 {
139 // NOTE: The 'utimesat' syscall was introduced in Linux 2.6.22.
140 // NOTE: See http://stackoverflow.com/questions/19374749/how-to-work-around-absence-of-futimes-in-android-ndk.
141 return ::syscall(__NR_utimensat, fd, path, times, flags);
142 }
143 #endif
144
145 namespace platform
146 {
147 }
148
platform::p
while p p
Definition: YCLib/FileSystem.h:225
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
platform::Descriptions::Warning
Definition: YCommon.h:157
platform::Descriptions
平台描述空间。
Definition: YCommon.h:144
NPL::A1::name
ctx const string & name
Definition: NPLA1.h:248
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
CHRLib::CharSet::Null
空字符集。
Definition: Encoding.h:63
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
platform::Descriptions::Emergent
Definition: YCommon.h:153
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:438
platform
默认平台命名空间。
Definition: Container.h:47
std
Definition: memory.hpp:1100
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:303
platform_ex
平台扩展命名空间。
Definition: Android.h:45
YSLib::path
YF_API const char * path
Definition: Initialization.h:74
YCL_TraceRaw
#define YCL_TraceRaw(_lv,...)
YCLib 直接调试跟踪。
Definition: Debug.h:271
TryRet
#define TryRet(...)
指定表达式在 try 块中求值并返回。
Definition: YBaseMacro.h:124
YModules.h
platform::Descriptions::Err
Definition: YCommon.h:156
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140