Skip to content

Commit

Permalink
Merge pull request NixOS#11973 from NixOS/mergify/bp/2.25-maintenance…
Browse files Browse the repository at this point in the history
…/pr-11959

more readable errors if symlinks cannot be created (backport NixOS#11959)
  • Loading branch information
Mic92 authored Nov 27, 2024
2 parents 14432ea + 8d51c90 commit 282bfbd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,11 @@ std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)

void createSymlink(const Path & target, const Path & link)
{
fs::create_symlink(target, link);
try {
fs::create_symlink(target, link);
} catch (fs::filesystem_error & e) {
throw SysError("creating symlink '%1%' -> '%2%'", link, target);
}
}

void replaceSymlink(const fs::path & target, const fs::path & link)
Expand All @@ -615,10 +619,16 @@ void replaceSymlink(const fs::path & target, const fs::path & link)
fs::create_symlink(target, tmp);
} catch (fs::filesystem_error & e) {
if (e.code() == std::errc::file_exists) continue;
throw;
throw SysError("creating symlink '%1%' -> '%2%'", tmp, target);
}

try {
fs::rename(tmp, link);
} catch (fs::filesystem_error & e) {
if (e.code() == std::errc::file_exists) continue;
throw SysError("renaming '%1%' to '%2%'", tmp, link);
}

fs::rename(tmp, link);

break;
}
Expand Down
2 changes: 0 additions & 2 deletions src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ void setWriteTime(const std::filesystem::path & path, const struct stat & st);
/**
* Create a symlink.
*
* In the process of being deprecated for
* `std::filesystem::create_symlink`.
*/
void createSymlink(const Path & target, const Path & link);

Expand Down
2 changes: 1 addition & 1 deletion src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
}
continue;
} else
fs::create_symlink(target, to2);
createSymlink(target, to2);
}
else
throw Error("file '%s' has unsupported type", from2);
Expand Down

0 comments on commit 282bfbd

Please sign in to comment.