-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathfs.h
293 lines (263 loc) · 7.69 KB
/
fs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2025 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#ifndef TBOX_UTIL_FS_H_20220103
#define TBOX_UTIL_FS_H_20220103
#include <string>
#include <vector>
#include <functional>
namespace tbox {
namespace util {
namespace fs {
////////////////////////////////////////////////////////////////////
// 文件相关
////////////////////////////////////////////////////////////////////
//! 文件类型
enum class FileType {
kNone, //!< 未知
kDirectory, //!< 目录
kRegular, //!< 常规文件
kCharacterDevice, //!< 字符设备
kBlockDevice, //!< 块设备
kSymbolLink, //!< 符号链接
kSocket, //!< 套接字
kNamedPipe, //!< 有名管道
};
/**
* 获取文件类型
*
* \param file_path 文件路径
* \return FileType 文件类型
*/
FileType GetFileType(const std::string &file_path);
/**
* 检查文件是否存在
*
* \param filename 文件名
*
* \return true 文件存在
* \return false 文件不存在
*/
bool IsFileExist(const std::string &filename);
/**
* 从文件中读取字串
*
* \param filename 文件名
* \param content 读取的文本输出的std::string
*
* \return true 成功
* \return false 失败
*/
bool ReadStringFromTextFile(const std::string &filename, std::string &content);
/// 遍历文本中的每一行
/**
* \param filename 文件名
* \param line_handle_func 行字串处理函数
*
* \return true 文件打开成功
* \return false 文件打开失败
*/
bool ReadEachLineFromTextFile(const std::string &filename, const std::function<void(const std::string&)> &line_handle_func);
/**
* 一次性读取文件中所有行
*
* \param filename 文件名
* \param lines 行文本数组
*
* \return true 文件打开成功
* \return false 文件打开失败
*/
bool ReadAllLinesFromTextFile(const std::string &filename, std::vector<std::string> &lines);
/**
* 从文件中读取第一行文本
*
* \param filename 文件名
* \param text 读出的行内容
*
* \return true 文件打开成功
* \return false 文件打开失败
*/
bool ReadFirstLineFromTextFile(const std::string &filename, std::string &text);
/**
* 将字串写入到文件
*
* \param filename 文件名
* \param content 将要写入的字串内容
* \param sync_now 是否需要立即sync
*
* \return true 成功
* \return false 失败
*/
bool WriteStringToTextFile(const std::string &filename, const std::string &content, bool sync_now = false);
/**
* 将字串追加到文件尾部
*
* \param filename 文件名
* \param content 将要写入的字串内容
* \param sync_now 是否需要立即sync
*
* \return true 成功
* \return false 失败
*/
bool AppendStringToTextFile(const std::string &filename, const std::string &content, bool sync_now = false);
/**
* 从文件中读取数据
*
* \param filename 文件名
* \param content 读取的数据,可能非字串
*
* \return true 成功
* \return false 失败
*/
bool ReadBinaryFromFile(const std::string &filename, std::string &content);
/**
* 将数据写入到文件
*
* \param filename 文件名
* \param content 将要写入的数据内容
* \param sync_now 是否需要立即sync
*
* \return true 成功
* \return false 失败
*/
bool WriteBinaryToFile(const std::string &filename, const std::string &content, bool sync_now = false);
/**
* 将数据写入到文件
*
* \param filename 文件名
* \param data_ptr 将要写入的数据地址
* \param data_size 将要写入的数据长度
* \param sync_now 是否需要立即sync
*
* \return true 成功
* \return false 失败
*/
bool WriteFile(const char *filename, const void *data_ptr, size_t data_size, bool sync_now = false);
/**
* 将数据追加到文件尾部
*
* \param filename 文件名
* \param data_ptr 将要写入的数据地址
* \param data_size 将要写入的数据长度
* \param sync_now 是否需要立即sync
*
* \return true 成功
* \return false 失败
*/
bool AppendFile(const char *filename, const void *data_ptr, size_t data_size, bool sync_now = false);
/**
* 删除文件
*
* \param filename 被删除的文件名
*
* \return true 成功
* \return false 失败
*/
bool RemoveFile(const std::string &filename, bool allow_log_print = true);
/// 创建符号链接文件
/**
* \param old_path 源路径
* \param new_path 符号链接文件的路径
* \param allow_log_print 是否允许错误日志打印
*
* \return true 成功
* \return false 失败
*/
bool MakeSymbolLink(const std::string &old_path, const std::string &new_path, bool allow_log_print = true);
/// 创建链接文件
/**
* \param old_path 源路径
* \param new_path 符号链接文件的路径
* \param allow_log_print 是否允许错误日志打印
*
* \return true 成功
* \return false 失败
*/
bool MakeLink(const std::string &old_path, const std::string &new_path, bool allow_log_print = true);
////////////////////////////////////////////////////////////////////
// 目录相关
////////////////////////////////////////////////////////////////////
/**
* 目录是否存在
*
* \param dir 目录
*
* \return true 目录存在
* \return false 目录不存在
*/
bool IsDirectoryExist(const std::string &dir);
/**
* 创建目录
* 等价于shell命令 "mkdir -p xxx"
*
* \param dir 目录
* \param allow_log_print 是否允许错误日志打印
* 在日志模块中使用要禁用,其它默认打开即可
*
* \return true 目录创建成功
* \return false 目录创建失败
*/
bool MakeDirectory(const std::string &dir, bool allow_log_print = true);
/**
* 递归删除指定目录
* 等价于shell命令:"rm -rf xxx"
*
* \param dir 需要删除的目录路径,路径需要全路径,如 /data/test
* \param is_remove_file_only 保存目录结构,仅删除文件
*
* \return true 目录被完全删除
* \return false 目录未被完全删除
*/
bool RemoveDirectory(const std::string &dir, bool is_remove_file_only = false);
/**
* 列出指定目录下的子文件与目录
* 等价于shell命令:"ls"
*
* \param dir 需要删除的目录路径,路径需要全路径,如 /data/test
*
* \return true 成功
* \return false 失改
*/
bool ListDirectory(const std::string &dir, std::vector<std::string> &names);
////////////////////////////////////////////////////////////////////
// 其它
////////////////////////////////////////////////////////////////////
/**
* 根据路径获取文件名
*/
std::string Basename(const std::string &full_path);
const char* Basename(const char *full_path);
/**
* 根据路径获取目录名
*/
std::string Dirname(const std::string &full_path);
/**
* 重命名
*
* \param old_name 旧文件名
* \param new_name 新文件名
*
* \return true 成功
* \return false 失败
*/
bool Rename(const std::string &old_name, const std::string &new_name);
}
}
}
#endif //TBOX_UTIL_FS_H_20220103