Skip to content

Commit 1176b00

Browse files
committed
unify path collection, add option to read inputs from stdin
1 parent f422bb9 commit 1176b00

7 files changed

+43
-85
lines changed

lib/rlib/common.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,35 @@ auto rlib::zstd_frame_decompress_size(std::span<char const> src) -> std::size_t
9898
rlib_assert_zstd(ZSTD_getFrameHeader(&header, src.data(), src.size()));
9999
return header.frameContentSize;
100100
}
101+
102+
auto rlib::collect_files(std::vector<std::string> const& inputs, function_ref<bool(fs::path const& path)> filter)
103+
-> std::vector<fs::path> {
104+
auto paths = std::vector<fs::path>{};
105+
if (inputs.size() == 1 && inputs.back() == "-") {
106+
std::string line;
107+
while (std::getline(std::cin, line)) {
108+
if (line.empty()) {
109+
continue;
110+
}
111+
paths.push_back(line);
112+
}
113+
} else {
114+
for (auto const& input : inputs) {
115+
rlib_assert(fs::exists(input));
116+
if (fs::is_regular_file(input)) {
117+
paths.push_back(input);
118+
} else {
119+
for (auto const& entry : fs::directory_iterator(input)) {
120+
if (!entry.is_regular_file()) {
121+
continue;
122+
}
123+
if (filter && !filter(entry.path())) {
124+
continue;
125+
}
126+
paths.push_back(entry.path());
127+
}
128+
}
129+
}
130+
}
131+
return paths;
132+
}

lib/rlib/common.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cinttypes>
88
#include <cstddef>
99
#include <cstring>
10+
#include <filesystem>
1011
#include <functional>
1112
#include <optional>
1213
#include <span>
@@ -54,6 +55,8 @@
5455
}()
5556

5657
namespace rlib {
58+
namespace fs = std::filesystem;
59+
5760
[[noreturn]] extern void throw_error(char const* from, char const* msg);
5861

5962
[[noreturn]] inline void throw_error(char const* from, std::error_code const& ec) {
@@ -155,4 +158,7 @@ namespace rlib {
155158
Ret (*invoke_)(void* ref, Args...) = nullptr;
156159
void* ref_ = nullptr;
157160
};
161+
162+
extern auto collect_files(std::vector<std::string> const& inputs, function_ref<bool(fs::path const& path)> filter)
163+
-> std::vector<fs::path>;
158164
}

src/rbun_chk.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,8 @@ struct Main {
3939
}
4040

4141
auto run() -> void {
42-
auto paths = std::vector<fs::path>();
4342
std::cerr << "Collecting input bundles ... " << std::endl;
44-
for (auto const& input : cli.inputs) {
45-
rlib_assert(fs::exists(input));
46-
if (fs::is_regular_file(input)) {
47-
paths.push_back(input);
48-
} else {
49-
for (auto const& entry : fs::directory_iterator(input)) {
50-
if (!entry.is_regular_file()) {
51-
continue;
52-
}
53-
if (entry.path().extension() != ".bundle") {
54-
continue;
55-
}
56-
paths.push_back(entry.path());
57-
}
58-
}
59-
}
43+
auto paths = collect_files(cli.inputs, [](fs::path const& p) { return p.extension() == ".bundle"; });
6044
std::cerr << "Processing input bundles ... " << std::endl;
6145
for (std::uint32_t index = paths.size(); auto const& path : paths) {
6246
verify_bundle(path, index--);

src/rbun_ex.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,8 @@ struct Main {
4848
}
4949

5050
auto run() -> void {
51-
auto paths = std::vector<fs::path>();
5251
std::cerr << "Collecting input bundles ... " << std::endl;
53-
for (auto const& input : cli.inputs) {
54-
rlib_assert(fs::exists(input));
55-
if (fs::is_regular_file(input)) {
56-
paths.push_back(input);
57-
} else {
58-
for (auto const& entry : fs::directory_iterator(input)) {
59-
if (!entry.is_regular_file()) {
60-
continue;
61-
}
62-
if (entry.path().extension() != ".bundle") {
63-
continue;
64-
}
65-
paths.push_back(entry.path());
66-
}
67-
}
68-
}
52+
auto paths = collect_files(cli.inputs, [](fs::path const& p) { return p.extension() == ".bundle"; });
6953
if (!paths.empty() && !cli.force) {
7054
std::cerr << "Processing existing chunks ... " << std::endl;
7155
fs::create_directories(cli.output);

src/rbun_ls.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,8 @@ struct Main {
2929
}
3030

3131
auto run() -> void {
32-
auto paths = std::vector<fs::path>();
3332
std::cerr << "Collecting input bundles ... " << std::endl;
34-
for (auto const& input : cli.inputs) {
35-
rlib_assert(fs::exists(input));
36-
if (fs::is_regular_file(input)) {
37-
paths.push_back(input);
38-
} else {
39-
for (auto const& entry : fs::directory_iterator(input)) {
40-
if (!entry.is_regular_file()) {
41-
continue;
42-
}
43-
if (entry.path().extension() != ".bundle") {
44-
continue;
45-
}
46-
paths.push_back(entry.path());
47-
}
48-
}
49-
}
33+
auto paths = collect_files(cli.inputs, [](fs::path const& p) { return p.extension() == ".bundle"; });
5034
std::cerr << "Processing input bundles ... " << std::endl;
5135
for (auto const& path : paths) {
5236
list_bundle(path);

src/rbun_merge.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,8 @@ struct Main {
5151
}
5252

5353
auto run() {
54-
auto paths = std::vector<fs::path>();
5554
std::cerr << "Collecting input bundles ... " << std::endl;
56-
for (auto const& input : cli.inputs) {
57-
rlib_assert(fs::exists(input));
58-
if (fs::is_regular_file(input)) {
59-
paths.push_back(input);
60-
} else {
61-
for (auto const& entry : fs::directory_iterator(input)) {
62-
if (!entry.is_regular_file()) {
63-
continue;
64-
}
65-
if (entry.path().extension() != ".bundle") {
66-
continue;
67-
}
68-
paths.push_back(entry.path());
69-
}
70-
}
71-
}
55+
auto paths = collect_files(cli.inputs, [](fs::path const& p) { return p.extension() == ".bundle"; });
7256
if (paths.empty()) {
7357
return;
7458
}

src/rbun_usage.cpp

+1-17
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,8 @@ struct Main {
3030
}
3131

3232
auto run() -> void {
33-
auto paths = std::vector<fs::path>();
3433
std::cerr << "Collecting input bundles ... " << std::endl;
35-
for (auto const& input : cli.inputs) {
36-
rlib_assert(fs::exists(input));
37-
if (fs::is_regular_file(input)) {
38-
paths.push_back(input);
39-
} else {
40-
for (auto const& entry : fs::directory_iterator(input)) {
41-
if (!entry.is_regular_file()) {
42-
continue;
43-
}
44-
if (entry.path().extension() != ".bundle") {
45-
continue;
46-
}
47-
paths.push_back(entry.path());
48-
}
49-
}
50-
}
34+
auto paths = collect_files(cli.inputs, [](fs::path const& p) { return p.extension() == ".bundle"; });
5135
std::cerr << "Processing input bundles ... " << std::endl;
5236
for (auto const& path : paths) {
5337
process_bundle(path);

0 commit comments

Comments
 (0)