Skip to content
Snippets Groups Projects

Improve fsearch

Merged Florian Fischer requested to merge aj46ezos/emper:improve-fsearch into master
2 files
+ 25
3
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 22
3
@@ -15,6 +15,7 @@
@@ -15,6 +15,7 @@
#include "CountingPrivateSemaphore.hpp"
#include "CountingPrivateSemaphore.hpp"
#include "Fiber.hpp"
#include "Fiber.hpp"
#include "Runtime.hpp"
#include "Runtime.hpp"
 
#include "Semaphore.hpp"
#include "emper.hpp"
#include "emper.hpp"
#include "io.hpp"
#include "io.hpp"
@@ -25,7 +26,13 @@ namespace fs = std::filesystem;
@@ -25,7 +26,13 @@ namespace fs = std::filesystem;
const char* needle;
const char* needle;
size_t needle_len;
size_t needle_len;
 
emper::Semaphore* max_running;
 
void search(const std::string& path) {
void search(const std::string& path) {
 
if (max_running) {
 
max_running->acquire();
 
}
 
int fd = emper::io::openAndWait(path.c_str(), O_RDONLY);
int fd = emper::io::openAndWait(path.c_str(), O_RDONLY);
if (fd < 0) {
if (fd < 0) {
DIE_MSG_ERRNO("open failed");
DIE_MSG_ERRNO("open failed");
@@ -38,7 +45,7 @@ void search(const std::string& path) {
@@ -38,7 +45,7 @@ void search(const std::string& path) {
while (bytes_read > 0) {
while (bytes_read > 0) {
if (memmem(&buf[0], bytes_read, needle, needle_len)) {
if (memmem(&buf[0], bytes_read, needle, needle_len)) {
printf("%s\n", path.c_str());
printf("%s\n", path.c_str());
return;
goto out;
}
}
bytes_searched += bytes_read;
bytes_searched += bytes_read;
@@ -48,6 +55,13 @@ void search(const std::string& path) {
@@ -48,6 +55,13 @@ void search(const std::string& path) {
if (bytes_read < 0) {
if (bytes_read < 0) {
DIE_MSG_ERRNO("read failed");
DIE_MSG_ERRNO("read failed");
}
}
 
 
out:
 
if (max_running) {
 
max_running->release();
 
}
 
 
emper::io::closeAndForget(fd);
}
}
void walk_dir() {
void walk_dir() {
@@ -64,8 +78,12 @@ void walk_dir() {
@@ -64,8 +78,12 @@ void walk_dir() {
auto main(int argc, char* argv[]) -> int {
auto main(int argc, char* argv[]) -> int {
if (argc < 2) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <needle>" << std::endl;
std::cerr << "Usage: " << argv[0] << " <needle> [max fibers]" << std::endl;
return EXIT_FAILURE;
return EXIT_SUCCESS;
 
}
 
 
if (argc == 3) {
 
max_running = new emper::Semaphore(std::stoi(argv[2]));
}
}
needle = argv[1];
needle = argv[1];
@@ -77,4 +95,5 @@ auto main(int argc, char* argv[]) -> int {
@@ -77,4 +95,5 @@ auto main(int argc, char* argv[]) -> int {
runtime.scheduleFromAnywhere(*dirWalker);
runtime.scheduleFromAnywhere(*dirWalker);
runtime.waitUntilFinished();
runtime.waitUntilFinished();
 
return EXIT_SUCCESS;
}
}
Loading