Skip to content

Commit

Permalink
more readable errors if symlinks cannot be created
Browse files Browse the repository at this point in the history
Before:

filesystem error: cannot create symlink: Permission denied [/nix/store/1s2p3a4rs172336hj2l8n20nz74hf71j-nix-eval-jobs-2.24.1.drv] [/1s2p3a4rs172336hj2l8n20nz74hf71j-nix-eval-jobs-2.24.1.drv.tmp-2772352-1316231068]

Now:

creating symlink '/wfxz2q489c811n08cdqj7ywxm3n4z6m5-nix-eval-jobs-2.24.1.drv.tmp-2971297-324653080' -> '/nix/store/wfxz2q489c811n08cdqj7ywxm3n4z6m5-nix-eval-jobs-2.24.1.drv': Permission denied
  • Loading branch information
Mic92 committed Nov 26, 2024
1 parent 9931881 commit 2ae5c78
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 @@ -936,7 +936,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 2ae5c78

Please sign in to comment.