[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdelibs/solid/solid/backends/hal
From:       Kevin Ottens <ervin () kde ! org>
Date:       2008-08-15 20:12:59
Message-ID: 1218831179.126512.11639.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 847628 by ervin:

Refactoring the HAL backend fstab handling to make it more useful. We
now keep a cache of all the information found in the fstab. It's updated
on demand only if more than 10 seconds elapsed since the last update.
This way we don't introduce a polling, and we also make sure that the
information used is not too old.

Opens the door to making some of our heuristics in the HAL backend for
ignoring devices more efficient.



 M  +33 -16    halfstabhandling.cpp  


--- trunk/KDE/kdelibs/solid/solid/backends/hal/halfstabhandling.cpp #847627:847628
@@ -20,10 +20,14 @@
 #include "halfstabhandling.h"
 
 #include <QtCore/QFile>
+#include <QtCore/QMultiHash>
 #include <QtCore/QObject>
 #include <QtCore/QProcess>
 #include <QtCore/QTextStream>
+#include <QtCore/QTime>
 
+#include <solid/soliddefs_p.h>
+
 #ifdef HAVE_MNTENT_H
 #include <mntent.h>
 #elif defined(HAVE_SYS_MNTENT_H)
@@ -36,6 +40,9 @@
 #define FSTAB "/etc/fstab"
 #endif
 
+typedef QMultiHash<QString, QString> QStringMultiHash;
+SOLID_GLOBAL_STATIC(QStringMultiHash, globalMountPointsCache);
+
 QString _k_resolveSymLink(const QString &filename)
 {
     QString resolved = filename;
@@ -49,29 +56,35 @@
     return resolved;
 }
 
-bool Solid::Backends::Hal::FstabHandling::isInFstab(const QString &device)
+void _k_updateMountPointsCache()
 {
-    const QString deviceToFind = _k_resolveSymLink(device);
+    static bool firstCall = true;
+    static QTime elapsedTime;
 
-    if (deviceToFind.isEmpty()) {
-        return false;
+    if (firstCall) {
+        firstCall = false;
+        elapsedTime.start();
+    } else if (elapsedTime.elapsed()>10000) {
+        elapsedTime.restart();
+    } else {
+        return;
     }
 
+    globalMountPointsCache->clear();
+
 #ifdef HAVE_SETMNTENT
 
     struct mntent *fstab;
     if ((fstab = setmntent(FSTAB, "r")) == 0) {
-        return false;
+        return;
     }
 
     struct mntent *fe;
     while ((fe = getmntent(fstab)) != 0) {
         const QString device = _k_resolveSymLink(QFile::decodeName(fe->mnt_fsname));
+        const QString mountpoint = _k_resolveSymLink(QFile::decodeName(fe->mnt_dir));
 
-        if (device==deviceToFind) {
-            endmntent(fstab);
-            return true;
-        }
+        globalMountPointsCache->insert(device, mountpoint);
     }
 
     endmntent(fstab);
@@ -80,7 +93,7 @@
 
     QFile fstab(FSTAB);
     if (!fstab.open(QIODevice::ReadOnly)) {
-        return false;
+        return;
     }
 
     QTextStream stream(&fstab);
@@ -105,18 +118,22 @@
         }
 #endif
 
-        const QString device = _k_resolveSymLink(items.first());
+        const QString device = _k_resolveSymLink(items.at(0));
+        const QString mountpoint = _k_resolveSymLink(items.at(1));
 
-        if (device==deviceToFind) {
-            fstab.close();
-            return true;
-        }
+        globalMountPointsCache->insert(device, mountpoint);
    }
 
    fstab.close();
 #endif
+}
 
-   return false;
+bool Solid::Backends::Hal::FstabHandling::isInFstab(const QString &device)
+{
+    _k_updateMountPointsCache();
+    const QString deviceToFind = _k_resolveSymLink(device);
+
+    return globalMountPointsCache->contains(deviceToFind);
 }
 
 QProcess *Solid::Backends::Hal::FstabHandling::callSystemCommand(const QString &commandName,
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic