mirror of
https://gitea.home.endeavr.de/Marcel/Tarstats-pp.git
synced 2025-12-10 00:39:01 +01:00
this added output of global stats to stdout
This commit is contained in:
49
main.cpp
49
main.cpp
@@ -21,7 +21,8 @@ int main(int argc, char** argv) {
|
|||||||
// GENERAL VARIABLES
|
// GENERAL VARIABLES
|
||||||
bool toFile = false;
|
bool toFile = false;
|
||||||
bool toJSON = false;
|
bool toJSON = false;
|
||||||
uintmax_t global_sizeofall{};
|
uintmax_t global_sizeofallitems{};
|
||||||
|
uintmax_t global_sizeofallfiles{};
|
||||||
std::map<std::string, uintmax_t> globaltypecount{
|
std::map<std::string, uintmax_t> globaltypecount{
|
||||||
{tarconstant::typeFile, 0},
|
{tarconstant::typeFile, 0},
|
||||||
{tarconstant::typeDir, 0},
|
{tarconstant::typeDir, 0},
|
||||||
@@ -36,10 +37,14 @@ int main(int argc, char** argv) {
|
|||||||
cmdparam.emplace_back(argv[i]);
|
cmdparam.emplace_back(argv[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::vector<std::string> archiveFilename {};
|
std::vector<std::string> archiveFilename {};
|
||||||
for (auto i : cmdparam) {
|
for (auto i : cmdparam) {
|
||||||
if (i[0] != '-' && i.size() > 1) {
|
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);
|
archiveFilename.push_back(i);
|
||||||
}
|
}
|
||||||
if (i[0] == '-') {
|
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) {
|
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
|
//count the types of all items
|
||||||
std::map<std::string, uintmax_t> typecount{
|
std::map<std::string, uintmax_t> typecount{
|
||||||
{tarconstant::typeFile, 0},
|
{tarconstant::typeFile, 0},
|
||||||
@@ -82,7 +82,7 @@ int main(int argc, char** argv) {
|
|||||||
{tarconstant::typeOther, 0}
|
{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);
|
bool isGz = tar::validGzip(archiveName);
|
||||||
|
|
||||||
if (isGz) {
|
if (isGz) {
|
||||||
@@ -101,7 +101,8 @@ int main(int argc, char** argv) {
|
|||||||
if (unzippedbytes == 0)
|
if (unzippedbytes == 0)
|
||||||
break;
|
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)) {
|
if (tar::eof(headbuffer)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -109,11 +110,13 @@ int main(int argc, char** argv) {
|
|||||||
// Read type of item
|
// Read type of item
|
||||||
std::string itemtype = tar::getitemtype(headbuffer[tarconstant::itemtypeByte]);
|
std::string itemtype = tar::getitemtype(headbuffer[tarconstant::itemtypeByte]);
|
||||||
typecount[itemtype] += 1;
|
typecount[itemtype] += 1;
|
||||||
|
globaltypecount[itemtype] += 1;
|
||||||
|
|
||||||
// read itemsize and add to total
|
// 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)
|
uintmax_t help = (tar::getitemsize(headbuffer) / tarconstant::blocksize)
|
||||||
* tarconstant::blocksize + tarconstant::blocksize;
|
* tarconstant::blocksize + tarconstant::blocksize;
|
||||||
char dump[help] = {0};
|
char dump[help] = {0};
|
||||||
@@ -124,10 +127,12 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
gzclose(gzIn);
|
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) {
|
if (toFile) {
|
||||||
tar::txtfilestats(typecount, std::filesystem::file_size(archiveName),
|
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
|
// Read type of item
|
||||||
std::string itemtype = tar::getitemtype(headbuffer[tarconstant::itemtypeByte]);
|
std::string itemtype = tar::getitemtype(headbuffer[tarconstant::itemtypeByte]);
|
||||||
typecount[itemtype] += 1;
|
typecount[itemtype] += 1;
|
||||||
|
globaltypecount[itemtype] += 1;
|
||||||
|
|
||||||
// read itemsize and add to total
|
// 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 have no content blocks (0 byte)
|
||||||
if (tar::getitemsize(headbuffer) != 0) {
|
if (tar::getitemsize(headbuffer) != 0) {
|
||||||
@@ -168,16 +175,18 @@ int main(int argc, char** argv) {
|
|||||||
delete[] headbuffer;
|
delete[] headbuffer;
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
|
global_sizeofallfiles += std::filesystem::file_size(archiveName);
|
||||||
|
|
||||||
std::cout << toJSON << '\n';
|
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) {
|
if (toFile) {
|
||||||
tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), sizeof_allfiles,
|
tar::txtfilestats(typecount, std::filesystem::file_size(archiveName),
|
||||||
archiveName);
|
sizeof_allitems, archiveName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tar::consoleglobalstats(globaltypecount, global_sizeofallfiles, global_sizeofallitems, archiveFilename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
18
tarfunc.cpp
18
tarfunc.cpp
@@ -10,6 +10,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "zlib.h"
|
#include "zlib.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// checks if a valid modern tar file - ustar
|
// checks if a valid modern tar file - ustar
|
||||||
bool tar::validTar(std::istream &file) {
|
bool tar::validTar(std::istream &file) {
|
||||||
@@ -82,6 +83,19 @@ void tar::consolestats (std::map<std::string, uintmax_t> &typecount, uintmax_t t
|
|||||||
for (auto &i : typecount) {
|
for (auto &i : typecount) {
|
||||||
std::cout << i.first <<": " << i.second << '\n';
|
std::cout << i.first <<": " << i.second << '\n';
|
||||||
}
|
}
|
||||||
|
std::cout << '\n' << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
void tar::consoleglobalstats (std::map<std::string, uintmax_t> &typecount, uintmax_t filesize,
|
||||||
|
uintmax_t itemsize, std::vector<std::string> &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
|
// writes default console output to txt file
|
||||||
@@ -123,7 +137,7 @@ bool tar::fileOpen(std::string &filename) {
|
|||||||
file.close();
|
file.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "tarconst.h"
|
#include "tarconst.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
#ifndef TARSTATS___TARFUNC_H
|
#ifndef TARSTATS___TARFUNC_H
|
||||||
@@ -30,6 +31,10 @@ namespace tar {
|
|||||||
// to write stats to console in default style
|
// to write stats to console in default style
|
||||||
void consolestats(std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall);
|
void consolestats(std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall);
|
||||||
|
|
||||||
|
// to write global stats to stdout
|
||||||
|
void consoleglobalstats (std::map<std::string, uintmax_t> &typecount, uintmax_t itemfilesize,
|
||||||
|
uintmax_t szfiles, std::vector<std::string> &files);
|
||||||
|
|
||||||
// to write default console output to txt file
|
// to write default console output to txt file
|
||||||
void txtfilestats(std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
|
void txtfilestats(std::map<std::string, uintmax_t> &typecount, uintmax_t tarfilesize, uintmax_t sizeofall,
|
||||||
std::string archiveName);
|
std::string archiveName);
|
||||||
|
|||||||
Reference in New Issue
Block a user