From dcc433de47d4bf4a27fe63bc8996e946164ae885 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 10 Sep 2004 13:32:08 +0000 Subject: * Operation `--delete-generations' to delete generations of a profile. Arguments are either generation number, or `old' to delete all non-current generations. Typical use: $ nix-env --delete-generations old $ nix-collect-garbage * istringstream -> string2Int. --- src/nix-env/profiles.cc | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'src/nix-env/profiles.cc') diff --git a/src/nix-env/profiles.cc b/src/nix-env/profiles.cc index 1eeddf3326bd..c52ddc0b6988 100644 --- a/src/nix-env/profiles.cc +++ b/src/nix-env/profiles.cc @@ -20,9 +20,11 @@ static int parseName(const string & profileName, const string & name) string s = string(name, profileName.size() + 1); int p = s.find("-link"); if (p == string::npos) return -1; - istringstream str(string(s, 0, p)); - unsigned int n; - if (str >> n && str.eof()) return n; else return -1; + int n; + if (string2Int(string(s, 0, p), n) && n >= 0) + return n; + else + return -1; } @@ -59,6 +61,16 @@ Generations findGenerations(Path profile, int & curGen) } +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"; +} + + Path createGeneration(Path profile, Path outPath, Path drvPath, Path clrPath) { @@ -72,10 +84,7 @@ Path createGeneration(Path profile, Path outPath, Path generation, gcrootDrv, gcrootClr; while (1) { - Path prefix = (format("%1%-%2%") % profile % num).str(); - generation = prefix + "-link"; - gcrootDrv = prefix + "-drv.gcroot"; - gcrootClr = prefix + "-clr.gcroot"; + makeNames(profile, num, generation, gcrootDrv, gcrootClr); if (symlink(outPath.c_str(), generation.c_str()) == 0) break; if (errno != EEXIST) throw SysError(format("creating symlink `%1%'") % generation); @@ -90,6 +99,23 @@ Path createGeneration(Path profile, Path outPath, } +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); +} + + void switchLink(Path link, Path target) { /* Hacky. */ -- cgit 1.4.1