Commit f4ed3643 authored by Andrew Branson's avatar Andrew Branson

[storage] Disk usage accuracy improvement. Fixes JB#34897

- Give apkd and rpm usage expandedPath values that will allow them to be
subtracted from the correct parent. Fixes JB#34897.
- Drop the -x flag to 'du'. It prevented the android_storage subdir from
being included in the /home/nemo total.
- Automatically chop off '/home/.android' prefix on Jolla1.
- Drop the automatic substitution of '/data/media' for
'/home/nemo/android_storage' to ensure expandedPath is correct.
Contributes to JB#31444. 
parent 110fd5bf
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <QThread> #include <QThread>
#include <QDebug> #include <QDebug>
#include <QJSEngine> #include <QJSEngine>
#include <QDir>
DiskUsageWorker::DiskUsageWorker(QObject *parent) DiskUsageWorker::DiskUsageWorker(QObject *parent)
...@@ -56,10 +57,16 @@ void DiskUsageWorker::submit(QStringList paths, QJSValue *callback) ...@@ -56,10 +57,16 @@ void DiskUsageWorker::submit(QStringList paths, QJSValue *callback)
QVariantMap DiskUsageWorker::calculate(QStringList paths) QVariantMap DiskUsageWorker::calculate(QStringList paths)
{ {
QVariantMap usage; QVariantMap usage;
// expanded Path places the object in the tree so parents can have it subtracted from its total
QMap<QString, QString> expandedPaths; // input path -> expanded path QMap<QString, QString> expandedPaths; // input path -> expanded path
QMap<QString, QString> originalPaths; // expanded path -> input path QMap<QString, QString> originalPaths; // expanded path -> input path
// Older adaptations (e.g. Jolla 1) don't have /home/.android/. Android home is in the root.
QString androidHome = QString("/home/.android");
bool androidHomeExists = QDir(androidHome).exists();
foreach (const QString &path, paths) { foreach (const QString &path, paths) {
QString expandedPath;
// Pseudo-path for querying RPM database for file sizes // Pseudo-path for querying RPM database for file sizes
// ---------------------------------------------------- // ----------------------------------------------------
// Example path with package name: ":rpm:python3-base" // Example path with package name: ":rpm:python3-base"
...@@ -67,18 +74,22 @@ QVariantMap DiskUsageWorker::calculate(QStringList paths) ...@@ -67,18 +74,22 @@ QVariantMap DiskUsageWorker::calculate(QStringList paths)
if (path.startsWith(":rpm:")) { if (path.startsWith(":rpm:")) {
QString glob = path.mid(5); QString glob = path.mid(5);
usage[path] = calculateRpmSize(glob); usage[path] = calculateRpmSize(glob);
expandedPath = "/usr/" + path;
} else if (path.startsWith(":apkd:")) { } else if (path.startsWith(":apkd:")) {
// Pseudo-path for querying Android apps' data usage // Pseudo-path for querying Android apps' data usage
QString rest = path.mid(6); QString rest = path.mid(6);
usage[path] = calculateApkdSize(rest); usage[path] = calculateApkdSize(rest);
expandedPath = (androidHomeExists ? androidHome : "") + "/data/data";
} else { } else {
QString expandedPath; quint64 size = calculateSize(path, &expandedPath, androidHomeExists);
quint64 size = calculateSize(path, &expandedPath); if (expandedPath.startsWith(androidHome) && !androidHomeExists) {
expandedPaths[path] = expandedPath; expandedPath = expandedPath.mid(androidHome.length());
originalPaths[expandedPath] = path; }
usage[path] = size; usage[path] = size;
} }
expandedPaths[path] = expandedPath;
originalPaths[expandedPath] = path;
if (m_quit) { if (m_quit) {
break; break;
} }
......
...@@ -43,13 +43,19 @@ ...@@ -43,13 +43,19 @@
#include <sys/statvfs.h> #include <sys/statvfs.h>
quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath) quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath, bool androidHomeExists)
{ {
// In lieu of wordexp(3) support in Qt, fake it // In lieu of wordexp(3) support in Qt, fake it
if (directory.startsWith("~/")) { if (directory.startsWith("~/")) {
directory = QDir::homePath() + '/' + directory.mid(2); directory = QDir::homePath() + '/' + directory.mid(2);
} }
QString androidHome = QString("/home/.android");
if (!androidHomeExists && directory.startsWith(androidHome)) {
directory = directory.mid(androidHome.length());
}
if (expandedPath) { if (expandedPath) {
*expandedPath = directory; *expandedPath = directory;
} }
...@@ -69,21 +75,13 @@ quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath) ...@@ -69,21 +75,13 @@ quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath)
return fsSize - freeSpace; return fsSize - freeSpace;
} }
// "/data/media/" is mounted in "/home/nemo/android_storage/" with read
// access for the "nemo" user ("/data/media/" itself isn't readable);
// Mounted via FUSE and /system/bin/sdcard, see here:
// https://source.android.com/devices/storage/config.html
if (directory == "/data/media/") {
directory = "/home/nemo/android_storage/";
}
QDir d(directory); QDir d(directory);
if (!d.exists() || !d.isReadable()) { if (!d.exists() || !d.isReadable()) {
return 0L; return 0L;
} }
QProcess du; QProcess du;
du.start("du", QStringList() << "-sxb" << directory, QIODevice::ReadOnly); du.start("du", QStringList() << "-sb" << directory, QIODevice::ReadOnly);
du.waitForFinished(); du.waitForFinished();
if (du.exitStatus() != QProcess::NormalExit) { if (du.exitStatus() != QProcess::NormalExit) {
qWarning() << "Could not determine size of:" << directory; qWarning() << "Could not determine size of:" << directory;
......
...@@ -55,7 +55,7 @@ signals: ...@@ -55,7 +55,7 @@ signals:
private: private:
QVariantMap calculate(QStringList paths); QVariantMap calculate(QStringList paths);
quint64 calculateSize(QString directory, QString *expandedPath); quint64 calculateSize(QString directory, QString *expandedPath, bool androidHomeExists);
quint64 calculateRpmSize(const QString &glob); quint64 calculateRpmSize(const QString &glob);
quint64 calculateApkdSize(const QString &rest); quint64 calculateApkdSize(const QString &rest);
......
...@@ -52,7 +52,7 @@ static QVariantMap g_mocked_apkd_size; ...@@ -52,7 +52,7 @@ static QVariantMap g_mocked_apkd_size;
/* Mocked implementations of size calculation functions */ /* Mocked implementations of size calculation functions */
quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath) quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath, bool androidHomeExists)
{ {
if (expandedPath) { if (expandedPath) {
*expandedPath = directory; *expandedPath = directory;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment