Create a wrapper around stdlib’s rename

Directly takes some c++ strings, and gently throws an exception on error
(rather than having to inline this logic everywhere)
This commit is contained in:
Théophane Hufschmitt 2022-03-17 15:28:46 +01:00 committed by Théophane Hufschmitt
parent 8119390abc
commit c2de0a232c
9 changed files with 22 additions and 22 deletions

View file

@ -705,8 +705,7 @@ static void movePath(const Path & src, const Path & dst)
if (changePerm) if (changePerm)
chmod_(src, st.st_mode | S_IWUSR); chmod_(src, st.st_mode | S_IWUSR);
if (rename(src.c_str(), dst.c_str())) moveFile(src, dst);
throw SysError("renaming '%1%' to '%2%'", src, dst);
if (changePerm) if (changePerm)
chmod_(dst, st.st_mode); chmod_(dst, st.st_mode);

View file

@ -223,8 +223,7 @@ static void movePath(const Path & src, const Path & dst)
if (changePerm) if (changePerm)
chmod_(src, st.st_mode | S_IWUSR); chmod_(src, st.st_mode | S_IWUSR);
if (rename(src.c_str(), dst.c_str())) moveFile(src, dst);
throw SysError("renaming '%1%' to '%2%'", src, dst);
if (changePerm) if (changePerm)
chmod_(dst, st.st_mode); chmod_(dst, st.st_mode);
@ -311,7 +310,7 @@ bool LocalDerivationGoal::cleanupDecideWhetherDiskFull()
if (buildMode != bmCheck && status.known->isValid()) continue; if (buildMode != bmCheck && status.known->isValid()) continue;
auto p = worker.store.printStorePath(status.known->path); auto p = worker.store.printStorePath(status.known->path);
if (pathExists(chrootRootDir + p)) if (pathExists(chrootRootDir + p))
rename((chrootRootDir + p).c_str(), p.c_str()); moveFile((chrootRootDir + p), p);
} }
return diskFull; return diskFull;
@ -2625,8 +2624,7 @@ DrvOutputs LocalDerivationGoal::registerOutputs()
Path prev = path + checkSuffix; Path prev = path + checkSuffix;
deletePath(prev); deletePath(prev);
Path dst = path + checkSuffix; Path dst = path + checkSuffix;
if (rename(path.c_str(), dst.c_str())) moveFile(path, dst);
throw SysError("renaming '%s' to '%s'", path, dst);
} }
} }

View file

@ -22,8 +22,7 @@ void builtinUnpackChannel(const BasicDerivation & drv)
auto entries = readDirectory(out); auto entries = readDirectory(out);
if (entries.size() != 1) if (entries.size() != 1)
throw Error("channel tarball '%s' contains more than one file", src); throw Error("channel tarball '%s' contains more than one file", src);
if (rename((out + "/" + entries[0].name).c_str(), (out + "/" + channelName).c_str()) == -1) moveFile((out + "/" + entries[0].name), (out + "/" + channelName));
throw SysError("renaming channel directory");
} }
} }

View file

@ -39,9 +39,7 @@ static void makeSymlink(const Path & link, const Path & target)
createSymlink(target, tempLink); createSymlink(target, tempLink);
/* Atomically replace the old one. */ /* Atomically replace the old one. */
if (rename(tempLink.c_str(), link.c_str()) == -1) moveFile(tempLink, link);
throw SysError("cannot rename '%1%' to '%2%'",
tempLink , link);
} }

View file

@ -57,8 +57,7 @@ protected:
AutoDelete del(tmp, false); AutoDelete del(tmp, false);
StreamToSourceAdapter source(istream); StreamToSourceAdapter source(istream);
writeFile(tmp, source); writeFile(tmp, source);
if (rename(tmp.c_str(), path2.c_str())) moveFile(tmp, path2);
throw SysError("renaming '%1%' to '%2%'", tmp, path2);
del.cancel(); del.cancel();
} }

View file

@ -1430,8 +1430,7 @@ StorePath LocalStore::addToStoreFromDump(Source & source0, std::string_view name
writeFile(realPath, dumpSource); writeFile(realPath, dumpSource);
} else { } else {
/* Move the temporary path we restored above. */ /* Move the temporary path we restored above. */
if (rename(tempPath.c_str(), realPath.c_str())) moveFile(tempPath, realPath);
throw Error("renaming '%s' to '%s'", tempPath, realPath);
} }
/* For computing the nar hash. In recursive SHA-256 mode, this /* For computing the nar hash. In recursive SHA-256 mode, this
@ -1942,8 +1941,7 @@ void LocalStore::addBuildLog(const StorePath & drvPath, std::string_view log)
writeFile(tmpFile, compress("bzip2", log)); writeFile(tmpFile, compress("bzip2", log));
if (rename(tmpFile.c_str(), logPath.c_str()) != 0) moveFile(tmpFile, logPath);
throw SysError("renaming '%1%' to '%2%'", tmpFile, logPath);
} }
std::optional<std::string> LocalStore::getVersion() std::optional<std::string> LocalStore::getVersion()

View file

@ -229,7 +229,9 @@ void LocalStore::optimisePath_(Activity * act, OptimiseStats & stats,
} }
/* Atomically replace the old file with the new hard link. */ /* Atomically replace the old file with the new hard link. */
if (rename(tempLink.c_str(), path.c_str()) == -1) { try {
moveFile(tempLink, path);
} catch (SysError & e) {
if (unlink(tempLink.c_str()) == -1) if (unlink(tempLink.c_str()) == -1)
printError("unable to unlink '%1%'", tempLink); printError("unable to unlink '%1%'", tempLink);
if (errno == EMLINK) { if (errno == EMLINK) {
@ -240,7 +242,7 @@ void LocalStore::optimisePath_(Activity * act, OptimiseStats & stats,
debug("'%s' has reached maximum number of links", linkPath); debug("'%s' has reached maximum number of links", linkPath);
return; return;
} }
throw SysError("cannot rename '%1%' to '%2%'", tempLink, path); throw;
} }
stats.filesLinked++; stats.filesLinked++;

View file

@ -34,11 +34,16 @@ void replaceSymlink(const Path & target, const Path & link,
throw; throw;
} }
if (rename(tmp.c_str(), link.c_str()) != 0) moveFile(tmp, link);
throw SysError("renaming '%1%' to '%2%'", tmp, link);
break; break;
} }
} }
void moveFile(const Path & oldName, const Path & newName)
{
if (::rename(oldName.c_str(), newName.c_str()))
throw SysError("renaming '%1%' to '%2%'", oldName, newName);
}
} }

View file

@ -168,6 +168,8 @@ void createSymlink(const Path & target, const Path & link,
void replaceSymlink(const Path & target, const Path & link, void replaceSymlink(const Path & target, const Path & link,
std::optional<time_t> mtime = {}); std::optional<time_t> mtime = {});
void moveFile(const Path & src, const Path & dst);
/* Wrappers arount read()/write() that read/write exactly the /* Wrappers arount read()/write() that read/write exactly the
requested number of bytes. */ requested number of bytes. */