From b3888cdab787039c1af7e7671d75debbf575f78e Mon Sep 17 00:00:00 2001 From: Marcel Nowicki Date: Sat, 12 Feb 2022 01:40:57 +0100 Subject: [PATCH] this added output of global stats to stdout --- main.cpp | 49 +++++++++++++++++++++++++++++-------------------- tarfunc.cpp | 18 ++++++++++++++++-- tarfunc.h | 5 +++++ 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/main.cpp b/main.cpp index a84c43d..c3e92de 100644 --- a/main.cpp +++ b/main.cpp @@ -21,7 +21,8 @@ int main(int argc, char** argv) { // GENERAL VARIABLES bool toFile = false; bool toJSON = false; - uintmax_t global_sizeofall{}; + uintmax_t global_sizeofallitems{}; + uintmax_t global_sizeofallfiles{}; std::map globaltypecount{ {tarconstant::typeFile, 0}, {tarconstant::typeDir, 0}, @@ -36,10 +37,14 @@ int main(int argc, char** argv) { cmdparam.emplace_back(argv[i]); } - std::vector archiveFilename {}; for (auto i : cmdparam) { if (i[0] != '-' && i.size() > 1) { + if(!tar::fileOpen(i)){ + tar::printhelp(); + std::cout << "Error opening file " << i <<" - closing down" << '\n'; + return 9; + } archiveFilename.push_back(i); } if (i[0] == '-') { @@ -65,14 +70,9 @@ int main(int argc, char** argv) { } } + // Core loop - opening files, check if compressed or not and parse accordingly. + for (auto &archiveName : archiveFilename) { - - if (!tar::fileOpen(archiveName)) { - std::cout << "Error opening file " << archiveName << "!" << '\n' << '\n'; - tar::printhelp(); - return 9; - } - //count the types of all items std::map typecount{ {tarconstant::typeFile, 0}, @@ -82,7 +82,7 @@ int main(int argc, char** argv) { {tarconstant::typeOther, 0} }; - uint64_t sizeof_allfiles{}; // total size of all files in the archive + uint64_t sizeof_allitems{}; // total size of all files in the archive bool isGz = tar::validGzip(archiveName); if (isGz) { @@ -101,7 +101,8 @@ int main(int argc, char** argv) { if (unzippedbytes == 0) break; - // tar file ends with 2 512byte blocks of 0. As no block should ever be 0 unless at the end, we check only once. + // tar file ends with 2 512byte blocks of 0. As no block should ever be 0 unless at the end, + // we check only once. if (tar::eof(headbuffer)) { break; } @@ -109,11 +110,13 @@ int main(int argc, char** argv) { // Read type of item std::string itemtype = tar::getitemtype(headbuffer[tarconstant::itemtypeByte]); typecount[itemtype] += 1; + globaltypecount[itemtype] += 1; // read itemsize and add to total - sizeof_allfiles += tar::getitemsize(headbuffer); + sizeof_allitems += tar::getitemsize(headbuffer); + global_sizeofallitems += tar::getitemsize(headbuffer); - // ignore file content. we want to get to the next header. item types != FILE have no content blocks (0 byte) + // ignore file content. we want to get to the next header. item types != FILE == 0 bytes uintmax_t help = (tar::getitemsize(headbuffer) / tarconstant::blocksize) * tarconstant::blocksize + tarconstant::blocksize; char dump[help] = {0}; @@ -124,10 +127,12 @@ int main(int argc, char** argv) { } } gzclose(gzIn); - tar::consolestats(typecount, std::filesystem::file_size(archiveName), sizeof_allfiles); + global_sizeofallfiles += std::filesystem::file_size(archiveName); + tar::consolestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems); if (toFile) { tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), - sizeof_allfiles, archiveName); + sizeof_allitems, archiveName); } } } @@ -154,9 +159,11 @@ int main(int argc, char** argv) { // Read type of item std::string itemtype = tar::getitemtype(headbuffer[tarconstant::itemtypeByte]); typecount[itemtype] += 1; + globaltypecount[itemtype] += 1; // read itemsize and add to total - sizeof_allfiles += tar::getitemsize(headbuffer); + sizeof_allitems += tar::getitemsize(headbuffer); + global_sizeofallitems += tar::getitemsize(headbuffer); // ignore file content. we want to get to the next header. item types != FILE have no content blocks (0 byte) if (tar::getitemsize(headbuffer) != 0) { @@ -168,16 +175,18 @@ int main(int argc, char** argv) { delete[] headbuffer; } file.close(); + global_sizeofallfiles += std::filesystem::file_size(archiveName); std::cout << toJSON << '\n'; - tar::consolestats(typecount, std::filesystem::file_size(archiveName), sizeof_allfiles); + tar::consolestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems); if (toFile) { - tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), sizeof_allfiles, - archiveName); + tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems, archiveName); } } } } - + tar::consoleglobalstats(globaltypecount, global_sizeofallfiles, global_sizeofallitems, archiveFilename); return 0; } diff --git a/tarfunc.cpp b/tarfunc.cpp index 69cd7a2..89601fb 100644 --- a/tarfunc.cpp +++ b/tarfunc.cpp @@ -10,6 +10,7 @@ #include #include #include "zlib.h" +#include // checks if a valid modern tar file - ustar bool tar::validTar(std::istream &file) { @@ -82,6 +83,19 @@ void tar::consolestats (std::map &typecount, uintmax_t t for (auto &i : typecount) { std::cout << i.first <<": " << i.second << '\n'; } + std::cout << '\n' << '\n'; +} + +void tar::consoleglobalstats (std::map &typecount, uintmax_t filesize, + uintmax_t itemsize, std::vector &files) { + std::cout << "GLOBAL STATS" << '\n'; + std::cout << "File amount: " << files.size() << '\n'; + std::cout << "Size of all archives: " << filesize << " Bytes" << '\n'; + std::cout << "Size of all items: " << itemsize << " Bytes" << '\n'; + for (auto &i : typecount) { + std::cout << i.first <<": " << i.second << '\n'; + } + std::cout << '\n' << '\n'; } // writes default console output to txt file @@ -123,7 +137,7 @@ bool tar::fileOpen(std::string &filename) { file.close(); return true; } - file.close(); return false; -} \ No newline at end of file +} + diff --git a/tarfunc.h b/tarfunc.h index 780b473..f4674e6 100644 --- a/tarfunc.h +++ b/tarfunc.h @@ -7,6 +7,7 @@ #include #include #include "tarconst.h" +#include #ifndef TARSTATS___TARFUNC_H @@ -30,6 +31,10 @@ namespace tar { // to write stats to console in default style void consolestats(std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall); +// to write global stats to stdout + void consoleglobalstats (std::map &typecount, uintmax_t itemfilesize, + uintmax_t szfiles, std::vector &files); + // to write default console output to txt file void txtfilestats(std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, std::string archiveName);