Browse Source

Merge pull request #6785 from KingLucius/freespaceondisk

Get disk free space size via QStorageInfo instead of native API. Closes #6780
adaptive-webui-19844
Eugene Shalygin 8 years ago committed by GitHub
parent
commit
4d53d8d1ba
  1. 40
      src/base/utils/fs.cpp
  2. 2
      src/base/utils/fs.h

40
src/base/utils/fs.cpp

@ -36,6 +36,7 @@ @@ -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) @@ -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)

2
src/base/utils/fs.h

@ -51,7 +51,7 @@ namespace Utils @@ -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);

Loading…
Cancel
Save