* `nix --delete' command.

This commit is contained in:
Eelco Dolstra 2003-06-23 14:40:49 +00:00
parent c0cbaef4be
commit 692b562342
6 changed files with 66 additions and 2 deletions

View file

@ -112,8 +112,20 @@ static void opEvaluate(Strings opFlags, Strings opArgs)
static void opDelete(Strings opFlags, Strings opArgs) static void opDelete(Strings opFlags, Strings opArgs)
{ {
getArgType(opFlags); getArgType(opFlags);
if (!opFlags.empty()) throw UsageError("unknown flag");
cerr << "delete!\n"; for (Strings::iterator it = opArgs.begin();
it != opArgs.end(); it++)
{
Hash hash;
if (argType == atpHash)
hash = parseHash(*it);
else if (argType == atpName)
throw Error("not implemented");
else
throw Error("invalid argument type");
deleteValue(hash);
}
} }

View file

@ -68,7 +68,7 @@ void runTests()
#endif #endif
/* Restoring. */ /* Restoring. */
#if 1 #if 0
MySource source; MySource source;
restorePath("outdir", source); restorePath("outdir", source);
cout << (string) hashPath("outdir") << endl; cout << (string) hashPath("outdir") << endl;
@ -116,6 +116,8 @@ void runTests()
Expr e3 = ATmake("Deref(Hash(<str>))", ((string) h3).c_str()); Expr e3 = ATmake("Deref(Hash(<str>))", ((string) h3).c_str());
evalTest(e3); evalTest(e3);
deleteValue(h3);
} }

View file

@ -1,5 +1,10 @@
#include <iostream> #include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "util.hh" #include "util.hh"
@ -49,6 +54,30 @@ string baseNameOf(string path)
} }
void deletePath(string path)
{
struct stat st;
if (lstat(path.c_str(), &st))
throw SysError("getting attributes of path " + path);
if (S_ISDIR(st.st_mode)) {
DIR * dir = opendir(path.c_str());
struct dirent * dirent;
while (errno = 0, dirent = readdir(dir)) {
string name = dirent->d_name;
if (name == "." || name == "..") continue;
deletePath(path + "/" + name);
}
closedir(dir); /* !!! close on exception */
}
if (remove(path.c_str()) == -1)
throw SysError("cannot unlink " + path);
}
void debug(string s) void debug(string s)
{ {
cerr << "debug: " << s << endl; cerr << "debug: " << s << endl;

View file

@ -54,6 +54,11 @@ string dirOf(string path);
string baseNameOf(string path); string baseNameOf(string path);
/* Delete a path; i.e., in the case of a directory, it is deleted
recursively. Don't use this at home, kids. */
void deletePath(string path);
void debug(string s); void debug(string s);

View file

@ -135,6 +135,18 @@ string fetchURL(string url)
#endif #endif
void deleteValue(Hash hash)
{
string name;
if (queryDB(nixDB, dbRefs, hash, name)) {
string fn = absValuePath(name);
deletePath(fn);
delDB(nixDB, dbRefs, hash);
}
}
/* !!! bad name, "query" implies no side effect => getValuePath() */
string queryValuePath(Hash hash) string queryValuePath(Hash hash)
{ {
bool checkedNet = false; bool checkedNet = false;

View file

@ -13,6 +13,10 @@ using namespace std;
Hash addValue(string pathName); Hash addValue(string pathName);
/* Delete a value from the nixValues directory. */
void deleteValue(Hash hash);
/* Obtain the path of a value with the given hash. If a file with /* Obtain the path of a value with the given hash. If a file with
that hash is known to exist in the local file system (as indicated that hash is known to exist in the local file system (as indicated
by the dbRefs database), we use that. Otherwise, we attempt to by the dbRefs database), we use that. Otherwise, we attempt to