From edc3b6fb6a0a4699427850759c9d1c2af0a75298 Mon Sep 17 00:00:00 2001
From: Daira Hopwood <daira@jacaranda.org>
Date: Tue, 28 Apr 2015 20:52:05 +0100
Subject: [PATCH] Add magic folder db.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
---
 src/allmydata/client.py                |  4 +++-
 src/allmydata/frontends/drop_upload.py | 16 +++++++++++++++-
 src/allmydata/test/test_client.py      |  5 +++--
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/allmydata/client.py b/src/allmydata/client.py
index 151017ed..4e3785ea 100644
--- a/src/allmydata/client.py
+++ b/src/allmydata/client.py
@@ -500,7 +500,9 @@ class Client(node.Node, pollmixin.PollMixin):
 
             try:
                 from allmydata.frontends import drop_upload
-                s = drop_upload.DropUploader(self, upload_dircap, local_dir)
+                dbfile = os.path.join(self.basedir, "private", "magicfolderdb.sqlite")
+                dbfile = abspath_expanduser_unicode(dbfile)
+                s = drop_upload.DropUploader(self, upload_dircap, local_dir, dbfile)
                 s.setServiceParent(self)
                 s.startService()
             except Exception, e:
diff --git a/src/allmydata/frontends/drop_upload.py b/src/allmydata/frontends/drop_upload.py
index 9f56ed8f..6a3149ce 100644
--- a/src/allmydata/frontends/drop_upload.py
+++ b/src/allmydata/frontends/drop_upload.py
@@ -12,12 +12,13 @@ from allmydata.util.fileutil import abspath_expanduser_unicode, precondition_abs
 from allmydata.util.encodingutil import listdir_unicode, to_filepath, \
      unicode_from_filepath, quote_local_unicode_path, FilenameEncodingError
 from allmydata.immutable.upload import FileName
+from allmydata.scripts import backupdb
 
 
 class DropUploader(service.MultiService):
     name = 'drop-upload'
 
-    def __init__(self, client, upload_dircap, local_dir, inotify=None):
+    def __init__(self, client, upload_dircap, local_dir, dbfile, inotify=None):
         precondition_abspath(local_dir)
 
         service.MultiService.__init__(self)
@@ -26,6 +27,7 @@ class DropUploader(service.MultiService):
         self._stats_provider = client.stats_provider
         self._convergence = client.convergence
         self._local_path = to_filepath(self._local_dir)
+        self._dbfile = dbfile
 
         if inotify is None:
             from twisted.internet import inotify
@@ -58,7 +60,19 @@ class DropUploader(service.MultiService):
         mask = inotify.IN_CLOSE_WRITE | inotify.IN_MOVED_TO | inotify.IN_ONLYDIR
         self._notifier.watch(self._local_path, mask=mask, callbacks=[self._notify])
 
+    def _check_db_file(self, childpath):
+        # returns True if the file must be uploaded.
+        assert self._db != None
+        r = self._db.check_file(childpath)
+        filecap = r.was_uploaded()
+        if filecap is False:
+            return True
+
     def startService(self):
+        self._db = backupdb.get_backupdb(self._dbfile)
+        if self._db is None:
+            return Failure(Exception('ERROR: Unable to load magic folder db.'))
+
         service.MultiService.startService(self)
         d = self._notifier.startReading()
         self._stats_provider.count('drop_upload.dirs_monitored', 1)
diff --git a/src/allmydata/test/test_client.py b/src/allmydata/test/test_client.py
index 6e4bbe36..34b9298b 100644
--- a/src/allmydata/test/test_client.py
+++ b/src/allmydata/test/test_client.py
@@ -318,11 +318,12 @@ class Basic(testutil.ReallyEqualMixin, testutil.NonASCIIPathMixin, unittest.Test
         class MockDropUploader(service.MultiService):
             name = 'drop-upload'
 
-            def __init__(self, client, upload_dircap, local_dir_utf8, inotify=None):
+            def __init__(self, client, upload_dircap, local_dir, dbfile, inotify=None):
                 service.MultiService.__init__(self)
                 self.client = client
                 self.upload_dircap = upload_dircap
-                self.local_dir_utf8 = local_dir_utf8
+                self.local_dir = local_dir
+                self.dbfile = dbfile
                 self.inotify = inotify
 
         self.patch(allmydata.frontends.drop_upload, 'DropUploader', MockDropUploader)
-- 
2.45.2