1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-15 15:02:42 +01:00

replaceSymlink(): Handle the case where the temporary file already exists

Not really necessary anymore for #849, but still nice to have.

(cherry picked from commit 2965d40612)
This commit is contained in:
Eelco Dolstra 2017-07-11 23:20:01 +02:00
parent 2a0112a370
commit 7577d35895
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -431,12 +431,21 @@ void createSymlink(const Path & target, const Path & link)
void replaceSymlink(const Path & target, const Path & link) void replaceSymlink(const Path & target, const Path & link)
{ {
Path tmp = canonPath(dirOf(link) + "/.new_" + baseNameOf(link)); for (unsigned int n = 0; true; n++) {
Path tmp = canonPath(fmt("%s/.%d_%s", dirOf(link), n, baseNameOf(link)));
try {
createSymlink(target, tmp); createSymlink(target, tmp);
} catch (SysError & e) {
if (e.errNo == EEXIST) continue;
throw;
}
if (rename(tmp.c_str(), link.c_str()) != 0) if (rename(tmp.c_str(), link.c_str()) != 0)
throw SysError(format("renaming %1% to %2%") % tmp % link); throw SysError(format("renaming %1% to %2%") % tmp % link);
break;
}
} }