From f719a36bc0c695f3875a8a830cdcf525ad094fc6 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 21 Oct 2025 10:44:43 -0400 Subject: [PATCH] file(CREATE_LINK): Refactor error handling for removal of destination Avoid relying on global state. --- Source/cmFileCommand.cxx | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 057b3fa0ec..b7a9be3b07 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -3233,18 +3233,20 @@ bool HandleCreateLinkCommand(std::vector const& args, } // Check if the new file already exists and remove it. - if (cmSystemTools::PathExists(newFileName) && - !cmSystemTools::RemoveFile(newFileName)) { - auto err = cmStrCat("Failed to create link '", newFileName, - "' because existing path cannot be removed: ", - cmSystemTools::GetLastSystemError(), '\n'); + if (cmSystemTools::PathExists(newFileName)) { + cmsys::Status rmStatus = cmSystemTools::RemoveFile(newFileName); + if (!rmStatus) { + auto err = cmStrCat("Failed to create link '", newFileName, + "' because existing path cannot be removed: ", + rmStatus.GetString(), '\n'); - if (!arguments.Result.empty()) { - status.GetMakefile().AddDefinition(arguments.Result, err); - return true; + if (!arguments.Result.empty()) { + status.GetMakefile().AddDefinition(arguments.Result, err); + return true; + } + status.SetError(err); + return false; } - status.SetError(err); - return false; } // Whether the operation completed successfully.