2012-07-18 21:59:03 +03:00
|
|
|
|
#pragma once
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* @file Implementation of Profiles.
|
|
|
|
|
*
|
|
|
|
|
* See the manual for additional information.
|
|
|
|
|
*/
|
2004-02-06 16:57:10 +02:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
#include "types.hh"
|
2010-04-21 18:08:58 +03:00
|
|
|
|
#include "pathlocks.hh"
|
2004-02-06 16:57:10 +02:00
|
|
|
|
|
2006-09-05 13:32:47 +03:00
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
2004-02-06 16:57:10 +02:00
|
|
|
|
|
2020-09-03 12:06:56 +03:00
|
|
|
|
class StorePath;
|
|
|
|
|
|
2004-02-06 16:57:10 +02:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* A positive number identifying a generation for a given profile.
|
|
|
|
|
*
|
|
|
|
|
* Generation numbers are assigned sequentially. Each new generation is
|
|
|
|
|
* assigned 1 + the current highest generation number.
|
|
|
|
|
*/
|
2021-09-14 20:05:28 +03:00
|
|
|
|
typedef uint64_t GenerationNumber;
|
2020-07-16 16:14:22 +03:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* A generation is a revision of a profile.
|
|
|
|
|
*
|
|
|
|
|
* Each generation is a mapping (key-value pair) from an identifier
|
|
|
|
|
* (`number`) to a store object (specified by `path`).
|
|
|
|
|
*/
|
2004-02-06 18:03:27 +02:00
|
|
|
|
struct Generation
|
|
|
|
|
{
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* The number of a generation is its unique identifier within the
|
|
|
|
|
* profile.
|
|
|
|
|
*/
|
2020-07-16 16:14:22 +03:00
|
|
|
|
GenerationNumber number;
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* The store path identifies the store object that is the contents
|
|
|
|
|
* of the generation.
|
|
|
|
|
*
|
|
|
|
|
* These store paths / objects are not unique to the generation
|
|
|
|
|
* within a profile. Nix tries to ensure successive generations have
|
|
|
|
|
* distinct contents to avoid bloat, but nothing stops two
|
|
|
|
|
* non-adjacent generations from having the same contents.
|
|
|
|
|
*
|
|
|
|
|
* @todo Use `StorePath` instead of `Path`?
|
|
|
|
|
*/
|
2004-02-06 18:03:27 +02:00
|
|
|
|
Path path;
|
2023-06-19 07:04:59 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When the generation was created. This is extra metadata about the
|
|
|
|
|
* generation used to make garbage collecting old generations more
|
|
|
|
|
* convenient.
|
|
|
|
|
*/
|
2004-02-06 18:03:27 +02:00
|
|
|
|
time_t creationTime;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* All the generations of a profile
|
|
|
|
|
*/
|
2020-07-16 16:14:22 +03:00
|
|
|
|
typedef std::list<Generation> Generations;
|
2004-02-06 18:03:27 +02:00
|
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
|
/**
|
2023-06-19 07:04:59 +03:00
|
|
|
|
* Find all generations for the given profile.
|
|
|
|
|
*
|
|
|
|
|
* @param profile A profile specified by its name and location combined
|
|
|
|
|
* into a path. E.g. if "foo" is the name of the profile, and "/bar/baz"
|
|
|
|
|
* is the directory it is in, then the path "/bar/baz/foo" would be the
|
|
|
|
|
* argument for this parameter.
|
|
|
|
|
*
|
|
|
|
|
* @return The pair of:
|
|
|
|
|
*
|
|
|
|
|
* - The list of currently present generations for the specified profile,
|
|
|
|
|
* sorted by ascending generation number.
|
|
|
|
|
*
|
|
|
|
|
* - The number of the current/active generation.
|
|
|
|
|
*
|
|
|
|
|
* Note that the current/active generation need not be the latest one.
|
2023-04-07 16:55:28 +03:00
|
|
|
|
*/
|
2020-07-16 16:14:22 +03:00
|
|
|
|
std::pair<Generations, std::optional<GenerationNumber>> findGenerations(Path profile);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
|
2016-06-02 14:33:49 +03:00
|
|
|
|
class LocalFSStore;
|
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 15:28:26 +02:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Create a new generation of the given profile
|
|
|
|
|
*
|
|
|
|
|
* If the previous generation (not the currently active one!) has a
|
|
|
|
|
* distinct store object, a fresh generation number is mapped to the
|
|
|
|
|
* given store object, referenced by path. Otherwise, the previous
|
|
|
|
|
* generation is assumed.
|
|
|
|
|
*
|
|
|
|
|
* The behavior of reusing existing generations like this makes this
|
|
|
|
|
* procedure idempotent. It also avoids clutter.
|
|
|
|
|
*/
|
|
|
|
|
Path createGeneration(LocalFSStore & store, Path profile, StorePath outPath);
|
2004-02-06 16:57:10 +02:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Unconditionally delete a generation
|
|
|
|
|
*
|
|
|
|
|
* @param profile A profile specified by its name and location combined into a path.
|
|
|
|
|
*
|
|
|
|
|
* @param gen The generation number specifying exactly which generation
|
|
|
|
|
* to delete.
|
|
|
|
|
*
|
|
|
|
|
* Because there is no check of whether the generation to delete is
|
|
|
|
|
* active, this is somewhat unsafe.
|
|
|
|
|
*
|
|
|
|
|
* @todo Should we expose this at all?
|
|
|
|
|
*/
|
2020-07-16 16:14:22 +03:00
|
|
|
|
void deleteGeneration(const Path & profile, GenerationNumber gen);
|
2004-09-10 16:32:08 +03:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Delete the given set of generations.
|
|
|
|
|
*
|
|
|
|
|
* @param profile The profile, specified by its name and location combined into a path, whose generations we want to delete.
|
|
|
|
|
*
|
|
|
|
|
* @param gensToDelete The generations to delete, specified by a set of
|
|
|
|
|
* numbers.
|
|
|
|
|
*
|
|
|
|
|
* @param dryRun Log what would be deleted instead of actually doing
|
|
|
|
|
* so.
|
|
|
|
|
*
|
|
|
|
|
* Trying to delete the currently active generation will fail, and cause
|
|
|
|
|
* no generations to be deleted.
|
|
|
|
|
*/
|
2020-07-16 16:14:22 +03:00
|
|
|
|
void deleteGenerations(const Path & profile, const std::set<GenerationNumber> & gensToDelete, bool dryRun);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Delete generations older than `max` passed the current generation.
|
|
|
|
|
*
|
|
|
|
|
* @param profile The profile, specified by its name and location combined into a path, whose generations we want to delete.
|
|
|
|
|
*
|
|
|
|
|
* @param max How many generations to keep up to the current one. Must
|
|
|
|
|
* be at least 1 so we don't delete the current one.
|
|
|
|
|
*
|
|
|
|
|
* @param dryRun Log what would be deleted instead of actually doing
|
|
|
|
|
* so.
|
|
|
|
|
*/
|
2020-07-16 16:14:22 +03:00
|
|
|
|
void deleteGenerationsGreaterThan(const Path & profile, GenerationNumber max, bool dryRun);
|
2016-01-07 03:15:19 +02:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Delete all generations other than the current one
|
|
|
|
|
*
|
|
|
|
|
* @param profile The profile, specified by its name and location combined into a path, whose generations we want to delete.
|
|
|
|
|
*
|
|
|
|
|
* @param dryRun Log what would be deleted instead of actually doing
|
|
|
|
|
* so.
|
|
|
|
|
*/
|
2015-05-21 17:26:03 +03:00
|
|
|
|
void deleteOldGenerations(const Path & profile, bool dryRun);
|
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Delete generations older than `t`, except for the most recent one
|
|
|
|
|
* older than `t`.
|
|
|
|
|
*
|
|
|
|
|
* @param profile The profile, specified by its name and location combined into a path, whose generations we want to delete.
|
|
|
|
|
*
|
|
|
|
|
* @param dryRun Log what would be deleted instead of actually doing
|
|
|
|
|
* so.
|
|
|
|
|
*/
|
2015-05-21 17:26:03 +03:00
|
|
|
|
void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun);
|
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Parse a temp spec intended for `deleteGenerationsOlderThan()`.
|
|
|
|
|
*
|
|
|
|
|
* Throws an exception if `timeSpec` fails to parse.
|
|
|
|
|
*/
|
|
|
|
|
time_t parseOlderThanTimeSpec(std::string_view timeSpec);
|
2015-05-21 17:26:03 +03:00
|
|
|
|
|
2023-06-19 07:04:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Smaller wrapper around `replaceSymlink` for replacing the current
|
|
|
|
|
* generation of a profile. Does not enforce proper structure.
|
|
|
|
|
*
|
|
|
|
|
* @todo Always use `switchGeneration()` instead, and delete this.
|
|
|
|
|
*/
|
2004-02-06 16:57:10 +02:00
|
|
|
|
void switchLink(Path link, Path target);
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* Roll back a profile to the specified generation, or to the most
|
|
|
|
|
* recent one older than the current.
|
|
|
|
|
*/
|
2021-09-14 20:05:28 +03:00
|
|
|
|
void switchGeneration(
|
|
|
|
|
const Path & profile,
|
|
|
|
|
std::optional<GenerationNumber> dstGen,
|
|
|
|
|
bool dryRun);
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* Ensure exclusive access to a profile. Any command that modifies
|
|
|
|
|
* the profile first acquires this lock.
|
|
|
|
|
*/
|
2010-04-21 18:08:58 +03:00
|
|
|
|
void lockProfile(PathLocks & lock, const Path & profile);
|
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* Optimistic locking is used by long-running operations like `nix-env
|
|
|
|
|
* -i'. Instead of acquiring the exclusive lock for the entire
|
|
|
|
|
* duration of the operation, we just perform the operation
|
|
|
|
|
* optimistically (without an exclusive lock), and check at the end
|
|
|
|
|
* whether the profile changed while we were busy (i.e., the symlink
|
|
|
|
|
* target changed). If so, the operation is restarted. Restarting is
|
|
|
|
|
* generally cheap, since the build results are still in the Nix
|
|
|
|
|
* store. Most of the time, only the user environment has to be
|
|
|
|
|
* rebuilt.
|
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
|
std::string optimisticLockProfile(const Path & profile);
|
2004-02-06 16:57:10 +02:00
|
|
|
|
|
2023-03-27 11:02:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Create and return the path to a directory suitable for storing the user’s
|
|
|
|
|
* profiles.
|
|
|
|
|
*/
|
2022-04-13 11:26:50 +03:00
|
|
|
|
Path profilesDir();
|
|
|
|
|
|
2023-03-27 11:02:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Return the path to the profile directory for root (but don't try creating it)
|
|
|
|
|
*/
|
2023-03-21 14:37:19 +02:00
|
|
|
|
Path rootProfilesDir();
|
|
|
|
|
|
2023-03-27 11:02:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Create and return the path to the file used for storing the users's channels
|
|
|
|
|
*/
|
2023-03-21 14:37:19 +02:00
|
|
|
|
Path defaultChannelsDir();
|
|
|
|
|
|
2023-03-27 11:02:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Return the path to the channel directory for root (but don't try creating it)
|
|
|
|
|
*/
|
2023-03-21 14:37:19 +02:00
|
|
|
|
Path rootChannelsDir();
|
|
|
|
|
|
2023-03-27 11:02:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Resolve the default profile (~/.nix-profile by default,
|
|
|
|
|
* $XDG_STATE_HOME/nix/profile if XDG Base Directory Support is enabled),
|
|
|
|
|
* and create if doesn't exist
|
|
|
|
|
*/
|
2020-03-24 15:26:13 +02:00
|
|
|
|
Path getDefaultProfile();
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
|
}
|