cmWIXFilesSourceWriter.cxx 4.6 KB

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