summaryrefslogtreecommitdiff
path: root/graphics/asymptote/util.cc
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/asymptote/util.cc')
-rw-r--r--graphics/asymptote/util.cc54
1 files changed, 54 insertions, 0 deletions
diff --git a/graphics/asymptote/util.cc b/graphics/asymptote/util.cc
index 2d66fb2316..e455ab940a 100644
--- a/graphics/asymptote/util.cc
+++ b/graphics/asymptote/util.cc
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <cstring>
#include <algorithm>
+#include <dirent.h>
#include "util.h"
#include "settings.h"
@@ -31,6 +32,7 @@
#include "interact.h"
using namespace settings;
+using camp::reportError;
bool False=false;
@@ -380,6 +382,58 @@ const char *setPath(const char *s, bool quiet)
return p;
}
+void fatal(const char *msg, const char *s=NULL)
+{
+ ostringstream buf;
+ buf << msg;
+ if(s) {
+ buf << " " << getPath();
+ if(*s) buf << "/";
+ buf << s;
+ }
+ buf << ": " << strerror(errno) << endl;
+ camp::reportError(buf);
+}
+
+void empty_current_dir()
+{
+ static struct stat buf;
+ DIR *dir=opendir(".");
+ if(dir == NULL) fatal("Cannot open directory","");
+ dirent *p;
+ while((p=readdir(dir)) != NULL) {
+ if(strcmp(p->d_name,".") == 0 || strcmp(p->d_name,"..") == 0) continue;
+ if(lstat(p->d_name,&buf) == 0) {
+ if(S_ISDIR(buf.st_mode)) {
+ if(chdir(p->d_name)) fatal("Cannot change directory to",p->d_name);
+ empty_current_dir();
+ if(chdir(".."))
+ fatal("Cannot change to parent directory of",p->d_name);
+ if(rmdir(p->d_name))
+ fatal("Cannot remove directory",p->d_name);
+ } else {
+ if(unlink(p->d_name)) fatal("Cannot remove file",p->d_name);
+ }
+ }
+ }
+ if(closedir(dir)) fatal("Invalid current directory stream descriptor");
+}
+
+void recursive_delete(char *name)
+{
+ static struct stat buf;
+ if(lstat(name,&buf) == 0) {
+ if(S_ISDIR(buf.st_mode)) {
+ const char *path=getPath();
+ if(chdir(name)) fatal("Cannot change directory to",name);
+ empty_current_dir();
+ if(chdir(path)) fatal("Cannot change to directory","");
+ if(rmdir(name)) fatal("Cannot remove directory", name);
+ }
+ else unlink(name);
+ }
+}
+
void push_command(mem::vector<string>& a, const string& s)
{
a.push_back(s);