-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem2_2.cpp
More file actions
311 lines (277 loc) · 8.55 KB
/
problem2_2.cpp
File metadata and controls
311 lines (277 loc) · 8.55 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <sys/types.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include <limits.h>
#include <glob.h>
#include <time.h>
#include <dirent.h>
#include <algorithm>
#include <string.h>
#include <signal.h>
#include <sys/resource.h>
#include <time.h>
#include <sstream>
#include <iomanip>
#include <thread>
#include <mutex>
#include <future>
#include <queue>
#include <mutex>
template<typename T>
struct pqueue{
std::queue<T> _queue;
std::mutex m;
void push(const T& e){
std::lock_guard<std::mutex> my_lock(m);
_queue.push(e);
}
T& front(){
std::lock_guard<std::mutex> my_lock(m);
T ret = _queue.front();
_queue.pop();
return ret;
}
size_t size(){
m.lock();
return _queue.size();
}
};
std::string getDir(){
char buff[PATH_MAX];
getcwd(buff, PATH_MAX);
return std::string(buff);
}
struct SearchParam{
bool onlyCurrentDir = false;
size_t threadsCount = 1;
std::string searchPattern = "";
std::string path = "";
};
char parce(int argc, char* argv[], SearchParam& ret){
bool gotPattern = false;
ret.threadsCount = 1;
ret.onlyCurrentDir = false;
ret.searchPattern = "";
ret.path = "";
for(int i = 1; i < argc; i++){
if(argv[i][0] == '-'){
if(argv[i][1] == '\0'){
write(2, "Неопознанный пустой аргумент -\n", 58);
return 1;
} else if(argv[i][1] == 'n'){
ret.onlyCurrentDir = true;
} else if(argv[i][1] == 't'){
ret.threadsCount = atoll(&argv[i][2]);
} else{
write(2, "Неопознанный аргумент ", 43);
write(2, argv[i], strlen(argv[i]));
write(2, "\n", 2);
return 1;
}
} else if(gotPattern){
ret.path = std::string(argv[i]);
} else{
gotPattern = true;
ret.searchPattern = std::string(argv[i]);
}
}
if(!gotPattern){
write(2, "Поиск по пустой строке невозможен\n", 63);
return 1;
}
return 0;
}
size_t _getFileSize(int fd){
size_t _file_size = 0;
struct stat _fileStatbuff;
if(fd == -1){
_file_size = 0;
}
else{
if ((fstat(fd, &_fileStatbuff) != 0) || (!S_ISREG(_fileStatbuff.st_mode))) {
_file_size = 0;
}
else{
_file_size = _fileStatbuff.st_size;
}
}
return _file_size;
}
struct asyncSize_t {
std::mutex m;
size_t i;
};
struct nodeAutomata{
char c;
size_t pref;
};
size_t goAutomate(const std::vector<nodeAutomata>& automate, size_t node_index, char c){
while(node_index && (node_index + 1 == automate.size() || automate[node_index + 1].c != c)){
node_index = automate[node_index].pref;
}
if(automate[node_index + 1].c == c){
node_index++;
}
return node_index;
}
size_t buildAutomate(std::vector<nodeAutomata>& automate, std::string pattern){
automate.clear();
automate.push_back({'\0', size_t(0)});
for(auto& e: pattern){
size_t node_index = automate.size() - 1;
while(node_index && (node_index + 1 == automate.size() || automate[node_index + 1].c != e)){
node_index = automate[node_index].pref;
}
if((node_index + 1 < automate.size()) && automate[node_index + 1].c == e){
node_index++;
}
automate.push_back({e, node_index});
}
return 0;
}
struct find_pattern_ret_str_pair{
size_t num;
std::string ans_string;
};
struct find_pattern_ret{
std::string path = "";
std::vector<find_pattern_ret_str_pair> ret;
};
struct searchInFile_ret_pair{
size_t strnum;
std::string str;
friend bool operator<(const searchInFile_ret_pair& a, const searchInFile_ret_pair& b){
return a.strnum < b.strnum;
}
};
struct searchInFile_ret{
std::string path;
std::vector<searchInFile_ret_pair> str;
};
void find_pattern(int fd, const SearchParam& comparam, const std::vector<nodeAutomata>& automate, std::vector<std::vector<find_pattern_ret>>& ret, size_t ret_i){
size_t buf_size = size_t(2 << 20);
size_t node_index = 0;
bool getans = true;
size_t strcnt = 0;
std::string ans_string = "";
ssize_t rdbites = 1;
char* buf = (char*)malloc(buf_size * sizeof(char));
while(rdbites){
rdbites = read(fd, buf, buf_size);
for(ssize_t i = 0; i < rdbites; i++){
if(buf[i] != '\n'){
ans_string += buf[i];
}
node_index = goAutomate(automate, node_index, buf[i]);
if(getans && (node_index == (automate.size() - 1))){
//printf("Found in [%d] at %d\n", offset, p + i);
getans = false;
}
if(buf[i] == '\n'){
if(!getans){
ret[ret_i][ret[ret_i].size() - 1].ret.push_back({strcnt, ans_string});
}
ans_string = "";
strcnt++;
getans = true;
}
}
}
free(buf);
return;
}
void seak_pattern(const SearchParam& comparam, const std::vector<nodeAutomata>& automate, std::vector<std::vector<find_pattern_ret>>& ret, size_t ret_i, pqueue<std::string>& queue, std::atomic_bool& cont){
while(cont || queue._queue.size()){
queue.m.lock();
if(queue._queue.size()){
std::string path = queue._queue.front();
queue._queue.pop();
queue.m.unlock();
ret[ret_i].push_back({path, std::vector<find_pattern_ret_str_pair>()});
int fd = open(path.c_str(), O_RDONLY);
find_pattern(fd, comparam, automate, ret, ret_i);
close(fd);
}
else{
queue.m.unlock();
}
}
}
void searchInFile(std::string path, const SearchParam& comparam, pqueue<std::string>& queue){
queue.push(path);
}
void searchInDir(std::string path, DIR* dir, const SearchParam& comparam, pqueue<std::string>& queue){
for (dirent *cdir = readdir(dir); cdir != nullptr; cdir = readdir(dir)){
std::string nextname(path+ "/" + cdir->d_name);
if (cdir->d_type == DT_REG){
searchInFile(nextname, comparam, queue);
}
}
rewinddir(dir);
if(!comparam.onlyCurrentDir){
for (dirent *cdir = readdir(dir); cdir != nullptr; cdir = readdir(dir)){
std::string nextname(path+ "/" + cdir->d_name);
if (cdir->d_type == DT_DIR && !(strcmp(cdir->d_name,".") == 0 || strcmp(cdir->d_name,"..") == 0)){
DIR* nextdir = opendir(nextname.c_str());
searchInDir(nextname, nextdir, comparam, queue);
closedir(nextdir);
}
}
}
}
int main(int argc, char* argv[]) {
SearchParam comparam;
size_t printed = 0;
if(parce(argc, argv, comparam)){
return 1;
}
DIR *dir = nullptr;
std::string path;
path = comparam.path;
if(comparam.path.size() == 0){
path = getDir();
}
dir = opendir(path.c_str());
size_t threadsUsed = 0;
std::vector<std::thread> curThreads;
std::vector<nodeAutomata> automate;
buildAutomate(automate, comparam.searchPattern);
std::vector<int> fd;
std::thread printret_thread;
if(dir == nullptr){
write(2, "Невозможно получить доступ к папке ", 66);
write(2, comparam.path.c_str(), comparam.path.size());
write(2, "\n", 2);
return 1;
}
pqueue<std::string> queue;
std::atomic_bool cont = true;
std::vector<std::vector<find_pattern_ret>> ret(comparam.threadsCount);
for(size_t t = 0; t < comparam.threadsCount; t++){
curThreads.push_back(std::thread(seak_pattern, std::ref(comparam), std::ref(automate), std::ref(ret), size_t(t), std::ref(queue), std::ref(cont)));
}
searchInDir(path, dir, comparam, queue);
cont = false;
for(size_t i = 0;i < curThreads.size(); i++){
curThreads[i].join();
for(auto& file:ret[i]){
if(file.ret.size()){
printf("Search pattern has been found in file: %s\n", file.path.c_str());
for(auto& e:file.ret){
printf("\t in string [%d]: %s\n", e.num + 1, e.ans_string.c_str());
}
}
}
}
closedir(dir);
return 0;
}