ACQ2XX_API
WorkingDir.cpp
Go to the documentation of this file.
00001 /*
00002  * WorkingDir.cpp
00003  *
00004  *  Created on: Feb 5, 2011
00005  *      Author: pgm
00006  */
00007 
00008 #include "local.h"
00009 
00010 #include <string>
00011 
00012 #include <stdlib.h>
00013 #include <libgen.h>     /* dirname() */
00014 #include <unistd.h>
00015 #include <errno.h>
00016 #include <fcntl.h>
00017 #include <sys/stat.h>
00018 
00019 #include "WorkingDir.h"
00020 
00021 using namespace std;
00022 
00023 static char *__mymkdir(const char *arg1, const char* suffix)
00024 {
00025         char* dir = new char[256];
00026         char tmp[256];
00027 
00028         dir[0] = '\0';
00029 
00030 
00031         if (WorkingDir::outbase){
00032                 strcpy(dir, WorkingDir::outbase);
00033         }
00034         if (arg1 != 0){
00035                 if (WorkingDir::outbase){
00036                         strcpy(tmp, arg1);
00037                         strcat(dir, basename(tmp));
00038                 }else{
00039                         strcpy(dir, arg1);
00040                 }
00041         }
00042         strcat(dir, suffix);
00043         if (mkdir(dir, 0777) == 0){
00044                 return dir;
00045         }else if (errno == EEXIST){
00046                 if (WorkingDir::cleanup){
00047                         string s = "rm -Rf ";
00048                         s += dir;
00049                         s += "/*";
00050                         system(s.c_str());
00051                 }
00052                 return dir;
00053         }else{
00054                 sprintf(tmp, "mkdir %s failed", dir);
00055                 exit(-errno);
00056         }
00057 }
00058 
00059 static void rmrf(const char* path)
00060 /* BEWARE: this could easily trash your system */
00061 {
00062         const char* FMT = "rm -Rf %s";
00063         char *cmd = new char[strlen(path)+strlen(FMT)];
00064 
00065         sprintf(cmd, FMT, path);
00066         system(cmd);
00067 }
00068 
00069 #define COOKING ".COOKING"
00070 #define COOKED  ".COOKED"
00071 
00072 
00073 
00074 WorkingDir::WorkingDir(const char *arg1) :
00075                                 wd(__mymkdir(arg1, COOKING))
00076 {}
00077 WorkingDir::WorkingDir(const char* arg1, bool absolute){
00078         wd = new char [strlen(arg1)+strlen(COOKING)+1];
00079         sprintf(wd, "%s%s", arg1, COOKING);
00080         if (mkdir(wd, 0777)){
00081                 err("failed to create wd \"%s\"", wd);
00082                 exit(errno);
00083         }
00084 }
00085 
00086 WorkingDir::~WorkingDir() {
00087         char cname[128];
00088 
00089         char *idx = strstr(wd, COOKING);
00090         if (!idx){
00091                 err("FAILED to find %s suffix", COOKING);
00092         }else{
00093                 int rootlen = idx - wd;
00094                 strncpy(cname, wd, rootlen);
00095                 cname[rootlen] = '\0';
00096                 strcat(cname, COOKED);
00097                 rmrf(cname);
00098                 rename(wd, cname);
00099         }
00100         delete [] wd;
00101 }
00102 
00103 const char* WorkingDir::outbase = 0;
00104 bool WorkingDir::cleanup = 0;