2004-02-06 14:57:10 +00:00
|
|
|
#include "profiles.hh"
|
|
|
|
|
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>
|
2004-02-06 14:57:10 +00:00
|
|
|
|
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);
|
|
|
|
int p = s.find("-link");
|
|
|
|
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);
|
|
|
|
|
|
|
|
Strings names = readDirectory(profileDir);
|
|
|
|
for (Strings::iterator i = names.begin(); i != names.end(); ++i) {
|
2004-02-06 16:16:55 +00:00
|
|
|
int n;
|
|
|
|
if ((n = parseName(profileName, *i)) != -1) {
|
2004-02-06 16:03:27 +00:00
|
|
|
Generation gen;
|
|
|
|
gen.path = profileDir + "/" + *i;
|
|
|
|
gen.number = n;
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(gen.path.c_str(), &st) != 0)
|
|
|
|
throw SysError(format("statting `%1%'") % gen.path);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-10 13:32:08 +00:00
|
|
|
static void makeNames(const Path & profile, unsigned int num,
|
|
|
|
Path & generation, Path & gcrootDrv, Path & gcrootClr)
|
|
|
|
{
|
|
|
|
Path prefix = (format("%1%-%2%") % profile % num).str();
|
|
|
|
generation = prefix + "-link";
|
|
|
|
gcrootDrv = prefix + "-drv.gcroot";
|
|
|
|
gcrootClr = prefix + "-clr.gcroot";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-11 10:25:31 +00:00
|
|
|
Path createGeneration(Path profile, Path outPath,
|
|
|
|
Path drvPath, Path clrPath)
|
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);
|
2004-02-06 16:03:27 +00:00
|
|
|
unsigned int num = gens.size() > 0 ? gens.front().number : 0;
|
|
|
|
|
|
|
|
/* Create the new generation. */
|
2004-02-11 10:25:31 +00:00
|
|
|
Path generation, gcrootDrv, gcrootClr;
|
2004-02-06 14:57:10 +00:00
|
|
|
|
|
|
|
while (1) {
|
2004-09-10 13:32:08 +00:00
|
|
|
makeNames(profile, num, generation, gcrootDrv, gcrootClr);
|
2004-02-06 14:57:10 +00:00
|
|
|
if (symlink(outPath.c_str(), generation.c_str()) == 0) break;
|
|
|
|
if (errno != EEXIST)
|
|
|
|
throw SysError(format("creating symlink `%1%'") % generation);
|
|
|
|
/* Somebody beat us to it, retry with a higher number. */
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
2004-02-11 10:25:31 +00:00
|
|
|
writeStringToFile(gcrootDrv, drvPath);
|
|
|
|
writeStringToFile(gcrootClr, clrPath);
|
2004-02-06 14:57:10 +00:00
|
|
|
|
|
|
|
return generation;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-10 13:32:08 +00:00
|
|
|
static void removeFile(const Path & path)
|
|
|
|
{
|
|
|
|
if (remove(path.c_str()) == -1)
|
|
|
|
throw SysError(format("cannot unlink `%1%'") % path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void deleteGeneration(const Path & profile, unsigned int gen)
|
|
|
|
{
|
|
|
|
Path generation, gcrootDrv, gcrootClr;
|
|
|
|
makeNames(profile, gen, generation, gcrootDrv, gcrootClr);
|
|
|
|
removeFile(generation);
|
|
|
|
if (pathExists(gcrootClr)) removeFile(gcrootClr);
|
|
|
|
if (pathExists(gcrootDrv)) removeFile(gcrootDrv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-06 14:57:10 +00:00
|
|
|
void switchLink(Path link, Path target)
|
|
|
|
{
|
|
|
|
/* Hacky. */
|
|
|
|
if (dirOf(target) == dirOf(link)) target = baseNameOf(target);
|
|
|
|
|
|
|
|
Path tmp = canonPath(dirOf(link) + "/.new_" + baseNameOf(link));
|
|
|
|
if (symlink(target.c_str(), tmp.c_str()) != 0)
|
|
|
|
throw SysError(format("creating symlink `%1%'") % tmp);
|
|
|
|
/* 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)
|
|
|
|
throw SysError(format("renaming `%1%' to `%2%'") % tmp % link);
|
|
|
|
}
|