Browse Source

Working multi-file build

main
Unbewohnte 3 years ago
parent
commit
a08f16852c
  1. 2
      build/CMakeLists.txt
  2. 26
      src/broom.cpp
  3. 9
      src/broom.hpp
  4. 25
      src/entry.cpp
  5. 9
      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)
add_executable(broom ../src/main.cpp)
add_executable(broom ../src/main.cpp ../src/entry.cpp ../src/broom.cpp)

26
src/broom.cpp

@ -17,22 +17,15 @@ You should have received a copy of the GNU General Public License
along with broom. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "entry.hpp"
#include "broom.hpp"
// A class to find and manage duplicate files
class Broom {
protected:
// how many files has been "sweeped"
uintmax_t m_sweeped_files;
// how many bytes was freed
uintmax_t m_sweeped_size;
public:
Broom() {};
~Broom() {};
Broom::Broom() {};
Broom::~Broom() {};
// Print current statistics
void print_statistics() {
void Broom::print_statistics() {
std::cout
<< "| sweeped " << m_sweeped_files << " files" << std::endl
<< "| with a total size of " << m_sweeped_size << " bytes" << std::endl
@ -40,7 +33,7 @@ public:
};
// Determines whether entry1 is a duplicate of entry2
bool is_duplicate(Entry entry1, Entry 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;
@ -53,17 +46,16 @@ public:
};
// find all duplicates in the directory
int find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive = false) {
int Broom::find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive = false) {
return 0;
};
// remove ALL duplicate files
int sweep_all(Entry entries[]) {
int Broom::sweep_all(Entry entries[]) {
return 0;
};
// 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;
};
};

9
src/broom.hpp

@ -21,7 +21,6 @@ along with broom. If not, see <https://www.gnu.org/licenses/>.
#define BROOM_HPP
#include <cstdint>
#include <iostream>
// A class to find and manage duplicate files
class Broom {
@ -32,8 +31,8 @@ protected:
uintmax_t m_sweeped_size;
public:
Broom() {};
~Broom() {};
Broom();
~Broom();
// Print current statistics
void print_statistics();
@ -42,13 +41,13 @@ public:
bool is_duplicate(Entry entry1, Entry entry2);
// 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
int sweep_all(Entry entries[]);
// 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

25
src/entry.cpp

@ -20,9 +20,7 @@ along with broom. If not, see <https://www.gnu.org/licenses/>.
#include "entry.hpp"
// A wrapper for every file with all necessary information
class Entry {
public:
Entry(std::filesystem::path path) {
Entry::Entry(std::filesystem::path path) {
// check for existense and being a directory
if (!std::filesystem::exists(path) || std::filesystem::is_directory(path)) {
throw "Does not exist or a directory";
@ -42,12 +40,18 @@ public:
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];
entry_file.read(start_buf, CHUNK_SIZE);
entry_file.seekg(CHUNK_SIZE, std::ios::end);
char end_buf[CHUNK_SIZE];
entry_file.read(end_buf, CHUNK_SIZE);
entry_file.seekg(CHUNK_SIZE, std::ios::beg);
char middle_buf[CHUNK_SIZE];
entry_file.read(middle_buf, CHUNK_SIZE);
@ -64,16 +68,14 @@ public:
};
};
~Entry() {};
entry_file.close();
};
std::string filename;
std::filesystem::path path;
uintmax_t filesize;
char checksum[CHECKSUM_SIZE];
Entry::~Entry() {};
// Compare this entry`s checksum with the other one.
// 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++) {
if (checksum[i] != other_checksum[i]) {
return false;
@ -82,8 +84,7 @@ public:
return true;
};
// Remove entity from the disk
void remove() {
// Remove entry from the disk
void Entry::remove() {
std::filesystem::remove(path);
};
};

9
src/entry.hpp

@ -30,19 +30,20 @@ const uint8_t CHECKSUM_SIZE = CHUNK_SIZE * 3;
// A wrapper for every file with all necessary information
class Entry {
public:
Entry(std::filesystem::path path);
~Entry();
std::string filename;
std::filesystem::path path;
uintmax_t filesize;
char checksum[CHECKSUM_SIZE];
Entry(std::filesystem::path path);
~Entry();
// Compare this entry`s checksum with the other one.
// If the checksums are the same -> returns true, else -> false
bool compare_checksums(char other_checksum[CHECKSUM_SIZE]);
// Remove entity from the disk
// Remove entry from the disk
void remove();
};

5
src/main.cpp

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

Loading…
Cancel
Save