1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCPackArchiveGenerator.h"
#include <map>
#include <ostream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "cmCPackComponentGroup.h"
#include "cmCPackGenerator.h"
#include "cmCPackLog.h"
#include "cmGeneratedFileStream.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmValue.h"
#include "cmWorkingDirectory.h"
enum class DeduplicateStatus
{
Skip,
Add,
Error
};
/**
* @class cmCPackArchiveGenerator::Deduplicator
* @brief A utility class for deduplicating files, folders, and symlinks.
*
* This class is responsible for identifying duplicate files, folders, and
* symlinks when generating an archive. It keeps track of the paths that have
* been processed and helps in deciding whether a new path should be added,
* skipped, or flagged as an error.
*/
class cmCPackArchiveGenerator::Deduplicator
{
private:
/**
* @brief Compares a file with already processed files.
*
* @param path The path of the file to compare.
* @param localTopLevel The top-level directory for the file.
* @return DeduplicateStatus indicating whether to add, skip, or flag an
* error for the file.
*/
DeduplicateStatus CompareFile(std::string const& path,
std::string const& localTopLevel)
{
auto fileItr = this->Files.find(path);
if (fileItr != this->Files.end()) {
return cmSystemTools::FilesDiffer(path, fileItr->second)
? DeduplicateStatus::Error
: DeduplicateStatus::Skip;
}
this->Files[path] = cmStrCat(localTopLevel, '/', path);
return DeduplicateStatus::Add;
}
/**
* @brief Compares a folder with already processed folders.
*
* @param path The path of the folder to compare.
* @return DeduplicateStatus indicating whether to add or skip the folder.
*/
DeduplicateStatus CompareFolder(std::string const& path)
{
if (this->Folders.find(path) != this->Folders.end()) {
return DeduplicateStatus::Skip;
}
this->Folders.emplace(path);
return DeduplicateStatus::Add;
}
/**
* @brief Compares a symlink with already processed symlinks.
*
* @param path The path of the symlink to compare.
* @return DeduplicateStatus indicating whether to add, skip, or flag an
* error for the symlink.
*/
DeduplicateStatus CompareSymlink(std::string const& path)
{
auto symlinkItr = this->Symlink.find(path);
std::string symlinkValue;
auto status = cmSystemTools::ReadSymlink(path, symlinkValue);
if (!status.IsSuccess()) {
return DeduplicateStatus::Error;
}
if (symlinkItr != this->Symlink.end()) {
return symlinkValue == symlinkItr->second ? DeduplicateStatus::Skip
: DeduplicateStatus::Error;
}
this->Symlink[path] = symlinkValue;
return DeduplicateStatus::Add;
}
public:
/**
* @brief Determines the deduplication status of a given path.
*
* This method identifies whether the given path is a file, folder, or
* symlink and then delegates to the appropriate comparison method.
*
* @param path The path to check for deduplication.
* @param localTopLevel The top-level directory for the path.
* @return DeduplicateStatus indicating the action to take for the given
* path.
*/
DeduplicateStatus IsDeduplicate(std::string const& path,
std::string const& localTopLevel)
{
DeduplicateStatus status;
if (cmSystemTools::FileIsDirectory(path)) {
status = this->CompareFolder(path);
} else if (cmSystemTools::FileIsSymlink(path)) {
status = this->CompareSymlink(path);
} else {
status = this->CompareFile(path, localTopLevel);
}
return status;
}
private:
std::unordered_map<std::string, std::string> Symlink;
std::unordered_set<std::string> Folders;
std::unordered_map<std::string, std::string> Files;
};
cmCPackGenerator* cmCPackArchiveGenerator::Create7ZGenerator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressNone, "7zip",
".7z");
}
cmCPackGenerator* cmCPackArchiveGenerator::CreateTBZ2Generator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressBZip2, "paxr",
".tar.bz2");
}
cmCPackGenerator* cmCPackArchiveGenerator::CreateTGZGenerator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressGZip, "paxr",
".tar.gz");
}
cmCPackGenerator* cmCPackArchiveGenerator::CreateTXZGenerator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressXZ, "paxr",
".tar.xz");
}
cmCPackGenerator* cmCPackArchiveGenerator::CreateTZGenerator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressCompress, "paxr",
".tar.Z");
}
cmCPackGenerator* cmCPackArchiveGenerator::CreateTZSTGenerator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressZstd, "paxr",
".tar.zst");
}
cmCPackGenerator* cmCPackArchiveGenerator::CreateTarGenerator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressNone, "gnutar",
".tar");
}
cmCPackGenerator* cmCPackArchiveGenerator::CreateZIPGenerator()
{
return new cmCPackArchiveGenerator(cmArchiveWrite::CompressNone, "zip",
".zip");
}
cmCPackArchiveGenerator::cmCPackArchiveGenerator(
cmArchiveWrite::Compress compress, std::string format, std::string extension)
: Compress(compress)
, ArchiveFormat(std::move(format))
, OutputExtension(std::move(extension))
{
}
cmCPackArchiveGenerator::~cmCPackArchiveGenerator() = default;
std::string cmCPackArchiveGenerator::GetArchiveFileName()
{
std::string packageFileName = this->toplevel + "/";
if (cmValue v = this->GetOptionIfSet("CPACK_ARCHIVE_FILE_NAME")) {
packageFileName += *v;
} else {
v = this->GetOption("CPACK_PACKAGE_FILE_NAME");
packageFileName += *v;
}
packageFileName += this->GetOutputExtension();
return packageFileName;
}
std::string cmCPackArchiveGenerator::GetArchiveComponentFileName(
std::string const& component, bool isGroupName)
{
std::string componentUpper(cmSystemTools::UpperCase(component));
std::string packageFileName;
if (cmValue v = this->GetOptionIfSet("CPACK_ARCHIVE_" + componentUpper +
"_FILE_NAME")) {
packageFileName += *v;
} else if ((v = this->GetOptionIfSet("CPACK_ARCHIVE_FILE_NAME"))) {
packageFileName +=
this->GetComponentPackageFileName(*v, component, isGroupName);
} else {
v = this->GetOption("CPACK_PACKAGE_FILE_NAME");
packageFileName +=
this->GetComponentPackageFileName(*v, component, isGroupName);
}
packageFileName += this->GetOutputExtension();
return packageFileName;
}
int cmCPackArchiveGenerator::InitializeInternal()
{
this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
cmValue newExtensionValue = this->GetOption("CPACK_ARCHIVE_FILE_EXTENSION");
if (!newExtensionValue.IsEmpty()) {
std::string newExtension = *newExtensionValue;
if (!cmHasLiteralPrefix(newExtension, ".")) {
newExtension = cmStrCat('.', newExtension);
}
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Using user-provided file extension "
<< newExtension << " instead of the default "
<< this->OutputExtension << std::endl);
this->OutputExtension = std::move(newExtension);
}
return this->Superclass::InitializeInternal();
}
int cmCPackArchiveGenerator::addOneComponentToArchive(
cmArchiveWrite& archive, cmCPackComponent* component,
Deduplicator* deduplicator)
{
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
" - packaging component: " << component->Name << std::endl);
// Add the files of this component to the archive
std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
localToplevel += "/" + this->GetSanitizedDirOrFileName(component->Name);
// Change to local toplevel
cmWorkingDirectory workdir(localToplevel);
if (workdir.Failed()) {
cmCPackLogger(cmCPackLog::LOG_ERROR, workdir.GetError() << std::endl);
return 0;
}
std::string filePrefix;
if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
filePrefix = cmStrCat(this->GetOption("CPACK_PACKAGE_FILE_NAME"), '/');
}
cmValue installPrefix = this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
if (installPrefix && installPrefix->size() > 1 &&
(*installPrefix)[0] == '/') {
// add to file prefix and remove the leading '/'
filePrefix += installPrefix->substr(1);
filePrefix += "/";
}
for (std::string const& file : component->Files) {
std::string rp = filePrefix + file;
DeduplicateStatus status = DeduplicateStatus::Add;
if (deduplicator) {
status = deduplicator->IsDeduplicate(rp, localToplevel);
}
if (!deduplicator || status == DeduplicateStatus::Add) {
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file: " << rp << std::endl);
archive.Add(rp, 0, nullptr, false);
} else if (status == DeduplicateStatus::Error) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"ERROR The data in files with the "
"same filename is different.");
return 0;
} else {
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Passing file: " << rp << std::endl);
}
if (!archive) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"ERROR while packaging files: " << archive.GetError()
<< std::endl);
return 0;
}
}
return 1;
}
/*
* The macro will open/create a file 'filename'
* an declare and open the associated
* cmArchiveWrite 'archive' object.
*/
#define DECLARE_AND_OPEN_ARCHIVE(filename, archive) \
cmGeneratedFileStream gf; \
gf.Open((filename), false, true); \
if (!GenerateHeader(&gf)) { \
cmCPackLogger(cmCPackLog::LOG_ERROR, \
"Problem to generate Header for archive <" \
<< (filename) << ">." << std::endl); \
return 0; \
} \
cmArchiveWrite archive(gf, this->Compress, this->ArchiveFormat, 0, \
this->GetThreadCount()); \
do { \
if (!archive.Open()) { \
cmCPackLogger(cmCPackLog::LOG_ERROR, \
"Problem to open archive <" \
<< (filename) << ">, ERROR = " << (archive).GetError() \
<< std::endl); \
return 0; \
} \
if (!(archive)) { \
cmCPackLogger(cmCPackLog::LOG_ERROR, \
"Problem to create archive <" \
<< (filename) << ">, ERROR = " << (archive).GetError() \
<< std::endl); \
return 0; \
} \
} while (false)
int cmCPackArchiveGenerator::PackageComponents(bool ignoreGroup)
{
this->packageFileNames.clear();
// The default behavior is to have one package by component group
// unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
if (!ignoreGroup) {
for (auto const& compG : this->ComponentGroups) {
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Packaging component group: " << compG.first << std::endl);
// Begin the archive for this group
std::string packageFileName = std::string(this->toplevel) + "/" +
this->GetArchiveComponentFileName(compG.first, true);
Deduplicator deduplicator;
// open a block in order to automatically close archive
// at the end of the block
{
DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
// now iterate over the component of this group
for (cmCPackComponent* comp : (compG.second).Components) {
// Add the files of this component to the archive
this->addOneComponentToArchive(archive, comp, &deduplicator);
}
}
// add the generated package to package file names list
this->packageFileNames.push_back(std::move(packageFileName));
}
// Handle Orphan components (components not belonging to any groups)
for (auto& comp : this->Components) {
// Does the component belong to a group?
if (!comp.second.Group) {
cmCPackLogger(
cmCPackLog::LOG_VERBOSE,
"Component <"
<< comp.second.Name
<< "> does not belong to any group, package it separately."
<< std::endl);
std::string packageFileName = std::string(this->toplevel);
packageFileName +=
"/" + this->GetArchiveComponentFileName(comp.first, false);
{
DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
// Add the files of this component to the archive
this->addOneComponentToArchive(archive, &(comp.second), nullptr);
}
// add the generated package to package file names list
this->packageFileNames.push_back(std::move(packageFileName));
}
}
}
// CPACK_COMPONENTS_IGNORE_GROUPS is set
// We build 1 package per component
else {
for (auto& comp : this->Components) {
std::string packageFileName = std::string(this->toplevel);
packageFileName +=
"/" + this->GetArchiveComponentFileName(comp.first, false);
{
DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
// Add the files of this component to the archive
this->addOneComponentToArchive(archive, &(comp.second), nullptr);
}
// add the generated package to package file names list
this->packageFileNames.push_back(std::move(packageFileName));
}
}
return 1;
}
int cmCPackArchiveGenerator::PackageComponentsAllInOne()
{
// reset the package file names
this->packageFileNames.clear();
this->packageFileNames.emplace_back(this->GetArchiveFileName());
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Packaging all groups in one package..."
"(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
<< std::endl);
DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
Deduplicator deduplicator;
// The ALL COMPONENTS in ONE package case
for (auto& comp : this->Components) {
// Add the files of this component to the archive
this->addOneComponentToArchive(archive, &(comp.second), &deduplicator);
}
// archive goes out of scope so it will finalized and closed.
return 1;
}
int cmCPackArchiveGenerator::PackageFiles()
{
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Toplevel: " << this->toplevel << std::endl);
if (this->WantsComponentInstallation()) {
// CASE 1 : COMPONENT ALL-IN-ONE package
// If ALL COMPONENTS in ONE package has been requested
// then the package file is unique and should be open here.
if (this->componentPackageMethod == ONE_PACKAGE) {
return this->PackageComponentsAllInOne();
}
// CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
// There will be 1 package for each component group
// however one may require to ignore component group and
// in this case you'll get 1 package for each component.
return this->PackageComponents(this->componentPackageMethod ==
ONE_PACKAGE_PER_COMPONENT);
}
// CASE 3 : NON COMPONENT package.
this->packageFileNames.clear();
this->packageFileNames.emplace_back(this->GetArchiveFileName());
DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
cmWorkingDirectory workdir(this->toplevel);
if (workdir.Failed()) {
cmCPackLogger(cmCPackLog::LOG_ERROR, workdir.GetError() << std::endl);
return 0;
}
for (std::string const& file : this->files) {
// Get the relative path to the file
std::string rp = cmSystemTools::RelativePath(this->toplevel, file);
archive.Add(rp, 0, nullptr, false);
if (!archive) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Problem while adding file <"
<< file << "> to archive <" << this->packageFileNames[0]
<< ">, ERROR = " << archive.GetError() << std::endl);
return 0;
}
}
// The destructor of cmArchiveWrite will close and finish the write
return 1;
}
int cmCPackArchiveGenerator::GenerateHeader(std::ostream* /*unused*/)
{
return 1;
}
bool cmCPackArchiveGenerator::SupportsComponentInstallation() const
{
// The Component installation support should only
// be activated if explicitly requested by the user
// (for backward compatibility reason)
return this->IsOn("CPACK_ARCHIVE_COMPONENT_INSTALL");
}
int cmCPackArchiveGenerator::GetThreadCount() const
{
int threads = 1;
// CPACK_ARCHIVE_THREADS overrides CPACK_THREADS
if (cmValue v = this->GetOptionIfSet("CPACK_ARCHIVE_THREADS")) {
threads = std::stoi(*v);
} else if (cmValue v2 = this->GetOptionIfSet("CPACK_THREADS")) {
threads = std::stoi(*v2);
}
return threads;
}
|