00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #if defined(__digital__)
00027 #include <sys/mode.h>
00028 #endif
00029 #include <dirent.h>
00030 #include <Basics.H>
00031 #include <FS.H>
00032 #include <AtomicFile.H>
00033 #include "StatError.H"
00034 #include "StatDir.H"
00035
00036 using std::cout;
00037 using std::endl;
00038
00039 int DirObj::Search(int verbose, Stat::Collection &stats)
00040 throw (StatError::UnevenLevels,
00041 StatError::BadMPKFile, StatError::EndOfFile,
00042 FS::Failure, FS::DoesNotExist)
00043 {
00044 if (verbose >= 1) {
00045 cout << " " << this->path << endl;
00046 }
00047
00048
00049 int chLevel = -1;
00050 int cnt;
00051 {
00052 DirEntry *entry = (DirEntry *) NULL;
00053 DirIter it(this);
00054 for (cnt = 0; it.Next( entry); cnt++) {
00055 int l = entry->Search(verbose, stats);
00056 if (chLevel < 0) chLevel = l;
00057 else if (chLevel != l) throw (StatError::UnevenLevels(this->path));
00058 }
00059 entry = (DirEntry *) NULL;
00060 }
00061
00062
00063 int thisLevel = chLevel + 1;
00064 StatCount *sc;
00065 if (stats.fanout.size() <= thisLevel) {
00066 assert(stats.fanout.size() == thisLevel);
00067 sc = NEW(StatCount);
00068 stats.fanout.addhi(sc);
00069 } else {
00070 sc = stats.fanout.get(thisLevel);
00071 }
00072 sc->AddVal(cnt, NEW_CONSTR(Stat::Location, (this->path)));
00073 return thisLevel;
00074 }
00075
00076 DirIter::DirIter(const DirObj *dirObj) throw (FS::Failure, FS::DoesNotExist)
00077 : path(dirObj->path), done(false)
00078 {
00079 if ((this->dir = opendir(this->path.cchars())) == NULL) {
00080 if (errno == ENOENT) throw FS::DoesNotExist();
00081 else throw FS::Failure(Text("opendir"), dirObj->path);
00082 }
00083 this->path += '/';
00084 }
00085
00086 static bool IsDotDir(char *arc) throw ()
00087 {
00088 return (!strcmp(arc, ".") || !strcmp(arc, ".."));
00089 }
00090
00091 static bool IsTempFile(const Text &path) throw ()
00092
00093 {
00094 int slashIx = path.FindCharR('/');
00095 int semiIx = path.FindChar(AtomicFile::reserved_char, slashIx);
00096 if (semiIx <= 0) return false;
00097 for (int i = slashIx + 1; i < semiIx; i++) {
00098 if (!isxdigit(path[i])) return false;
00099 }
00100 return true;
00101 }
00102
00103 bool DirIter::Next( DirEntry* &entry)
00104 throw (StatError::EndOfFile, FS::Failure, FS::DoesNotExist)
00105 {
00106 if (done) return false;
00107 while (true) {
00108
00109 struct dirent *sysDirEnt;
00110 if ((sysDirEnt = readdir(this->dir)) == (struct dirent *)NULL) {
00111 done = true;
00112 if (closedir(this->dir) < 0) {
00113 throw FS::Failure(Text("closedir"), this->path);
00114 }
00115 return false;
00116 }
00117
00118
00119 if (IsDotDir(sysDirEnt->d_name)) continue;
00120
00121
00122 Text fullname(this->path + sysDirEnt->d_name);
00123 struct stat statBuff;
00124 DirEntry::FSStat(fullname, &statBuff);
00125 if (S_ISREG(statBuff.st_mode) && IsTempFile(fullname)) {
00126 continue;
00127 }
00128
00129
00130 entry = DirEntry::Open(fullname, &statBuff);
00131 break;
00132 }
00133 return true;
00134 }