mirror of
https://github.com/NixOS/nix
synced 2025-06-30 19:57:59 +02:00
* `nix --delete' command.
This commit is contained in:
parent
c0cbaef4be
commit
692b562342
6 changed files with 66 additions and 2 deletions
29
src/util.cc
29
src/util.cc
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue