From 315183f194881591480d9acd1eef685d6b64d7f8 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 10 Oct 2007 13:43:04 +0000 Subject: [PATCH] * nix-store --optimise: flag "--dry-run" to just query what the disk savings would be. --- src/libstore/local-store.cc | 33 +++++++++++++++++++-------------- src/nix-store/nix-store.cc | 7 +++++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index c77ab3c6ce..9a85c48d92 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -1016,25 +1016,30 @@ static void hashAndLink(bool dryRun, HashToPath & hashToPath, return; } - printMsg(lvlTalkative, format("linking `%1%' to `%2%'") % path % prevPath.first); + if (!dryRun) { + + printMsg(lvlTalkative, format("linking `%1%' to `%2%'") % path % prevPath.first); - Path tempLink = (format("%1%.tmp-%2%-%3%") - % path % getpid() % rand()).str(); + Path tempLink = (format("%1%.tmp-%2%-%3%") + % path % getpid() % rand()).str(); - toggleWritable(dirOf(path), true); + toggleWritable(dirOf(path), true); - if (link(prevPath.first.c_str(), tempLink.c_str()) == -1) - throw SysError(format("cannot link `%1%' to `%2%'") - % tempLink % prevPath.first); + if (link(prevPath.first.c_str(), tempLink.c_str()) == -1) + throw SysError(format("cannot link `%1%' to `%2%'") + % tempLink % prevPath.first); - /* Atomically replace the old file with the new hard link. */ - if (rename(tempLink.c_str(), path.c_str()) == -1) - throw SysError(format("cannot rename `%1%' to `%2%'") - % tempLink % path); + /* Atomically replace the old file with the new hard link. */ + if (rename(tempLink.c_str(), path.c_str()) == -1) + 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); + /* 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.bytesFreed += st.st_size; diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 678ce2ae9c..0d474946bf 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -640,16 +640,19 @@ static void opOptimise(Strings opFlags, Strings opArgs) if (!opArgs.empty()) throw UsageError("no arguments expected"); + bool dryRun = false; + for (Strings::iterator i = opFlags.begin(); 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(store.get())); if (!store2) throw Error("you don't have sufficient rights to use --optimise"); OptimiseStats stats; try { - store2->optimiseStore(true, stats); + store2->optimiseStore(dryRun, stats); } catch (...) { showOptimiseStats(stats); throw;