Browse Source

Safety additions. Do not follow links, track only regular files

main
Unbewohnte 3 years ago
parent
commit
0d39dae095
  1. 38
      src/broom.cpp
  2. 3
      src/broom.hpp
  3. 14
      src/main.cpp

38
src/broom.cpp

@ -40,23 +40,34 @@ void Broom::print_statistics() {
<< std::endl; << std::endl;
}; };
// get all files from path recursively and track them // recursively track every file that lies in given path. Throws an invalid_argument
void Broom::track(const std::filesystem::path dir) { // error in case path does not exist
void Broom::track(const std::filesystem::path path) {
auto t0 = std::chrono::high_resolution_clock::now(); auto t0 = std::chrono::high_resolution_clock::now();
std::filesystem::directory_options options = ( // check if given path even exists
std::filesystem::directory_options::follow_directory_symlink | if (!std::filesystem::exists(path)) {
std::filesystem::directory_options::skip_permission_denied throw std::invalid_argument("\"" + path.string() + "\"" + " does not exist !");
); };
for (std::filesystem::directory_entry dir_entry : std::filesystem::recursive_directory_iterator(dir, options)) { if (std::filesystem::is_directory(path)) {
if (dir_entry.is_directory()) { // it`s a directory. Track every regular file recursively
continue; std::filesystem::directory_options options = (
}; std::filesystem::directory_options::skip_permission_denied
);
m_tracked_filepaths.push_back(dir_entry); for (std::filesystem::directory_entry dir_entry : std::filesystem::recursive_directory_iterator(path, options)) {
if (!dir_entry.is_regular_file()) {
continue;
};
m_tracked_filepaths.push_back(dir_entry);
};
} else if (std::filesystem::is_regular_file(path)) {
m_tracked_filepaths.push_back(path);
}; };
if (m_benchmarking) { if (m_benchmarking) {
auto tracking_time = std::chrono::high_resolution_clock::now(); auto tracking_time = std::chrono::high_resolution_clock::now();
@ -78,6 +89,7 @@ uintmax_t Broom::untrack_unique_sizes() {
// if yes --> increment occurences counter // if yes --> increment occurences counter
// if not --> add it to the map with a counter of 1 // if not --> add it to the map with a counter of 1
uintmax_t filesize = std::filesystem::file_size(filepath); uintmax_t filesize = std::filesystem::file_size(filepath);
auto iterator = sizes_map.find(filesize); auto iterator = sizes_map.find(filesize);
if (iterator == sizes_map.end()) { if (iterator == sizes_map.end()) {
// there is no such size // there is no such size
@ -161,9 +173,7 @@ void Broom::find_duplicates() {
std::cout std::cout
<< "Untracking by size took " << "Untracking by size took "
<< std::chrono::duration_cast<std::chrono::milliseconds>(sizes_untrack_time - t0).count() << std::chrono::duration_cast<std::chrono::milliseconds>(sizes_untrack_time - t0).count()
<< " ms" << std::endl << " ms" << std::endl;
<< std::endl;
} else { } else {
size_t startsize = m_tracked_filepaths.size(); size_t startsize = m_tracked_filepaths.size();
std::cout << "Tracking " << startsize << std::endl; std::cout << "Tracking " << startsize << std::endl;

3
src/broom.hpp

@ -51,7 +51,8 @@ public:
// Print current statistics // Print current statistics
void print_statistics(); void print_statistics();
// recursively track every file that lies in given path // recursively track every file that lies in given path. Throws an invalid_argument
// error in case path does not exist
void track(const std::filesystem::path path); void track(const std::filesystem::path path);
// find all duplicates in the directory // find all duplicates in the directory

14
src/main.cpp

@ -18,6 +18,7 @@ along with broom. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <iostream> #include <iostream>
#include <stdexcept>
#include <string.h> #include <string.h>
#include <vector> #include <vector>
@ -82,13 +83,11 @@ int main(int argc, char* argv[]) {
} }
else { else {
// add path // add path
if (std::filesystem::exists(argv[i])) { tracked_path = argv[i];
tracked_path = argv[i];
};
}; };
}; };
// no path was specified at all or every path was nonexistent // no path was specified at all
if (tracked_path.string() == "") { if (tracked_path.string() == "") {
print_help(); print_help();
return 1; return 1;
@ -97,7 +96,12 @@ int main(int argc, char* argv[]) {
Broom broom(options); Broom broom(options);
broom.track(tracked_path); try {
broom.track(tracked_path);
} catch(const std::invalid_argument& e) {
std::cout << e.what() << std::endl;
return 1;
};
broom.find_duplicates(); broom.find_duplicates();
return 0; return 0;

Loading…
Cancel
Save