Browse Source

cross-platform free space on disk

adaptive-webui-19844
KingLucius 8 years ago
parent
commit
aea887a30f
  1. 40
      src/base/utils/fs.cpp
  2. 2
      src/base/utils/fs.h

40
src/base/utils/fs.cpp

@ -36,6 +36,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QDirIterator> #include <QDirIterator>
#include <QCoreApplication> #include <QCoreApplication>
#include <QStorageInfo>
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD) #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); 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); return QStorageInfo(path).bytesAvailable();
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
} }
QString Utils::Fs::branchPath(const QString& file_path, QString* removed) QString Utils::Fs::branchPath(const QString& file_path, QString* removed)

2
src/base/utils/fs.h

@ -51,7 +51,7 @@ namespace Utils
QString toValidFileSystemName(const QString &name, bool allowSeparators = false QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" ")); , const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString& name, bool allowSeparators = false); 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); QString branchPath(const QString& file_path, QString* removed = 0);
bool sameFileNames(const QString& first, const QString& second); bool sameFileNames(const QString& first, const QString& second);
QString expandPath(const QString& path); QString expandPath(const QString& path);

Loading…
Cancel
Save