changeset 455:856da063d76e

Remove stale auth cache files
author Chris Cannam
date Wed, 29 Jun 2011 16:18:47 +0100
parents 6f5acaf27d60
children 2f25cc7b81fa
files easyhg.py src/common.cpp src/common.h src/hgrunner.cpp src/hgrunner.h
diffstat 5 files changed, 112 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/easyhg.py	Wed Jun 29 15:37:20 2011 +0100
+++ b/easyhg.py	Wed Jun 29 16:18:47 2011 +0100
@@ -333,6 +333,7 @@
     user, passwd = authinfo
 
     if user and passwd:
+        self.ui.write("note: user and passwd both provided\n")
         return orig_find(self, realm, authuri)
 
     self.ui.write("want username and/or password for %s\n" % authuri)
--- a/src/common.cpp	Wed Jun 29 15:37:20 2011 +0100
+++ b/src/common.cpp	Wed Jun 29 16:18:47 2011 +0100
@@ -31,6 +31,7 @@
 #define SECURITY_WIN32 
 #include <windows.h>
 #include <security.h>
+#include <process.h>
 #else
 #include <errno.h>
 #include <pwd.h>
@@ -199,6 +200,30 @@
 #endif
 }
 
+ProcessStatus
+GetProcessStatus(int pid)
+{
+#ifdef _WIN32
+    HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+    if (!handle) {
+        return ProcessNotRunning;
+    } else {
+        CloseHandle(handle);
+        return ProcessRunning;
+    }
+#else
+    if (kill(getpid(), 0) == 0) {
+        if (kill(pid, 0) == 0) {
+            return ProcessRunning;
+        } else {
+            return ProcessNotRunning;
+        }
+    } else {
+        return UnknownProcessStatus;
+    }
+#endif
+}
+
 FolderStatus getFolderStatus(QString path)
 {
     if (path != "/" && path.endsWith("/")) {
--- a/src/common.h	Wed Jun 29 15:37:20 2011 +0100
+++ b/src/common.h	Wed Jun 29 16:18:47 2011 +0100
@@ -42,6 +42,14 @@
 
 FolderStatus getFolderStatus(QString path);
 
+enum ProcessStatus {
+    ProcessRunning,
+    ProcessNotRunning,
+    UnknownProcessStatus
+};
+
+ProcessStatus GetProcessStatus(int pid);
+
 /**
  * If the given path is somewhere within an existing repository,
  * return the path of the root directory of the repository (i.e. the
--- a/src/hgrunner.cpp	Wed Jun 29 15:37:20 2011 +0100
+++ b/src/hgrunner.cpp	Wed Jun 29 16:18:47 2011 +0100
@@ -391,6 +391,73 @@
     startCommand(toRun);
 }
 
+void HgRunner::pruneOldAuthFiles()
+{
+    QString path = QDesktopServices::storageLocation
+        (QDesktopServices::CacheLocation);
+    QDir d(path);
+    if (!d.exists()) return;
+    QStringList filters;
+    filters << "easyhg.*.dat";
+    QStringList fl = d.entryList(filters);
+    foreach (QString f, fl) {
+        QStringList parts = f.split('.');
+        if (parts.size() > 1) {
+            int pid = parts[1].toInt();
+            DEBUG << "Checking pid " << pid << " for cache file " << f << endl;
+
+            ProcessStatus ps = GetProcessStatus(pid);
+            if (ps == ProcessNotRunning) {
+                DEBUG << "Removing stale cache file " << f << endl;
+                QDir(d).remove(f);
+            }
+        }
+    }
+}
+
+QString HgRunner::getAuthFilePath()
+{
+    if (m_authFilePath == "") {
+
+        pruneOldAuthFiles();
+
+        QByteArray fileExt = randomKey();
+        if (fileExt == QByteArray()) {
+            DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl;
+            return "";
+        }
+        QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16)
+            .replace('+', '-').replace('/', '_');
+        QString path = QDesktopServices::storageLocation
+            (QDesktopServices::CacheLocation);
+        QDir().mkpath(path);
+        if (path == "") {
+            DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl;
+            return "";
+        }
+
+        m_authFilePath = QString("%1/easyhg.%2.%3.dat").arg(path)
+            .arg(getpid()).arg(fileExt16);
+    }
+
+    return m_authFilePath;
+}
+
+QString HgRunner::getAuthKey()
+{
+    if (m_authKey == "") {
+        QByteArray key = randomKey();
+        if (key == QByteArray()) {
+            DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
+            return "";
+        }
+        QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
+        m_authKey = key16;
+    }
+
+    return m_authKey;
+}
+
 QStringList HgRunner::addExtensionOptions(QStringList params)
 {
     QString extpath = getExtensionLocation();
@@ -398,41 +465,17 @@
         DEBUG << "HgRunner::addExtensionOptions: Failed to get extension location" << endl;
         return params;
     }
-    
-    if (m_authFilePath == "") {
 
-        QByteArray key = randomKey();
-        if (key == QByteArray()) {
-            DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
-            return params;
-        }
-        QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
+    QString afp = getAuthFilePath();
+    QString afk = getAuthKey();
 
-        QByteArray fileExt = randomKey();
-        if (fileExt == QByteArray()) {
-            DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl;
-            return params;
-        }
-        QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16)
-            .replace('+', '-').replace('/', '.');
-        QString path = QDesktopServices::storageLocation
-            (QDesktopServices::CacheLocation);
-        QDir().mkpath(path);
-        if (path == "") {
-            DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl;
-            return params;
-        }
-
-        m_authKey = key16;
-        m_authFilePath = QString("%1/easyhg_%2.dat").arg(path).arg(fileExt16);
+    if (afp != "" && afk != "") {
+        params.push_front(QString("easyhg.authkey=%1").arg(m_authKey));
+        params.push_front("--config");
+        params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath));
+        params.push_front("--config");
     }
 
-    params.push_front(QString("easyhg.authkey=%1").arg(m_authKey));
-    params.push_front("--config");
-
-    params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath));
-    params.push_front("--config");
-
     // Looks like this one must be without quotes, even though the SSH
     // one above only works on Windows if it has quotes (at least where
     // there is a space in the path).  Odd
--- a/src/hgrunner.h	Wed Jun 29 15:37:20 2011 +0100
+++ b/src/hgrunner.h	Wed Jun 29 16:18:47 2011 +0100
@@ -71,6 +71,10 @@
     QString getSshBinaryName();
     QString getExtensionLocation();
 
+    QString getAuthFilePath();
+    QString getAuthKey();
+    void pruneOldAuthFiles();
+
     QString getUnbundledFileName();
     QString unbundleExtension();