-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_utility.h
103 lines (86 loc) · 2.29 KB
/
fs_utility.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
/**
# -*- coding:UTF-8 -*-
*/
#ifndef _FS_UTILITY_H_
#define _FS_UTILITY_H_
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifndef MAX_NAME
#define MAX_NAME 255
#endif
#define WIN_DELIMITER '\\'
#define LINUX_DELIMITER '/'
#ifdef __GNUC__
#include <unistd.h>
#include <dirent.h>
#ifndef MAX_PATH
#define MAX_PATH 4096
#endif
#define DIR_DELIMITER LINUX_DELIMITER
#elif defined _WIN32
#include <direct.h>
#include <share.h>
#include <io.h>
#ifndef STDIN_FILENO
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
#define DIR_DELIMITER WIN_DELIMITER
#define mkdir(path, mode) _mkdir(path)
#endif
#else
#error os not support
#endif
#ifdef __cplusplus
extern "C"
{
#endif
/**
*检测文件或目录是否已存在
*@return 当且仅当检测到该路径不存在时返回 0
* 如果出现错误,返回 3
* 如果是个文件,返回 1
* 如果是个目录,返回 2
*/
int32_t is_path_exists(const char* path);
/**
*检测目录是否可写
*尝试在目标目录创建一个文件并删除
*@return 当且仅当该目录存在并且创建文件成功时返回true
* 如果在检测过程中出现错误,返回false
*/
int32_t is_dir_writable(const char* path);
/**
*删除目录
*删除目标目录及目录内所有文件
*@return 成功返回0,否则返回错误码
*/
int32_t remove_dir(const char* path, uint32_t path_len);
/**
*删除空目录
*删除目标目录及其所有子目录中的空目录
*如果不想递归删除空目录,请使用 rmdir(path);
*@return 成功返回0,否则返回错误码
*/
int32_t remove_empty_dir(const char* path, uint32_t path_len);
/**
*格式化路径
*将路径中的目录分隔符转换到相应平台,并去除重复出现的目录分隔符
*可以用于文件或目录
*@return 总是成功
*/
int32_t format_path(char* path, uint32_t* path_len);
/**
*创建目录
*如果路径中的目录不存在,创建
*@return 0表示成功
*/
int32_t create_dir(const char* path, uint32_t path_len);
#ifdef __cplusplus
}
#endif
#endif