From 36749def6b10c71853800aaf2b65de8d25be8fce Mon Sep 17 00:00:00 2001
From: David Stainton <dstainton415@gmail.com>
Date: Wed, 28 Oct 2015 16:41:48 +0100
Subject: [PATCH] Add last uploaded timestamp comparison for remote conflict
 detection

---
 src/allmydata/frontends/magic_folder.py | 10 ++++++----
 src/allmydata/magicfolderdb.py          | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/allmydata/frontends/magic_folder.py b/src/allmydata/frontends/magic_folder.py
index 1be3e683..52a146bb 100644
--- a/src/allmydata/frontends/magic_folder.py
+++ b/src/allmydata/frontends/magic_folder.py
@@ -707,10 +707,12 @@ class Downloader(QueueMixin, WriteFileMixin):
                     if dmd_last_downloaded_uri != local_last_downloaded_uri:
                         is_conflict = True
                         self._count('objects_conflicted')
-
-                #dmd_last_uploaded_uri = metadata.get('last_uploaded_uri', None)
-                #local_last_uploaded_uri = ...
-
+                    else:
+                        dmd_last_uploaded_uri = metadata.get('last_uploaded_uri', None)
+                        local_last_uploaded_uri = self._db.get_last_uploaded_uri(relpath_u)
+                        if dmd_last_uploaded_uri != local_last_uploaded_uri:
+                            is_conflict = True
+                            self._count('objects_conflicted')
             if relpath_u.endswith(u"/"):
                 if metadata.get('deleted', False):
                     self._log("rmdir(%r) ignored" % (abspath_u,))
diff --git a/src/allmydata/magicfolderdb.py b/src/allmydata/magicfolderdb.py
index 0c76a7b0..e3649d4e 100644
--- a/src/allmydata/magicfolderdb.py
+++ b/src/allmydata/magicfolderdb.py
@@ -91,6 +91,22 @@ class MagicFolderDB(object):
         else:
             return row[0]
 
+    def get_last_uploaded_uri(self, relpath_u):
+        """
+        Return the last downloaded uri recorded in the magic folder db.
+        If none are found then return None.
+        """
+        c = self.cursor
+        c.execute("SELECT last_uploaded_uri"
+                  " FROM local_files"
+                  " WHERE path=?",
+                  (relpath_u,))
+        row = self.cursor.fetchone()
+        if not row:
+            return None
+        else:
+            return row[0]
+
     def get_local_file_version(self, relpath_u):
         """
         Return the version of a local file tracked by our magic folder db.
-- 
2.45.2