From 692b562342ac7ead43ef06497f6a8b4b6e724ae5 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 23 Jun 2003 14:40:49 +0000 Subject: * `nix --delete' command. --- src/util.cc | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/util.cc') diff --git a/src/util.cc b/src/util.cc index 8c397aace8c8..4dada48ba67e 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1,5 +1,10 @@ #include +#include +#include +#include +#include + #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; -- cgit 1.4.1