YSTest: FileSystem.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

YSLib/Service/FileSystem.cpp
浏览该文件的文档.
1 /*
2 © 2010-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 "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_FileSystem
30 #include <ystdex/cstring.h>
31 #include <ystdex/exception.h> // for ystdex::throw_error, system_error;
32 #include YFM_YSLib_Service_File // for OpenFile;
33
34 namespace YSLib
35 {
36
37 using namespace Text;
38
39 namespace IO
40 {
41
42 String
43 Path::GetString(char16_t delimiter) const
44 {
45 const auto res(ystdex::to_string_d(GetBase(), delimiter));
46
47 YAssert(res.empty() || res.back() == delimiter,
48 "Invalid conversion result found.");
49 return res;
50 }
51
52 String
53 Path::Verify(char16_t delimiter) const
54 {
55 return VerifyDirectoryPathTail(GetString(delimiter));
56 }
57
58
59 Path
60 MakeNormalizedAbsolute(const Path& pth, size_t init_size)
61 {
62 Path res(pth);
63
64 if(IsRelative(res))
65 res = Path(FetchCurrentWorkingDirectory<char16_t>(init_size)) / res;
66 res.Normalize();
67 YTraceDe(Debug, "Converted path is '%s'.", res.VerifyAsMBCS().c_str());
68 YAssert(IsAbsolute(res), "Invalid path converted.");
69 return res;
70 }
71
72
74 namespace
75 {
76
77 template<typename _tChar>
78 YB_NONNULL(1) bool
79 VerifyDirectoryImpl(const _tChar* path)
80 {
81 try
82 {
83 DirectorySession ss(path);
84
85 return true;
86 }
87 CatchExpr(std::system_error& e, YTraceDe(Debug,
88 "Directory verfication failed."), ExtractAndTrace(e, Debug))
89 return {};
90 }
91
92 } // unnamed namespace;
93
94 bool
95 VerifyDirectory(const char* path)
96 {
97 return VerifyDirectoryImpl(path);
98 }
99 bool
100 VerifyDirectory(const char16_t* path)
101 {
102 return VerifyDirectoryImpl(path);
103 }
104
105 void
106 EnsureDirectory(const Path& pth)
107 {
108 string upath;
109
110 for(const auto& name : pth)
111 {
112 upath += MakeMBCS(name.c_str()) + FetchSeparator<char>();
113 if(!VerifyDirectory(upath) && !umkdir(upath.c_str()) && errno != EEXIST)
114 {
115 YTraceDe(Err, "Failed making directory path '%s'", upath.c_str());
116 ystdex::throw_error(errno, yfsig);
117 }
118 }
119 }
120
121
123 namespace
124 {
125
127 YB_NONNULL(1) UniqueFile
128 OpenFileForCopy(const char* src)
129 {
130 return
131 OpenFile(src, omode_convb(std::ios_base::in | std::ios_base::binary));
132 }
133
134 } // unnamed namespace;
135
136 void
137 CopyFile(UniqueFile p_dst, FileDescriptor src_fd)
138 {
139 FileDescriptor::WriteContent(Nonnull(p_dst.get()), Nonnull(src_fd));
140 }
141 void
142 CopyFile(UniqueFile p_dst, const char* src)
143 {
144 CopyFile(std::move(p_dst), OpenFileForCopy(src).get());
145 }
146 void
147 CopyFile(const char* dst, FileDescriptor src_fd, mode_t dst_mode,
148 size_t allowed_links, bool share)
149 {
150 FileDescriptor::WriteContent(EnsureUniqueFile(dst, dst_mode, allowed_links,
151 share).get(), Nonnull(src_fd));
152 }
153 void
154 CopyFile(const char* dst, const char* src, mode_t dst_mode,
155 size_t allowed_links, bool share)
156 {
157 CopyFile(dst, OpenFileForCopy(src).get(), dst_mode, allowed_links, share);
158 }
159 void
160 CopyFile(const char* dst, FileDescriptor src_fd, CopyFileHandler f,
161 mode_t dst_mode, size_t allowed_links, bool share)
162 {
163 const auto p_dst(EnsureUniqueFile(dst, dst_mode, allowed_links,
164 share));
165
166 FileDescriptor::WriteContent(p_dst.get(), Nonnull(src_fd));
167 // NOTE: Guards are not used since the post operations should only be
168 // performed after success copying.
169 f(p_dst.get(), src_fd);
170 }
171 void
172 CopyFile(const char* dst, const char* src, CopyFileHandler f, mode_t dst_mode,
173 size_t allowed_links, bool share)
174 {
175 CopyFile(dst, OpenFileForCopy(src).get(), f, dst_mode, allowed_links,
176 share);
177 }
178
179
180 void
181 ClearTree(const Path& pth)
182 {
183 TraverseChildren(pth, [&](NodeCategory c, NativePathView npv){
184 const auto child(pth / String(npv));
185
186 if(c == NodeCategory::Directory)
187 DeleteTree(child);
188 Remove(string(child).c_str());
189 });
190 }
191
192 void
193 ListFiles(const Path& pth, vector<String>& lst)
194 {
195 Traverse(pth, [&](NodeCategory c, NativePathView npv){
196 lst.push_back(!PathTraits::is_parent(npv)
197 && bool(c & NodeCategory::Directory) ? String(npv)
198 + FetchSeparator<char16_t>() : String(npv));
199 });
200 }
201
202
203 NodeCategory
204 ClassifyNode(const Path& pth)
205 {
206 if(pth.empty())
207 return NodeCategory::Empty;
208
209 const auto& fname(pth.back());
210
211 switch(ystdex::classify_path<String, PathTraits>(fname))
212 {
213 case ystdex::path_category::empty:
214 return NodeCategory::Empty;
215 case ystdex::path_category::self:
216 case ystdex::path_category::parent:
217 break;
218 default:
219 return VerifyDirectory(pth) ? NodeCategory::Directory
220 : (ufexists(String(pth).c_str()) ? NodeCategory::Regular
221 : NodeCategory::Missing);
222 // TODO: Implementation for other categories.
223 }
224 return NodeCategory::Unknown;
225 }
226
227 } // namespace IO;
228
229 } // namespace YSLib;
230
YSLib::IO::VerifyDirectory
bool VerifyDirectory(const char16_t *path)
Definition: YSLib/Service/FileSystem.cpp:100
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:313
platform::Descriptions::Debug
Definition: YCommon.h:170
NPL::SXML::ParseOption::String
platform::FileDescriptor
文件描述符包装类。
Definition: FileIO.h:146
ystdex::basic_string_view
Definition: string_view.hpp:49
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:95
YModules.h
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
yfsig
#define yfsig
展开为表示函数签名的预定义变量的宏。
Definition: ydef.h:728
CHRLib::f
f(d, s)
YSLib::IO::EnsureDirectory
void EnsureDirectory(const Path &pth)
Definition: YSLib/Service/FileSystem.cpp:106
CHRLib::MakeMBCS
_tDst MakeMBCS(string_view sv, Encoding enc)
Definition: CharacterProcessing.h:603
Text
YSLib::IO::ListFiles
void ListFiles(const Path &pth, vector< String > &lst)
Definition: YSLib/Service/FileSystem.cpp:193
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1280
NPL::c_str
YF_API const char src c_str())) YF_API void InstallDirectory(const string &
安装目录：复制目录树。
Definition: Dependency.h:131
platform::UniqueFile
unique_ptr_from< FileDescriptor::Deleter > UniqueFile
Definition: FileIO.h:464
platform::omode_convb
std::ios_base::sync_with_stdio(sync)) YF_API YB_STATELESS int omode_conv(std YF_API YB_STATELESS int omode_convb(std::ios_base::openmode) ynothrow
ISO C++ 标准输入输出接口打开模式转换为 POSIX 文件打开模式。
Definition: FileIO.cpp:840
YSLib::IO::DeleteTree
DeleteTree
Definition: YSLib/Service/FileSystem.h:799
YSLib::IO::CopyFileHandler
std::function< void(FileDescriptor, FileDescriptor)> CopyFileHandler
复制文件处理器：通知文件复制事件。
Definition: YSLib/Service/FileSystem.h:699
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
YSLib::IO::ClearTree
YF_API void ClearTree(const Path &)
路径指向的不是一个目录或删除失败。
Definition: YSLib/Service/FileSystem.cpp:181
ystdex::path_category::self
platform::EnsureUniqueFile
UniqueFile EnsureUniqueFile(const char *dst, mode_t mode, size_t allowed_links, bool share)
Definition: FileIO.cpp:1201
ystdex::path_category::empty
ystdex::throw_error
void throw_error(std::error_code ec, _tParams &&...args)
抛出错误： std::system_error 或允许相同构造函数参数的异常。
Definition: ystdex/exception.h:206
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
platform::FileDescriptor::WriteContent
static void WriteContent(FileDescriptor, FileDescriptor, size_t=yimpl(size_t(BUFSIZ<< 4)))
Definition: FileIO.cpp:679
YSLib::IO::ClassifyNode
const Path &pth const string &pth vector< String > &YF_API NodeCategory ClassifyNode(const Path &)
按文件系统节点类别对路径分类。
Definition: YSLib/Service/FileSystem.cpp:204
IO
platform::DirectorySession
目录会话：表示打开的目录。
Definition: YCLib/FileSystem.h:468
YSLib::IO::PathTraits::is_parent
static yconstfn bool is_parent(const _tString &str) ynothrow
Definition: YSLib/Service/FileSystem.h:107
cstring.h
ISO C 标准字符串扩展。
exception.h
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
platform::Nonnull
_type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:422
YSLib::IO::CopyFile
void CopyFile(const char *dst, const char *src, CopyFileHandler f, mode_t dst_mode, size_t allowed_links, bool share)
Definition: YSLib/Service/FileSystem.cpp:172
platform::Descriptions::Err
Definition: YCommon.h:166
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:130
platform::KeyCodes::Win32::Empty
Definition: Keys.h:279
CHRLib::c
c
Definition: CharacterProcessing.h:81
platform::umkdir
bool umkdir(const char *path) ynothrowv
Definition: FileIO.cpp:1063
YSLib::Text::YB_NONNULL
YB_NONNULL(1, 2) PDefH(bool
检查第一参数是否具有指定的 BOM 。
CHRLib::char16_t
char16_t((*c_ptr<< CHAR_BIT)|c_ptr[1])) inline PDefH(char16_t
取 c_ptr 指向的小端序双字节字符。
YSLib::IO::Path
路径。
Definition: YSLib/Service/FileSystem.h:214
platform::ufexists
bool ufexists(const char *filename) ynothrowv
Definition: FileIO.cpp:948
YSLib::path
YF_API const char * path
Definition: Initialization.h:77
platform::mode_t
::mode_t mode_t
文件模式类型。
Definition: FileIO.h:114
ystdex::path_category::parent
YSLib::ExtractAndTrace
YF_API void ExtractAndTrace(const std::exception &, RecordLevel=Err) ynothrow
使用 TraceException 展开和跟踪异常类型和信息。
YSLib::IO::MakeNormalizedAbsolute
Path MakeNormalizedAbsolute(const Path &pth, size_t init_size)
Definition: YSLib/Service/FileSystem.cpp:60
YSLib::IO::OpenFile
UniqueFile OpenFile(const char *filename, int omode, mode_t pmode)
Definition: File.cpp:38
YSLib::IO::IsAbsolute
IsAbsolute
Definition: YSLib/Service/FileSystem.h:57
platform_ex::Windows::Directory
Definition: MinGW32.h:452
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: YString.h:46