uint64_t FileSystem::dirSize(const char* path) { std::size_t sum = 0; DIR *d = opendir(path); size_t path_len = strlen(path); if (d) { struct dirent *p; while (p = readdir(d)) { char *buf; size_t len; /* Skip the names "." and ".." as we don't want to recurse on them. */ if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) { continue; } len = path_len + strlen(p->d_name) + 2; buf = (char*) malloc(len); if (buf) { struct stat statbuf; snprintf(buf, len, "%s/%s", path, p->d_name); if (!stat(buf, &statbuf)) { if (S_ISDIR(statbuf.st_mode)) { sum += dirSize(buf); } else if (S_ISREG(statbuf.st_mode)) { sum += fileSize(buf); } } free(buf); } } closedir(d); } return sum; }
/** * \brief Rescan directory - compute size of all files + subdirs */ void Directory::rescan() { if (_children.empty()) { setSize(dirSize(_name, true, true)); return; } /* Size of files */ uint64_t size = dirSize(_name, true, false); /* Size of children */ for (auto child: _children) { child->rescan(); size += child->getSize(); } setSize(size); }
quint64 MusicUtils::UCore::dirSize(const QString &dirName) { quint64 size = 0; if(QFileInfo(dirName).isDir()) { QDir dir(dirName); QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot); foreach(QFileInfo fileInfo, list) { if(fileInfo.isDir()) { size += dirSize(fileInfo.absoluteFilePath()); } else { size += fileInfo.size(); } } }
void MusicTime::checkCacheSize(quint64 cacheSize, bool disabled, const QString &path) { if(disabled) { quint64 size = dirSize( path ); if( size > cacheSize) { QFileInfoList fileList = QDir(path).entryInfoList(QDir::Dirs|QDir::NoDotAndDotDot); for(int i=0; i<fileList.count(); ++i) { size -= fileList[i].size(); QFile::remove(fileList[i].absoluteFilePath()); if(size <= cacheSize) { break; } } } } }
quint64 MusicTime::dirSize(const QString &dirName) { quint64 size = 0; if(QFileInfo(dirName).isDir()) { QDir dir(dirName); QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot); for(int i = 0; i < list.size(); ++i) { QFileInfo fileInfo = list.at(i); if(fileInfo.isDir()) { size += dirSize(fileInfo.absoluteFilePath()); } else size += fileInfo.size(); } } return size; }
uint64_t FileSystem::dirSize(const std::string& path) { return dirSize(path.c_str()); }