|
|
@ -20,9 +20,7 @@ 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: |
|
|
|
|
|
|
|
Entry(std::filesystem::path path) { |
|
|
|
|
|
|
|
// check for existense and being a directory
|
|
|
|
// check for existense and being a directory
|
|
|
|
if (!std::filesystem::exists(path) || std::filesystem::is_directory(path)) { |
|
|
|
if (!std::filesystem::exists(path) || std::filesystem::is_directory(path)) { |
|
|
|
throw "Does not exist or a directory"; |
|
|
|
throw "Does not exist or a directory"; |
|
|
@ -42,12 +40,18 @@ public: |
|
|
|
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,16 +68,14 @@ 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; |
|
|
@ -82,8 +84,7 @@ public: |
|
|
|
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); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|