-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir_util.cpp
More file actions
163 lines (133 loc) · 4.36 KB
/
dir_util.cpp
File metadata and controls
163 lines (133 loc) · 4.36 KB
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
#include "dir_util.h"
#include <unistd.h>
extern "C" {
#include <stdlib.h>
#include <string.h>
};
#undef _DEBUG // 临时取消调试信息
#ifdef _DEBUG
#include "macro.h"
#endif // _DEBUG
#include <string>
using namespace std;
bool DirUtil::Ls(const char* directory, std::vector<std::string>* result, size_t* count,
const mode_t* mode /* = NULL */, bool fill_path /* = false */) {
size_t dir_len = directory ? strlen(directory) : 0;
string dir_str = (0 == dir_len) ? "." : directory;
// 保证路径后面只有一个斜杠"/"
dir_str += '/';
dir_str.erase(dir_str.rfind('/'));
dir_str += '/';
DIR* dir;
struct dirent* ptr = NULL;
if ((dir = opendir(dir_str.c_str())) == NULL) {
return false;
}
#ifdef _DEBUG
cout << "In DirUtil::Ls()" << endl;
#endif // _DEBUG
struct stat buf;
while ((ptr = readdir(dir)) != NULL) {
if (strcmp(".", ptr->d_name) == 0 || strcmp("..", ptr->d_name) == 0)
continue;
string full_filename = dir_str + ptr->d_name;
// check file type and authority
if (mode) {
memset(&buf, 0, sizeof(buf));
stat(full_filename.c_str(), &buf);
if ((*mode & buf.st_mode) != *mode)
continue;
}
#ifdef _DEBUG
printf("-- find file: %s\n", ptr->d_name);
#endif // _DEBUG
if (result) {
if (fill_path)
result->push_back(full_filename);
else
result->push_back(ptr->d_name);
}
if (count)
++(*count);
}
closedir(dir);
return true;
}
bool DirUtil::LsDir(const char* directory, std::vector<std::string>& dirs, bool fill_path /* = false */) {
mode_t mode = S_IFDIR;
return Ls(directory, &dirs, NULL, &mode, fill_path);
}
bool DirUtil::LsFile(const char* directory, std::vector<std::string>& files, bool fill_path /* = false */) {
mode_t mode = S_IFREG;
return Ls(directory, &files, NULL, &mode, fill_path);
}
bool DirUtil::DirCount(const char* directory, size_t& count) {
mode_t mode = S_IFDIR;
if (! Ls(directory, NULL, &count, &mode))
return false;
return true;
}
bool DirUtil::FileCount(const char* directory, size_t& count) {
mode_t mode = S_IFREG;
if (! Ls(directory, NULL, &count, &mode))
return false;
return true;
}
string DirUtil::Pwd() {
char* pch = get_current_dir_name();
string s(pch);
free(pch);
return s;
}
bool DirUtil::LsOrdered(
const char* directory, DirUtil::OrderedResult_t* result, size_t* count,
const mode_t* mode /* = NULL */, bool fill_path /* = false */) {
size_t dir_len = directory ? strlen(directory) : 0;
string dir_str = (0 == dir_len) ? "." : directory;
// 保证路径后面只有一个斜杠"/"
dir_str += '/';
dir_str.erase(dir_str.rfind('/'));
dir_str += '/';
DIR* dir;
struct dirent* ptr = NULL;
if ((dir = opendir(dir_str.c_str())) == NULL) {
return false;
}
#ifdef _DEBUG
cout << "In DirUtil::LsOrdered()" << endl;
#endif // _DEBUG
struct stat buf;
while ((ptr = readdir(dir)) != NULL) {
if (strcmp(".", ptr->d_name) == 0 || strcmp("..", ptr->d_name) == 0)
continue;
string full_filename = dir_str + ptr->d_name;
// check file type and authority
if (mode) {
memset(&buf, 0, sizeof(buf));
stat(full_filename.c_str(), &buf);
if ((*mode & buf.st_mode) != *mode)
continue;
}
#ifdef _DEBUG
cout << "-- find file: " << ptr->d_name << ", inode: " << ptr->d_ino << endl;
#endif // _DEBUG
if (result) {
if (fill_path)
result->insert(make_pair(ptr->d_ino, full_filename));
else
result->insert(make_pair(ptr->d_ino, ptr->d_name));
}
if (count)
++(*count);
}
closedir(dir);
return true;
}
bool DirUtil::LsDirOrdered(const char* directory, DirUtil::OrderedResult_t& dirs, bool fill_path /* = false */) {
mode_t mode = S_IFDIR;
return LsOrdered(directory, &dirs, NULL, &mode, fill_path);
}
bool DirUtil::LsFileOrdered(const char* directory, DirUtil::OrderedResult_t& files, bool fill_path /* = false */) {
mode_t mode = S_IFREG;
return LsOrdered(directory, &files, NULL, &mode, fill_path);
}