-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir.c
225 lines (189 loc) · 4.94 KB
/
dir.c
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
#include "dir.h"
/*********************************************************
************** Function Declaration ***************
*********************************************************/
/**
* @brief get type of file
*
* @param pathname [in] pathname of file
*
* @return file type, if succ; -1, if failed.
*/
file_type get_file_type(const char *pathname)
{
struct stat st = {0};
if (pathname == NULL) return F_UNKNOWN;
if (lstat(pathname, &st) < 0) return F_UNKNOWN;
if (S_ISREG(st.st_mode)) return F_REG;
else if (S_ISDIR(st.st_mode)) return F_DIR;
else if (S_ISCHR(st.st_mode)) return F_CHR;
else if (S_ISBLK(st.st_mode)) return F_BLK;
else if (S_ISFIFO(st.st_mode)) return F_FIFO;
else if (S_ISSOCK(st.st_mode)) return F_SOCK;
else if (S_ISLNK(st.st_mode)) return F_LNK;
else return F_UNKNOWN;
}
/**
* @brief get size of dir
*
* @param pathname [in] path of dir
*
* @return size of dir, if succ; -1, if failed.
*/
int get_dir_size(const char *pathname)
{
struct dirent *dir = NULL;
struct stat st = {0};
DIR *d = NULL;
int total_size = 0;
char buf[512] = {0};
if (pathname == NULL) return -1;
if (lstat(pathname, &st) < 0) return -1;
if (!S_ISDIR(st.st_mode)) return -1;
if ((d = opendir(pathname)) == NULL) return -1;
while ((dir = readdir(d)) != NULL)
{
if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) continue;
sprintf(buf, "%s/%s", pathname, dir->d_name);
if (lstat(buf, &st) >= 0)
{
if (S_ISDIR(st.st_mode))
{
total_size += get_dir_size(dir->d_name);
}
else total_size += st.st_size;
}
}
closedir(d);
return total_size;
}
/**
* @brief get size of file
*
* @param pathname [in] pathname of file
*
* @return size, if succ; -1, if failed.
*/
int get_file_size(const char *pathname)
{
struct stat st = {0};
if (pathname == NULL) return -1;
if (lstat(pathname, &st) < 0) return -1;
if (S_ISDIR(st.st_mode)) return get_dir_size(pathname);
else return st.st_size;
return -1;
}
/**
* @brief detect whether file is exist
*
* @param pathname [in] path of file
*
* @return 1, if exist; 0, if no exist; -1, if failed.
*/
int file_is_exist(const char *pathname)
{
if (pathname == NULL) return -1;
if (!access(pathname, F_OK)) return 1;
return 0;
}
/**
* @brief print file name of the dir
*
* @param pathname [in] path of dir
*/
void print_dir(const char *pathname)
{
DIR *dir = NULL;
struct dirent *entry = NULL;
struct stat st = {0};
if (pathname == NULL) return;
if (lstat(pathname, &st) < 0) return;
if (!S_ISDIR(st.st_mode)) return;
if ((dir = opendir(pathname)) == NULL) return;
if (chdir(pathname) !=0 ) return;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
return;
}
/**
* @brief create a multi directoy
*
* @param pathname [in] path of directory
* @param mode [in] mode of dir
*
* @return 0, if succ; -1, if failed.
*/
int make_dir(char *pathname, mode_t mode)
{
int i;
int size = (pathname != NULL) ? strlen(pathname) : -1;
if (size <= 0) return -1;
for (i = 1; i < size; i++)
{
if (pathname[i] == '/')
{
pathname[i] = '\0';
if (file_is_exist(pathname) != 1)
mkdir(pathname, mode);
pathname[i] = '/';
}
}
if (file_is_exist(pathname) != 1)
mkdir(pathname, mode);
if (file_is_exist(pathname) == 1) return 0;
return -1;
}
/**
* @brief remove directory
*
* @param pathname [in] path of directory
*
* @return 0, if succ; -1, if failed.
*/
int remove_dir(const char *pathname)
{
struct dirent *dir = NULL;
struct stat st = {0};
DIR *d = NULL;
char buf[512] = {0};
if (pathname == NULL) return -1;
if (lstat(pathname, &st) < 0) return -1;
if (!S_ISDIR(st.st_mode)) return -1;
if ((d = opendir(pathname)) == NULL) return -1;
while ((dir = readdir(d)) != NULL)
{
if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) continue;
sprintf(buf, "%s/%s", pathname, dir->d_name);
if (lstat(buf, &st) >= 0)
{
if (S_ISDIR(st.st_mode))
{
remove_dir(buf);
rmdir(buf);
}
else remove(buf);
}
}
if (file_is_exist(pathname) == 1) rmdir(pathname);
closedir(d);
if (file_is_exist(pathname) == 1) return -1;
return 0;
}
/**
* @brief remove file or dir
*
* @param pathname [in] path of file or directoy
*
* @return 0, if succ; -1, if failed.
*/
int remove_file(const char *pathname)
{
struct stat st = {0};
if (pathname == NULL) return -1;
if (lstat(pathname, &st) < 0) return -1;
if (S_ISDIR(st.st_mode)) return remove_dir(pathname);
else remove(pathname);
return 0;
}