Browse Source

Change Utils::Fs::freeDiskSpaceOnPath() signature

refactor the function internals
adaptive-webui-19844
Chocobo1 8 years ago
parent
commit
5cf86d3677
  1. 61
      src/base/utils/fs.cpp
  2. 2
      src/base/utils/fs.h

61
src/base/utils/fs.cpp

@ -269,50 +269,43 @@ bool Utils::Fs::isValidFileSystemName(const QString &name, bool allowSeparators)
return !name.contains(regex); return !name.contains(regex);
} }
qlonglong Utils::Fs::freeDiskSpaceOnPath(QString path) qulonglong Utils::Fs::freeDiskSpaceOnPath(const QString &path)
{ {
if (path.isEmpty()) return -1; if (path.isEmpty()) return 0;
QDir dir_path(path);
if (!dir_path.exists()) { QDir dirPath(path);
if (!dirPath.exists()) {
QStringList parts = path.split("/"); QStringList parts = path.split("/");
while (parts.size() > 1 && !QDir(parts.join("/")).exists()) { while (parts.size() > 1 && !QDir(parts.join("/")).exists())
parts.removeLast(); parts.removeLast();
}
dir_path = QDir(parts.join("/")); dirPath = QDir(parts.join("/"));
if (!dir_path.exists()) return -1; if (!dirPath.exists()) return 0;
} }
Q_ASSERT(dir_path.exists()); Q_ASSERT(dirPath.exists());
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
ULARGE_INTEGER bytesFree; ULARGE_INTEGER bytesFree;
if (GetDiskFreeSpaceExW((LPCTSTR)(toNativePath(dir_path.path())).utf16(), &bytesFree, NULL, NULL) == 0) LPCWSTR nativePath = reinterpret_cast<LPCWSTR>((toNativePath(dirPath.path())).utf16());
return -1; if (GetDiskFreeSpaceExW(nativePath, &bytesFree, NULL, NULL) == 0)
return qlonglong(bytesFree.QuadPart); return 0;
return bytesFree.QuadPart;
#elif defined(Q_OS_HAIKU) #elif defined(Q_OS_HAIKU)
unsigned long long available; const QString statfsPath = dirPath.path() + "/.";
const QString statfs_path = dir_path.path() + "/."; dev_t device = dev_for_path(qPrintable(statfsPath));
dev_t device = dev_for_path (qPrintable(statfs_path)); if (device < 0)
if (device >= 0) { return 0;
fs_info info; fs_info info;
if (fs_stat_dev(device, &info) == B_OK) { if (fs_stat_dev(device, &info) != B_OK)
available = ((unsigned long long)(info.free_blocks * info.block_size)); return 0;
return available; return ((qulonglong) info.free_blocks * (qulonglong) info.block_size);
}
}
return -1;
#else #else
unsigned long long available;
struct statfs stats; struct statfs stats;
const QString statfs_path = dir_path.path() + "/."; const QString statfsPath = dirPath.path() + "/.";
const int ret = statfs(qPrintable(statfs_path), &stats); const int ret = statfs(qPrintable(statfsPath), &stats);
if (ret == 0) { if (ret != 0)
available = ((unsigned long long)stats.f_bavail) return 0;
* ((unsigned long long)stats.f_bsize); return ((qulonglong) stats.f_bavail * (qulonglong) stats.f_bsize);
return available;
}
else {
return -1;
}
#endif #endif
} }

2
src/base/utils/fs.h

@ -50,7 +50,7 @@ namespace Utils
bool sameFiles(const QString& path1, const QString& path2); bool sameFiles(const QString& path1, const QString& path2);
QString toValidFileSystemName(const QString &name, bool allowSeparators = false); QString toValidFileSystemName(const QString &name, bool allowSeparators = false);
bool isValidFileSystemName(const QString& name, bool allowSeparators = false); bool isValidFileSystemName(const QString& name, bool allowSeparators = false);
qlonglong freeDiskSpaceOnPath(QString path); qulonglong 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