| 526 | } |
| 527 | |
| 528 | bool cmFileCopier::InstallSymlink(std::string const& fromFile, |
| 529 | std::string const& toFile) |
| 530 | { |
| 531 | // Read the original symlink. |
| 532 | std::string symlinkTarget; |
| 533 | auto read_symlink_status = |
| 534 | cmSystemTools::ReadSymlink(fromFile, symlinkTarget); |
| 535 | if (!read_symlink_status) { |
| 536 | std::ostringstream e; |
| 537 | e << this->Name << " cannot read symlink \"" << fromFile |
| 538 | << "\" to duplicate at \"" << toFile |
| 539 | << "\": " << read_symlink_status.GetString() << "."; |
| 540 | this->Status.SetError(e.str()); |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | // Compare the symlink value to that at the destination if not |
| 545 | // always installing. |
| 546 | bool copy = true; |
| 547 | if (!this->Always) { |
| 548 | std::string oldSymlinkTarget; |
| 549 | if (cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget)) { |
| 550 | if (symlinkTarget == oldSymlinkTarget) { |
| 551 | copy = false; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // Inform the user about this file installation. |
| 557 | this->ReportCopy(toFile, TypeLink, copy); |
| 558 | |
| 559 | if (copy) { |
| 560 | // Remove the destination file so we can always create the symlink. |
| 561 | cmSystemTools::RemoveFile(toFile); |
| 562 | |
| 563 | // Create destination directory if it doesn't exist |
| 564 | cmSystemTools::MakeDirectory(cmSystemTools::GetFilenamePath(toFile)); |
| 565 | |
| 566 | // Create the symlink. |
| 567 | cmsys::Status status = |
| 568 | cmSystemTools::CreateSymlinkQuietly(symlinkTarget, toFile); |
| 569 | if (!status) { |
| 570 | #ifdef _WIN32 |
| 571 | bool const errorFileExists = status.GetWindows() == ERROR_FILE_EXISTS; |
| 572 | #else |
| 573 | bool const errorFileExists = status.GetPOSIX() == EEXIST; |
| 574 | #endif |
| 575 | std::string reason; |
| 576 | if (errorFileExists && cmSystemTools::FileIsDirectory(toFile)) { |
| 577 | reason = "A directory already exists at that location"; |
| 578 | } else { |
| 579 | reason = status.GetString(); |
| 580 | } |
| 581 | std::string e = |
| 582 | cmStrCat(this->Name, " cannot duplicate symlink\n ", fromFile, |
| 583 | "\nat\n ", toFile, "\nbecause: ", reason); |
| 584 | this->Status.SetError(e); |
| 585 | return false; |