Skip to content
Snippets Groups Projects
Commit 9d88f3e5 authored by Florian Fischer's avatar Florian Fischer Committed by Florian Schmaus
Browse files

[apps/fsearch] add simple "fgrep -r -n . <string>" implementation

parent e38375a8
No related branches found
No related tags found
1 merge request!1WIP: Emper shutdown
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2021 Florian Fischer
#include <fcntl.h>
#include <sys/types.h>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <string>
#include "Common.hpp"
#include "CountingPrivateSemaphore.hpp"
#include "Fiber.hpp"
#include "Runtime.hpp"
#include "emper.hpp"
#include "io.hpp"
namespace fs = std::filesystem;
#define EMPER_RIPGREP_BUFSIZE 4096
const char* needle;
size_t needle_len;
void search(const std::string& path) {
int fd = emper::io::openAndWait(path.c_str(), O_RDONLY);
if (fd < 0) {
DIE_MSG_ERRNO("open failed");
}
std::array<char, EMPER_RIPGREP_BUFSIZE> buf;
size_t bytes_searched = 0;
ssize_t bytes_read = emper::io::readFileAndWait(fd, buf.data(), buf.size(), bytes_searched);
while (bytes_read > 0) {
if (memmem(&buf[0], bytes_read, needle, needle_len)) {
printf("%s\n", path.c_str());
return;
}
bytes_searched += static_cast<size_t>(bytes_read);
bytes_read = emper::io::readFileAndWait(fd, buf.data(), buf.size(), -1);
}
if (bytes_read < 0) {
DIE_MSG_ERRNO("read failed");
}
}
void walk_dir() {
CPS cps;
for (const auto& p : fs::recursive_directory_iterator(".")) {
if (p.is_regular_file()) {
spawn([=] { search(p.path()); }, cps);
}
}
cps.wait();
exit(EXIT_SUCCESS);
}
auto main(int argc, char* argv[]) -> int {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <needle>" << std::endl;
return EXIT_FAILURE;
}
needle = argv[1];
needle_len = strlen(needle);
Runtime runtime;
auto* dirWalker = Fiber::from(walk_dir);
runtime.scheduleFromAnywhere(*dirWalker);
runtime.waitUntilFinished();
}
fsearch_exe = executable(
'fsearch',
'fsearch.cpp',
dependencies: emper_dep,
)
......@@ -21,3 +21,5 @@ echoclient_exe = executable(
'EchoClient.cpp',
dependencies: emper_dep,
)
subdir('fsearch')
......@@ -3,5 +3,7 @@
{ include: ["@<gtest/.*>", "private", "<gtest/gtest.h>", "public"] },
{ include: ["<urcu/map/urcu-memb.h>", "private", "<urcu.h>", "public"] },
{ include: ["<bits/cxxabi_forced.h>", "private", "<ctime>", "public" ] },
{ symbol: ["__kernel_timespec", "private", "<liburing.h>", "public" ] },
{ symbol: ["std::filesystem", "private", "<filesystem>", "public" ] },
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment