summaryrefslogtreecommitdiffstats
path: root/combox/events.py
blob: 13aca92214ebdfba99cd6a80beada586ef7df5bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# -*- coding: utf-8 -*-
#
#    Copyright (C) 2016 Dr. Robert C. Green II.
#
#    This file is part of Combox.
#
#   Combox is free software: you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Combox is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Combox (see COPYING).  If not, see
#   <http://www.gnu.org/licenses/>.

import os
import platform
import logging
import time

from os import path
from threading import Lock
from threading import Timer

from watchdog.events import LoggingEventHandler

from combox.config import get_nodedirs
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, hash_file, rm_path,
                         node_paths, no_of_shards)
from combox.log import log_i, log_e
from combox.silo import ComboxSilo


class ComboxDirMonitor(LoggingEventHandler):
    """Monitors combox directory for changes and makes corresponding changes in node directories.

     - When a file is created in the combox directory, the
       :class:`.ComboxDirMonitor` creates encrypted shards of the file
       and spreads them across the node directories.

     - When a file is modified in the combox directory, the
       :class:`.ComboxDirMonitor` updates the encrypted shards of the
       file in the node directories.

     - When a file is moved/renamed in the combox directory, the
       :class:`.ComboxDirMonitor` moves/renames the encrypted shards
       of the file in the node directories.

     - When a file is deleted in the combox directory, the
       :class:`.ComboxDirMonitor` delets the encrypted shards of the
       file in the node directories.

    :param dict config:
        A dictionary that contains configuration information about
        combox.
    :param threading.Lock dblock:
        Lock to access the :class:`.ComboxSilo`. object.
    :param threading.Lock monitor_lock:
        Lock shared by :class:`.ComboxDirMonitor` and all the
        :class:`.NodeDirMonitor` objects.

    """

    def __init__(self, config, dblock, monitor_lock):
        """Initialize :class:`.ComboxDirMonitor`.

        """
        super(ComboxDirMonitor, self).__init__()

        logging.basicConfig(level=logging.INFO,
                            format='%(asctime)s - %(message)s',
                            datefmt='%Y-%m-%d %H:%M:%S')

        self.config = config
        self.silo = ComboxSilo(self.config, dblock)
        self.lock = monitor_lock

        # tracks files that are created during the course of this run.
        self.just_created = {}

        self.housekeep()


    def tmp_file(self, file_path):
        """Returns `True` if `file_path` is a temporary file.

        :param str file_path:
            Path of a file.
        :returns:
            `True` if `file_path` is a temporary file.
        :rtype: bool

        """
        if file_path.endswith("~"):
            return True
        elif path.basename(file_path).startswith(".#"):
            return True
        elif (path.basename(file_path).startswith("#") and
              file_path.endswith("#")):
            return True
        else:
            return False


    def housekeep(self):
        """Recursively traverses combox directory, discovers changes and updates silo and node directories. This method must **never** be called directly.

        This method is called before :class:`.ComboxDirMonitor` starts
        monitoring the combox directory.

        - First, if it finds tracked files in the combox directory
          have been deleted, it clears their information from the DB
          and it purges the encrypted shards of the respective files
          from the node directories.

        - Second, if it finds files that were modified in the combox
          directory, it updates their respective encrypted shards in
          the node directories.

        - Third, if it finds files that are not yet tracked by combox,
          it creates their respective encrypted shards and spreads
          them across the node directories.

        """
        log_i("combox monitor is housekeeping")
        log_i("Please don't make any changes to combox directory now")

        # Remove information about files that were deleted.
        fpath_filter = lambda x: x not in self.silo.nodedicts()
        fpaths = filter(fpath_filter, self.silo.keys())

        for fpath in fpaths:
            if not path.exists(fpath):
                # remove this file's info from silo.
                log_i("%s was deleted. Removing it from DB" % fpath)
                self.silo.remove(fpath)
                # also purge the file's shards in node directories.
                rm_shards(fpath, self.config)

        # Add/update information about files that were created/modded.
        # Also do split_and_encrypt on files that were created/modded.
        for root, dirs, files in os.walk(self.config['combox_dir']):

            for f in files:
                fpath = path.join(root, f)

                if (self.silo.exists(fpath) and
                    self.silo.stale(fpath)):
                    # file was modified
                    log_i("%s was modified. Updating DB and shards..." % fpath)
                    split_and_encrypt(fpath, self.config)
                    self.silo.update(fpath)
                elif (not self.silo.exists(fpath)
                      and not self.tmp_file(fpath)):
                    # new file
                    log_i("Adding new file %s..." % fpath)
                    split_and_encrypt(fpath, self.config)
                    self.silo.update(fpath)


        log_i("combox monitor is done with housekeeping")
        log_i("Do what you want to the combox directory")


    def on_moved(self, event):
        """Called when a file/directory is moved/renamed in the combox directory.

        If a directory is renamed/moved, it renames/moves the
        corresponding directory in all the node directories.

        If a file is renamed/moved, it renames/moves the shards of the
        file in all the node directories and updates the DB.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`

        """
        super(ComboxDirMonitor, self).on_moved(event)

        if event.is_directory:
            with self.lock:
                # creates a corresponding directory at the node dirs.
                move_nodedir(event.src_path, event.dest_path, self.config)
        else:
            with self.lock:
                # file moved
                move_shards(event.src_path, event.dest_path, self.config)
                # update file info in silo.
                self.silo.remove(event.src_path)
                self.silo.update(event.dest_path)


    def on_created(self, event):
        """Called when a file/directory is created in the combox directory.

        If a directory is created, it creates the corresponding
        directory in all the node directories.

        If a file is created, it:

            - Splits the file into shards.
            - Encrypts all the shards.
            - Spreads the encrypted shards across the node
              directories.
            - Store hash of the file in DB.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`

        """
        super(ComboxDirMonitor, self).on_created(event)

        if self.tmp_file(event.src_path):
            # ignore tmp files.
            log_i("Created tmp file %s...ignoring" %
                  event.src_path)
            return

        # make note of this file creation; will be used when
        # on_modified is called after this file creatiion.
        self.just_created[event.src_path] = True

        file_node_path = node_path(event.src_path, self.config,
                                   not event.is_directory)

        if event.is_directory and (not path.exists(file_node_path)):
            with self.lock:
                # creates a corresponding directory at the node dirs.
                mk_nodedir(event.src_path, self.config)
        elif (not event.is_directory) and (not path.exists(
                file_node_path)):
            with self.lock:
                # file was created
                split_and_encrypt(event.src_path, self.config)
                # store file info in silo.
                self.silo.update(event.src_path)


    def on_deleted(self, event):
        """Called when a file/directory is deleted in the combox directory.

        If a directory is deleted, it deletes the corresponding
        directory in all the node directories.

        If a file is deleted, it deletes all of the file's shards in
        the node directories and removes information about the file in
        the DB.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`

        """
        super(ComboxDirMonitor, self).on_deleted(event)

        file_node_path = node_path(event.src_path, self.config,
                                   not event.is_directory)

        if event.is_directory and (path.exists(file_node_path)):
            # Delete corresponding directory in the nodes.
            with self.lock:
                rm_nodedir(event.src_path, self.config)
        elif(not event.is_directory) and (path.exists(file_node_path)):
            with self.lock:
                # remove the corresponding file shards in the node
                # directories.
                rm_shards(event.src_path, self.config)
                # remove file info from silo.
                self.silo.remove(event.src_path)


    def on_modified(self, event):
        """Called when a file/directory is modified in the combox directory.

        If a directory is modified, nothing is done.

        If a file is modified, it:

            - Splits the file into shards.
            - Encrypts all the shards.
            - Spreads the encrypted shards across the node
              directories, replacing the ol' shards.
            - Update the hash of the file stored in the DB.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`

        """
        super(ComboxDirMonitor, self).on_modified(event)

        if self.tmp_file(event.src_path):
            # ignore tmp files.
            log_i("Modified tmp file %s...ignoring" %
                  event.src_path)
            return

        if event.is_directory:
            # do nothing
            pass
        else:
            # file was modified
            with self.lock:
                f_size_MiB = path.getsize(event.src_path) / 1048576.0
                log_i('%s modified %f' % (event.src_path, f_size_MiB))

                # introduce delay to prevent multiple "file modified"
                # events from being generated.
                if f_size_MiB >= 30:
                    sleep_time = (f_size_MiB / 30.0)
                    log_i("waiting on_modified combox monitor %f" % sleep_time)
                    time.sleep(sleep_time)
                    log_i("end waiting on_modified combox monitor")
                else:
                    time.sleep(1)

                # watchdog's weirdness
                # --------------------
                #
                # On GNU/Linux, when a file is created, watchdog
                # generates a 'file created' and a 'file modified'
                # event; we're tracking this behaviour and ignoring
                # the 'file modified' event.
                #
                if (self.just_created.has_key(event.src_path) and
                    self.just_created[event.src_path] and
                    platform.system() == 'Linux'):
                    self.just_created[event.src_path] = False
                    log_i("Just created file %s. So ignoring on_modified call." % (
                        event.src_path))
                    return

                split_and_encrypt(event.src_path, self.config)
                # update file info in silo.
                self.silo.update(event.src_path)


class NodeDirMonitor(LoggingEventHandler):
    """Monitors a node directory for changes and makes corresponding changes in the combox directory.

    An instance of :class:`.NodeDirMonitor` is created for each node
    directory; so, if there are two node directories, two instances of
    this class must be created.

    - When a shard is created in the node directory by virtue of a
      file created in the combox directory located in *another*
      computer, the :class:`.NodeDirMonitor` glues the decrypted
      version of all the shards (of a file) in the node directories
      and puts the glued file in the combox directory of *this*
      computer.

    - When a shard is modified in the node directory by virtue of a
      file modified in the combox directory located in *another*
      computer, the :class:`.NodeDirMonitor`, glues the decrypted
      version of all the shards (of a file) in the node directories
      and puts the updated glued file in the combox directory of
      *this* computer.

    - When a shard is moved/renamed in the node directory by virtue of
      the corresponding file moved/renamed in the combox directory
      located in *another* computer, the :class:`.NodeDirMonitor`
      moves/renames all the shards (of a file) in the node
      directories.

    - When a shard is deleted in the node directory by virtue of the
      corresponding file deleted in the combox directory located in
      *another* computer, the :class:`.NodeDirMonitor` deletes all the
      shards (of a file) in the node directories.


    :param dict config:
        A dictionary that contains configuration information about
        combox.
    :param threading.Lock dblock:
        Lock to access the :class:`.ComboxSilo`. object.
    :param threading.Lock monitor_lock:
        Lock shared by :class:`.ComboxDirMonitor` and all the
        :class:`.NodeDirMonitor` objects.

    """

    def __init__(self, config, dblock, monitor_lock):
        """Initialize :class:`.NodeDirMonitor`.

        """
        super(NodeDirMonitor, self).__init__()

        self.lock = monitor_lock
        logging.basicConfig(level=logging.INFO,
                            format='%(asctime)s - %(message)s',
                            datefmt='%Y-%m-%d %H:%M:%S')

        self.config = config
        self.silo = ComboxSilo(self.config, dblock)

        self.num_nodes = len(get_nodedirs(self.config))

        # track file shards that are just created during this run.
        self.just_created = {}


    def shardp(self, path):
        """Checks if `path` is a shard.

        Shards end with `.shardN` where `N` is a natural number.

        :returns:
            Returns `True` if `path` is a shard; `False` otherwise.
        :rtype: bool

        """
        if path[:-1].endswith('.shard'):
            return True
        else:
            return False


    def delete_later(self, file_cb_path):
        """Delete `file_cb_path` if its  still under 'file_deleted' dictionary in DB.

        This is used by the :meth:`~.NodeDirMonitor.on_deleted` method.

        This is a workaround to make combox predict official Google
        Drive client's behavior.
        """
        with self.lock:
            num = self.silo.node_get('file_deleted', file_cb_path)

            if num == self.num_nodes:
                log_i("Deleting %s..." % file_cb_path)
                # remove the corresponding file under the combox
                # directory.
                rm_path(file_cb_path)
                # remove file info from silo.
                self.silo.remove(file_cb_path)
                self.silo.node_rem('file_deleted', file_cb_path)


    def housekeep(self):
        """Recursively traverses node directory, discovers changes and updates silo and combox directory. This method must **never** be called directly.

        This method is called before :class:`.NodeDirMonitor` starts
        monitoring the node directory.

        - First, if it finds a shard of a tracked file deleted in the
          node directory, it deletes the respective file in the combox
          directory and removes the file's information from the DB,
          iff the shards of this file has been removed in all other
          node directories too.

        - Second, if it finds a shard of a file that is not tracked in
          the node directory, it resurrects the file from the shards,
          writes it to the combox directory, and stores the file's
          information in the DB, iff all of the shards of this file
          are found in the node directories.

        """
        log_i("combox node monitor is housekeeping")
        log_i("Please don't make any changes to combox directory now")

        # Remove files from the combox directory whose shards were
        # deleted.
        # Remove information about files that were deleted.
        fpath_filter = lambda x: x not in self.silo.nodedicts()
        fpaths = filter(fpath_filter, self.silo.keys())

        for fpath in fpaths:
            del_num = 0
            fshards = node_paths(fpath, self.config, True)

            for fshard in fshards:
                if not path.exists(fshard):
                    del_num += 1

            if del_num == self.num_nodes:
                # remove the file from combox directory.
                rm_path(fpath)
                log_i("%s was deleted on another computer. Removing it" %
                      fpath)
                # update silo.
                self.silo.remove(fpath)
                self.silo.node_rem('file_deleted', fpath)
            elif del_num > 0:
                # means, all the shards of the file have not been
                # deleted yet, so, we store the no. of shards was
                # deleted in the 'file_deleted' dict inside the silo.
                self.silo.node_set('file_deleted', fpath, del_num)

        # Re-construct files created on another computer when combox
        # was switched off. Only files who's all the shards have made
        # it to the node directories are re-constructed.

        # Dict that keeps track of the files that were created.
        files_created = {}
        for node_dir in get_nodedirs(self.config):
            for root, dirs, files in os.walk(node_dir):
                for f in files:
                    shard = path.join(root, f)

                    if (not self.shardp(shard) or
                        '.dropbox.cache' in shard):
                        continue

                    file_cb_path = cb_path(shard, self.config)
                    if not self.silo.exists(file_cb_path):
                        if not files_created.get(file_cb_path):
                            files_created[file_cb_path] = 1
                        else:
                            files_created[file_cb_path] += 1

        for f_cb_path, crt_num in files_created.items():
            if crt_num == self.num_nodes:
                log_i("%s was created remotely. Creating it locally now..." %
                      f_cb_path)
                decrypt_and_glue(f_cb_path, self.config)
                # update silo.
                self.silo.update(f_cb_path)
                self.silo.node_rem('file_created', f_cb_path)
            elif crt_num > 0:
                # means, all the shards of the file have not arrived
                # yet, so, we store the no. of shards that did arrive
                # in the 'file_created' dict inside the silo.
                self.silo.node_set('file_created', f_cb_path, crt_num)

        log_i("combox node monitor with housekeeping")
        log_i("Do what you want to the combox directory")


    def on_moved(self, event):
        """Called when a shard/directory is moved/renamed in the node directory.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`
        """
        super(NodeDirMonitor, self).on_moved(event)

        src_cb_path = cb_path(event.src_path, self.config)
        dest_cb_path = cb_path(event.dest_path, self.config)

        silo_node_dict = 'file_moved'
        cb_filename = src_cb_path

        if (not self.shardp(event.src_path) and
            not self.shardp(event.dest_path) and
            not event.is_directory):
            # The file moved is of no importance.
            return
        elif (not self.shardp(event.src_path) and
              self.shardp(event.dest_path) and
              not path.exists(dest_cb_path) and
              not event.is_directory):
            # This is Dropbox specific.
            #
            # Temp. file inside .dropbox.cache is renamed to a shard;
            # so this the first time the shard appears in this node
            # directory -- it is created.
            log_i("Got it! %s was created" % event.dest_path)
            silo_node_dict = 'file_created'
            cb_filename = dest_cb_path
        elif (self.shardp(event.src_path) and
              self.shardp(event.dest_path) and
              '.dropbox.cache' in event.dest_path and
              not event.is_directory):
            # This is Dropbox specific :|
            #
            # The shard is moved to the Dropbox's cache. This is
            # happens when a shard is either deleted or modified in
            # the Dropbox directory.
            #
            # When here we cannot tell whether the shard is
            # deleted/modified. So, first we assume that the file is
            # deleted and store this assumption in the silo[1].
            #
            # If the shard is being modified, then next Dropbox moves
            # the modified version of the shard from its cache
            # ('.dropbox.cache/' directory under the Dropox
            # directory); this we catch in the next 'elif' statement.
            silo_node_dict = 'file_deleted'
            with self.lock:
                # [1]: Store the assumption in silo.
                log_i("Assuming %s (%s) is deleted" %
                      (cb_filename, event.src_path))
                self.silo.node_set(silo_node_dict, cb_filename)
                num = self.silo.node_get(silo_node_dict, cb_filename)
                if num == self.num_nodes:
                    # If here, then our assumption that the file is
                    # deleted is true.
                    rm_path(cb_filename)
                    # remove file info from silo.
                    self.silo.remove(cb_filename)
                    self.silo.node_rem(silo_node_dict, cb_filename)
            return
        elif (not self.shardp(event.src_path) and
              self.shardp(event.dest_path) and
              path.exists(dest_cb_path) and
              not event.is_directory):
            # This is Dropbox specific :|
            #
            # Okay, if we are here that means the shard was actually
            # being modified, so our previous assumption that it was
            # deleted was wrong, so we remove that information from
            # the silo.
            cb_filename = dest_cb_path
            with self.lock:
                log_i("Okay, %s (%s) was actually modified" %
                      (cb_filename, event.dest_path))
                self.silo.node_rem('file_deleted', cb_filename)
                # Next, if we're on GNU/Linux, watchdog detects that
                # the shard was modified and generates calls the
                # on_modified method. So we don't have to store
                # information in the silo about this shard being
                # modified; we do that later in the on_modified
                # method.
                #
                # But, on OS X (a.k.a Darwin), watchdog detect the
                # shard is modified and does not generate a call to
                # the on_modified method. So we store the information
                # about the modified shard here itself.
                if platform.system() == 'Darwin':
                    self.silo.node_set('file_modified', cb_filename)
                    num = self.silo.node_get('file_modified', cb_filename)
                    if num == self.num_nodes:
                        decrypt_and_glue(cb_filename, self.config)
                        # update db.
                        self.silo.update(cb_filename)
                        self.silo.node_rem('file_modified', cb_filename)
            return


        if not path.exists(dest_cb_path):
            # means this path was moved on another computer that is
            # running combox.
            with self.lock:
                self.silo.node_set(silo_node_dict, cb_filename)
                num = self.silo.node_get(silo_node_dict, cb_filename)
                if num != self.num_nodes:
                    return
                elif silo_node_dict == 'file_created':
                    # This is Dropbox specific :|
                    # create file in cb directory.
                    log_i("Creating %s..." % cb_filename)
                    decrypt_and_glue(cb_filename, self.config)
                    # update db.
                    self.silo.update(cb_filename)
                    self.silo.node_rem('file_created', cb_filename)
                    return
                else:
                    try:
                        os.renames(src_cb_path, dest_cb_path)
                    except OSError, e:
                        log_e("Jeez, failed to rename path. %r" % e)
                    self.silo.node_rem(silo_node_dict, src_cb_path)

        if not event.is_directory:
            self.silo.remove(src_cb_path)
            self.silo.update(dest_cb_path)


    def on_created(self, event):
        """Called when a shard/directory is created in the node directory.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`
        """
        super(NodeDirMonitor, self).on_created(event)

        if not self.shardp(event.src_path) and not event.is_directory:
            # the file created can be ignored as it is not a shard or
            # a directory.
            return


        # make note of this file creation; will be used when
        # on_modified is called after this file creatiion.
        self.just_created[event.src_path] = True

        file_cb_path = cb_path(event.src_path, self.config)

        if event.is_directory and (not path.exists(file_cb_path)):
            # means, the directory was created on another computer
            # (also running combox). so, create this directory
            # under the combox directory
            with self.lock:
                self.silo.node_set('file_created', file_cb_path)
                num = self.silo.node_get('file_created', file_cb_path)

                if num == self.num_nodes:
                    os.mkdir(file_cb_path)
                    self.silo.node_rem('file_created', file_cb_path)
        elif (not event.is_directory) and path.exists(file_cb_path):
            # This can either mean the file was create on this
            # computer or if this is a Google Drive node directory and
            # the official Google Drive client is in use this means
            # the file was modified.
            #
            # Google Drive client's behavior when a file (shard) is
            # modified in the Google Drive node directory:
            #
            # - First it deletes the file.
            # - Creates the latest version the file.
            with self.lock:
                num = self.silo.node_get('file_deleted', file_cb_path)
                if num:
                    log_i("Looks like %s was actually modified!" %
                          event.src_path)
                    # This means we're in the Google Drive node
                    # directory and the official Google Drive client
                    # is in use and the file was actually modified on
                    # another computer.
                    self.silo.node_rem('file_deleted', file_cb_path)
                    self.silo.node_set('file_modified', file_cb_path)
                    num = self.silo.node_get('file_modified', file_cb_path)
                    if num == self.num_nodes:
                        log_i("Updating %s ...." % file_cb_path)
                        decrypt_and_glue(file_cb_path, self.config)
                        # update db.
                        self.silo.update(file_cb_path)
                        self.silo.node_rem('file_modified', file_cb_path)
        elif (not event.is_directory) and (not path.exists(file_cb_path)):
            # shard created.

            # means, file was created on another computer (also
            # running combox). so, reconstruct the file and put it
            # in the combox directory.
            with self.lock:
                self.silo.node_set('file_created', file_cb_path)
                num = self.silo.node_get('file_created', file_cb_path)
                if num == self.num_nodes:
                    decrypt_and_glue(file_cb_path, self.config)
                    # update db.
                    self.silo.update(file_cb_path)
                    self.silo.node_rem('file_created', file_cb_path)


    def on_deleted(self, event):
        """Called when a shard/directory is deleted in the node directory.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`
        """
        super(NodeDirMonitor, self).on_deleted(event)

        if not self.shardp(event.src_path) and not event.is_directory:
            # the file created can be ignored as it is not a shard or
            # a directory.
            return

        file_cb_path = cb_path(event.src_path, self.config)

        if event.is_directory and path.exists(file_cb_path):
            # This means the directory was deleted on a remote
            # computer.
            with self.lock:
                self.silo.node_set('file_deleted', file_cb_path)
                num = self.silo.node_get('file_deleted', file_cb_path)

                if num == self.num_nodes:
                    # Delete corresponding directory under the combox
                    # directory.
                    if os.listdir(file_cb_path):
                        # There are files under this directory that
                        # are not deleted yet, so we got to delay
                        # deletion :|
                        log_i("Marking %s for later deletion" % file_cb_path)
                        delayed_thread = Timer(15, self.delete_later,
                                               [file_cb_path])
                        delayed_thread.start()
                    else:
                        rm_path(file_cb_path)
                        self.silo.node_rem('file_deleted', file_cb_path)
        elif not event.is_directory and path.exists(file_cb_path):
            log_i("%s must have been deleted on another computer" %
                  event.src_path)
            with self.lock:
                self.silo.node_set('file_deleted', file_cb_path)
                num = self.silo.node_get('file_deleted', file_cb_path)
                # If we are in a Google Drive node directory and
                # the official Google Drive client is in use, at
                # this point we cannot tell if the file was
                # deleted; it can be a file modification or rename
                # or deletion.
                #
                # Therefore, wait for 2secs and then delete the
                # file_cb_path iff the file_cb_path was really
                # removed on the another computer.
                log_i("Marking %s for later deletion" % file_cb_path)
                delayed_thread = Timer(3, self.delete_later,
                                       [file_cb_path])
                delayed_thread.start()


    def on_modified(self, event):
        """Called when a shard/directory is modified in the node directory.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`~watchdog.events.FileSystemEvent`
        """
        super(NodeDirMonitor, self).on_modified(event)

        if not self.shardp(event.src_path) and not event.is_directory:
            # the file created can be ignored as it is not a shard or
            # a directory.
            return

        file_cb_path = cb_path(event.src_path, self.config)

        # get no. shards available
        shards_there = no_of_shards(file_cb_path, self.config)

        if shards_there != self.num_nodes:
            # got to wait for other shards to arrive!
            return

        if event.is_directory:
            # do nothing
            pass
        elif (not event.is_directory):
            # get file size first
            f_size_MiB = path.getsize(event.src_path) / 1048576.0
            log_i('%s modified %f' % (event.src_path, f_size_MiB))

            # introduce delay to prevent multiple "file modified"
            # events from being generated.
            if f_size_MiB >= 30:
                sleep_time = (f_size_MiB / 30.0)
                log_i("waiting on_modified node monitor %f" % sleep_time)
                time.sleep(sleep_time)
                log_i("end waiting on_modified node monitor")
            else:
                time.sleep(1)

            # watchdog's weirdness
            # --------------------
            #
            # On GNU/Linux, when a file is created, watchdog generates
            # a 'file created' and 'file modified' event; we're
            # tracking this behaviour and ignoring the 'file modified'
            # event.
            #
            if (self.just_created.has_key(event.src_path) and
                self.just_created[event.src_path] and
                platform.system() == 'Linux'):
                self.just_created[event.src_path] = False
                log_i("Just created file %s; ignoring on_modified call." % (
                    event.src_path))
                return

            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) == True:
                log_i("Found %s stale. Updating it..." % file_cb_path)
                # shard modified

                # means, file was modified on another computer (also
                # running combox). so, reconstruct the file and put it
                # in the combox directory.
                with self.lock:
                    self.silo.node_set('file_modified', file_cb_path)
                    num = self.silo.node_get('file_modified', file_cb_path)
                    if num == self.num_nodes:
                        decrypt_and_glue(file_cb_path, self.config)
                        # update db.
                        self.silo.update(file_cb_path)
                        self.silo.node_rem('file_modified', file_cb_path)
            else:
                log_i("Local modification of %s" % file_cb_path)