unlimited-storage

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8fe5e6726a2cf65aa020ca00b28f87bfc1814bd2
parent e7885fb64bea0e16fdc6be053dd18bd765361858
Author: Andrew Laack <andrew@laack.co>
Date:   Thu, 17 Jul 2025 11:14:33 -0500

started working on stratification test + functionallity

Diffstat:
Minclude/chunk.h | 6+++++-
Msrc/chunk.cpp | 33++++++++++++++++++++++++++++++++-
Msrc/decode.cpp | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Atests/test-stratified.sh | 30++++++++++++++++++++++++++++++
4 files changed, 143 insertions(+), 7 deletions(-)

diff --git a/include/chunk.h b/include/chunk.h @@ -29,13 +29,17 @@ public: // read in the written chunk from pbm format. Chunk(std::string filename); std::vector<char> getChunk(); + std::string getFilename() const; + int getChunkNumber() const; // assumes header is populated in private member. + + bool operator<(const Chunk& other) const; + void writeChunk(); void writeChunk(std::string filename); void writeImage(std::string filename, uint chunkNumber, std::string originalFilename, uint x, uint y); void print(); std::string toString(); uint written; - }; diff --git a/src/chunk.cpp b/src/chunk.cpp @@ -165,6 +165,25 @@ Chunk::Chunk(std::string filename, uint start, uint x, uint y) { this->chunk = bytes; } + +bool Chunk::operator<(const Chunk& other) const { + + if (this->header.filename == other.header.filename){ + return this->header.chunkNumber < other.header.chunkNumber; + } + + return this->header.filename < other.header.filename; +} + + +int Chunk::getChunkNumber() const{ + return this->header.chunkNumber; +} + +std::string Chunk::getFilename() const{ + return this->header.filename; +} + std::vector<char> Chunk::getChunk() { return this->chunk; } @@ -176,6 +195,18 @@ std::vector<char> Chunk::getChunk() { void Chunk::writeChunk() { LOG(this->header.filename) + + + if(this->header.chunkNumber == 0){ + bool deleteExisting = std::filesystem::remove(this->header.filename); + if(deleteExisting){ + LOG("FILE DELETED") + } + else{ + LOG("NO FILE TO DELETE, CONTINUING") + } + } + std::ofstream outFile; std::vector<char> bytes = this->chunk; @@ -198,7 +229,7 @@ void Chunk::writeChunk(std::string filename) { std::vector<char> bytes = this->chunk; - if(this->header.chunkNumber != 1) { + if(this->header.chunkNumber != 0) { outFile.open(filename, std::ios::binary | std::ios::app); } else { outFile.open(filename, std::ios::binary | std::ios::out); diff --git a/src/decode.cpp b/src/decode.cpp @@ -1,21 +1,92 @@ #include "../include/chunk.h" #include "iostream" #include "../include/debug.h" +#include "filesystem" +#include <algorithm> -// decode only one input file. +// this does not respect chunk numbers + +void writeSingleFile(std::string filename){ + Chunk chunk = Chunk(filename); + chunk.writeChunk(); + +} + + +// This loads each file from the directory into memory. +// it then orders them by file name then chunk number and writes +// them back to the original file. + +void writeDirectory(std::string dirName){ + LOG("WRITING DIRECTORY") + + std::vector<std::string> filenames; + + for (const auto & entry : std::filesystem::directory_iterator(dirName)){ + + std::string current = entry.path(); + + + if(current.find(".pbm") != std::string::npos){ + filenames.push_back(current); + } + } + + std::vector<Chunk> chunks; + + + for (std::string filename : filenames){ + Chunk chunk (filename); + + LOG("UNSORTED") + LOG(std::to_string(chunk.getChunkNumber()) + " - " + chunk.getFilename()) + + chunks.push_back(chunk); + } + + + // sort chunks first by filename then by chunk # + + std::sort(chunks.begin(), chunks.end()); + + LOG("SORTED") + for (Chunk chunk : chunks){ + LOG(std::to_string(chunk.getChunkNumber()) + " - " + chunk.getFilename()); + } +} + int main(int argc, char* argv[]) { if (argc < 2) { - std::cout << "usage: [filename]" << std::endl; + std::cout << "usage: [filename] OR -d [directory]" << std::endl; return 10; } - std::string filename = argv[1]; + std::string filename; + std::string directory; + bool useDirectory = false; + if (std::string(argv[1]) == "-d") { + if (argc < 3) { + std::cout << "usage: -d [directory]" << std::endl; + return 11; + } + directory = argv[2]; + useDirectory = true; + } + else{ + filename = argv[1]; + } + + if (useDirectory){ + writeDirectory(directory); + } + else{ + writeSingleFile(filename); + } - Chunk chunk = Chunk(filename); - chunk.writeChunk(); return 0; } + diff --git a/tests/test-stratified.sh b/tests/test-stratified.sh @@ -0,0 +1,30 @@ + +hash=$(sha256sum tests/resources/stratified.txt) + +mkdir tests/artifacts/strat + +./build/debugEncode.out tests/resources/stratified.txt tests/artifacts/strat/strat.out 100 100 + +mv tests/resources/stratified.txt tests/resources/stratified.bak + +./build/debugDecode.out -d tests/artifacts/strat + +res_hash=$(sha256sum tests/resources/stratified.txt) + +mv tests/resources/stratified.txt tests/resources/cp_stratified.txt +mv tests/resources/stratified.bak tests/resources/stratified.txt + + +echo $res_hash +echo $hash + +if [[ "$res_hash" == "$hash" ]]; then + echo + echo "Hashes match" +else + echo + echo "Hashes do not match!!!!" + echo "res_hash: $res_hash" + echo "hash: $hash" + exit 1 +fi