mirror of
https://gitea.home.endeavr.de/Marcel/Tarstats-pp.git
synced 2025-12-10 00:39:01 +01:00
this added a check if the given file is a valid tar by checking the magic field for ustar
this also cleaned up some stringliterals.
This commit is contained in:
51
main.cpp
51
main.cpp
@@ -6,6 +6,24 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
// global itemtype definition
|
||||||
|
std::string const typeFile = "FILES";
|
||||||
|
std::string const typeDir = "DIRECTORIES";
|
||||||
|
std::string const typeSym = "SYMLINKS";
|
||||||
|
std::string const typeHard = "HARDLINKS";
|
||||||
|
std::string const typeOther = "OTHER";
|
||||||
|
|
||||||
|
// checks if a valid modern tar file - ustar
|
||||||
|
bool validTar (std::istream & file) {
|
||||||
|
file.seekg(257);
|
||||||
|
char* buffer = new char [6];
|
||||||
|
file.read(buffer, 6);
|
||||||
|
std::string magicfield(&buffer[0], 5);
|
||||||
|
delete[] buffer;
|
||||||
|
file.seekg(0);
|
||||||
|
return magicfield == "ustar" ? true: false;
|
||||||
|
}
|
||||||
|
|
||||||
// checks if a 512byte block consist only of 0 or \0
|
// checks if a 512byte block consist only of 0 or \0
|
||||||
bool eof (const char* buf) {
|
bool eof (const char* buf) {
|
||||||
std::string test(&buf[0]);
|
std::string test(&buf[0]);
|
||||||
@@ -16,13 +34,6 @@ bool eof (const char* buf) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// gets name of an item (for test purposes)
|
|
||||||
std::string getitemname (const char* buf) {
|
|
||||||
std::string name (&buf[0], 100);
|
|
||||||
std::erase(name, '\0');
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
// gets size of an item in bytes. assumes octal encoding.
|
// gets size of an item in bytes. assumes octal encoding.
|
||||||
uint64_t getitemsize(const char* buf){
|
uint64_t getitemsize(const char* buf){
|
||||||
std::string asciisize(&buf[124], 11); // at offset 124 because first byte marks octal or base256 coding
|
std::string asciisize(&buf[124], 11); // at offset 124 because first byte marks octal or base256 coding
|
||||||
@@ -41,15 +52,15 @@ std::string getitemtype(char &n) {
|
|||||||
// read itemtype
|
// read itemtype
|
||||||
switch (n){
|
switch (n){
|
||||||
case '0': case '\0':
|
case '0': case '\0':
|
||||||
return "FILE ";
|
return typeFile;
|
||||||
case '1':
|
case '1':
|
||||||
return "HARDLINK ";
|
return typeHard;
|
||||||
case '2':
|
case '2':
|
||||||
return "SYMLINK ";
|
return typeSym;
|
||||||
case '5':
|
case '5':
|
||||||
return "DIRECTORY ";
|
return typeDir;
|
||||||
default:
|
default:
|
||||||
return "OTHER ";
|
return typeOther;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +70,13 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
// GENERAL VARIABLES
|
// GENERAL VARIABLES
|
||||||
//count the types of all items
|
//count the types of all items
|
||||||
std::map <std::string, uint> typecount{
|
std::map<std::string, uint> typecount{
|
||||||
{"FILE ", 0}, {"HARDLINK ", 0}, {"SYMLINK ", 0},
|
{typeFile, 0}, {typeDir, 0}, {typeSym, 0},
|
||||||
{"DIRECTORY ", 0}, {"OTHER ", 0}
|
{typeHard, 0}, {typeOther, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
uint64_t sizeof_allfiles{}; // total size of all files in the archive
|
uint64_t sizeof_allfiles{}; // total size of all files in the archive
|
||||||
|
|
||||||
std::string helptext
|
std::string helptext
|
||||||
{"usage: tarstats-pp [-h] [-j] [-f] tarfile.\n"
|
{"usage: tarstats-pp [-h] [-j] [-f] tarfile.\n"
|
||||||
"A tool to calculate basics statistics on tarball. Shamelessly inspired by github.com/isotopp/tarstats!\n\n"
|
"A tool to calculate basics statistics on tarball. Shamelessly inspired by github.com/isotopp/tarstats!\n\n"
|
||||||
@@ -86,11 +98,16 @@ int main(int argc, char** argv) {
|
|||||||
//Open tar File.
|
//Open tar File.
|
||||||
std::ifstream file(archiveFilename, std::ios::binary);
|
std::ifstream file(archiveFilename, std::ios::binary);
|
||||||
if(!file) {
|
if(!file) {
|
||||||
std::cout << "Error opening file" << std::endl;
|
std::cout << "Error opening file!" << '\n' << '\n';
|
||||||
std::cout << helptext << '\n';
|
std::cout << helptext << '\n';
|
||||||
return 9;
|
return 9;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!validTar(file)) {
|
||||||
|
std::cout << archiveFilename << " is not a valid tar file for tarstats-pp" << '\n';
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
// Tar spec is working with continous 512 byte size blocks. Header is 512 bytes.
|
// Tar spec is working with continous 512 byte size blocks. Header is 512 bytes.
|
||||||
int buffersize = 512;
|
int buffersize = 512;
|
||||||
while (file) {
|
while (file) {
|
||||||
@@ -123,7 +140,7 @@ int main(int argc, char** argv) {
|
|||||||
file.close();
|
file.close();
|
||||||
std::cout << "Archive size: " << std::filesystem::file_size(archiveFilename) << " Bytes"<< '\n';
|
std::cout << "Archive size: " << std::filesystem::file_size(archiveFilename) << " Bytes"<< '\n';
|
||||||
std::cout << "Size of all items: " << sizeof_allfiles << " Bytes" << '\n' << '\n';
|
std::cout << "Size of all items: " << sizeof_allfiles << " Bytes" << '\n' << '\n';
|
||||||
for (auto i : typecount) {
|
for (auto &i : typecount) {
|
||||||
std::cout << i.first <<": " << i.second << '\n';
|
std::cout << i.first <<": " << i.second << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user