cmCPackArchiveGenerator.cxx 18 KB

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