Browse Source

Merge pull request #6010 from Chocobo1/warnings

Fix memory leak
adaptive-webui-19844
sledgehammer999 8 years ago committed by GitHub
parent
commit
07edb7b74e
  1. 95
      src/base/utils/fs.cpp
  2. 2
      src/base/utils/fs.h

95
src/base/utils/fs.cpp

@ -269,67 +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("/"));
if (!dir_path.exists()) return -1;
}
Q_ASSERT(dir_path.exists());
#ifndef Q_OS_WIN dirPath = QDir(parts.join("/"));
unsigned long long available; if (!dirPath.exists()) return 0;
#ifdef Q_OS_HAIKU
const QString statfs_path = dir_path.path() + "/.";
dev_t device = dev_for_path (qPrintable(statfs_path));
if (device >= 0) {
fs_info info;
if (fs_stat_dev(device, &info) == B_OK) {
available = ((unsigned long long)(info.free_blocks * info.block_size));
return available;
}
} }
return -1; 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 #else
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
#else
typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR,
PULARGE_INTEGER,
PULARGE_INTEGER,
PULARGE_INTEGER);
GetDiskFreeSpaceEx_t pGetDiskFreeSpaceEx =
(GetDiskFreeSpaceEx_t)::GetProcAddress(::GetModuleHandleW(L"kernel32.dll"), "GetDiskFreeSpaceExW");
if (pGetDiskFreeSpaceEx) {
ULARGE_INTEGER bytesFree, bytesTotal;
unsigned long long *ret;
if (pGetDiskFreeSpaceEx((LPCTSTR)(toNativePath(dir_path.path())).utf16(), &bytesFree, &bytesTotal, NULL)) {
ret = (unsigned long long*)&bytesFree;
return *ret;
}
else {
return -1;
}
}
else {
return -1;
}
#endif #endif
} }
@ -378,19 +354,17 @@ QString Utils::Fs::expandPathAbs(const QString& path)
QString Utils::Fs::QDesktopServicesDataLocation() QString Utils::Fs::QDesktopServicesDataLocation()
{ {
QString result; QString result;
#ifdef Q_OS_WIN #if defined(Q_OS_WIN)
LPWSTR path=new WCHAR[256]; wchar_t path[MAX_PATH + 1] = {L'\0'};
if (SHGetSpecialFolderPath(0, path, CSIDL_LOCAL_APPDATA, FALSE)) if (SHGetSpecialFolderPathW(0, path, CSIDL_LOCAL_APPDATA, FALSE))
result = fromNativePath(QString::fromWCharArray(path)); result = fromNativePath(QString::fromWCharArray(path));
if (!QCoreApplication::applicationName().isEmpty()) if (!QCoreApplication::applicationName().isEmpty())
result += QLatin1String("/") + qApp->applicationName(); result += QLatin1String("/") + qApp->applicationName();
#else #elif defined(Q_OS_MAC)
#ifdef Q_OS_MAC
FSRef ref; FSRef ref;
OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref); OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref);
if (err) if (err)
return QString(); return QString();
QString path;
QByteArray ba(2048, 0); QByteArray ba(2048, 0);
if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr) if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
result = QString::fromUtf8(ba).normalized(QString::NormalizationForm_C); result = QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
@ -402,7 +376,6 @@ QString Utils::Fs::QDesktopServicesDataLocation()
xdgDataHome += QLatin1String("/data/") xdgDataHome += QLatin1String("/data/")
+ qApp->applicationName(); + qApp->applicationName();
result = xdgDataHome; result = xdgDataHome;
#endif
#endif #endif
if (!result.endsWith("/")) if (!result.endsWith("/"))
result += "/"; result += "/";

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