From 1a16d77740cbbd1211a54602147044188c3b60ef Mon Sep 17 00:00:00 2001
From: David Stainton <dstainton415@gmail.com>
Date: Wed, 15 Jul 2015 12:14:12 -0700
Subject: [PATCH] fix remote scan procedure

---
 src/allmydata/frontends/magic_folder.py | 15 ++++++++-------
 src/allmydata/test/test_magic_folder.py | 10 +++++-----
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/src/allmydata/frontends/magic_folder.py b/src/allmydata/frontends/magic_folder.py
index 4cdc5752..9179ebb7 100644
--- a/src/allmydata/frontends/magic_folder.py
+++ b/src/allmydata/frontends/magic_folder.py
@@ -186,25 +186,26 @@ class MagicFolder(service.MultiService):
             collective_dirmap, others_list = result
             for dir_name in others_list:
                 d.addCallback(lambda x, dir_name=dir_name: self._scan_remote(dir_name, collective_dirmap[dir_name][0]))
-                collective_dirmap_d.addCallback(self._filter_scan_batch)
-                collective_dirmap_d.addCallback(self._add_batch_to_download_queue)
+                # XXX todo add errback
             return d
         collective_dirmap_d.addCallback(scan_collective)
+        collective_dirmap_d.addCallback(self._filter_scan_batch)
+        collective_dirmap_d.addCallback(self._add_batch_to_download_queue)
         return collective_dirmap_d
 
     def _add_batch_to_download_queue(self, result):
         self._download_deque.extend(result)
-        self._download_pending.update(map(lambda x: x[1], result)) # XXX x[0] or x[1]?
+        self._download_pending.update(map(lambda x: x[0], result))
 
     def _filter_scan_batch(self, result):
         extension = []
+        max_version_dict = {}
         for name in self._download_scan_batch.keys():
             if name in self._download_pending:
                 continue
-            for item in self._download_scan_batch[name]:
-                (nickname, file_node, metadata) = item
-                if self._should_download(name, metadata['version']):
-                    extension += [(name, file_node, metadata)]
+            name, file_node, metadata = max(self._download_scan_batch[name], key=lambda x: x[2]['version'])
+            if self._should_download(name, metadata['version']):
+                extension += [(name, file_node, metadata)]
         return extension
 
     def _download_file(self, name, file_node):
diff --git a/src/allmydata/test/test_magic_folder.py b/src/allmydata/test/test_magic_folder.py
index 279cffc3..a45ded9e 100644
--- a/src/allmydata/test/test_magic_folder.py
+++ b/src/allmydata/test/test_magic_folder.py
@@ -125,7 +125,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
         def _check_move_empty_tree(res):
             self.mkdir_nonascii(empty_tree_dir)
             d2 = defer.Deferred()
-            self.magicfolder.set_processed_callback(d2.callback, ignore_count=0)
+            self.magicfolder.set_processed_callback(d2.callback)
             os.rename(empty_tree_dir, new_empty_tree_dir)
             self.notify(to_filepath(new_empty_tree_dir), self.inotify.IN_MOVED_TO)
             return d2
@@ -151,7 +151,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
 
         def _check_moved_tree_is_watched(res):
             d2 = defer.Deferred()
-            self.magicfolder.set_processed_callback(d2.callback, ignore_count=0)
+            self.magicfolder.set_processed_callback(d2.callback)
             fileutil.write(abspath_expanduser_unicode(u"another", base=new_small_tree_dir), "file")
             self.notify(to_filepath(abspath_expanduser_unicode(u"another", base=new_small_tree_dir)), self.inotify.IN_CLOSE_WRITE)
             return d2
@@ -201,7 +201,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
 
         def create_test_file(result):
             d2 = defer.Deferred()
-            self.magicfolder.set_processed_callback(d2.callback, ignore_count=0)
+            self.magicfolder.set_processed_callback(d2.callback)
             test_file = abspath_expanduser_unicode(u"what", base=self.local_dir)
             fileutil.write(test_file, "meow")
             self.notify(to_filepath(test_file), self.inotify.IN_CLOSE_WRITE)
@@ -328,7 +328,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
         def Alice_wait_for_upload(result):
             print "Alice waits for an upload\n"
             d2 = defer.Deferred()
-            self.alice_magicfolder.set_processed_callback(d2.callback, ignore_count=0)
+            self.alice_magicfolder.set_processed_callback(d2.callback)
             return d2
         d.addCallback(Alice_wait_for_upload)
         def Alice_prepare_for_alice_stats(result):
@@ -342,7 +342,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
         def Bob_wait_for_download(result):
             print "Bob waits for a download\n"
             d2 = defer.Deferred()
-            self.bob_magicfolder.set_download_callback(d2.callback, ignore_count=0)
+            self.bob_magicfolder.set_download_callback(d2.callback)
             return d2
         d.addCallback(Bob_wait_for_download)
         def Bob_prepare_for_stats(result):
-- 
2.45.2