#include #include "dirent.h" #include #include #include "file.h" void File::_getDetails() { struct stat statData; if (this->name.length() < 5) { this->does_exist = true; this->size = 0; this->is_dir = true; this->is_writable = true; } else { if (stat (this->name.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->name.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->name; 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) { this->name = filename; this->_getDetails(); if (this->is_dir) { this->getChildren(); } } File::File(string filename, unsigned long filesize) { this->name = 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::getFileName() { return this->name.c_str(); } unsigned long File::getRawFileSize() { return this->size; } unsigned long File::getFileSize() { 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->getFileSize(); here ++; } } totalSize += this->size; return totalSize; } 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; } void File::copy(char *to) { }