-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindFile.cpp
More file actions
109 lines (79 loc) · 2.52 KB
/
Copy pathFindFile.cpp
File metadata and controls
109 lines (79 loc) · 2.52 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
#include "FindFile.hpp"
namespace fs = std::filesystem;
std::vector<std::string> rootDirs;
// std::condition_variable cv;
// std::mutex m;
void iterate(char* path, char* fileName/*, std::atomic<bool> & workdone, std::condition_variable & cv, std::mutex & m*/)
{
// std::unique_lock<std::mutex> lk(m);
for (const auto& entry : fs::recursive_directory_iterator(path, fs::directory_options::skip_permission_denied))
{
try
{
if(fs::exists(entry))
{
if(entry.path().filename() == fileName)
{
std::cout << fileName << " found\n";
std::cout << "PATH TO FILE: " << entry.path() << "\n\n";
// std::notify_all_at_thread_exit(std::ref(cv), std::move(lk));
}
}
}catch(fs::__cxx11::filesystem_error &e)
{
//ignore
}
}
}
int goFind(char* filename)
{
fs::path rootPath = fs::current_path();
if(!rootPath.has_root_directory())
std::cout << "root directory is empty\n";
for(const auto &entry : fs::directory_iterator(rootPath.root_directory()))
{
if(fs::is_directory(entry.symlink_status()))
rootDirs.push_back(entry.path());
}
std::vector<std::thread> threads;
std::atomic<bool> workdone(false);
unsigned i = 0;
bool endOfDirs = false;
while(i < rootDirs.size())
{
for(int j = 0; j < 8; j++, i++)
{
if(i == rootDirs.size())
{
std::cout << "\nFILE NOT FOUND\n" << std::endl;
endOfDirs = 1;
break;
}
threads.push_back(std::thread(iterate, &rootDirs[i][0], filename/*, std::ref(workdone), std::ref(cv), std::ref(m)*/));
threads.back().join();
}
if(endOfDirs)
break;
i++;
}
// int workingThreads = 0;
// while(i < rootDirs.size())
// {
// for(; workingThreads < 8; workingThreads++, i++)
// {
// if(i == rootDirs.size())
// {
// std::cout << "\nFILE NOT FOUND\n\n";
// break;
// }
// threads.push_back(std::thread(iterate, &rootDirs[i][0], filename/*, std::ref(workdone), std::ref(cv), std::ref(m)*/));
// }
// for(int j = 0; j < workingThreads; j++)
// {
// threads[j].join();
// }
// workingThreads = 0;
// i++;
// }
return 0;
}