cross-platform free space on disk

This commit is contained in:
KingLucius 2017-05-14 23:57:24 +02:00
parent a2d5d48aff
commit aea887a30f
2 changed files with 5 additions and 37 deletions

View file

@ -36,6 +36,7 @@
#include <QFileInfo>
#include <QDirIterator>
#include <QCoreApplication>
#include <QStorageInfo>
#ifndef Q_OS_WIN
#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD)
@ -234,44 +235,11 @@ bool Utils::Fs::isValidFileSystemName(const QString &name, bool allowSeparators)
return !name.contains(regex);
}
qulonglong Utils::Fs::freeDiskSpaceOnPath(const QString &path)
qint64 Utils::Fs::freeDiskSpaceOnPath(const QString &path)
{
if (path.isEmpty()) return 0;
if (path.isEmpty()) return -1;
QDir dirPath(path);
if (!dirPath.exists()) {
QStringList parts = path.split("/");
while (parts.size() > 1 && !QDir(parts.join("/")).exists())
parts.removeLast();
dirPath = QDir(parts.join("/"));
if (!dirPath.exists()) return 0;
}
Q_ASSERT(dirPath.exists());
#if defined(Q_OS_WIN)
ULARGE_INTEGER bytesFree;
LPCWSTR nativePath = reinterpret_cast<LPCWSTR>((toNativePath(dirPath.path())).utf16());
if (GetDiskFreeSpaceExW(nativePath, &bytesFree, NULL, NULL) == 0)
return 0;
return bytesFree.QuadPart;
#elif defined(Q_OS_HAIKU)
const QString statfsPath = dirPath.path() + "/.";
dev_t device = dev_for_path(qPrintable(statfsPath));
if (device < 0)
return 0;
fs_info info;
if (fs_stat_dev(device, &info) != B_OK)
return 0;
return ((qulonglong) info.free_blocks * (qulonglong) info.block_size);
#else
struct statfs stats;
const QString statfsPath = dirPath.path() + "/.";
const int ret = statfs(qPrintable(statfsPath), &stats);
if (ret != 0)
return 0;
return ((qulonglong) stats.f_bavail * (qulonglong) stats.f_bsize);
#endif
return QStorageInfo(path).bytesAvailable();
}
QString Utils::Fs::branchPath(const QString& file_path, QString* removed)

View file

@ -51,7 +51,7 @@ namespace Utils
QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString& name, bool allowSeparators = false);
qulonglong freeDiskSpaceOnPath(const QString &path);
qint64 freeDiskSpaceOnPath(const QString &path);
QString branchPath(const QString& file_path, QString* removed = 0);
bool sameFileNames(const QString& first, const QString& second);
QString expandPath(const QString& path);