|
|
|
@ -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); |
|
|
|
|
}; |
|
|
|
|