#include #include "dirent.h" #include // #include #include "file.h" #include void File::_getDetails() { struct stat statData; if (this->path.length() < 5) { this->does_exist = true; this->size = 0; this->is_dir = true; this->is_writable = true; } else { if (stat (this->path.c_str(), &statData) == -1) { this->does_exist = false; this->size = 0; this->is_dir = false; this->is_writable = false; } else { this->does_exist = (statData.st_mode & (S_IFDIR | S_IFREG)) != 0; this->is_dir = ((statData.st_mode & S_IFDIR) != 0); this->size = statData.st_size; this->is_writable = ((statData.st_mode & S_IWRITE) != 0); } } } void File::getChildren() { DIR * directory; struct dirent * entry; directory = opendir(this->path.c_str()); if (directory == NULL) { return; } while ((entry = readdir(directory)) != NULL) { string childName; if (strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, ".") == 0) { continue; } childName = this->path; childName.append("\\"); childName.append(entry->d_name); this->children.push_back(*(new File(childName))); } closedir(directory); } File::File() { this->size = 0; } File::File(string filename) : FilePath(filename) { // this->path = filename; this->_getDetails(); if (this->is_dir) { this->getChildren(); } } /* File::File(string filename, unsigned long filesize) { this->path = filename; this->_getDetails(); if (this->is_dir) { this->getChildren(); } } */ unsigned long File::getDiskSpace(DiskSpace &diskSpace) { unsigned long totalSize = 0; if (this->is_dir) { vector::iterator here = this->children.begin(); vector::iterator end = this->children.end(); while (here != end) { totalSize += here->getDiskSpace(diskSpace); here ++; } } totalSize += diskSpace.fileSpace(this->size); return totalSize; } /* const char *File::cString() { return this->path.c_str(); } */ unsigned long File::getRawFileSize() { return this->size; } unsigned long File::getFileSize(){ unsigned long size, files; this->getFileSize(size, files); return size;} void File::getFileSize(unsigned long &totalSize, unsigned long &totalFiles) { totalSize = 0; totalFiles = 1; if (this->is_dir) { vector::iterator here = this->children.begin(); vector::iterator end = this->children.end(); while (here != end) { unsigned long ldata, lcount; here->getFileSize(ldata, lcount); totalSize += ldata; totalFiles += lcount; here ++; } } totalSize += this->size;} vector::iterator File::childBegin() { return this->children.begin(); } vector::iterator File::childEnd() { return this->children.end(); } bool File::isDirectory() { return this->is_dir; } bool File::isWriteable() { return this->is_writable; } bool File::exists() { return this->does_exist; } bool File::remove(){ bool ok = true; if (this->is_dir) { vector::iterator here = this->children.begin(); vector::iterator end = this->children.end(); while (here != end) { ok = here->remove() && ok; here ++; } ok = RemoveDirectory(this->path.c_str()) && ok; } else { ok = DeleteFile(this->path.c_str()) != 0 ? true : false; } return ok;} void File::copy(char *to) { }