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 @@
#include <QThread>
#include <QDebug>
#include <QJSEngine>
#include <QDir>
DiskUsageWorker::DiskUsageWorker(QObject *parent)
......@@ -56,10 +57,16 @@ void DiskUsageWorker::submit(QStringList paths, QJSValue *callback)
QVariantMap DiskUsageWorker::calculate(QStringList paths)
{
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> 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) {
QString expandedPath;
// Pseudo-path for querying RPM database for file sizes
// ----------------------------------------------------
// Example path with package name: ":rpm:python3-base"
......@@ -67,18 +74,22 @@ QVariantMap DiskUsageWorker::calculate(QStringList paths)
if (path.startsWith(":rpm:")) {
QString glob = path.mid(5);
usage[path] = calculateRpmSize(glob);
expandedPath = "/usr/" + path;
} else if (path.startsWith(":apkd:")) {
// Pseudo-path for querying Android apps' data usage
QString rest = path.mid(6);
usage[path] = calculateApkdSize(rest);
expandedPath = (androidHomeExists ? androidHome : "") + "/data/data";
} else {
QString expandedPath;
quint64 size = calculateSize(path, &expandedPath);
expandedPaths[path] = expandedPath;
originalPaths[expandedPath] = path;
quint64 size = calculateSize(path, &expandedPath, androidHomeExists);
if (expandedPath.startsWith(androidHome) && !androidHomeExists) {
expandedPath = expandedPath.mid(androidHome.length());
}
usage[path] = size;
}
expandedPaths[path] = expandedPath;
originalPaths[expandedPath] = path;
if (m_quit) {
break;
}
......
......@@ -43,13 +43,19 @@
#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
if (directory.startsWith("~/")) {
directory = QDir::homePath() + '/' + directory.mid(2);
}
QString androidHome = QString("/home/.android");
if (!androidHomeExists && directory.startsWith(androidHome)) {
directory = directory.mid(androidHome.length());
}
if (expandedPath) {
*expandedPath = directory;
}
......@@ -69,21 +75,13 @@ quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath)
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);
if (!d.exists() || !d.isReadable()) {
return 0L;
}
QProcess du;
du.start("du", QStringList() << "-sxb" << directory, QIODevice::ReadOnly);
du.start("du", QStringList() << "-sb" << directory, QIODevice::ReadOnly);
du.waitForFinished();
if (du.exitStatus() != QProcess::NormalExit) {
qWarning() << "Could not determine size of:" << directory;
......
......@@ -55,7 +55,7 @@ signals:
private:
QVariantMap calculate(QStringList paths);
quint64 calculateSize(QString directory, QString *expandedPath);
quint64 calculateSize(QString directory, QString *expandedPath, bool androidHomeExists);
quint64 calculateRpmSize(const QString &glob);
quint64 calculateApkdSize(const QString &rest);
......
......@@ -52,7 +52,7 @@ static QVariantMap g_mocked_apkd_size;
/* Mocked implementations of size calculation functions */
quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath)
quint64 DiskUsageWorker::calculateSize(QString directory, QString *expandedPath, bool androidHomeExists)
{
if (expandedPath) {
*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