Skip to content

Commit fc7ea43

Browse files
committed
Prevent from adding duplicate path in the process list
Use absolute path to uniquely store the path. Signed-off-by: Shashank Verma <[email protected]>
1 parent 227f574 commit fc7ea43

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

all.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <fstream>
88
#include <functional>
99
#include <iostream>
10+
#include <map>
1011
#include <mutex>
1112
#include <queue>
1213
#include <sstream>

cli.hpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace cli {
2727
print_help(true);
2828
}
2929

30-
void list_all_files(std::filesystem::path &root_path, std::vector<std::filesystem::path> *files, XorCryptor::Mode &mode) {
30+
void list_all_files(std::filesystem::path &root_path, std::map<std::filesystem::path, uint8_t> *files, XorCryptor::Mode &mode) {
3131
std::queue<std::filesystem::path> queue;
3232
queue.push(root_path);
3333
while (!queue.empty()) {
@@ -41,8 +41,10 @@ namespace cli {
4141
continue;
4242
}
4343
if (entry.is_regular_file()) {
44-
if (mode == XorCryptor::Mode::DECRYPT && entry.path().extension() == ".xrc") files->push_back(entry.path());
45-
if (mode == XorCryptor::Mode::ENCRYPT && entry.path().extension() != ".xrc") files->push_back(entry.path());
44+
if (mode == XorCryptor::Mode::DECRYPT && entry.path().extension() == ".xrc")
45+
(*files)[std::filesystem::absolute(entry.path())]++;
46+
if (mode == XorCryptor::Mode::ENCRYPT && entry.path().extension() != ".xrc")
47+
(*files)[std::filesystem::absolute(entry.path())]++;
4648
}
4749
}
4850
} catch (std::exception &e) {
@@ -77,8 +79,8 @@ namespace cli {
7779
[[nodiscard]] XorCryptor::Mode get_mode() const { return mode; }
7880

7981
static CmdArgs *parse_args(std::vector<std::string> &args) {
80-
auto *file_args = new std::vector<std::filesystem::path>(),
81-
*files = new std::vector<std::filesystem::path>();
82+
auto *file_args = new std::vector<std::filesystem::path>();
83+
auto *file_unq = new std::map<std::filesystem::path, uint8_t>();
8284

8385
XorCryptor::Mode mode = XorCryptor::Mode::INVALID;
8486
bool preserve_src = false, recursive = false, verbose = false;
@@ -118,29 +120,38 @@ namespace cli {
118120
std::cout << "Retrieving files ...\n";
119121
for (auto &path : *file_args) {
120122
if (std::filesystem::is_regular_file(path)) {
121-
if (mode == XorCryptor::Mode::DECRYPT && path.extension() == ".xrc") files->push_back(path);
122-
if (mode == XorCryptor::Mode::ENCRYPT && path.extension() != ".xrc") files->push_back(path);
123+
if (mode == XorCryptor::Mode::DECRYPT && path.extension() == ".xrc") (*file_unq)[std::filesystem::absolute(path)]++;
124+
if (mode == XorCryptor::Mode::ENCRYPT && path.extension() != ".xrc") (*file_unq)[std::filesystem::absolute(path)]++;
123125
}
124126
if (std::filesystem::is_directory(path)) {
125127
if (recursive) {
126-
list_all_files(path, files, mode);
128+
list_all_files(path, file_unq, mode);
127129
} else {
128130
for (const auto &entry : std::filesystem::directory_iterator(path)) {
129131
if (entry.is_directory()) continue;
130132
if (entry.is_regular_file()) {
131-
if (mode == XorCryptor::Mode::DECRYPT && entry.path().extension() == ".xrc") files->push_back(entry.path());
132-
if (mode == XorCryptor::Mode::ENCRYPT && entry.path().extension() != ".xrc") files->push_back(entry.path());
133+
if (mode == XorCryptor::Mode::DECRYPT && entry.path().extension() == ".xrc")
134+
(*file_unq)[std::filesystem::absolute(entry.path())]++;
135+
if (mode == XorCryptor::Mode::ENCRYPT && entry.path().extension() != ".xrc")
136+
(*file_unq)[std::filesystem::absolute(entry.path())]++;
133137
}
134138
}
135139
}
136140
}
137141
}
138-
if (files->empty()) {
142+
if (file_unq->empty()) {
139143
print_error("No " + std::string(mode == XorCryptor::Mode::DECRYPT ? ".xrc " : "") + "file(s) found");
140144
return nullptr;
141145
}
142-
std::cout << files->size() << (mode == XorCryptor::Mode::DECRYPT ? " .xrc" : "") << " file(s) found\n";
143-
return new CmdArgs(files, preserve_src, verbose, mode);
146+
std::cout << file_unq->size() << (mode == XorCryptor::Mode::DECRYPT ? " .xrc" : "") << " file(s) found\n";
147+
148+
auto *files = new std::vector<std::filesystem::path>();
149+
for (auto &file : *file_unq) files->emplace_back(file.first);
150+
auto *res = new CmdArgs(files, preserve_src, verbose, mode);
151+
152+
delete file_args;
153+
delete file_unq;
154+
return res;
144155
}
145156
};
146157

main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ int exec_cli_file(cli::CmdArgs *cmd_args, const std::string &progress, size_t id
144144
std::string dest_file_name(file_name);
145145
bool res;
146146

147-
auto parent = file_path.parent_path().parent_path().string();
147+
auto parent = file_path.parent_path().parent_path().parent_path().parent_path().string();
148148
auto short_path = file_path.string().replace(0, parent.length(), "...");
149149
if (cmd_args->get_verbose()) {
150150
cli_pr->print_status("\nProcessing: " + short_path);

0 commit comments

Comments
 (0)