2004-02-06 14:57:10 +00:00
|
|
|
|
#include "profiles.hh"
|
2006-12-05 02:18:46 +00:00
|
|
|
|
#include "store-api.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
#include "util.hh"
|
2004-02-06 14:57:10 +00:00
|
|
|
|
|
2004-02-06 16:03:27 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
2004-07-01 11:11:16 +00:00
|
|
|
|
#include <errno.h>
|
2005-05-04 16:33:20 +00:00
|
|
|
|
#include <stdio.h>
|
2004-02-06 14:57:10 +00:00
|
|
|
|
|
2004-02-06 16:03:27 +00:00
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
2004-02-06 16:03:27 +00:00
|
|
|
|
static bool cmpGensByNumber(const Generation & a, const Generation & b)
|
|
|
|
|
{
|
|
|
|
|
return a.number < b.number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-06 16:16:55 +00: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 02:19:43 +00:00
|
|
|
|
string::size_type p = s.find("-link");
|
2004-02-06 16:16:55 +00:00
|
|
|
|
if (p == string::npos) return -1;
|
2004-09-10 13:32:08 +00:00
|
|
|
|
int n;
|
|
|
|
|
if (string2Int(string(s, 0, p), n) && n >= 0)
|
|
|
|
|
return n;
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
2004-02-06 16:16:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Generations findGenerations(Path profile, int & curGen)
|
2004-02-06 14:57:10 +00:00
|
|
|
|
{
|
2004-02-06 16:03:27 +00:00
|
|
|
|
Generations gens;
|
|
|
|
|
|
2004-02-06 14:57:10 +00:00
|
|
|
|
Path profileDir = dirOf(profile);
|
|
|
|
|
string profileName = baseNameOf(profile);
|
2012-12-03 17:19:49 +00:00
|
|
|
|
|
2014-08-01 14:37:47 +00:00
|
|
|
|
for (auto & i : readDirectory(profileDir)) {
|
2004-02-06 16:16:55 +00:00
|
|
|
|
int n;
|
2014-08-01 14:37:47 +00:00
|
|
|
|
if ((n = parseName(profileName, i.name)) != -1) {
|
2004-02-06 16:03:27 +00:00
|
|
|
|
Generation gen;
|
2014-08-01 14:37:47 +00:00
|
|
|
|
gen.path = profileDir + "/" + i.name;
|
2004-02-06 16:03:27 +00:00
|
|
|
|
gen.number = n;
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(gen.path.c_str(), &st) != 0)
|
2014-08-20 15:00:17 +00:00
|
|
|
|
throw SysError(format("statting ‘%1%’") % gen.path);
|
2004-02-06 16:03:27 +00:00
|
|
|
|
gen.creationTime = st.st_mtime;
|
|
|
|
|
gens.push_back(gen);
|
|
|
|
|
}
|
2004-02-06 14:57:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-02-06 16:03:27 +00:00
|
|
|
|
gens.sort(cmpGensByNumber);
|
|
|
|
|
|
2004-02-06 16:16:55 +00:00
|
|
|
|
curGen = pathExists(profile)
|
|
|
|
|
? parseName(profileName, readLink(profile))
|
|
|
|
|
: -1;
|
|
|
|
|
|
2004-02-06 16:03:27 +00:00
|
|
|
|
return gens;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 10:44:57 +00:00
|
|
|
|
static void makeName(const Path & profile, unsigned int num,
|
|
|
|
|
Path & outLink)
|
2004-09-10 13:32:08 +00:00
|
|
|
|
{
|
|
|
|
|
Path prefix = (format("%1%-%2%") % profile % num).str();
|
2005-02-11 16:56:45 +00:00
|
|
|
|
outLink = prefix + "-link";
|
2004-09-10 13:32:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 16:16:02 +00:00
|
|
|
|
Path createGeneration(Path profile, Path outPath)
|
2004-02-06 16:03:27 +00:00
|
|
|
|
{
|
|
|
|
|
/* The new generation number should be higher than old the
|
|
|
|
|
previous ones. */
|
2004-02-06 16:16:55 +00:00
|
|
|
|
int dummy;
|
|
|
|
|
Generations gens = findGenerations(profile, dummy);
|
2006-09-14 22:48:59 +00:00
|
|
|
|
unsigned int num = gens.size() > 0 ? gens.back().number : 0;
|
|
|
|
|
|
2006-09-14 22:30:33 +00:00
|
|
|
|
/* Create the new generation. Note that addPermRoot() blocks if
|
|
|
|
|
the garbage collector is running to prevent the stuff we've
|
2006-11-30 18:35:36 +00:00
|
|
|
|
built from moving from the temporary roots (which the GC knows)
|
2006-09-14 22:30:33 +00: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);
|
2011-08-31 21:11:50 +00:00
|
|
|
|
addPermRoot(*store, outPath, generation, false, true);
|
2006-09-14 22:30:33 +00:00
|
|
|
|
|
|
|
|
|
return generation;
|
2004-02-06 14:57:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-09-10 13:32:08 +00:00
|
|
|
|
static void removeFile(const Path & path)
|
|
|
|
|
{
|
|
|
|
|
if (remove(path.c_str()) == -1)
|
2014-08-20 15:00:17 +00:00
|
|
|
|
throw SysError(format("cannot unlink ‘%1%’") % path);
|
2004-09-10 13:32:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deleteGeneration(const Path & profile, unsigned int gen)
|
|
|
|
|
{
|
2005-02-14 10:44:57 +00:00
|
|
|
|
Path generation;
|
|
|
|
|
makeName(profile, gen, generation);
|
2004-09-10 13:32:08 +00:00
|
|
|
|
removeFile(generation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-02-06 14:57:10 +00:00
|
|
|
|
void switchLink(Path link, Path target)
|
|
|
|
|
{
|
|
|
|
|
/* Hacky. */
|
|
|
|
|
if (dirOf(target) == dirOf(link)) target = baseNameOf(target);
|
2012-12-03 17:19:49 +00:00
|
|
|
|
|
2004-02-06 14:57:10 +00:00
|
|
|
|
Path tmp = canonPath(dirOf(link) + "/.new_" + baseNameOf(link));
|
2014-02-27 22:17:53 +00:00
|
|
|
|
createSymlink(target, tmp);
|
2004-02-06 14:57:10 +00:00
|
|
|
|
/* The rename() system call is supposed to be essentially atomic
|
|
|
|
|
on Unix. That is, if we have links `current -> X' and
|
|
|
|
|
`new_current -> Y', and we rename new_current to current, a
|
|
|
|
|
process accessing current will see X or Y, but never a
|
|
|
|
|
file-not-found or other error condition. This is sufficient to
|
|
|
|
|
atomically switch user environments. */
|
|
|
|
|
if (rename(tmp.c_str(), link.c_str()) != 0)
|
2014-08-20 15:00:17 +00:00
|
|
|
|
throw SysError(format("renaming ‘%1%’ to ‘%2%’") % tmp % link);
|
2004-02-06 14:57:10 +00:00
|
|
|
|
}
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
|
|
|
|
|
|
void lockProfile(PathLocks & lock, const Path & profile)
|
|
|
|
|
{
|
|
|
|
|
lock.lockPaths(singleton<PathSet>(profile),
|
2014-08-20 15:00:17 +00:00
|
|
|
|
(format("waiting for lock on profile ‘%1%’") % profile).str());
|
2010-04-21 15:08:58 +00:00
|
|
|
|
lock.setDeletion(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string optimisticLockProfile(const Path & profile)
|
|
|
|
|
{
|
|
|
|
|
return pathExists(profile) ? readLink(profile) : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
|
}
|