diff --git a/src/libstore/optimise-store.cc b/src/libstore/optimise-store.cc index dca093e04..d1e858300 100644 --- a/src/libstore/optimise-store.cc +++ b/src/libstore/optimise-store.cc @@ -234,7 +234,7 @@ void LocalStore::optimisePath_( its timestamp back to 0. */ MakeReadOnly makeReadOnly(mustToggle ? dirOfPath : ""); - std::filesystem::path tempLink = fmt("%1%/.tmp-link-%2%-%3%", config->realStoreDir, getpid(), rand()); + std::filesystem::path tempLink = makeTempPath(config->realStoreDir.get(), ".tmp-link"); try { std::filesystem::create_hard_link(linkPath, tempLink); @@ -255,8 +255,12 @@ void LocalStore::optimisePath_( try { std::filesystem::rename(tempLink, path); } catch (std::filesystem::filesystem_error & e) { - std::filesystem::remove(tempLink); - printError("unable to unlink %1%", tempLink); + { + std::error_code ec; + remove(tempLink, ec); /* Clean up after ourselves. */ + if (ec) + printError("unable to unlink %1%: %2%", tempLink, ec.message()); + } if (e.code() == std::errc::too_many_links) { /* Some filesystems generate too many links on the rename, rather than on the original link. (Probably it diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 6b145b343..9e27940c5 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -737,11 +737,10 @@ std::filesystem::path makeTempPath(const std::filesystem::path & root, const std void createSymlink(const Path & target, const Path & link) { - try { - std::filesystem::create_symlink(target, link); - } catch (std::filesystem::filesystem_error & e) { - throw SysError("creating symlink '%1%' -> '%2%'", link, target); - } + std::error_code ec; + std::filesystem::create_symlink(target, link, ec); + if (ec) + throw SysError(ec.value(), "creating symlink '%1%' -> '%2%'", link, target); } void replaceSymlink(const std::filesystem::path & target, const std::filesystem::path & link) diff --git a/src/libutil/include/nix/util/file-system.hh b/src/libutil/include/nix/util/file-system.hh index 7673c24fd..d8b266e02 100644 --- a/src/libutil/include/nix/util/file-system.hh +++ b/src/libutil/include/nix/util/file-system.hh @@ -294,6 +294,10 @@ class AutoDelete public: AutoDelete(); AutoDelete(const std::filesystem::path & p, bool recursive = true); + AutoDelete(AutoDelete &&) = delete; + AutoDelete(const AutoDelete &) = delete; + AutoDelete & operator=(AutoDelete &&) = delete; + AutoDelete & operator=(const AutoDelete &) = delete; ~AutoDelete(); void cancel();