add a dav method for getting a list of server files/directories; delete all server data when private key isn't found (this will cause everyone's server data to get wiped, since no one has a key yet); fix a bug in checkStatus with specific status codes (as opposed to ranges)
authorDan Mills <thunder@mozilla.com>
Fri, 07 Mar 2008 04:20:55 -0800
changeset 44366 7accea66ae35d7b94984d5d94aead74a84616eb8
parent 44365 ce5c78ea49fe1abaf2f75cd98747faff453bf382
child 44367 e206c07082431b6b405c96462b6a0149a1c000fe
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
add a dav method for getting a list of server files/directories; delete all server data when private key isn't found (this will cause everyone's server data to get wiped, since no one has a key yet); fix a bug in checkStatus with specific status codes (as opposed to ranges)
services/sync/modules/dav.js
services/sync/modules/service.js
services/sync/modules/util.js
--- a/services/sync/modules/dav.js
+++ b/services/sync/modules/dav.js
@@ -219,16 +219,46 @@ DAVCollection.prototype = {
   },
 
   UNLOCK: function DC_UNLOCK(path, onComplete) {
     let headers = {'Lock-Token': '<' + this._token + '>'};
     headers.__proto__ = this._defaultHeaders;
     return this._makeRequest.async(this, onComplete, "UNLOCK", path, headers);
   },
 
+  // Get all files
+  listFiles: function DC_listFiles(path) {
+    let self = yield;
+
+    if (!path)
+      path = "";
+
+    let headers = {'Content-type': 'text/xml; charset="utf-8"',
+                   'Depth': '1'};
+    headers.__proto__ = this._defaultHeaders;
+
+    this._makeRequest.async(this, self.cb, "PROPFIND", path, headers,
+                           "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
+                            "<D:propfind xmlns:D='DAV:'><D:prop/></D:propfind>");
+    let resp = yield;
+    Utils.ensureStatus(resp.status, "propfind failed");
+
+    // FIXME: shouldn't depend on the first one being the root
+    let tokens = Utils.xpath(resp.responseXML, '//D:href');
+    let ret = [],
+        token,
+        root = tokens.iterateNext();
+    root = root.textContent;
+
+    while (token = tokens.iterateNext())
+      ret.push(token.textContent.replace(root, ''));
+
+    self.done(ret);
+  },
+
   // Login / Logout
 
   login: function DC_login(username, password) {
     let self = yield;
 
     if (this._loggedIn) {
       this._log.debug("Login requested, but already logged in");
       self.done(true);
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -345,16 +345,20 @@ WeaveSyncService.prototype = {
       let keyResp = yield;
       Utils.ensureStatus(keyResp.status,
                          "Could not get private key from server", [[200,300],404]);
 
       if (keyResp.status != 404) {
         this._cryptoId.key = keyResp.responseText;
 
       } else {
+        // FIXME: hack to wipe everyone's server data... needs to be removed at some point
+        this._serverWipe.async(this, self.cb);
+        yield;
+        
         // generate a new key
         this._log.debug("Generating new RSA key");
         Crypto.RSAkeygen.async(Crypto, self.cb, this._cryptoId.password);
         let [privkey, pubkey] = yield;
 
         this._cryptoId.key = privkey;
 
         this._dav.MKCOL("private/", self.cb);
@@ -447,38 +451,53 @@ WeaveSyncService.prototype = {
       self.done();
     }
   },
 
   _resetServer: function WeaveSync__resetServer() {
     let self = yield;
 
     if (!this._lock())
-      return;
+      throw "Could not acrquire lock";
 
     this._bmkEngine.resetServer(self.cb);
     this._histEngine.resetServer(self.cb);
 
     this._unlock();
     self.done();
   },
 
   _resetClient: function WeaveSync__resetClient() {
     let self = yield;
 
     if (!this._lock())
-      return;
+      throw "Could not acrquire lock";
 
     this._bmkEngine.resetClient(self.cb);
     this._histEngine.resetClient(self.cb);
 
     this._unlock();
     self.done();
   },
 
+  _serverWipe: function WeaveSync__serverWipe() {
+    let self = yield;
+
+    this._dav.listFiles.async(this._dav, self.cb);
+    let names = yield;
+
+    for (let i = 0; i < names.length; i++) {
+      this._dav.DELETE(names[i], self.cb);
+      let resp = yield;
+      this._log.debug(resp.status);
+    }
+
+    self.done();
+  },
+
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupports]),
 
   // nsIObserver
 
   observe: function WeaveSync__observe(subject, topic, data) {
     if (topic != "nsPref:changed")
       return;
 
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -133,18 +133,19 @@ let Utils = {
   checkStatus: function Weave_checkStatus(code, msg, ranges) {
     if (!ranges)
       ranges = [[200,300]];
 
     for (let i = 0; i < ranges.length; i++) {
       rng = ranges[i];
       if (typeof(rng) == "object" && code >= rng[0] && code < rng[1])
         return true;
-      else if (typeof(rng) == "integer" && code == rng)
+      else if (typeof(rng) == "number" && code == rng) {
         return true;
+      }
     }
 
     let log = Log4Moz.Service.getLogger("Service.Util");
     log.error(msg + " Error code: " + code);
     return false;
   },
 
   ensureStatus: function Weave_ensureStatus(args) {