From 47b9ebdb4fe0bbc3985ef48cbeb04f023526a7b8 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Tue, 19 May 2020 14:25:45 -0400 Subject: [PATCH] CPack/DragNDrop: keep the dmg mounting as private as possible This is done by mounting it under the CPack work directory and hiding it from Finder. --- .../dev/cpack-dragndrop-local-mount.rst | 6 ++++ Source/CPack/cmCPackDragNDropGenerator.cxx | 31 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 Help/release/dev/cpack-dragndrop-local-mount.rst diff --git a/Help/release/dev/cpack-dragndrop-local-mount.rst b/Help/release/dev/cpack-dragndrop-local-mount.rst new file mode 100644 index 00000000000..6f31366a92b --- /dev/null +++ b/Help/release/dev/cpack-dragndrop-local-mount.rst @@ -0,0 +1,6 @@ +cpack-dragndrop-local-mount +--------------------------- + +* The :cpack_gen:`CPack DragNDrop Generator` learned to place in-progress + packages out of the view of Finder and mounts it underneath CPack's work + directory instead of globally. diff --git a/Source/CPack/cmCPackDragNDropGenerator.cxx b/Source/CPack/cmCPackDragNDropGenerator.cxx index fe7abf42239..b8c4bf20155 100644 --- a/Source/CPack/cmCPackDragNDropGenerator.cxx +++ b/Source/CPack/cmCPackDragNDropGenerator.cxx @@ -441,9 +441,18 @@ int cmCPackDragNDropGenerator::CreateDMG(const std::string& src_dir, // before we exit. bool had_error = false; + std::string const mountroot = + cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), "/CPackVolumes"); + cmSystemTools::MakeDirectory(mountroot); + std::ostringstream attach_command; attach_command << this->GetOption("CPACK_COMMAND_HDIUTIL"); attach_command << " attach"; + // Mount into our local packaging directory. + attach_command << " -mountroot " + << "\"" << mountroot << "\""; + // Hide from Finder. + attach_command << " -nobrowse"; attach_command << " \"" << temp_image << "\""; std::string attach_output; @@ -454,11 +463,27 @@ int cmCPackDragNDropGenerator::CreateDMG(const std::string& src_dir, return 0; } - cmsys::RegularExpression mountpoint_regex(".*(/Volumes/[^\n]+)\n.*"); + // The backslash must be first to avoid escaping the escape characters + // added for other special characters. + constexpr cm::string_view specialChars = "\\^$[](|)+*?."; + // Escape all regex-special characters. + std::string regexSafeMountroot = mountroot; + for (char specialChar : specialChars) { + size_t pos = std::string::npos; + while ((pos = regexSafeMountroot.rfind(specialChar, pos)) != + std::string::npos) { + regexSafeMountroot.replace(pos, 1, { '\\', specialChar, '\0' }); + if (pos == 0) { + break; + } + --pos; + } + } + cmsys::RegularExpression mountpoint_regex(".*(" + regexSafeMountroot + + "/[^\n]+)\n.*"); mountpoint_regex.find(attach_output.c_str()); std::string const temp_mount = mountpoint_regex.match(1); - std::string const temp_mount_name = - temp_mount.substr(sizeof("/Volumes/") - 1); + std::string const temp_mount_name = temp_mount.substr(mountroot.size()); // Remove dummy padding file so we have enough space on RW image ... std::ostringstream dummy_padding; -- GitLab