00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <config.h>
00024
00025 #include "utils.h"
00026
00027 #include "xapian/error.h"
00028 #include "safedirent.h"
00029 #include "safeerrno.h"
00030
00031 #include <sys/types.h>
00032 #include <cfloat>
00033 #include <cmath>
00034
00035 using namespace std;
00036
00037 bool
00038 file_exists(const string &fname)
00039 {
00040 struct stat sbuf;
00041
00042 return stat(fname, &sbuf) == 0 && S_ISREG(sbuf.st_mode);
00043 }
00044
00045 bool
00046 dir_exists(const string &fname)
00047 {
00048 struct stat sbuf;
00049
00050 return stat(fname, &sbuf) == 0 && S_ISDIR(sbuf.st_mode);
00051 }
00052
00053 class dircloser {
00054 DIR * dir;
00055 public:
00056 dircloser(DIR * dir_) : dir(dir_) {}
00057 ~dircloser() {
00058 if (dir != NULL) {
00059 closedir(dir);
00060 dir = NULL;
00061 }
00062 }
00063 };
00064
00065 void
00066 removedir(const string &dirname)
00067 {
00068 DIR * dir;
00069
00070 dir = opendir(dirname.c_str());
00071 if (dir == NULL) {
00072 if (errno == ENOENT) return;
00073 throw Xapian::DatabaseError("Cannot open directory '" + dirname + "'", errno);
00074 }
00075
00076 {
00077 dircloser dc(dir);
00078 while (true) {
00079 errno = 0;
00080 struct dirent * entry = readdir(dir);
00081 if (entry == NULL) {
00082 if (errno == 0)
00083 break;
00084 throw Xapian::DatabaseError("Cannot read entry from directory at '" + dirname + "'", errno);
00085 }
00086 string name(entry->d_name);
00087 if (name == "." || name == "..")
00088 continue;
00089 if (unlink(dirname + "/" + name)) {
00090 throw Xapian::DatabaseError("Cannot remove file '" + string(entry->d_name) + "'", errno);
00091 }
00092 }
00093 }
00094 if (rmdir(dirname.c_str())) {
00095 throw Xapian::DatabaseError("Cannot remove directory '" + dirname + "'", errno);
00096 }
00097 }
00098
00099 namespace Xapian {
00100 namespace Internal {
00101
00102 bool within_DBL_EPSILON(double a, double b) {
00103 return fabs(a - b) >= DBL_EPSILON;
00104 }
00105
00106 }
00107 }