cmCPackArchiveGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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(std::string const& path,
  45. std::string const& 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(std::string const& 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(std::string const& 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(std::string const& path,
  105. std::string const& 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. std::string const& component, bool isGroupName)
  167. {
  168. std::string componentUpper(cmSystemTools::UpperCase(component));
  169. std::string packageFileName;
  170. if (cmValue v = this->GetOptionIfSet("CPACK_ARCHIVE_" + componentUpper +
  171. "_FILE_NAME")) {
  172. packageFileName += *v;
  173. } else if ((v = this->GetOptionIfSet("CPACK_ARCHIVE_FILE_NAME"))) {
  174. packageFileName +=
  175. this->GetComponentPackageFileName(*v, component, isGroupName);
  176. } else {
  177. v = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  178. packageFileName +=
  179. this->GetComponentPackageFileName(*v, component, isGroupName);
  180. }
  181. packageFileName += this->GetOutputExtension();
  182. return packageFileName;
  183. }
  184. int cmCPackArchiveGenerator::InitializeInternal()
  185. {
  186. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  187. cmValue newExtensionValue = this->GetOption("CPACK_ARCHIVE_FILE_EXTENSION");
  188. if (!newExtensionValue.IsEmpty()) {
  189. std::string newExtension = *newExtensionValue;
  190. if (!cmHasLiteralPrefix(newExtension, ".")) {
  191. newExtension = cmStrCat('.', newExtension);
  192. }
  193. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  194. "Using user-provided file extension "
  195. << newExtension << " instead of the default "
  196. << this->OutputExtension << std::endl);
  197. this->OutputExtension = std::move(newExtension);
  198. }
  199. return this->Superclass::InitializeInternal();
  200. }
  201. int cmCPackArchiveGenerator::addOneComponentToArchive(
  202. cmArchiveWrite& archive, cmCPackComponent* component,
  203. Deduplicator* deduplicator)
  204. {
  205. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  206. " - packaging component: " << component->Name << std::endl);
  207. // Add the files of this component to the archive
  208. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  209. localToplevel += "/" + this->GetSanitizedDirOrFileName(component->Name);
  210. // Change to local toplevel
  211. cmWorkingDirectory workdir(localToplevel);
  212. if (workdir.Failed()) {
  213. cmCPackLogger(cmCPackLog::LOG_ERROR, workdir.GetError() << std::endl);
  214. return 0;
  215. }
  216. std::string filePrefix;
  217. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
  218. filePrefix = cmStrCat(this->GetOption("CPACK_PACKAGE_FILE_NAME"), '/');
  219. }
  220. cmValue installPrefix = this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  221. if (installPrefix && installPrefix->size() > 1 &&
  222. (*installPrefix)[0] == '/') {
  223. // add to file prefix and remove the leading '/'
  224. filePrefix += installPrefix->substr(1);
  225. filePrefix += "/";
  226. }
  227. for (std::string const& file : component->Files) {
  228. std::string rp = filePrefix + file;
  229. DeduplicateStatus status = DeduplicateStatus::Add;
  230. if (deduplicator) {
  231. status = deduplicator->IsDeduplicate(rp, localToplevel);
  232. }
  233. if (!deduplicator || status == DeduplicateStatus::Add) {
  234. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file: " << rp << std::endl);
  235. archive.Add(rp, 0, nullptr, false);
  236. } else if (status == DeduplicateStatus::Error) {
  237. cmCPackLogger(cmCPackLog::LOG_ERROR,
  238. "ERROR The data in files with the "
  239. "same filename is different.");
  240. return 0;
  241. } else {
  242. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  243. "Passing file: " << rp << std::endl);
  244. }
  245. if (!archive) {
  246. cmCPackLogger(cmCPackLog::LOG_ERROR,
  247. "ERROR while packaging files: " << archive.GetError()
  248. << std::endl);
  249. return 0;
  250. }
  251. }
  252. return 1;
  253. }
  254. /*
  255. * The macro will open/create a file 'filename'
  256. * an declare and open the associated
  257. * cmArchiveWrite 'archive' object.
  258. */
  259. #define DECLARE_AND_OPEN_ARCHIVE(filename, archive) \
  260. cmGeneratedFileStream gf; \
  261. gf.Open((filename), false, true); \
  262. if (!GenerateHeader(&gf)) { \
  263. cmCPackLogger(cmCPackLog::LOG_ERROR, \
  264. "Problem to generate Header for archive <" \
  265. << (filename) << ">." << std::endl); \
  266. return 0; \
  267. } \
  268. cmArchiveWrite archive(gf, this->Compress, this->ArchiveFormat, 0, \
  269. this->GetThreadCount()); \
  270. do { \
  271. if (!archive.Open()) { \
  272. cmCPackLogger(cmCPackLog::LOG_ERROR, \
  273. "Problem to open archive <" \
  274. << (filename) << ">, ERROR = " << (archive).GetError() \
  275. << std::endl); \
  276. return 0; \
  277. } \
  278. if (!(archive)) { \
  279. cmCPackLogger(cmCPackLog::LOG_ERROR, \
  280. "Problem to create archive <" \
  281. << (filename) << ">, ERROR = " << (archive).GetError() \
  282. << std::endl); \
  283. return 0; \
  284. } \
  285. } while (false)
  286. int cmCPackArchiveGenerator::PackageComponents(bool ignoreGroup)
  287. {
  288. this->packageFileNames.clear();
  289. // The default behavior is to have one package by component group
  290. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  291. if (!ignoreGroup) {
  292. for (auto const& compG : this->ComponentGroups) {
  293. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  294. "Packaging component group: " << compG.first << std::endl);
  295. // Begin the archive for this group
  296. std::string packageFileName = std::string(this->toplevel) + "/" +
  297. this->GetArchiveComponentFileName(compG.first, true);
  298. Deduplicator deduplicator;
  299. // open a block in order to automatically close archive
  300. // at the end of the block
  301. {
  302. DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
  303. // now iterate over the component of this group
  304. for (cmCPackComponent* comp : (compG.second).Components) {
  305. // Add the files of this component to the archive
  306. this->addOneComponentToArchive(archive, comp, &deduplicator);
  307. }
  308. }
  309. // add the generated package to package file names list
  310. this->packageFileNames.push_back(std::move(packageFileName));
  311. }
  312. // Handle Orphan components (components not belonging to any groups)
  313. for (auto& comp : this->Components) {
  314. // Does the component belong to a group?
  315. if (!comp.second.Group) {
  316. cmCPackLogger(
  317. cmCPackLog::LOG_VERBOSE,
  318. "Component <"
  319. << comp.second.Name
  320. << "> does not belong to any group, package it separately."
  321. << std::endl);
  322. std::string packageFileName = std::string(this->toplevel);
  323. packageFileName +=
  324. "/" + this->GetArchiveComponentFileName(comp.first, false);
  325. {
  326. DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
  327. // Add the files of this component to the archive
  328. this->addOneComponentToArchive(archive, &(comp.second), nullptr);
  329. }
  330. // add the generated package to package file names list
  331. this->packageFileNames.push_back(std::move(packageFileName));
  332. }
  333. }
  334. }
  335. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  336. // We build 1 package per component
  337. else {
  338. for (auto& comp : this->Components) {
  339. std::string packageFileName = std::string(this->toplevel);
  340. packageFileName +=
  341. "/" + this->GetArchiveComponentFileName(comp.first, false);
  342. {
  343. DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
  344. // Add the files of this component to the archive
  345. this->addOneComponentToArchive(archive, &(comp.second), nullptr);
  346. }
  347. // add the generated package to package file names list
  348. this->packageFileNames.push_back(std::move(packageFileName));
  349. }
  350. }
  351. return 1;
  352. }
  353. int cmCPackArchiveGenerator::PackageComponentsAllInOne()
  354. {
  355. // reset the package file names
  356. this->packageFileNames.clear();
  357. this->packageFileNames.emplace_back(this->toplevel);
  358. this->packageFileNames[0] += "/";
  359. if (cmValue v = this->GetOptionIfSet("CPACK_ARCHIVE_FILE_NAME")) {
  360. this->packageFileNames[0] += *v;
  361. } else {
  362. v = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  363. this->packageFileNames[0] += *v;
  364. }
  365. this->packageFileNames[0] += this->GetOutputExtension();
  366. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  367. "Packaging all groups in one package..."
  368. "(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
  369. << std::endl);
  370. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
  371. Deduplicator deduplicator;
  372. // The ALL COMPONENTS in ONE package case
  373. for (auto& comp : this->Components) {
  374. // Add the files of this component to the archive
  375. this->addOneComponentToArchive(archive, &(comp.second), &deduplicator);
  376. }
  377. // archive goes out of scope so it will finalized and closed.
  378. return 1;
  379. }
  380. int cmCPackArchiveGenerator::PackageFiles()
  381. {
  382. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  383. "Toplevel: " << this->toplevel << std::endl);
  384. if (this->WantsComponentInstallation()) {
  385. // CASE 1 : COMPONENT ALL-IN-ONE package
  386. // If ALL COMPONENTS in ONE package has been requested
  387. // then the package file is unique and should be open here.
  388. if (this->componentPackageMethod == ONE_PACKAGE) {
  389. return this->PackageComponentsAllInOne();
  390. }
  391. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  392. // There will be 1 package for each component group
  393. // however one may require to ignore component group and
  394. // in this case you'll get 1 package for each component.
  395. return this->PackageComponents(this->componentPackageMethod ==
  396. ONE_PACKAGE_PER_COMPONENT);
  397. }
  398. // CASE 3 : NON COMPONENT package.
  399. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
  400. cmWorkingDirectory workdir(this->toplevel);
  401. if (workdir.Failed()) {
  402. cmCPackLogger(cmCPackLog::LOG_ERROR, workdir.GetError() << std::endl);
  403. return 0;
  404. }
  405. for (std::string const& file : this->files) {
  406. // Get the relative path to the file
  407. std::string rp = cmSystemTools::RelativePath(this->toplevel, file);
  408. archive.Add(rp, 0, nullptr, false);
  409. if (!archive) {
  410. cmCPackLogger(cmCPackLog::LOG_ERROR,
  411. "Problem while adding file <"
  412. << file << "> to archive <" << this->packageFileNames[0]
  413. << ">, ERROR = " << archive.GetError() << std::endl);
  414. return 0;
  415. }
  416. }
  417. // The destructor of cmArchiveWrite will close and finish the write
  418. return 1;
  419. }
  420. int cmCPackArchiveGenerator::GenerateHeader(std::ostream* /*unused*/)
  421. {
  422. return 1;
  423. }
  424. bool cmCPackArchiveGenerator::SupportsComponentInstallation() const
  425. {
  426. // The Component installation support should only
  427. // be activated if explicitly requested by the user
  428. // (for backward compatibility reason)
  429. return this->IsOn("CPACK_ARCHIVE_COMPONENT_INSTALL");
  430. }
  431. int cmCPackArchiveGenerator::GetThreadCount() const
  432. {
  433. int threads = 1;
  434. // CPACK_ARCHIVE_THREADS overrides CPACK_THREADS
  435. if (cmValue v = this->GetOptionIfSet("CPACK_ARCHIVE_THREADS")) {
  436. threads = std::stoi(*v);
  437. } else if (cmValue v2 = this->GetOptionIfSet("CPACK_THREADS")) {
  438. threads = std::stoi(*v2);
  439. }
  440. return threads;
  441. }