Browse Source

Merge pull request #17412 from Chocobo1/stat

Improve free disk space reporting
adaptive-webui-19844
Chocobo1 2 years ago committed by GitHub
parent
commit
88556078be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/base/path.cpp
  2. 3
      src/base/path.h
  3. 32
      src/base/utils/fs.cpp
  4. 19
      src/gui/addnewtorrentdialog.cpp
  5. 2
      src/webui/webapplication.cpp

9
src/base/path.cpp

@ -218,6 +218,15 @@ QString Path::toString() const
return QDir::toNativeSeparators(m_pathStr); return QDir::toNativeSeparators(m_pathStr);
} }
std::filesystem::path Path::toStdFsPath() const
{
#ifdef Q_OS_WIN
return {data().toStdWString(), std::filesystem::path::format::generic_format};
#else
return {data().toStdString(), std::filesystem::path::format::generic_format};
#endif
}
Path &Path::operator/=(const Path &other) Path &Path::operator/=(const Path &other)
{ {
*this = *this / other; *this = *this / other;

3
src/base/path.h

@ -29,6 +29,8 @@
#pragma once #pragma once
#include <filesystem>
#include <QtGlobal> #include <QtGlobal>
#include <QMetaType> #include <QMetaType>
#include <QString> #include <QString>
@ -69,6 +71,7 @@ public:
QString data() const; QString data() const;
QString toString() const override; QString toString() const override;
std::filesystem::path toStdFsPath() const;
Path &operator/=(const Path &other); Path &operator/=(const Path &other);
Path &operator+=(QStringView str); Path &operator+=(QStringView str);

32
src/base/utils/fs.cpp

@ -31,6 +31,7 @@
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <filesystem>
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
#include <memory> #include <memory>
@ -57,8 +58,8 @@
#include <QDirIterator> #include <QDirIterator>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QStorageInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStorageInfo>
#include "base/global.h" #include "base/global.h"
#include "base/path.h" #include "base/path.h"
@ -161,15 +162,23 @@ qint64 Utils::Fs::computePathSize(const Path &path)
/** /**
* Makes deep comparison of two files to make sure they are identical. * Makes deep comparison of two files to make sure they are identical.
* The point is about the file contents. If the files do not exist then
* the paths refers to nothing and therefore we cannot say the files are same
* (because there are no files!)
*/ */
bool Utils::Fs::sameFiles(const Path &path1, const Path &path2) bool Utils::Fs::sameFiles(const Path &path1, const Path &path2)
{ {
QFile f1 {path1.data()}; QFile f1 {path1.data()};
QFile f2 {path2.data()}; QFile f2 {path2.data()};
if (!f1.exists() || !f2.exists()) return false;
if (f1.size() != f2.size()) return false; if (!f1.exists() || !f2.exists())
if (!f1.open(QIODevice::ReadOnly)) return false; return false;
if (!f2.open(QIODevice::ReadOnly)) return false; if (path1 == path2)
return true;
if (f1.size() != f2.size())
return false;
if (!f1.open(QIODevice::ReadOnly) || !f2.open(QIODevice::ReadOnly))
return false;
const int readSize = 1024 * 1024; // 1 MiB const int readSize = 1024 * 1024; // 1 MiB
while (!f1.atEnd() && !f2.atEnd()) while (!f1.atEnd() && !f2.atEnd())
@ -214,17 +223,8 @@ Path Utils::Fs::tempPath()
bool Utils::Fs::isRegularFile(const Path &path) bool Utils::Fs::isRegularFile(const Path &path)
{ {
struct ::stat st; std::error_code ec;
if (::stat(path.toString().toUtf8().constData(), &st) != 0) return std::filesystem::is_regular_file(path.toStdFsPath(), ec);
{
// analyse erno and log the error
const auto err = errno;
qDebug("Could not get file stats for path '%s'. Error: %s"
, qUtf8Printable(path.toString()), strerror(err));
return false;
}
return (st.st_mode & S_IFMT) == S_IFREG;
} }
bool Utils::Fs::isNetworkFileSystem(const Path &path) bool Utils::Fs::isNetworkFileSystem(const Path &path)

19
src/gui/addnewtorrentdialog.cpp

@ -135,6 +135,22 @@ namespace
return -1; return -1;
} }
qint64 queryFreeDiskSpace(const Path &path)
{
const Path root = path.rootItem();
Path current = path;
qint64 freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);
// for non-existent directories (which will be created on demand) `Utils::Fs::freeDiskSpaceOnPath`
// will return invalid value so instead query its parent/ancestor paths
while ((freeSpace < 0) && (current != root))
{
current = current.parentPath();
freeSpace = Utils::Fs::freeDiskSpaceOnPath(current);
}
return freeSpace;
}
void setPath(FileSystemPathComboEdit *fsPathEdit, const Path &newPath) void setPath(FileSystemPathComboEdit *fsPathEdit, const Path &newPath)
{ {
int existingIndex = indexOfPath(fsPathEdit, newPath); int existingIndex = indexOfPath(fsPathEdit, newPath);
@ -530,9 +546,10 @@ void AddNewTorrentDialog::updateDiskSpaceLabel()
} }
} }
const QString freeSpace = Utils::Misc::friendlyUnit(queryFreeDiskSpace(m_ui->savePath->selectedPath()));
const QString sizeString = tr("%1 (Free space on disk: %2)").arg( const QString sizeString = tr("%1 (Free space on disk: %2)").arg(
((torrentSize > 0) ? Utils::Misc::friendlyUnit(torrentSize) : tr("Not available", "This size is unavailable.")) ((torrentSize > 0) ? Utils::Misc::friendlyUnit(torrentSize) : tr("Not available", "This size is unavailable."))
, Utils::Misc::friendlyUnit(Utils::Fs::freeDiskSpaceOnPath(m_ui->savePath->selectedPath()))); , freeSpace);
m_ui->labelSizeData->setText(sizeString); m_ui->labelSizeData->setText(sizeString);
} }

2
src/webui/webapplication.cpp

@ -165,14 +165,12 @@ void WebApplication::sendWebUIFile()
if (m_isAltUIUsed) if (m_isAltUIUsed)
{ {
#ifdef Q_OS_UNIX
if (!Utils::Fs::isRegularFile(localPath)) if (!Utils::Fs::isRegularFile(localPath))
{ {
status(500, u"Internal Server Error"_qs); status(500, u"Internal Server Error"_qs);
print(tr("Unacceptable file type, only regular file is allowed."), Http::CONTENT_TYPE_TXT); print(tr("Unacceptable file type, only regular file is allowed."), Http::CONTENT_TYPE_TXT);
return; return;
} }
#endif
QFileInfo fileInfo {localPath.data()}; QFileInfo fileInfo {localPath.data()};
while (Path(fileInfo.filePath()) != m_rootFolder) while (Path(fileInfo.filePath()) != m_rootFolder)

Loading…
Cancel
Save