Dodaj pliki projektów.
This commit is contained in:
parent
4912ed6774
commit
f6150b0d17
18 changed files with 9210 additions and 0 deletions
439
CreateCargo.cpp
Normal file
439
CreateCargo.cpp
Normal file
|
|
@ -0,0 +1,439 @@
|
|||
/*
|
||||
* This file is part of VoidArchiveTool.
|
||||
*
|
||||
* Copyright (C) 2025 Yanczi
|
||||
*
|
||||
* Void Archive Toolis free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CreateCargo.h"
|
||||
|
||||
CreateCargo::CreateCargo()
|
||||
:compressingFlag(false)
|
||||
, filteringFlag(false)
|
||||
, signature(SIGNATURE)
|
||||
, extension(EXTENSION)
|
||||
, version(VERSION)
|
||||
, offset(0)
|
||||
{
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
CreateCargo::~CreateCargo() {
|
||||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Punk wejœcia do tworzenia archivum
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::Create(const std::string& path, bool compress, bool filters)
|
||||
{
|
||||
cargoFile = path + "." + extension;
|
||||
catalogPath = path;
|
||||
compressingFlag = compress;
|
||||
filteringFlag = filters;
|
||||
|
||||
//Sprawdzanie pakowanego kontentu
|
||||
if (!std::filesystem::is_directory(path))
|
||||
{
|
||||
std::cerr << "Error: The specified directory is a file!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!std::filesystem::exists(path))
|
||||
{
|
||||
std::cerr << "Error: The specified directory does not exist!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Pobieranie listy plików wyj¹tków
|
||||
if (filters)
|
||||
{
|
||||
std::string filterFile = path + ".txt";
|
||||
if (!std::filesystem::exists(filterFile))
|
||||
{
|
||||
std::cerr << "Error: Missing " << filterFile << " file!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
GetFilters(filterFile);
|
||||
}
|
||||
|
||||
//Pobieranie listy plików do spakowania
|
||||
std::cout << "Creating a file list..." << std::endl;
|
||||
if (!GetFileList(path))
|
||||
{
|
||||
std::cerr << "Error: The specified directory contains no files!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Utworzenie kontenera
|
||||
cargo.open(cargoFile, std::ios::binary);
|
||||
|
||||
//Rozpocznij process zapisywania danych do kontenera
|
||||
if (!WriteCargo())
|
||||
{
|
||||
std::cerr << "Error: Failed to create container!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Tworzenie listy plików do spakowania
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::GetFileList(const std::string& path)
|
||||
{
|
||||
for (const auto& entry : std::filesystem::directory_iterator(path))
|
||||
{
|
||||
std::string tmpPath = entry.path().string();
|
||||
if (std::filesystem::is_directory(tmpPath))
|
||||
{
|
||||
GetFileList(tmpPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (CheckIgnorePath(tmpPath))
|
||||
{
|
||||
filesList.push_back(PathToUnixLike(tmpPath));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return filesList.size() > 0 ? true : false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Konwersja œcie¿ki na unix like
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string CreateCargo::PathToUnixLike(std::string path)
|
||||
{
|
||||
std::replace(path.begin(), path.end(), '\\', '/');
|
||||
return path;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Kasowanie wskazanego katalogu ze œcie¿ki
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string CreateCargo::RemoveStartPath(const std::filesystem::path& path)
|
||||
{
|
||||
std::filesystem::path cleanPath;
|
||||
|
||||
auto it = path.begin();
|
||||
if (it != path.end()) { ++it; }
|
||||
|
||||
for (; it != path.end(); ++it)
|
||||
{
|
||||
cleanPath /= *it;
|
||||
}
|
||||
|
||||
return cleanPath.string();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Tworzenie nag³owka pliku
|
||||
//-----------------------------------------------------------------------------
|
||||
CargoHead CreateCargo::CreateCargoHead(const uint32_t& filesLen, const uint64_t& table)
|
||||
{
|
||||
CargoHead ch;
|
||||
|
||||
ch.signature = signature;
|
||||
ch.version = version;
|
||||
ch.files = filesLen;
|
||||
ch.table = table;
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Kompresowanie
|
||||
//-----------------------------------------------------------------------------
|
||||
std::vector<char> CreateCargo::Compressing(const std::vector<char>& raw)
|
||||
{
|
||||
int maxZipSize = LZ4_compressBound(raw.size());
|
||||
std::vector<char> zip(maxZipSize);
|
||||
int zipSize = LZ4_compress_default(raw.data(), zip.data(), raw.size(), maxZipSize);
|
||||
zip.resize(zipSize);
|
||||
|
||||
return zip;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Sprawdza czy plik znajduje siê na liœcie
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::CheckFileOnTheList(const std::string& path, 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
|
||||
if (filteringFlag) {
|
||||
if (FilteringData(path))
|
||||
{
|
||||
output = Compressing(input);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
output = std::move(input);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Flaga aktywna kompresuje dane
|
||||
if (compressingFlag)
|
||||
{
|
||||
output = Compressing(input);
|
||||
return true;
|
||||
}
|
||||
|
||||
output = std::move(input);
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Przygotowanie nag³ówków i plików
|
||||
//-----------------------------------------------------------------------------
|
||||
std::vector<FilesTable> CreateCargo::ComputingHeadFiles()
|
||||
{
|
||||
//Utwórz header TMP. Zabezpiecza Pierwsze bajty na w³aœciwy nag³ówek
|
||||
CargoHead cargoHead = CreateCargoHead(0, 0);
|
||||
offset += cargoHead.signature.length() + sizeof(cargoHead.version) + sizeof(cargoHead.files) + sizeof(cargoHead.table);
|
||||
|
||||
//Zapisanie TMP nag³owka do pliku
|
||||
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));
|
||||
cargo.write(reinterpret_cast<const char*>(&cargoHead.table), sizeof(cargoHead.table));
|
||||
|
||||
std::vector<FilesTable> filesTable;
|
||||
|
||||
//Tworzenie nag³ówków plików
|
||||
for (const auto& file : filesList)
|
||||
{
|
||||
std::string path = PathToUnixLike(RemoveStartPath(file));
|
||||
std::ifstream f(file, 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);
|
||||
f.close();
|
||||
|
||||
//Tworzenie hashu CRC
|
||||
uint64_t crc = XXH64(buffor.data(), buffor.size(), VERSION);
|
||||
|
||||
//Kompresjia
|
||||
std::vector<char> zip;
|
||||
bool isZip = CheckFileOnTheList(path, buffor, zip);
|
||||
|
||||
FilesTable ft;
|
||||
ft.nameFile = path;
|
||||
ft.nameLen = path.length();
|
||||
ft.offset = offset;
|
||||
ft.size = zip.size();
|
||||
ft.rawSize = size;
|
||||
ft.isZip = isZip;
|
||||
ft.crc = crc;
|
||||
|
||||
cargo.write(reinterpret_cast<const char*>(zip.data()), zip.size());
|
||||
|
||||
filesTable.push_back(ft);
|
||||
offset += zip.size();
|
||||
}
|
||||
return filesTable;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Wczytanie filtrów wyj¹tków
|
||||
//-----------------------------------------------------------------------------
|
||||
void CreateCargo::GetFilters(const std::string& filterFile)
|
||||
{
|
||||
std::cout << "Downloading the exception list" << std::endl;
|
||||
|
||||
Txtpp ff(filterFile);
|
||||
|
||||
// Lista plików do skompresowania
|
||||
zipList = ff.Get(KEY_ZIP);
|
||||
|
||||
// Lista plików do pominiêcia
|
||||
ignoreList = ff.Get(KEY_IGNORE);
|
||||
|
||||
ff.Close();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// ZnajdŸ wskazany element na liœcie
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::FindOnTheList(const std::vector<std::string>& list, const std::string& element)
|
||||
{
|
||||
auto it = std::find(list.begin(), list.end(), element);
|
||||
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());
|
||||
|
||||
return FindOnTheList(patterns, ext);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Zamieñ ca³y ci¹g na du¿e litery
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string CreateCargo::UpperString(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
|
||||
return s;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// 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
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CreateCargo::WriteCargo()
|
||||
{
|
||||
std::cout << "Packing files..." << std::endl;
|
||||
|
||||
uint32_t filesLen = filesList.size();
|
||||
|
||||
//Przygotowanie nag³ówków plików i przetworzenie danych
|
||||
std::vector<FilesTable> filesHead = ComputingHeadFiles();
|
||||
|
||||
//std::ofstream cargo(cargoFile, std::ios::binary);
|
||||
|
||||
if (!cargo.is_open())
|
||||
{
|
||||
std::cerr << "ERROR: Failed to create container " << cargoFile << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& head : filesHead)
|
||||
{
|
||||
// Œcie¿ka do plików
|
||||
cargo.write(reinterpret_cast<const char*>(&head.nameLen), sizeof(head.nameLen));
|
||||
cargo.write(head.nameFile.data(), head.nameLen);
|
||||
|
||||
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.rawSize), sizeof(head.rawSize));
|
||||
cargo.write(reinterpret_cast<const char*>(&head.crc), sizeof(head.crc));
|
||||
cargo.write(reinterpret_cast<const char*>(&head.isZip), sizeof(head.isZip));
|
||||
}
|
||||
|
||||
//Cofnij siê na pocz¹tek pliku
|
||||
cargo.seekp(std::ios::beg);
|
||||
|
||||
//Utwórz header
|
||||
CargoHead cargoHead = CreateCargoHead(filesLen, offset);
|
||||
|
||||
//Nadpisz tymczasowy nag³ówek
|
||||
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));
|
||||
cargo.write(reinterpret_cast<const char*>(&cargoHead.table), sizeof(cargoHead.table));
|
||||
|
||||
cargo.close();
|
||||
|
||||
std::cout << "The container was successfully created! " << cargoFile << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue