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)
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/>.
*/
#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;
Broom::Broom() {};
Broom::~Broom() {};
public:
Broom() {};
~Broom() {};
// Print current statistics
void print_statistics() {
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;
}
// Print current 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
<< std::endl;
};
// 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;
};
}
else if (entry1.compare_checksums(entry2.checksum)) {
return true;
}
// find all duplicates in the directory
int find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive = false) {
return 0;
};
return false;
};
// remove ALL duplicate files
int sweep_all(Entry entries[]) {
return 0;
};
// find all duplicates in the directory
int Broom::find_duplicates(std::filesystem::path directory, Entry entries[], bool recursive = false) {
return 0;
};
// remove ALL duplicate files
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) {
return 0;
};
// remove ALL duplicates but the one with specified index
int Broom::sweep_all_but(Entry entries[], uint32_t index = 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/>.
*/
# ifndef BROOM_HPP
# define BROOM_HPP
#ifndef BROOM_HPP
#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
#endif

69
src/entry.cpp

@ -20,34 +20,38 @@ 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) {
// check for existense and being a directory
if (!std::filesystem::exists(path) || std::filesystem::is_directory(path)) {
throw "Does not exist or a directory";
};
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";
};
// filename
filename = path.filename();
// filename
filename = path.filename();
// filesize
filesize = std::filesystem::file_size(path);
// filesize
filesize = std::filesystem::file_size(path);
// checksum
std::fstream entry_file;
entry_file.open(path);
// checksum
std::fstream entry_file;
entry_file.open(path);
if (!entry_file.is_open()) {
throw "Could not open file";
}
if (!entry_file.is_open()) {
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,26 +68,23 @@ 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]) {
for (uint8_t i = 0; i < CHECKSUM_SIZE; i++) {
if (checksum[i] != other_checksum[i]) {
return false;
};
// Compare this entry`s checksum with the other one.
// If the checksums are the same -> returns true, else -> false
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;
};
return true;
};
return true;
};
// Remove entity from the disk
void remove() {
std::filesystem::remove(path);
};
// Remove entry from the disk
void Entry::remove() {
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/>.
*/
# ifndef ENTRY_HPP
# define ENTRY_HPP
#ifndef ENTRY_HPP
#define ENTRY_HPP
#include <filesystem>
#include <fstream>
@ -30,21 +30,22 @@ 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();
};
# endif
#endif

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