1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-30 19:57:59 +02:00

* `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

@ -1,5 +1,10 @@
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#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)
{
cerr << "debug: " << s << endl;