1 /***************************************************************************
2 escape.cpp - escaping of strings
4 begin : Sun Nov 14 1999
5 copyright : (C) 1999 by Intra2net AG
6 email : info@intra2net.com
7 ***************************************************************************/
14 #include <sys/types.h>
22 #include <filefunc.hxx>
26 bool file_exists (const string &name)
30 if (stat(name.c_str(), &statbuff) < 0)
36 long fsize (const string &name)
42 if (lstat(name.c_str(), &statbuff) < 0)
45 if (!S_ISREG(statbuff.st_mode))
48 iReturn=statbuff.st_size;
53 string load_file(const string &name)
58 if (name.find ("..") != string::npos)
59 return ("can't load file (..): " + name);
61 f=::fopen(name.c_str(),"rb");
67 ::fseek(f,0,SEEK_END);
69 ::fseek(f,0,SEEK_SET);
70 char *c=new char[size+1];
79 bool chown(const char* file,const char* owner, const char* group)
96 if (!::chown(file,uid,gid))
103 * Recursive delete of files and directories
104 * @param path File or directory to delete
105 * @param error Will contain the error if the return value is false
106 * @return true on success, false otherwise
108 bool recursive_delete(const std::string &path, std::string *error)
114 if (stat(path.c_str(), &my_stat) != 0) {
115 throw runtime_error("can't stat " + path);
118 if (S_ISDIR(my_stat.st_mode)) {
119 DIR *dir = opendir(path.c_str());
121 throw runtime_error("can't open directory " + path);
124 struct dirent *entry;
125 while ((entry = readdir(dir))) {
126 string filename = entry->d_name;
127 if (filename == "." || filename == "..") {
131 // Delete subdir or file.
132 rtn = recursive_delete(path + "/" + filename, error);
139 if (rmdir(path.c_str()) != 0) {
140 throw runtime_error("can't remove directory " + path);
143 if (unlink(path.c_str()) != 0) {
144 throw runtime_error("can't unlink " + path);
147 } catch (exception &e) {
150 out << e.what() << " (" << strerror(errno) << ")";
157 out << "unknown error (" << strerror(errno) << ")";