forked from lix-project/lix
* nix-store --optimise: flag "--dry-run" to just query what the disk
savings would be.
This commit is contained in:
parent
a8629de827
commit
315183f194
|
@ -1016,25 +1016,30 @@ static void hashAndLink(bool dryRun, HashToPath & hashToPath,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printMsg(lvlTalkative, format("linking `%1%' to `%2%'") % path % prevPath.first);
|
if (!dryRun) {
|
||||||
|
|
||||||
Path tempLink = (format("%1%.tmp-%2%-%3%")
|
printMsg(lvlTalkative, format("linking `%1%' to `%2%'") % path % prevPath.first);
|
||||||
% path % getpid() % rand()).str();
|
|
||||||
|
|
||||||
toggleWritable(dirOf(path), true);
|
Path tempLink = (format("%1%.tmp-%2%-%3%")
|
||||||
|
% path % getpid() % rand()).str();
|
||||||
|
|
||||||
if (link(prevPath.first.c_str(), tempLink.c_str()) == -1)
|
toggleWritable(dirOf(path), true);
|
||||||
throw SysError(format("cannot link `%1%' to `%2%'")
|
|
||||||
% tempLink % prevPath.first);
|
|
||||||
|
|
||||||
/* Atomically replace the old file with the new hard link. */
|
if (link(prevPath.first.c_str(), tempLink.c_str()) == -1)
|
||||||
if (rename(tempLink.c_str(), path.c_str()) == -1)
|
throw SysError(format("cannot link `%1%' to `%2%'")
|
||||||
throw SysError(format("cannot rename `%1%' to `%2%'")
|
% tempLink % prevPath.first);
|
||||||
% tempLink % path);
|
|
||||||
|
|
||||||
/* Make the directory read-only again and reset its timestamp
|
/* Atomically replace the old file with the new hard link. */
|
||||||
back to 0. */
|
if (rename(tempLink.c_str(), path.c_str()) == -1)
|
||||||
_canonicalisePathMetaData(dirOf(path), false);
|
throw SysError(format("cannot rename `%1%' to `%2%'")
|
||||||
|
% tempLink % path);
|
||||||
|
|
||||||
|
/* Make the directory read-only again and reset its
|
||||||
|
timestamp back to 0. */
|
||||||
|
_canonicalisePathMetaData(dirOf(path), false);
|
||||||
|
|
||||||
|
} else
|
||||||
|
printMsg(lvlTalkative, format("would link `%1%' to `%2%'") % path % prevPath.first);
|
||||||
|
|
||||||
stats.filesLinked++;
|
stats.filesLinked++;
|
||||||
stats.bytesFreed += st.st_size;
|
stats.bytesFreed += st.st_size;
|
||||||
|
|
|
@ -640,16 +640,19 @@ static void opOptimise(Strings opFlags, Strings opArgs)
|
||||||
if (!opArgs.empty())
|
if (!opArgs.empty())
|
||||||
throw UsageError("no arguments expected");
|
throw UsageError("no arguments expected");
|
||||||
|
|
||||||
|
bool dryRun = false;
|
||||||
|
|
||||||
for (Strings::iterator i = opFlags.begin();
|
for (Strings::iterator i = opFlags.begin();
|
||||||
i != opFlags.end(); ++i)
|
i != opFlags.end(); ++i)
|
||||||
throw UsageError(format("unknown flag `%1%'") % *i);
|
if (*i == "--dry-run") dryRun = true;
|
||||||
|
else throw UsageError(format("unknown flag `%1%'") % *i);
|
||||||
|
|
||||||
LocalStore * store2(dynamic_cast<LocalStore *>(store.get()));
|
LocalStore * store2(dynamic_cast<LocalStore *>(store.get()));
|
||||||
if (!store2) throw Error("you don't have sufficient rights to use --optimise");
|
if (!store2) throw Error("you don't have sufficient rights to use --optimise");
|
||||||
|
|
||||||
OptimiseStats stats;
|
OptimiseStats stats;
|
||||||
try {
|
try {
|
||||||
store2->optimiseStore(true, stats);
|
store2->optimiseStore(dryRun, stats);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
showOptimiseStats(stats);
|
showOptimiseStats(stats);
|
||||||
throw;
|
throw;
|
||||||
|
|
Loading…
Reference in a new issue