2004-02-06 15:57:10 +01:00
|
|
|
|
#include "profiles.hh"
|
2006-12-05 03:18:46 +01:00
|
|
|
|
#include "store-api.hh"
|
2006-09-04 23:06:23 +02:00
|
|
|
|
#include "util.hh"
|
2004-02-06 15:57:10 +01:00
|
|
|
|
|
2004-02-06 17:03:27 +01:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
2004-07-01 13:11:16 +02:00
|
|
|
|
#include <errno.h>
|
2005-05-04 18:33:20 +02:00
|
|
|
|
#include <stdio.h>
|
2004-02-06 15:57:10 +01:00
|
|
|
|
|
2004-02-06 17:03:27 +01:00
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
2004-02-06 17:03:27 +01:00
|
|
|
|
static bool cmpGensByNumber(const Generation & a, const Generation & b)
|
|
|
|
|
{
|
|
|
|
|
return a.number < b.number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-06 17:16:55 +01:00
|
|
|
|
/* Parse a generation name of the format
|
|
|
|
|
`<profilename>-<number>-link'. */
|
|
|
|
|
static int parseName(const string & profileName, const string & name)
|
|
|
|
|
{
|
|
|
|
|
if (string(name, 0, profileName.size() + 1) != profileName + "-") return -1;
|
|
|
|
|
string s = string(name, profileName.size() + 1);
|
2006-05-11 04:19:43 +02:00
|
|
|
|
string::size_type p = s.find("-link");
|
2004-02-06 17:16:55 +01:00
|
|
|
|
if (p == string::npos) return -1;
|
2004-09-10 15:32:08 +02:00
|
|
|
|
int n;
|
|
|
|
|
if (string2Int(string(s, 0, p), n) && n >= 0)
|
|
|
|
|
return n;
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
2004-02-06 17:16:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Generations findGenerations(Path profile, int & curGen)
|
2004-02-06 15:57:10 +01:00
|
|
|
|
{
|
2004-02-06 17:03:27 +01:00
|
|
|
|
Generations gens;
|
|
|
|
|
|
2004-02-06 15:57:10 +01:00
|
|
|
|
Path profileDir = dirOf(profile);
|
|
|
|
|
string profileName = baseNameOf(profile);
|
2012-12-03 18:19:49 +01:00
|
|
|
|
|
2014-08-01 16:37:47 +02:00
|
|
|
|
for (auto & i : readDirectory(profileDir)) {
|
2004-02-06 17:16:55 +01:00
|
|
|
|
int n;
|
2014-08-01 16:37:47 +02:00
|
|
|
|
if ((n = parseName(profileName, i.name)) != -1) {
|
2004-02-06 17:03:27 +01:00
|
|
|
|
Generation gen;
|
2014-08-01 16:37:47 +02:00
|
|
|
|
gen.path = profileDir + "/" + i.name;
|
2004-02-06 17:03:27 +01:00
|
|
|
|
gen.number = n;
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(gen.path.c_str(), &st) != 0)
|
2014-08-20 17:00:17 +02:00
|
|
|
|
throw SysError(format("statting ‘%1%’") % gen.path);
|
2004-02-06 17:03:27 +01:00
|
|
|
|
gen.creationTime = st.st_mtime;
|
|
|
|
|
gens.push_back(gen);
|
|
|
|
|
}
|
2004-02-06 15:57:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-06 17:03:27 +01:00
|
|
|
|
gens.sort(cmpGensByNumber);
|
|
|
|
|
|
2004-02-06 17:16:55 +01:00
|
|
|
|
curGen = pathExists(profile)
|
|
|
|
|
? parseName(profileName, readLink(profile))
|
|
|
|
|
: -1;
|
|
|
|
|
|
2004-02-06 17:03:27 +01:00
|
|
|
|
return gens;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 11:44:57 +01:00
|
|
|
|
static void makeName(const Path & profile, unsigned int num,
|
|
|
|
|
Path & outLink)
|
2004-09-10 15:32:08 +02:00
|
|
|
|
{
|
|
|
|
|
Path prefix = (format("%1%-%2%") % profile % num).str();
|
2005-02-11 17:56:45 +01:00
|
|
|
|
outLink = prefix + "-link";
|
2004-09-10 15:32:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 14:28:26 +01:00
|
|
|
|
Path createGeneration(ref<StoreAPI> store, Path profile, Path outPath)
|
2004-02-06 17:03:27 +01:00
|
|
|
|
{
|
|
|
|
|
/* The new generation number should be higher than old the
|
|
|
|
|
previous ones. */
|
2004-02-06 17:16:55 +01:00
|
|
|
|
int dummy;
|
|
|
|
|
Generations gens = findGenerations(profile, dummy);
|
2015-05-18 08:38:49 +02:00
|
|
|
|
|
|
|
|
|
unsigned int num;
|
|
|
|
|
if (gens.size() > 0) {
|
2015-05-19 20:03:36 +02:00
|
|
|
|
Generation last = gens.back();
|
|
|
|
|
|
2015-05-20 17:29:52 +02:00
|
|
|
|
if (readLink(last.path) == outPath) {
|
|
|
|
|
/* We only create a new generation symlink if it differs
|
|
|
|
|
from the last one.
|
2015-05-19 20:03:36 +02:00
|
|
|
|
|
|
|
|
|
This helps keeping gratuitous installs/rebuilds from piling
|
|
|
|
|
up uncontrolled numbers of generations, cluttering up the
|
|
|
|
|
UI like grub. */
|
|
|
|
|
return last.path;
|
2015-05-18 08:38:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num = gens.back().number;
|
|
|
|
|
} else {
|
|
|
|
|
num = 0;
|
|
|
|
|
}
|
2006-09-15 00:48:59 +02:00
|
|
|
|
|
2006-09-15 00:30:33 +02:00
|
|
|
|
/* Create the new generation. Note that addPermRoot() blocks if
|
|
|
|
|
the garbage collector is running to prevent the stuff we've
|
2006-11-30 19:35:36 +01:00
|
|
|
|
built from moving from the temporary roots (which the GC knows)
|
2006-09-15 00:30:33 +02:00
|
|
|
|
to the permanent roots (of which the GC would have a stale
|
|
|
|
|
view). If we didn't do it this way, the GC might remove the
|
|
|
|
|
user environment etc. we've just built. */
|
|
|
|
|
Path generation;
|
|
|
|
|
makeName(profile, num + 1, generation);
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 14:28:26 +01:00
|
|
|
|
addPermRoot(store, outPath, generation, false, true);
|
2006-09-15 00:30:33 +02:00
|
|
|
|
|
|
|
|
|
return generation;
|
2004-02-06 15:57:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-09-10 15:32:08 +02:00
|
|
|
|
static void removeFile(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
if (remove(path.c_str()) == -1)
|
2014-08-20 17:00:17 +02:00
|
|
|
|
throw SysError(format("cannot unlink ‘%1%’") % path);
|
2004-09-10 15:32:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deleteGeneration(const Path & profile, unsigned int gen)
|
|
|
|
|
{
|
2005-02-14 11:44:57 +01:00
|
|
|
|
Path generation;
|
|
|
|
|
makeName(profile, gen, generation);
|
2004-09-10 15:32:08 +02:00
|
|
|
|
removeFile(generation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-05-21 16:26:03 +02:00
|
|
|
|
static void deleteGeneration2(const Path & profile, unsigned int gen, bool dryRun)
|
|
|
|
|
{
|
|
|
|
|
if (dryRun)
|
|
|
|
|
printMsg(lvlInfo, format("would remove generation %1%") % gen);
|
|
|
|
|
else {
|
|
|
|
|
printMsg(lvlInfo, format("removing generation %1%") % gen);
|
|
|
|
|
deleteGeneration(profile, gen);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deleteGenerations(const Path & profile, const std::set<unsigned int> & gensToDelete, bool dryRun)
|
|
|
|
|
{
|
|
|
|
|
PathLocks lock;
|
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
|
|
int curGen;
|
|
|
|
|
Generations gens = findGenerations(profile, curGen);
|
|
|
|
|
|
|
|
|
|
if (gensToDelete.find(curGen) != gensToDelete.end())
|
|
|
|
|
throw Error(format("cannot delete current generation of profile %1%’") % profile);
|
|
|
|
|
|
|
|
|
|
for (auto & i : gens) {
|
|
|
|
|
if (gensToDelete.find(i.number) == gensToDelete.end()) continue;
|
|
|
|
|
deleteGeneration2(profile, i.number, dryRun);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deleteOldGenerations(const Path & profile, bool dryRun)
|
|
|
|
|
{
|
|
|
|
|
PathLocks lock;
|
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
|
|
int curGen;
|
|
|
|
|
Generations gens = findGenerations(profile, curGen);
|
|
|
|
|
|
|
|
|
|
for (auto & i : gens)
|
|
|
|
|
if (i.number != curGen)
|
|
|
|
|
deleteGeneration2(profile, i.number, dryRun);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun)
|
|
|
|
|
{
|
|
|
|
|
PathLocks lock;
|
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
|
|
int curGen;
|
|
|
|
|
Generations gens = findGenerations(profile, curGen);
|
|
|
|
|
|
|
|
|
|
bool canDelete = false;
|
|
|
|
|
for (auto i = gens.rbegin(); i != gens.rend(); ++i)
|
|
|
|
|
if (canDelete) {
|
|
|
|
|
assert(i->creationTime < t);
|
|
|
|
|
if (i->number != curGen)
|
|
|
|
|
deleteGeneration2(profile, i->number, dryRun);
|
|
|
|
|
} else if (i->creationTime < t) {
|
|
|
|
|
/* We may now start deleting generations, but we don't
|
|
|
|
|
delete this generation yet, because this generation was
|
|
|
|
|
still the one that was active at the requested point in
|
|
|
|
|
time. */
|
|
|
|
|
canDelete = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deleteGenerationsOlderThan(const Path & profile, const string & timeSpec, bool dryRun)
|
|
|
|
|
{
|
|
|
|
|
time_t curTime = time(0);
|
|
|
|
|
string strDays = string(timeSpec, 0, timeSpec.size() - 1);
|
|
|
|
|
int days;
|
|
|
|
|
|
|
|
|
|
if (!string2Int(strDays, days) || days < 1)
|
|
|
|
|
throw Error(format("invalid number of days specifier ‘%1%’") % timeSpec);
|
|
|
|
|
|
|
|
|
|
time_t oldTime = curTime - days * 24 * 3600;
|
|
|
|
|
|
|
|
|
|
deleteGenerationsOlderThan(profile, oldTime, dryRun);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-06 15:57:10 +01:00
|
|
|
|
void switchLink(Path link, Path target)
|
|
|
|
|
{
|
|
|
|
|
/* Hacky. */
|
|
|
|
|
if (dirOf(target) == dirOf(link)) target = baseNameOf(target);
|
2012-12-03 18:19:49 +01:00
|
|
|
|
|
2015-04-09 11:42:04 +02:00
|
|
|
|
replaceSymlink(target, link);
|
2004-02-06 15:57:10 +01:00
|
|
|
|
}
|
2006-09-04 23:06:23 +02:00
|
|
|
|
|
2010-04-21 17:08:58 +02:00
|
|
|
|
|
|
|
|
|
void lockProfile(PathLocks & lock, const Path & profile)
|
|
|
|
|
{
|
|
|
|
|
lock.lockPaths(singleton<PathSet>(profile),
|
2014-08-20 17:00:17 +02:00
|
|
|
|
(format("waiting for lock on profile ‘%1%’") % profile).str());
|
2010-04-21 17:08:58 +02:00
|
|
|
|
lock.setDeletion(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string optimisticLockProfile(const Path & profile)
|
|
|
|
|
{
|
|
|
|
|
return pathExists(profile) ? readLink(profile) : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
|
}
|