Prototyping:

- Opening a GNU Tar file via comand line argument
- Parse itemname, itemtype and itemsize in bytes of first item in Tar archive
- Print those to stdout.
This commit is contained in:
Marcel Nowicki
2022-02-06 13:27:36 +01:00
parent 529497fe54
commit 3cedfc5b27
6 changed files with 84 additions and 17 deletions

View File

@@ -1,26 +1,75 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
int main() {
std::ifstream datei("test.tar", std::ios::binary);
if(!datei)
std::cout << "Fehler beim Oeffnen der Datei" << std::endl;
int headersize = 512;
char* headbuffer = new char[headersize];
datei.read(headbuffer, headersize);
std::string filename{};
for (int i = 0; i <= 99; i++)
{
if (headbuffer[i] != '\0')
filename.push_back(headbuffer[i]);
int main(int argc, char** argv) {
// Trivial check for arguments. Errorprone and has to be changed.
if (argc < 2) {
std::cout << "Please enter filename" << std::endl;
return 9;
}
std::cout << filename << std::endl;
// Getting name from argument lists on startup. Trivial and errorprone. Placeholder for now.
std::string archiveFilename(argv[1]);
std::string filename2(&headbuffer[0], 100);
std::cout << filename2 << std::endl;
//Open tar File.
std::ifstream datei(archiveFilename, std::ios::binary);
if(!datei)
std::cout << "Error opening file" << std::endl;
// Tar spec is working with continous 512 byte size blocks. Header is 512 bytes.
int headersize = 512;
//Read header of first item in tar archive
char* headbuffer = new char[headersize];
datei.read(headbuffer, headersize);
// Read name of the next item
std::string itemname(&headbuffer[0], 100);
std::erase(itemname, '\0');
// Read type of item
std::string itemtype{};
switch (headbuffer[156]){
case '0': case '\0':
itemtype = "FILE";
break;
case '1':
itemtype = "HARDLINK";
break;
case '2':
itemtype = "SYMLINK";
break;
case '5':
itemtype = "DIRECTORY";
break;
default:
itemtype = "OTHER";
break;
}
// Read size of the next file
long double filesize{};
int power = 10;
std::string asciifilesize(&headbuffer[124], 11);
for (auto i : asciifilesize) {
int value = i - '0';
filesize += value * std::pow(8,power);
power--;
}
//Printing to stdout
std::cout << itemname << std::endl;
std::cout << itemtype << std::endl;
std::cout << filesize << " Bytes" << std::endl;
return 0;
}