Remove stale syncstate if we have a key mismatch
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 10 Apr 2010 15:50:08 +0000 (11:50 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 10 Apr 2010 15:50:08 +0000 (11:50 -0400)
framework/ActiveSync/lib/Horde/ActiveSync/Request/Sync.php
framework/ActiveSync/lib/Horde/ActiveSync/State/Base.php
framework/ActiveSync/lib/Horde/ActiveSync/State/File.php

index 5261747..1db5979 100644 (file)
@@ -507,7 +507,10 @@ class Horde_ActiveSync_Request_Sync extends Horde_ActiveSync_Request_Base
             $collection['synckey'] == '0') {
 
             $collection['newsynckey'] = Horde_ActiveSync_State_Base::getNewSyncKey(($this->_statusCode == self::STATUS_KEYMISM) ? 0 : $collection['synckey']);
-            // @TODO: Need to reset the state??
+            if ($collection['synckey'] != 0) {
+                $state = &$this->_driver->getStateObject($collection);
+                $state->removeState($collection['synckey']);
+            }
         }
 
         $this->_encoder->startTag(Horde_ActiveSync::SYNC_FOLDER);
index 6dd5a0d..814ccd5 100644 (file)
@@ -253,6 +253,13 @@ abstract class Horde_ActiveSync_State_Base
     abstract public function setDeviceInfo($devId, $data);
 
     /**
+     * Explicitly remove a state from storage.
+     *
+     * @param string $synckey
+     */
+    abstract public function removeState($synckey);
+
+    /**
      * Set the backend driver
      * (should really only be called by a backend object when passing this
      * object to client code)
index e93300b..7b15725 100644 (file)
@@ -628,6 +628,17 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
     }
 
     /**
+     * Explicitly remove a specific state. Normally used if a request results in
+     * a synckey mismatch. This isn't strictly needed, but helps keep the state
+     * storage clean.
+     *
+     */
+    public function removeState($syncKey)
+    {
+        $this->_gc($syncKey, true);
+    }
+
+    /**
      * Garbage collector - clean up from previous sync
      * requests.
      *
@@ -636,7 +647,7 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
      * @return boolean
      * @throws Horde_ActiveSync_Exception
      */
-    private function _gc($syncKey)
+    private function _gc($syncKey, $all = false)
     {
         if (!preg_match('/^s{0,1}\{([0-9A-Za-z-]+)\}([0-9]+)$/', $syncKey, $matches)) {
             return false;
@@ -650,7 +661,7 @@ class Horde_ActiveSync_State_File extends Horde_ActiveSync_State_Base
         }
         while ($entry = readdir($dir)) {
             if (preg_match('/^s{0,1}\{([0-9A-Za-z-]+)\}([0-9]+)$/', $entry, $matches)) {
-                if ($matches[1] == $guid && $matches[2] < $n) {
+                if ($matches[1] == $guid && ((!$all && $matches[2] < $n) || $all)) {
                     unlink($this->_stateDir . '/' . $this->_backend->getUser() . '/' . $entry);
                 }
             }