Zcalanie ręczne. Jak git nie chce po dobroci to będzie siłą
This commit is contained in:
parent
690e5278c5
commit
3bf98ba472
22 changed files with 790 additions and 457 deletions
246
CreateCargo.cpp
246
CreateCargo.cpp
|
|
@ -20,12 +20,12 @@
|
|||
#include "CreateCargo.h"
|
||||
|
||||
CreateCargo::CreateCargo()
|
||||
:compressingFlag(false)
|
||||
, filteringFlag(false)
|
||||
, signature(SIGNATURE)
|
||||
: signature(SIGNATURE)
|
||||
, extension(EXTENSION)
|
||||
, version(VERSION)
|
||||
, methodFlags(0)
|
||||
, offset(0)
|
||||
, hppKey(false)
|
||||
{
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
|
@ -38,12 +38,11 @@ CreateCargo::~CreateCargo() {
|
|||
//-----------------------------------------------------------------------------
|
||||
// Punk wejścia do tworzenia archivum
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::Create(const std::string& path, bool compress, bool filters)
|
||||
bool CreateCargo::Create(const std::string& path, const int16_t& flag)
|
||||
{
|
||||
cargoFile = path + "." + extension;
|
||||
catalogPath = path;
|
||||
compressingFlag = compress;
|
||||
filteringFlag = filters;
|
||||
methodFlags = flag;
|
||||
|
||||
//Sprawdzanie pakowanego kontentu
|
||||
if (!std::filesystem::is_directory(path))
|
||||
|
|
@ -59,9 +58,9 @@ bool CreateCargo::Create(const std::string& path, bool compress, bool filters)
|
|||
}
|
||||
|
||||
// Pobieranie listy plików wyjątków
|
||||
if (filters)
|
||||
if (flag == -1)
|
||||
{
|
||||
std::string filterFile = path + ".txt";
|
||||
std::string filterFile = path + ".json";
|
||||
if (!std::filesystem::exists(filterFile))
|
||||
{
|
||||
std::cerr << "Error: Missing " << filterFile << " file!" << std::endl;
|
||||
|
|
@ -88,6 +87,12 @@ bool CreateCargo::Create(const std::string& path, bool compress, bool filters)
|
|||
return false;
|
||||
}
|
||||
|
||||
// Zapisywanie klucza szyfruj¹cego
|
||||
if (flag == 2 || flag == 3 || encList.size() > 0)
|
||||
{
|
||||
crypt.saveKey(catalogPath, hppKey);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -105,14 +110,35 @@ bool CreateCargo::GetFileList(const std::string& path)
|
|||
}
|
||||
else
|
||||
{
|
||||
if (CheckIgnorePath(tmpPath))
|
||||
std::string fileRef = RemoveStartPath(PathToUnixLike(tmpPath));
|
||||
PathConf pc;
|
||||
if (methodFlags > -1)
|
||||
{
|
||||
filesList.push_back(PathToUnixLike(tmpPath));
|
||||
pc.path = PathToUnixLike(tmpPath);
|
||||
pc.parameter = methodFlags;
|
||||
filesPaths.push_back(pc);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!FindOnTheList(ignoreList, fileRef) || !CheckFileExtension(fileRef, ignoreList))
|
||||
{
|
||||
if (FindOnTheList(zipList, fileRef) || CheckFileExtension(fileRef, zipList))
|
||||
{
|
||||
pc.parameter = FindOnTheList(encList, fileRef) || CheckFileExtension(fileRef, encList) ? 3 : 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
pc.parameter = FindOnTheList(encList, fileRef) || CheckFileExtension(fileRef, encList) ? 2 : 0;
|
||||
}
|
||||
pc.path = PathToUnixLike(tmpPath);
|
||||
std::cout << pc.path << " - " << pc.parameter << std::endl;
|
||||
filesPaths.push_back(pc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return filesList.size() > 0 ? true : false;
|
||||
return filesPaths.size() > 0 ? true : false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -160,35 +186,34 @@ CargoHead CreateCargo::CreateCargoHead(const uint32_t& filesLen, const uint64_t&
|
|||
//-----------------------------------------------------------------------------
|
||||
// Sprawdza czy plik znajduje się na liście
|
||||
//-----------------------------------------------------------------------------
|
||||
uint8_t CreateCargo::CheckFileOnTheList(const std::string& path, std::vector<char>& input, std::vector<char>& output)
|
||||
void CreateCargo::computingBytes(const int16_t& flag, std::vector<char>& input, std::vector<char>& output)
|
||||
{
|
||||
//Flaga aktywna sprawdza czy plik jest na liście. Jeśli jest to zwraca surowedane
|
||||
//Przeciwnie kompresuje dane
|
||||
CompressingManager cm;
|
||||
ChunkManager cm(crypt);
|
||||
|
||||
if (filteringFlag) {
|
||||
if (FilteringData(path))
|
||||
{
|
||||
output = cm.compress(input);
|
||||
return ZIP_FILE;
|
||||
}
|
||||
else
|
||||
{
|
||||
output = std::move(input);
|
||||
return RAW_FILE;
|
||||
}
|
||||
}
|
||||
|
||||
//Flaga aktywna kompresuje dane
|
||||
if (compressingFlag)
|
||||
switch (flag)
|
||||
{
|
||||
case 1:
|
||||
output = cm.chunked(input, true, false);
|
||||
break;
|
||||
|
||||
output = cm.compress(input);
|
||||
return ZIP_FILE;
|
||||
case 2:
|
||||
output = cm.chunked(input, false, true);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
output = cm.chunked(input, true, true);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
output = cm.chunked(input, false, false);
|
||||
break;
|
||||
|
||||
default:
|
||||
output = std::move(input);
|
||||
break;
|
||||
}
|
||||
|
||||
output = std::move(input);
|
||||
return RAW_FILE;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -200,7 +225,7 @@ std::vector<FilesTable> CreateCargo::ComputingHeadFiles()
|
|||
CargoHead cargoHead = CreateCargoHead(0, 0);
|
||||
offset += cargoHead.signature.length() + sizeof(cargoHead.version) + sizeof(cargoHead.files) + sizeof(cargoHead.table);
|
||||
|
||||
//Zapisanie TMP nag³owka do pliku
|
||||
//Zapisanie tymczasowego nag³owka jako rezerwacja miejsca
|
||||
cargo.write(cargoHead.signature.data(), cargoHead.signature.length());
|
||||
cargo.write(reinterpret_cast<const char*>(&cargoHead.version), sizeof(cargoHead.version));
|
||||
cargo.write(reinterpret_cast<const char*>(&cargoHead.files), sizeof(cargoHead.files));
|
||||
|
|
@ -208,41 +233,41 @@ std::vector<FilesTable> CreateCargo::ComputingHeadFiles()
|
|||
|
||||
std::vector<FilesTable> filesTable;
|
||||
|
||||
//Tworzenie nag³ówków plików
|
||||
for (const auto& file : filesList)
|
||||
//Tworzenie nag³ówków plików jednoczeœnie zapisywanie plików
|
||||
for (const auto& file : filesPaths)
|
||||
{
|
||||
std::string path = PathToUnixLike(RemoveStartPath(file));
|
||||
std::ifstream f(file, std::ios::binary | std::ios::ate);
|
||||
std::string path = PathToUnixLike(RemoveStartPath(file.path));
|
||||
std::ifstream f(file.path, std::ios::binary | std::ios::ate);
|
||||
|
||||
//Obliczanie rozmiaru pliku
|
||||
size_t size = f.tellg();
|
||||
f.seekg(0, std::ios::beg);
|
||||
|
||||
//Wczytanie pliku do pamięci
|
||||
std::vector<char> buffor(size);
|
||||
f.read(buffor.data(), size);
|
||||
std::vector<char> buffer(size);
|
||||
f.read(buffer.data(), size);
|
||||
f.close();
|
||||
|
||||
//Tworzenie hashu CRC
|
||||
uint64_t crc = XXH64(buffor.data(), buffor.size(), VERSION);
|
||||
const uint64_t crc = XXH64(buffer.data(), buffer.size(), VERSION);
|
||||
|
||||
//Kompresjia
|
||||
std::vector<char> zip;
|
||||
uint8_t method = CheckFileOnTheList(path, buffor, zip);
|
||||
std::vector<char> pakBuffer;
|
||||
computingBytes(file.parameter, buffer, pakBuffer);
|
||||
|
||||
FilesTable ft;
|
||||
ft.nameFile = path;
|
||||
ft.nameLen = path.length();
|
||||
ft.hashName = fnv64(path);
|
||||
ft.offset = offset;
|
||||
ft.size = zip.size();
|
||||
ft.isZip = method;
|
||||
ft.size = pakBuffer.size();
|
||||
ft.flag = file.parameter;
|
||||
ft.crc = crc;
|
||||
|
||||
cargo.write(reinterpret_cast<const char*>(zip.data()), zip.size());
|
||||
cargo.write(reinterpret_cast<const char*>(pakBuffer.data()), pakBuffer.size());
|
||||
|
||||
filesTable.push_back(ft);
|
||||
offset += zip.size();
|
||||
offset += pakBuffer.size();
|
||||
}
|
||||
return filesTable;
|
||||
}
|
||||
|
|
@ -254,18 +279,32 @@ void CreateCargo::GetFilters(const std::string& filterFile)
|
|||
{
|
||||
std::cout << "Downloading the exception list" << std::endl;
|
||||
|
||||
Txtpp ff(filterFile);
|
||||
std::ifstream file(filterFile);
|
||||
nlohmann::json jslist;
|
||||
file >> jslist;
|
||||
file.close();
|
||||
|
||||
// Lista plików do skompresowania
|
||||
zipList = ff.Get(KEY_ZIP);
|
||||
if (jslist.contains(KEY_ZIP))
|
||||
{
|
||||
zipList = jslist[KEY_ZIP].get<std::vector<std::string>>();
|
||||
}
|
||||
|
||||
// Lista plików do zaszyfrowania
|
||||
if (jslist.contains(KEY_ENCRYPT))
|
||||
{
|
||||
encList = jslist[KEY_ENCRYPT].get<std::vector<std::string>>();
|
||||
}
|
||||
|
||||
// Lista plików do pominięcia
|
||||
ignoreList = ff.Get(KEY_IGNORE);
|
||||
if (jslist.contains(KEY_IGNORE))
|
||||
{
|
||||
ignoreList = jslist[KEY_IGNORE].get<std::vector<std::string>>();
|
||||
}
|
||||
|
||||
ff.Close();
|
||||
hppKey = jslist.value("keyhpp", false);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Znajdź wskazany element na liście
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -275,33 +314,23 @@ bool CreateCargo::FindOnTheList(const std::vector<std::string>& list, const std:
|
|||
return it == list.end() ? false : true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Rozdzielanie paternu od œcie¿ki
|
||||
//-----------------------------------------------------------------------------
|
||||
void CreateCargo::ExtPatternAndPathDetection(const std::vector<std::string>& data, std::vector<std::string>& pattern, std::vector<std::string>& path)
|
||||
{
|
||||
for (const auto& d : data)
|
||||
{
|
||||
if (d.front() == '*')
|
||||
{
|
||||
std::string tmpPattern = d;
|
||||
tmpPattern.erase(tmpPattern.begin());
|
||||
pattern.push_back(UpperString(tmpPattern));
|
||||
}
|
||||
else
|
||||
{
|
||||
path.push_back(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Sprawdzanie rozszeżeń plików
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::CheckFileExtension(const std::filesystem::path& p, const std::vector<std::string>& patterns) {
|
||||
std::string ext = UpperString(p.extension().string());
|
||||
bool CreateCargo::CheckFileExtension(const std::string& p, const std::vector<std::string>& patterns) {
|
||||
std::filesystem::path _p = p;
|
||||
std::string ext = "*" + UpperString(_p.extension().string());
|
||||
|
||||
return FindOnTheList(patterns, ext);
|
||||
for (const auto& e : patterns)
|
||||
{
|
||||
std::string element = UpperString(e);
|
||||
if (element == ext)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -331,69 +360,6 @@ uint64_t CreateCargo::fnv64(const std::string& data)
|
|||
return hash;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Sprawdzanie czy plik znajduje siê na liœcie
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::FilteringData(const std::string& path)
|
||||
{
|
||||
std::vector<std::string> cmPatterns;
|
||||
std::vector<std::string> cmPaths;
|
||||
|
||||
// Rozdziel œcie¿ki i patterny na osobne listy
|
||||
ExtPatternAndPathDetection(zipList, cmPatterns, cmPaths);
|
||||
|
||||
if (FindOnTheList(cmPatterns, ALL_FILE))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Sprawd¿ czy istnieje plik o danym rozsze¿eniu
|
||||
if (CheckFileExtension(path, cmPatterns))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// SprawdŸ czy instnieje dany plik w danej lokalizacji
|
||||
if (FindOnTheList(cmPaths, path))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Kasowanie z listy plików ignorow
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::CheckIgnorePath(const std::string& path)
|
||||
{
|
||||
std::vector<std::string> igPatterns;
|
||||
std::vector<std::string> igPaths;
|
||||
|
||||
ExtPatternAndPathDetection(ignoreList, igPatterns, igPaths);
|
||||
|
||||
// Sprawd¿ czy istnieje plik o danym rozsze¿eniu
|
||||
if (CheckFileExtension(path, igPatterns))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Obrubka œcierzki
|
||||
// Usuwanie katalogu root
|
||||
std::string cleanPath = RemoveStartPath(path);
|
||||
|
||||
// Przekszta³cenie œcierzki na format unixowy
|
||||
std::string unixPath = PathToUnixLike(cleanPath);
|
||||
|
||||
// SprawdŸ czy instnieje dany plik w danej lokalizacji
|
||||
if (FindOnTheList(igPaths, unixPath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Trworzenie archiwum
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
@ -401,7 +367,7 @@ bool CreateCargo::WriteCargo()
|
|||
{
|
||||
std::cout << "Packing files..." << std::endl;
|
||||
|
||||
uint32_t filesLen = filesList.size();
|
||||
uint32_t filesLen = filesPaths.size();
|
||||
|
||||
//Przygotowanie nagłówków plików i przetworzenie danych
|
||||
std::vector<FilesTable> filesHead = ComputingHeadFiles();
|
||||
|
|
@ -424,7 +390,7 @@ bool CreateCargo::WriteCargo()
|
|||
cargo.write(reinterpret_cast<const char*>(&head.offset), sizeof(head.offset));
|
||||
cargo.write(reinterpret_cast<const char*>(&head.size), sizeof(head.size));
|
||||
cargo.write(reinterpret_cast<const char*>(&head.crc), sizeof(head.crc));
|
||||
cargo.write(reinterpret_cast<const char*>(&head.isZip), sizeof(head.isZip));
|
||||
cargo.write(reinterpret_cast<const char*>(&head.flag), sizeof(head.flag));
|
||||
}
|
||||
|
||||
//Cofnij się na początek pliku
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue