HxUtils/HxDisk.cpp

114 lines
2.2 KiB
C++
Raw Normal View History

2024-01-11 16:14:21 +08:00
#include "HxDisk.h"
void HxDisk::mkpath(QString path)
{
QDir dir;
if (!dir.exists(path))
dir.mkpath(path);
}
void HxDisk::mkpath(QStringList names)
{
foreach (QString name, names) {
HxDisk::mkpath(name);
}
}
bool HxDisk::exist(QString rootPath)
{
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes())
{
if (rootPath == storage.rootPath())
return true;
}
return false;
}
bool HxDisk::empty(QString path)
{
return QDir(path).removeRecursively();
}
QList<QStorageInfo> HxDisk::mounted_volumes()
{
QList<QStorageInfo> storageinfos;
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes())
{
if (storage.isValid() &&
storage.isReady() &&
!storage.rootPath().isNull() &&
!storage.isReadOnly())
{
#ifndef Q_OS_WIN32
if (storage.rootPath() == "/")
continue;
if (storage.fileSystemType() != "ext4")
continue;
#endif
storageinfos.append(storage);
}
}
return storageinfos;
}
double HxDisk::total_size(QString rootPath)
{
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes())
{
if (rootPath == storage.rootPath())
return static_cast<double>(storage.bytesTotal()) / 1024 / 1024 / 1024;
}
return 0;
}
double HxDisk::free_size(QString rootPath)
{
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes())
{
if (rootPath == storage.rootPath())
return static_cast<double>(storage.bytesAvailable()) / 1024 / 1024 / 1024;
}
return 0;
}
int HxDisk::entry_info_list(QString path)
{
QDir dir(path);
if (!dir.exists())
return 0;
/* 设置过滤器 */
dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
return dir.entryInfoList().count();
}
void HxDisk::remove(QString filepath)
{
QFileInfo fileinfo(filepath);
if (!fileinfo.exists())
return;
QFile::remove(fileinfo.filePath());
QDir dir(fileinfo.path());
while (entry_info_list(dir.path()) == 0)
{
dir.rmdir(dir.path());
/* 返回上一级目录 */
if (!dir.cdUp())
return;
}
}