Conscience Core
untar.h
Go to the documentation of this file.
1 #pragma once
2 
18 #include "conscience_util.h"
19 #include <filesystem>
20 #include <fstream>
21 
22 namespace fs = std::filesystem;
23 
24 namespace conscience_utils::untar {
25 
26 // Type of file in the TAR documentation
29  FileType = '0',
30  HardlinkType = '1',
38 };
39 
40 // Limit entries to Files or Dir to limit the map size
41 enum tarMode {
42  File = 1,
43  Hardlink = 2,
44  Symlink = 4,
47  Dir = 32,
48  Fifo = 64,
49  Reserved = 128,
50  All = 255
51 };
52 
53 // The tarEntry class represents an ENTRY inside a TAR file. It can be a File,
54 // Dir, Symlink, ... Important parts to be able to use it :
55 // tarEntry::tarEntry(std::string filename, int filesize, std::size_t
56 // startOfFile, tarEntryType type, std::string parentTarFilename, std::ifstream
57 // * _tarfile); std::ifstream * tarEntry::wantToExtract(int * filesize,
58 // std::size_t * startInMemory); std::string getFilename(); std::size_t
59 // getStartingByte();
60 class tarEntry {
61  friend class tarFile;
62 
63 public:
64  // To be able to create null tarEntry
65  tarEntry() = default;
66  // Default constructor, prefer this one
67  tarEntry(std::string filename, int filesize, std::size_t startOfFile,
68  tarEntryType type, std::string parentTarFilename,
69  std::ifstream *_tarfile);
70  // Constructor to avoid error for people who failed the instantiation. Could
71  // disapear when I will have properly documented the library.
72  tarEntry(tarEntry const &cpy);
73  // Destructor
74  ~tarEntry() = default;
75 
76  // Get the tar filename where this file comes from
77  std::string getParentFilename();
78  // Get the file size
79  int getFileSize();
80  // Get the filename (containing the path)
81  std::string getFilename();
82  // Get the starting byte in the stream
83  std::size_t getStartingByte();
84  // Get the tar type. Is it a DirType, FileType, SymlinkType ?
86  // Get all usefull data we need to extract our data in one call
87  std::ifstream *wantToExtract(int *filesize, std::size_t *startInMemory);
88 
89 private:
90  // The stream, this is the same as the one of the tarFile object. Opened by
91  // default.
92  std::ifstream *_tarfile;
93  // If the file is extacted, set to true.
94  // For future usage, in my project in SFML
95  bool _extracted;
96  // Starting byte in the tar file (stream)
97  std::size_t _startOfFile;
98  // Filesize
99  int _filesize;
100  // Filename (containing the path)
101  std::string _filename;
102  // Type of the entry
103  tarEntryType _type;
104  // What is my dad TAR ?
105  std::string _parentTarFilename;
106 };
107 
108 // tarFile represents a TAR FILE opened. Important parts to be able to use it :
109 // tarFile::tarFile(char * filename, int filter = All);
110 // static map<std::string, tarEntry *> tarFile::entries
111 class tarFile {
112  friend class tarEntry;
113 
114 public:
115  // If you don't give a filename, don't forget to initiate via
116  // tarFile::open(...)
117  tarFile() = default;
118  // Default initiation
119  tarFile(const string &filePath, int filter = All);
120  // The destructor
121  ~tarFile();
122 
123  // Wrapping the map.find(std::string). Returns the tarEntry, or Null on error;
124  tarEntry *find(std::string filename);
125  // Wrapping the map.find(std::string). Returns the stream of file, filesize
126  // and start bit
127  std::ifstream *find(std::string filename, int *filesize, std::size_t *start);
128 
129  // Open a file in case you didn't instanciated the class with a filename
130  void open(const string &filePath, int filter = All);
131  // Get the filename of the opened file
132  std::string getFilename();
133 
134  std::map<std::string, tarEntry *> entries;
135 
136 private:
137  // Read the file and store entries
138  void getAllEntries(int filter = All);
139  // Add a new tarEntry into the map (entries)
140  void addEntryToMap(std::string filename, int filesize, tarEntryType type);
141  // Check if the header is Null. The tar file ends on two Null headers
142  bool isNullHeader(const char *p);
143  // Verify the checksum, actually, it stops the reading on error
144  static int verifyChecksum(const char *p);
145  // Used to read the header
146  static int parseoct(const char *p, std::size_t n);
147  // Did we get entries ? If yes, don't get them again
148  bool _get_all_entries;
149  // Remember where comes the data form
150  string _filename;
151 
152  std::ifstream _tarfile;
153 };
154 
155 void untarToDirectory(const fs::path &tarFilePath, const fs::path &targetDirectoryPath);
156 }
conscience_utils::untar::tarFile::find
tarEntry * find(std::string filename)
Definition: untar.cpp:53
conscience_utils::untar::tarEntry::getFilename
std::string getFilename()
Definition: untar.cpp:28
conscience_utils::untar::tarEntry::getType
tarEntryType getType()
Definition: untar.cpp:32
conscience_util.h
conscience_utils::untar::HardlinkType
@ HardlinkType
Definition: untar.h:30
conscience_utils::untar::FifoType
@ FifoType
Definition: untar.h:35
conscience_utils::untar::DirType
@ DirType
Definition: untar.h:34
conscience_utils::untar::tarEntry::getFileSize
int getFileSize()
Definition: untar.cpp:26
conscience_utils::untar::tarFile::open
void open(const string &filePath, int filter=All)
Definition: untar.cpp:73
conscience_utils::untar::BlockDevice
@ BlockDevice
Definition: untar.h:46
conscience_utils::untar::tarMode
tarMode
Definition: untar.h:41
conscience_utils::untar::FileCompatibilityType
@ FileCompatibilityType
Definition: untar.h:28
conscience_utils::untar::tarEntry::~tarEntry
~tarEntry()=default
conscience_utils::untar::Dir
@ Dir
Definition: untar.h:47
conscience_utils::untar::OtherType
@ OtherType
Definition: untar.h:37
conscience_utils::untar::tarEntry::tarEntry
tarEntry()=default
conscience_utils::untar::tarFile::tarFile
tarFile()=default
conscience_utils::untar::tarFile::getFilename
std::string getFilename()
Definition: untar.cpp:86
conscience_utils::untar::tarFile
Definition: untar.h:111
conscience_utils::untar::tarEntry::getParentFilename
std::string getParentFilename()
Definition: untar.cpp:24
conscience_utils::untar::Fifo
@ Fifo
Definition: untar.h:48
conscience_utils::untar::tarFile::~tarFile
~tarFile()
Definition: untar.cpp:46
conscience_utils::untar::tarEntry::wantToExtract
std::ifstream * wantToExtract(int *filesize, std::size_t *startInMemory)
Definition: untar.cpp:34
conscience_utils::untar::Symlink
@ Symlink
Definition: untar.h:44
conscience_utils::untar::All
@ All
Definition: untar.h:50
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:1794
conscience_utils::untar::Reserved
@ Reserved
Definition: untar.h:49
conscience_utils::untar::tarEntry::getStartingByte
std::size_t getStartingByte()
Definition: untar.cpp:30
conscience_utils::untar::tarEntryType
tarEntryType
Definition: untar.h:27
conscience_utils::untar::tarEntry
Definition: untar.h:60
conscience_utils::untar
Definition: untar.cpp:5
conscience_utils::untar::ReservedType
@ ReservedType
Definition: untar.h:36
conscience_utils::untar::FileType
@ FileType
Definition: untar.h:29
conscience_utils::untar::untarToDirectory
void untarToDirectory(const fs::path &tarFilePath, const fs::path &targetDirectoryPath)
Definition: untar.cpp:239
conscience_utils::untar::BlockDeviceType
@ BlockDeviceType
Definition: untar.h:33
conscience_utils::untar::File
@ File
Definition: untar.h:42
conscience_utils::untar::CharacterDevice
@ CharacterDevice
Definition: untar.h:45
conscience_utils::untar::CharacterDeviceType
@ CharacterDeviceType
Definition: untar.h:32
conscience_utils::untar::SymlinkType
@ SymlinkType
Definition: untar.h:31
conscience_utils::untar::tarFile::entries
std::map< std::string, tarEntry * > entries
Definition: untar.h:134
conscience_utils::untar::Hardlink
@ Hardlink
Definition: untar.h:43