2004-02-06 14:57:10 +00:00
|
|
|
#include "profiles.hh"
|
2006-12-05 02:18:46 +00:00
|
|
|
#include "store-api.hh"
|
2020-10-09 20:18:08 +00:00
|
|
|
#include "local-fs-store.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:16:55 +00:00
|
|
|
/* Parse a generation name of the format
|
|
|
|
`<profilename>-<number>-link'. */
|
2020-07-16 13:14:22 +00:00
|
|
|
static std::optional<GenerationNumber> parseName(const string & profileName, const string & name)
|
2004-02-06 16:16:55 +00:00
|
|
|
{
|
2020-07-16 13:14:22 +00:00
|
|
|
if (string(name, 0, profileName.size() + 1) != profileName + "-") return {};
|
2004-02-06 16:16:55 +00:00
|
|
|
string s = string(name, profileName.size() + 1);
|
2006-05-11 02:19:43 +00:00
|
|
|
string::size_type p = s.find("-link");
|
2020-07-16 13:14:22 +00:00
|
|
|
if (p == string::npos) return {};
|
2021-01-08 11:22:21 +00:00
|
|
|
if (auto n = string2Int<unsigned int>(s.substr(0, p)))
|
|
|
|
return *n;
|
2004-09-10 13:32:08 +00:00
|
|
|
else
|
2020-07-16 13:14:22 +00:00
|
|
|
return {};
|
2004-02-06 16:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path profile)
|
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);
|
2019-12-05 18:11:09 +00:00
|
|
|
auto profileName = std::string(baseNameOf(profile));
|
2012-12-03 17:19:49 +00:00
|
|
|
|
2014-08-01 14:37:47 +00:00
|
|
|
for (auto & i : readDirectory(profileDir)) {
|
2020-07-16 13:14:22 +00:00
|
|
|
if (auto n = parseName(profileName, i.name)) {
|
|
|
|
auto path = profileDir + "/" + i.name;
|
|
|
|
gens.push_back({
|
|
|
|
.number = *n,
|
|
|
|
.path = path,
|
2020-09-23 17:17:28 +00:00
|
|
|
.creationTime = lstat(path).st_mtime
|
2020-07-16 13:14:22 +00:00
|
|
|
});
|
2004-02-06 16:03:27 +00:00
|
|
|
}
|
2004-02-06 14:57:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
gens.sort([](const Generation & a, const Generation & b)
|
|
|
|
{
|
|
|
|
return a.number < b.number;
|
|
|
|
});
|
2004-02-06 16:03:27 +00:00
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
return {
|
|
|
|
gens,
|
|
|
|
pathExists(profile)
|
2004-02-06 16:16:55 +00:00
|
|
|
? parseName(profileName, readLink(profile))
|
2020-07-16 13:14:22 +00:00
|
|
|
: std::nullopt
|
|
|
|
};
|
2004-02-06 16:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
static void makeName(const Path & profile, GenerationNumber num,
|
2005-02-14 10:44:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-03 09:06:56 +00:00
|
|
|
Path createGeneration(ref<LocalFSStore> store, Path profile, StorePath outPath)
|
2004-02-06 16:03:27 +00:00
|
|
|
{
|
|
|
|
/* The new generation number should be higher than old the
|
|
|
|
previous ones. */
|
2020-07-16 13:14:22 +00:00
|
|
|
auto [gens, dummy] = findGenerations(profile);
|
2015-05-18 06:38:49 +00:00
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
GenerationNumber num;
|
2015-05-18 06:38:49 +00:00
|
|
|
if (gens.size() > 0) {
|
2015-05-19 18:03:36 +00:00
|
|
|
Generation last = gens.back();
|
|
|
|
|
2020-09-03 09:06:56 +00:00
|
|
|
if (readLink(last.path) == store->printStorePath(outPath)) {
|
2015-05-20 15:29:52 +00:00
|
|
|
/* We only create a new generation symlink if it differs
|
|
|
|
from the last one.
|
2015-05-19 18:03:36 +00: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 06:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
num = gens.back().number;
|
|
|
|
} else {
|
|
|
|
num = 0;
|
|
|
|
}
|
2006-09-14 22:48:59 +00:00
|
|
|
|
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);
|
2020-09-03 09:26:36 +00:00
|
|
|
store->addPermRoot(outPath, generation);
|
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)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("cannot unlink '%1%'", path);
|
2004-09-10 13:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
void deleteGeneration(const Path & profile, GenerationNumber gen)
|
2004-09-10 13:32:08 +00:00
|
|
|
{
|
2005-02-14 10:44:57 +00:00
|
|
|
Path generation;
|
|
|
|
makeName(profile, gen, generation);
|
2004-09-10 13:32:08 +00:00
|
|
|
removeFile(generation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
static void deleteGeneration2(const Path & profile, GenerationNumber gen, bool dryRun)
|
2015-05-21 14:26:03 +00:00
|
|
|
{
|
|
|
|
if (dryRun)
|
2016-09-21 14:11:01 +00:00
|
|
|
printInfo(format("would remove generation %1%") % gen);
|
2015-05-21 14:26:03 +00:00
|
|
|
else {
|
2016-09-21 14:11:01 +00:00
|
|
|
printInfo(format("removing generation %1%") % gen);
|
2015-05-21 14:26:03 +00:00
|
|
|
deleteGeneration(profile, gen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
void deleteGenerations(const Path & profile, const std::set<GenerationNumber> & gensToDelete, bool dryRun)
|
2015-05-21 14:26:03 +00:00
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
2015-05-21 14:26:03 +00:00
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
if (gensToDelete.count(*curGen))
|
2020-04-21 23:07:07 +00:00
|
|
|
throw Error("cannot delete current generation of profile %1%'", profile);
|
2015-05-21 14:26:03 +00:00
|
|
|
|
|
|
|
for (auto & i : gens) {
|
2020-07-16 13:14:22 +00:00
|
|
|
if (!gensToDelete.count(i.number)) continue;
|
2015-05-21 14:26:03 +00:00
|
|
|
deleteGeneration2(profile, i.number, dryRun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
void deleteGenerationsGreaterThan(const Path & profile, GenerationNumber max, bool dryRun)
|
2016-01-07 01:15:19 +00:00
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
2018-03-02 03:59:00 +00:00
|
|
|
bool fromCurGen = false;
|
2020-07-16 13:14:22 +00:00
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
2016-01-07 01:15:19 +00:00
|
|
|
for (auto i = gens.rbegin(); i != gens.rend(); ++i) {
|
2018-03-02 03:59:00 +00:00
|
|
|
if (i->number == curGen) {
|
|
|
|
fromCurGen = true;
|
2016-05-19 19:42:54 +00:00
|
|
|
max--;
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-02 03:59:00 +00:00
|
|
|
if (fromCurGen) {
|
|
|
|
if (max) {
|
|
|
|
max--;
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-02 03:22:02 +00:00
|
|
|
deleteGeneration2(profile, i->number, dryRun);
|
2018-03-02 03:59:00 +00:00
|
|
|
}
|
2016-01-07 01:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-21 14:26:03 +00:00
|
|
|
|
|
|
|
void deleteOldGenerations(const Path & profile, bool dryRun)
|
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
2015-05-21 14:26:03 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-07-16 13:14:22 +00:00
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
2015-05-21 14:26:03 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-07-11 10:15:06 +00:00
|
|
|
if (timeSpec.empty() || timeSpec[timeSpec.size() - 1] != 'd')
|
2021-07-12 13:04:46 +00:00
|
|
|
throw UsageError("invalid number of days specifier '%1%', expected something like '14d'", timeSpec);
|
2021-07-11 10:15:06 +00:00
|
|
|
|
2015-05-21 14:26:03 +00:00
|
|
|
time_t curTime = time(0);
|
|
|
|
string strDays = string(timeSpec, 0, timeSpec.size() - 1);
|
2021-01-08 11:22:21 +00:00
|
|
|
auto days = string2Int<int>(strDays);
|
2015-05-21 14:26:03 +00:00
|
|
|
|
2021-01-08 11:22:21 +00:00
|
|
|
if (!days || *days < 1)
|
2021-07-12 13:04:46 +00:00
|
|
|
throw UsageError("invalid number of days specifier '%1%'", timeSpec);
|
2015-05-21 14:26:03 +00:00
|
|
|
|
2021-01-08 11:22:21 +00:00
|
|
|
time_t oldTime = curTime - *days * 24 * 3600;
|
2015-05-21 14:26:03 +00:00
|
|
|
|
|
|
|
deleteGenerationsOlderThan(profile, oldTime, dryRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-04-09 09:42:04 +00:00
|
|
|
replaceSymlink(target, 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
|
|
|
|
2021-09-14 17:05:28 +00:00
|
|
|
void switchGeneration(
|
|
|
|
const Path & profile,
|
|
|
|
std::optional<GenerationNumber> dstGen,
|
|
|
|
bool dryRun)
|
|
|
|
{
|
|
|
|
PathLocks lock;
|
|
|
|
lockProfile(lock, profile);
|
|
|
|
|
|
|
|
auto [gens, curGen] = findGenerations(profile);
|
|
|
|
|
|
|
|
std::optional<Generation> dst;
|
|
|
|
for (auto & i : gens)
|
|
|
|
if ((!dstGen && i.number < curGen) ||
|
|
|
|
(dstGen && i.number == *dstGen))
|
|
|
|
dst = i;
|
|
|
|
|
|
|
|
if (!dst) {
|
|
|
|
if (dstGen)
|
|
|
|
throw Error("generation %1% does not exist", *dstGen);
|
|
|
|
else
|
|
|
|
throw Error("no generation older than the current (%1%) exists", curGen.value_or(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
notice("switching from generation %d to %d", curGen.value_or(0), dst->number);
|
|
|
|
|
|
|
|
if (dryRun) return;
|
|
|
|
|
|
|
|
switchLink(profile, dst->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 15:08:58 +00:00
|
|
|
void lockProfile(PathLocks & lock, const Path & profile)
|
|
|
|
{
|
2017-07-30 11:27:57 +00:00
|
|
|
lock.lockPaths({profile}, (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) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-24 13:26:13 +00:00
|
|
|
Path getDefaultProfile()
|
|
|
|
{
|
|
|
|
Path profileLink = getHome() + "/.nix-profile";
|
|
|
|
try {
|
|
|
|
if (!pathExists(profileLink)) {
|
|
|
|
replaceSymlink(
|
|
|
|
getuid() == 0
|
|
|
|
? settings.nixStateDir + "/profiles/default"
|
|
|
|
: fmt("%s/profiles/per-user/%s/profile", settings.nixStateDir, getUserName()),
|
|
|
|
profileLink);
|
|
|
|
}
|
|
|
|
return absPath(readLink(profileLink), dirOf(profileLink));
|
|
|
|
} catch (Error &) {
|
|
|
|
return profileLink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|