Browse Source

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

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

26
src/broom.cpp

@ -40,22 +40,33 @@ 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();
// check if given path even exists
if (!std::filesystem::exists(path)) {
throw std::invalid_argument("\"" + path.string() + "\"" + " does not exist !");
};
if (std::filesystem::is_directory(path)) {
// it`s a directory. Track every regular file recursively
std::filesystem::directory_options options = ( std::filesystem::directory_options options = (
std::filesystem::directory_options::follow_directory_symlink |
std::filesystem::directory_options::skip_permission_denied std::filesystem::directory_options::skip_permission_denied
); );
for (std::filesystem::directory_entry dir_entry : std::filesystem::recursive_directory_iterator(dir, options)) { for (std::filesystem::directory_entry dir_entry : std::filesystem::recursive_directory_iterator(path, options)) {
if (dir_entry.is_directory()) { if (!dir_entry.is_regular_file()) {
continue; continue;
}; };
m_tracked_filepaths.push_back(dir_entry); 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

10
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);
try {
broom.track(tracked_path); 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