Browse Source

Working multi-file build

main
Unbewohnte 3 years ago
parent
commit
a08f16852c
  1. 2
      build/CMakeLists.txt
  2. 74
      src/broom.cpp
  3. 15
      src/broom.hpp
  4. 69
      src/entry.cpp
  5. 15
      src/entry.hpp
  6. 5
      src/main.cpp

2
build/CMakeLists.txt

@ -13,4 +13,4 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -Werror -O2")
set(EXECUTABLE_OUTPUT_PATH ../bin) set(EXECUTABLE_OUTPUT_PATH ../bin)
add_executable(broom ../src/main.cpp) add_executable(broom ../src/main.cpp ../src/entry.cpp ../src/broom.cpp)

74
src/broom.cpp

@ -17,53 +17,45 @@ You should have received a copy of the GNU General Public License
along with broom. If not, see <https://www.gnu.org/licenses/>. along with broom. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <iostream>
#include "entry.hpp" #include "entry.hpp"
#include "broom.hpp"
// A class to find and manage duplicate files Broom::Broom() {};
class Broom { Broom::~Broom() {};
protected:
// how many files has been "sweeped"
uintmax_t m_sweeped_files;
// how many bytes was freed
uintmax_t m_sweeped_size;
public: // Print current statistics
Broom() {}; void Broom::print_statistics() {
~Broom() {}; std::cout
<< "| sweeped " << m_sweeped_files << " files" << std::endl
// Print current statistics << "| with a total size of " << m_sweeped_size << " bytes" << std::endl
void print_statistics() { << std::endl;
std::cout };
<< "| sweeped " << m_sweeped_files << " files" << std::endl
<< "| with a total size of " << m_sweeped_size << " bytes" << std::endl
<< std::endl;
};
// Determines whether entry1 is a duplicate of entry2
bool is_duplicate(Entry entry1, Entry entry2) {
if (entry1.path == entry2.path) {
// well, it`s the same file we`re talking about
return false;
}
else if (entry1.compare_checksums(entry2.checksum)) {
return true;
}
// Determines whether entry1 is a duplicate of entry2
bool Broom::is_duplicate(Entry entry1, Entry entry2) {
if (entry1.path == entry2.path) {
// well, it`s the same file we`re talking about
return false; return false;
}; }
else if (entry1.compare_checksums(entry2.checksum)) {
return true;
}
// find all duplicates in the directory return false;
int find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive = false) { };
return 0;
};
// remove ALL duplicate files // find all duplicates in the directory
int sweep_all(Entry entries[]) { int Broom::find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive = false) {
return 0; return 0;
}; };
// remove ALL duplicate files
int Broom::sweep_all(Entry entries[]) {
return 0;
};
// remove ALL duplicates but the one with specified index // remove ALL duplicates but the one with specified index
int sweep_all_but(Entry entries[], uint32_t index = 0) { int Broom::sweep_all_but(Entry entries[], uint32_t index = 0) {
return 0; return 0;
};
}; };

15
src/broom.hpp

@ -17,11 +17,10 @@ You should have received a copy of the GNU General Public License
along with broom. If not, see <https://www.gnu.org/licenses/>. along with broom. If not, see <https://www.gnu.org/licenses/>.
*/ */
# ifndef BROOM_HPP #ifndef BROOM_HPP
# define BROOM_HPP #define BROOM_HPP
#include <cstdint> #include <cstdint>
#include <iostream>
// A class to find and manage duplicate files // A class to find and manage duplicate files
class Broom { class Broom {
@ -32,8 +31,8 @@ protected:
uintmax_t m_sweeped_size; uintmax_t m_sweeped_size;
public: public:
Broom() {}; Broom();
~Broom() {}; ~Broom();
// Print current statistics // Print current statistics
void print_statistics(); void print_statistics();
@ -42,13 +41,13 @@ public:
bool is_duplicate(Entry entry1, Entry entry2); bool is_duplicate(Entry entry1, Entry entry2);
// find all duplicates in the directory // find all duplicates in the directory
int find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive = false); int find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive);
// remove ALL duplicate files // remove ALL duplicate files
int sweep_all(Entry entries[]); int sweep_all(Entry entries[]);
// remove ALL duplicates but the one with specified index // remove ALL duplicates but the one with specified index
int sweep_all_but(Entry entries[], uint32_t index = 0); int sweep_all_but(Entry entries[], uint32_t index);
}; };
# endif #endif

69
src/entry.cpp

@ -20,34 +20,38 @@ along with broom. If not, see <https://www.gnu.org/licenses/>.
#include "entry.hpp" #include "entry.hpp"
// A wrapper for every file with all necessary information // A wrapper for every file with all necessary information
class Entry { Entry::Entry(std::filesystem::path path) {
public: // check for existense and being a directory
Entry(std::filesystem::path path) { if (!std::filesystem::exists(path) || std::filesystem::is_directory(path)) {
// check for existense and being a directory throw "Does not exist or a directory";
if (!std::filesystem::exists(path) || std::filesystem::is_directory(path)) { };
throw "Does not exist or a directory";
};
// filename // filename
filename = path.filename(); filename = path.filename();
// filesize // filesize
filesize = std::filesystem::file_size(path); filesize = std::filesystem::file_size(path);
// checksum // checksum
std::fstream entry_file; std::fstream entry_file;
entry_file.open(path); entry_file.open(path);
if (!entry_file.is_open()) { if (!entry_file.is_open()) {
throw "Could not open file"; throw "Could not open file";
} }
// TODO(Properly test it)
if (filesize <= CHECKSUM_SIZE) {
entry_file.read(checksum, CHECKSUM_SIZE);
} else {
char start_buf[CHUNK_SIZE]; char start_buf[CHUNK_SIZE];
entry_file.read(start_buf, CHUNK_SIZE); entry_file.read(start_buf, CHUNK_SIZE);
entry_file.seekg(CHUNK_SIZE, std::ios::end);
char end_buf[CHUNK_SIZE]; char end_buf[CHUNK_SIZE];
entry_file.read(end_buf, CHUNK_SIZE); entry_file.read(end_buf, CHUNK_SIZE);
entry_file.seekg(CHUNK_SIZE, std::ios::beg);
char middle_buf[CHUNK_SIZE]; char middle_buf[CHUNK_SIZE];
entry_file.read(middle_buf, CHUNK_SIZE); entry_file.read(middle_buf, CHUNK_SIZE);
@ -64,26 +68,23 @@ public:
}; };
}; };
~Entry() {}; entry_file.close();
};
std::string filename; Entry::~Entry() {};
std::filesystem::path path;
uintmax_t filesize;
char checksum[CHECKSUM_SIZE];
// Compare this entry`s checksum with the other one. // Compare this entry`s checksum with the other one.
// If the checksums are the same -> returns true, else -> false // If the checksums are the same -> returns true, else -> false
bool compare_checksums(char other_checksum[CHECKSUM_SIZE]) { bool Entry::compare_checksums(char other_checksum[CHECKSUM_SIZE]) {
for (uint8_t i = 0; i < CHECKSUM_SIZE; i++) { for (uint8_t i = 0; i < CHECKSUM_SIZE; i++) {
if (checksum[i] != other_checksum[i]) { if (checksum[i] != other_checksum[i]) {
return false; return false;
};
}; };
return true;
}; };
return true;
};
// Remove entity from the disk // Remove entry from the disk
void remove() { void Entry::remove() {
std::filesystem::remove(path); std::filesystem::remove(path);
};
}; };

15
src/entry.hpp

@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License
along with broom. If not, see <https://www.gnu.org/licenses/>. along with broom. If not, see <https://www.gnu.org/licenses/>.
*/ */
# ifndef ENTRY_HPP #ifndef ENTRY_HPP
# define ENTRY_HPP #define ENTRY_HPP
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
@ -30,21 +30,22 @@ const uint8_t CHECKSUM_SIZE = CHUNK_SIZE * 3;
// A wrapper for every file with all necessary information // A wrapper for every file with all necessary information
class Entry { class Entry {
public: public:
Entry(std::filesystem::path path);
~Entry();
std::string filename; std::string filename;
std::filesystem::path path; std::filesystem::path path;
uintmax_t filesize; uintmax_t filesize;
char checksum[CHECKSUM_SIZE]; char checksum[CHECKSUM_SIZE];
Entry(std::filesystem::path path);
~Entry();
// Compare this entry`s checksum with the other one. // Compare this entry`s checksum with the other one.
// If the checksums are the same -> returns true, else -> false // If the checksums are the same -> returns true, else -> false
bool compare_checksums(char other_checksum[CHECKSUM_SIZE]); bool compare_checksums(char other_checksum[CHECKSUM_SIZE]);
// Remove entity from the disk // Remove entry from the disk
void remove(); void remove();
}; };
# endif #endif

5
src/main.cpp

@ -98,8 +98,9 @@ int main(int argc, char* argv[]) {
// printing all directories just for testing // printing all directories just for testing
for (uint32_t i = 0; i < options.paths.size(); i++) { for (uint32_t i = 0; i < options.paths.size(); i++) {
for (auto& p : std::filesystem::recursive_directory_iterator(options.paths.at(i))) { for (auto& p : std::filesystem::recursive_directory_iterator(options.paths.at(i))) {
if (p.is_directory()) { if (!p.is_directory()) {
std::cout << p.path() << std::endl; Entry entry(p);
std::cout << p.path() << "Checksum: " << entry.checksum << std::endl;
} }
}; };
}; };

Loading…
Cancel
Save