From 1217539f47c923a22434e05eaa9e271696c6a637 Mon Sep 17 00:00:00 2001 From: Siddharth Ravikumar Date: Thu, 26 Feb 2015 22:44:58 -0500 Subject: fleshed out the NodeDirMonitor's on_modified method + wrote tests for it. modified: combox/events.py modified: tests/events_test.py --- combox/events.py | 32 ++++++++++++++++++++++++++++++-- tests/events_test.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/combox/events.py b/combox/events.py index 779b6f6..3692adc 100644 --- a/combox/events.py +++ b/combox/events.py @@ -26,7 +26,7 @@ from watchdog.events import LoggingEventHandler from combox.crypto import split_and_encrypt, decrypt_and_glue from combox.file import (mk_nodedir, rm_nodedir, rm_shards, relative_path, move_shards, move_nodedir, - cb_path, node_path) + cb_path, node_path, hash_file) from combox.silo import ComboxSilo @@ -179,6 +179,13 @@ class NodeDirMonitor(LoggingEventHandler): self.silo = ComboxSilo(self.config) + def silo_update(self): + """ + Re-reads the silo from disk. + """ + self.silo = ComboxSilo(self.config) + + def housekeep(self): """Recursively traverses node directory, discovers changes and updates silo and combox directory. @@ -230,4 +237,25 @@ class NodeDirMonitor(LoggingEventHandler): def on_modified(self, event): super(NodeDirMonitor, self).on_modified(event) - pass + self.silo_update() + + file_cb_path = cb_path(event.src_path, self.config) + + if event.is_directory: + # do nothing + pass + elif (not event.is_directory): + file_content = decrypt_and_glue(file_cb_path, + self.config, + write=False) + file_content_hash = hash_file(file_cb_path, file_content) + + if self.silo.stale(file_cb_path, file_content_hash): + # shard modified + + # means, file was modified on another computer (also + # running combox). so, reconstruct the file and put it + # in the combox directory. + decrypt_and_glue(file_cb_path, self.config) + # update db. + self.silo.update(file_cb_path) diff --git a/tests/events_test.py b/tests/events_test.py index c62d3f2..f645e75 100644 --- a/tests/events_test.py +++ b/tests/events_test.py @@ -266,6 +266,40 @@ class TestEvents(object): self.purge_list.append(self.TEST_FILE_MUTANT) self.purge_list.append(self.FOO_DIR) + # Test - shard modification + self.lorem_file = path.join(self.FILES_DIR, 'lorem.txt') + lorem_content = read_file(self.lorem_file) + self.lorem_file_copy = "%s.copy" % self.lorem_file + + copyfile(self.lorem_file, self.lorem_file_copy) + split_and_encrypt(self.lorem_file_copy, self.config, + lorem_content) + + silo = ComboxSilo(self.config) + silo.update(self.lorem_file_copy) + shardedp(self.lorem_file_copy) + + silo = ComboxSilo(self.config) + lorem_file_copy_hash = silo.db.get(self.lorem_file_copy) + + self.ipsum_file = path.join(self.FILES_DIR, 'ipsum.txt') + ipsum_content = read_file(self.ipsum_file) + lorem_copy_content = "%s\n%s" % (lorem_content, ipsum_content) + + split_and_encrypt(self.lorem_file_copy, self.config, + lorem_copy_content) + time.sleep(1) + ## check if the lorem_file_copy's info is updated in silo + silo = ComboxSilo(self.config) + + assert lorem_copy_content == read_file(self.lorem_file_copy) + assert lorem_file_copy_hash != silo.db.get(self.lorem_file_copy) + + self.purge_list.append(self.lorem_file_copy) + + observer.stop() + observer.join() + def teardown(self): """Cleans up things after each test in this class""" -- cgit v1.2.3