cmWIXFilesSourceWriter.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #if defined(__CYGWIN__)
  4. // For S_IWRITE symbol
  5. # define _DEFAULT_SOURCE
  6. #endif
  7. #include "cmWIXFilesSourceWriter.h"
  8. #include "cm_sys_stat.h"
  9. #include "cmCMakeToWixPath.h"
  10. #include "cmInstalledFile.h"
  11. #include "cmSystemTools.h"
  12. #include "cmUuid.h"
  13. #include "cmWIXAccessControlList.h"
  14. cmWIXFilesSourceWriter::cmWIXFilesSourceWriter(unsigned long wixVersion,
  15. cmCPackLog* logger,
  16. std::string const& filename,
  17. GuidType componentGuidType)
  18. : cmWIXSourceWriter(wixVersion, logger, filename, componentGuidType)
  19. {
  20. }
  21. void cmWIXFilesSourceWriter::EmitShortcut(std::string const& id,
  22. cmWIXShortcut const& shortcut,
  23. std::string const& shortcutPrefix,
  24. size_t shortcutIndex)
  25. {
  26. std::ostringstream shortcutId;
  27. shortcutId << shortcutPrefix << id;
  28. if (shortcutIndex > 0) {
  29. shortcutId << "_" << shortcutIndex;
  30. }
  31. std::string fileId = std::string("CM_F") + id;
  32. BeginElement("Shortcut");
  33. AddAttribute("Id", shortcutId.str());
  34. AddAttribute("Name", shortcut.label);
  35. std::string target = "[#" + fileId + "]";
  36. AddAttribute("Target", target);
  37. AddAttribute("WorkingDirectory", shortcut.workingDirectoryId);
  38. EndElement("Shortcut");
  39. }
  40. void cmWIXFilesSourceWriter::EmitRemoveFolder(std::string const& id)
  41. {
  42. BeginElement("RemoveFolder");
  43. AddAttribute("Id", id);
  44. AddAttribute("On", "uninstall");
  45. EndElement("RemoveFolder");
  46. }
  47. void cmWIXFilesSourceWriter::EmitInstallRegistryValue(
  48. std::string const& registryKey, std::string const& cpackComponentName,
  49. std::string const& suffix)
  50. {
  51. std::string valueName;
  52. if (!cpackComponentName.empty()) {
  53. valueName = cpackComponentName + "_";
  54. }
  55. valueName += "installed";
  56. valueName += suffix;
  57. BeginElement("RegistryValue");
  58. AddAttribute("Root", "HKCU");
  59. AddAttribute("Key", registryKey);
  60. AddAttribute("Name", valueName);
  61. AddAttribute("Type", "integer");
  62. AddAttribute("Value", "1");
  63. AddAttribute("KeyPath", "yes");
  64. EndElement("RegistryValue");
  65. }
  66. void cmWIXFilesSourceWriter::EmitUninstallShortcut(
  67. std::string const& packageName)
  68. {
  69. BeginElement("Shortcut");
  70. AddAttribute("Id", "UNINSTALL");
  71. AddAttribute("Name", "Uninstall " + packageName);
  72. AddAttribute("Description", "Uninstalls " + packageName);
  73. AddAttribute("Target", "[SystemFolder]msiexec.exe");
  74. AddAttribute("Arguments", "/x [ProductCode]");
  75. EndElement("Shortcut");
  76. }
  77. std::string cmWIXFilesSourceWriter::EmitComponentCreateFolder(
  78. std::string const& directoryId, std::string const& guid,
  79. cmInstalledFile const* installedFile)
  80. {
  81. std::string componentId = std::string("CM_C_EMPTY_") + directoryId;
  82. BeginElement("DirectoryRef");
  83. AddAttribute("Id", directoryId);
  84. BeginElement("Component");
  85. AddAttribute("Id", componentId);
  86. AddAttribute("Guid", guid);
  87. BeginElement("CreateFolder");
  88. if (installedFile) {
  89. cmWIXAccessControlList acl(Logger, *installedFile, *this);
  90. acl.Apply();
  91. }
  92. EndElement("CreateFolder");
  93. EndElement("Component");
  94. EndElement("DirectoryRef");
  95. return componentId;
  96. }
  97. std::string cmWIXFilesSourceWriter::EmitComponentFile(
  98. std::string const& directoryId, std::string const& id,
  99. std::string const& filePath, cmWIXPatch& patch,
  100. cmInstalledFile const* installedFile)
  101. {
  102. std::string componentId = std::string("CM_C") + id;
  103. std::string fileId = std::string("CM_F") + id;
  104. std::string guid = CreateGuidFromComponentId(componentId);
  105. BeginElement("DirectoryRef");
  106. AddAttribute("Id", directoryId);
  107. BeginElement("Component");
  108. AddAttribute("Id", componentId);
  109. AddAttribute("Guid", guid);
  110. if (installedFile) {
  111. if (installedFile->GetPropertyAsBool("CPACK_NEVER_OVERWRITE")) {
  112. AddAttribute("NeverOverwrite", "yes");
  113. }
  114. if (installedFile->GetPropertyAsBool("CPACK_PERMANENT")) {
  115. AddAttribute("Permanent", "yes");
  116. }
  117. }
  118. patch.ApplyFragment(componentId, *this);
  119. BeginElement("File");
  120. AddAttribute("Id", fileId);
  121. AddAttribute("Source", CMakeToWixPath(filePath));
  122. AddAttribute("KeyPath", "yes");
  123. mode_t fileMode = 0;
  124. cmSystemTools::GetPermissions(filePath.c_str(), fileMode);
  125. if (!(fileMode & S_IWRITE)) {
  126. AddAttribute("ReadOnly", "yes");
  127. }
  128. patch.ApplyFragment(fileId, *this);
  129. if (installedFile) {
  130. cmWIXAccessControlList acl(Logger, *installedFile, *this);
  131. acl.Apply();
  132. }
  133. EndElement("File");
  134. EndElement("Component");
  135. EndElement("DirectoryRef");
  136. return componentId;
  137. }