Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/gdatastore/datastore.py
blob: cfb6074528f01b8e59f0735488667ddd67bacf0e (plain)
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
#
# Author: Sascha Silbe <sascha-pgp@silbe.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""
Gdatastore D-Bus service API
"""

import ast
import hashlib
import logging
import os
import pprint
import re
import shutil
from subprocess import Popen, PIPE
import tempfile
import time
import uuid

import dbus
import dbus.service
import gconf

try:
    from sugar import mime as sugar_mime
except ImportError:
    # Only used for helping legacy applications that use the file
    # extension rather than the MIME type
    sugar_mime = None

from gdatastore.index import Index


DBUS_SERVICE_NATIVE_V1 = 'org.silbe.GDataStore'
DBUS_INTERFACE_NATIVE_V1 = 'org.silbe.GDataStore1'
DBUS_PATH_NATIVE_V1 = '/org/silbe/GDataStore1'

DBUS_SERVICE_SUGAR_V2 = 'org.laptop.sugar.DataStore'
DBUS_INTERFACE_SUGAR_V2 = 'org.laptop.sugar.DataStore'
DBUS_PATH_SUGAR_V2 = '/org/laptop/sugar/DataStore'

DBUS_SERVICE_SUGAR_V3 = 'org.laptop.sugar.DataStore'
DBUS_INTERFACE_SUGAR_V3 = 'org.laptop.sugar.DataStore2'
DBUS_PATH_SUGAR_V3 = '/org/laptop/sugar/DataStore2'

_DBUS_METADATA_BASIC_RE = re.compile(
    r"""dbus.(U?Int(16|32|64)|Double|String|ByteArray)\((?P<value>(-?[0-9]+(\.[0-9]*)?)|(u?('([^'\\]|\\.)*'|"([^"\\]|\\.)*")))(, variant_level=[0-9]+)?\)""")
_DBUS_METADATA_DICTIONARY_RE = re.compile(
    r"""dbus.Dictionary\((?P<value>\{.*\}), signature=dbus.Signature\('s[sv]'\)\)""")


class DataStoreError(Exception):
    pass


class GitError(DataStoreError):
    def __init__(self, returncode, stderr):
        self.returncode = returncode
        self.stderr = unicode(stderr)
        Exception.__init__(self)

    def __unicode__(self):
        return u'Git returned with exit code #%d: %s' % (self.returncode,
                                                         self.stderr)

    def __str__(self):
        return self.__unicode__()


class DBusApiNativeV1(dbus.service.Object):
    """Native gdatastore D-Bus API
    """

    def __init__(self, internal_api):
        self._internal_api = internal_api
        bus_name = dbus.service.BusName(DBUS_SERVICE_NATIVE_V1,
                                        bus=dbus.SessionBus(),
                                        replace_existing=False,
                                        allow_replacement=False,
                                        do_not_queue=True)
        dbus.service.Object.__init__(self, bus_name, DBUS_PATH_NATIVE_V1)
        self._internal_api.add_callback('change_metadata',
                                        self.__change_metadata_cb)
        self._internal_api.add_callback('delete', self.__delete_cb)
        self._internal_api.add_callback('save', self.__save_cb)

    @dbus.service.signal(DBUS_INTERFACE_NATIVE_V1, signature='sssa{sv}')
    def AddedNewVersion(self, tree_id, child_id, parent_id, metadata):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.signal(DBUS_INTERFACE_NATIVE_V1, signature='ssa{sv}')
    def Created(self, tree_id, child_id, metadata):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.signal(DBUS_INTERFACE_NATIVE_V1, signature='ssa{sv}')
    def ChangedMetadata(self, tree_id, version_id, metadata):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.signal(DBUS_INTERFACE_NATIVE_V1, signature='ss')
    def Deleted(self, tree_id, version_id):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='a{sv}ay', out_signature='ss',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def create(self, metadata, data_path, async_cb, async_err_cb):
        """
        - add new entry, assign ids
        - data='' indicates no data to store
        - bad design? (data OOB)
        """
        # TODO: what about transfer_ownership/delete_after?
        self._internal_api.save(tree_id='', parent_id='', metadata=metadata,
                                path=data_path,
                                delete_after=True,
                                async_cb=async_cb,
                                async_err_cb=async_err_cb)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='a{sv}h', out_signature='ss',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def create_fd(self, metadata, data_fd, async_cb, async_err_cb):
        """
        - add new entry, assign ids
        """
        # FIXME: avoid unnecessary copy, instead change
        # InternalAPI.save() to take fd and adapt existing callers.
        tmp_file = tempfile.NamedTemporaryFile(delete=False)
        with os.fdopen(data_fd.take(), 'rb') as data_file:
            shutil.copyfileobj(data_file, tmp_file)

        tmp_file.flush()
        self._internal_api.save(tree_id='', parent_id='', metadata=metadata,
                                path=tmp_file.name, delete_after=True,
                                async_cb=async_cb,
                                async_err_cb=async_err_cb)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='ssa{sv}ay', out_signature='s',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def add_version(self, tree_id, parent_id, metadata, data_path, async_cb,
                    async_err_cb):
        """
        - add new version to existing object
        """
        def success_cb(tree_id, child_id):
            async_cb(child_id)

        if not tree_id:
            raise ValueError('No tree_id given')

        if not parent_id:
            raise ValueError('No parent_id given')

        self._internal_api.save(tree_id, parent_id, metadata,
                                data_path,
                                delete_after=True,
                                async_cb=success_cb,
                                async_err_cb=async_err_cb)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='ssa{sv}h', out_signature='s',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def add_version_fd(self, tree_id, parent_id, metadata, data_fd, async_cb,
                    async_err_cb):
        """
        - add new version to existing object
        """
        def success_cb(tree_id, child_id):
            async_cb(child_id)

        if not tree_id:
            raise ValueError('No tree_id given')

        if not parent_id:
            raise ValueError('No parent_id given')

        # FIXME: avoid unnecessary copy, instead change
        # InternalAPI.save() to take fd and adapt existing callers.
        tmp_file = tempfile.NamedTemporaryFile(delete=False)
        with os.fdopen(data_fd.take(), 'rb') as data_file:
            shutil.copyfileobj(data_file, tmp_file)

        tmp_file.flush()
        self._internal_api.save(tree_id, parent_id, metadata,
                                path=tmp_file.name, delete_after=True,
                                async_cb=success_cb,
                                async_err_cb=async_err_cb)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='ssa{sv}', out_signature='',
                         byte_arrays=True)
    def change_metadata(self, tree_id, version_id, metadata):
        """
        - change the metadata of an existing version
        """
        object_id = (tree_id, version_id)
        self._internal_api.change_metadata(object_id, metadata)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='ss', out_signature='')
    def delete(self, tree_id, version_id):
        object_id = (tree_id, version_id)
        self._internal_api.delete(object_id)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='a{sv}a{sv}', out_signature='aa{sv}u',
                         byte_arrays=True)
    def find(self, query_dict, options):
        return self._internal_api.find(query_dict, options)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='ss', out_signature='ay',
                         sender_keyword='sender')
    def get_data_path(self, tree_id, version_id, sender=None):
        object_id = (tree_id, version_id)
        return self._internal_api.get_data_path(object_id, sender=sender)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='ss', out_signature='h',
                         sender_keyword='sender')
    def get_data_fd(self, tree_id, version_id, sender=None):
        object_id = (tree_id, version_id)
        path = self._internal_api.get_data_path(object_id, sender=sender)
        return os.open(path, os.O_RDONLY)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='ss', out_signature='a{sv}')
    def get_metadata(self, tree_id, version_id):
        object_id = (tree_id, version_id)
        return self._internal_api.get_properties(object_id)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='a{sv}sa{sv}', out_signature='aa{sv}u',
                         byte_arrays=True)
    def text_search(self, query_dict, query_string, options):
        return self._internal_api.find(query_dict, options, query_string)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='sssa{sv}ay', out_signature='ss',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def restore(self, tree_id, parent_id, version_id, metadata, data_path,
                async_cb, async_err_cb):
        """
        - add a new version with the given ids
        - there must be no existing entry with the same (tree_id, version_id)
        """
        if not tree_id:
            raise ValueError('No tree_id given')

        metadata['version_id'] = version_id
        self._internal_api.save(tree_id, parent_id, metadata, data_path,
                                delete_after=True, allow_new_parent=True,
                                async_cb=async_cb,
                                async_err_cb=async_err_cb)

    @dbus.service.method(DBUS_INTERFACE_NATIVE_V1,
                         in_signature='sssa{sv}s', out_signature='ss',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def restore_fd(self, tree_id, parent_id, version_id, metadata, data_fd,
                async_cb, async_err_cb):
        """
        - add a new version with the given ids
        - there must be no existing entry with the same (tree_id, version_id)
        """
        if not tree_id:
            raise ValueError('No tree_id given')

        metadata['version_id'] = version_id
        # FIXME: avoid unnecessary copy, instead change
        # InternalAPI.save() to take fd and adapt existing callers.
        tmp_file = tempfile.NamedTemporaryFile(delete=False)
        with os.fdopen(data_fd.take(), 'rb') as data_file:
            shutil.copyfileobj(data_file, tmp_file)

        tmp_file.flush()
        self._internal_api.save(tree_id, parent_id, metadata, tmp_file.name,
                                delete_after=True, allow_new_parent=True,
                                async_cb=async_cb,
                                async_err_cb=async_err_cb)

    def __change_metadata_cb(self, (tree_id, version_id), metadata):
        self.ChangedMetadata(tree_id, version_id, metadata)

    def __delete_cb(self, (tree_id, version_id)):
        self.Deleted(tree_id, version_id)

    def __save_cb(self, tree_id, child_id, parent_id, metadata):
        if parent_id:
            self.AddedNewVersion(tree_id, child_id, parent_id, metadata)
        else:
            self.Created(tree_id, child_id, metadata)


class DBusApiSugarV2(dbus.service.Object):
    """Compatibility layer for the Sugar 0.84+ data store D-Bus API
    """

    def __init__(self, internal_api):
        self._internal_api = internal_api
        bus_name = dbus.service.BusName(DBUS_SERVICE_SUGAR_V2,
                                        bus=dbus.SessionBus(),
                                        replace_existing=False,
                                        allow_replacement=False,
                                        do_not_queue=True)
        dbus.service.Object.__init__(self, bus_name, DBUS_PATH_SUGAR_V2)
        self._internal_api.add_callback('change_metadata',
                                        self.__change_metadata_cb)
        self._internal_api.add_callback('delete', self.__delete_cb)
        self._internal_api.add_callback('save', self.__save_cb)

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='a{sv}sb', out_signature='s',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def create(self, props, file_path, transfer_ownership,
               async_cb, async_err_cb):
        def success_cb(tree_id, child_id):
            async_cb(tree_id)

        self._internal_api.save(tree_id='', parent_id='', metadata=props,
                                path=file_path.encode('utf-8'),
                                delete_after=transfer_ownership,
                                async_cb=success_cb,
                                async_err_cb=async_err_cb)

    @dbus.service.signal(DBUS_INTERFACE_SUGAR_V2, signature='s')
    def Created(self, uid):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='sa{sv}sb', out_signature='',
                         async_callbacks=('async_cb', 'async_err_cb'),
                         byte_arrays=True)
    def update(self, uid, props, file_path, transfer_ownership,
               async_cb, async_err_cb):
        def success_cb(tree_id, child_id):
            async_cb()

        latest_versions = self._get_latest(uid)
        if not latest_versions:
            raise ValueError('Trying to update non-existant entry %s - wanted'
                ' to use create()?' % (uid, ))

        parent = latest_versions[0]
        object_id = parent['tree_id'], parent['version_id']
        if self._check_identical(parent, file_path):
            self._internal_api.change_metadata(object_id, props)
            return success_cb(uid, None)

        self._internal_api.save(tree_id=uid,
            parent_id=parent['version_id'], metadata=props,
            path=file_path.encode('utf-8'), delete_after=transfer_ownership,
            async_cb=success_cb, async_err_cb=async_err_cb)

    @dbus.service.signal(DBUS_INTERFACE_SUGAR_V2, signature='s')
    def Updated(self, uid):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='a{sv}as', out_signature='aa{sv}u')
    def find(self, query, properties):
        if 'uid' in properties:
            properties.append('tree_id')
            properties.remove('uid')

        options = {'metadata': properties}
        for name in ['offset', 'limit', 'order_by']:
            if name in query:
                options[name] = query.pop(name)

        if 'uid' in query:
            query['tree_id'] = query.pop('uid')

        results, count = self._internal_api.find(query, options,
            query.pop('query', None))

        if not properties or 'tree_id' in properties:
            for entry in results:
                entry['uid'] = entry.pop('tree_id')

        return results, count

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='s', out_signature='s',
                         sender_keyword='sender')
    def get_filename(self, uid, sender=None):
        latest_versions = self._get_latest(uid)
        if not latest_versions:
            raise ValueError('Entry %s does not exist' % (uid, ))

        object_id = (uid, latest_versions[0]['version_id'])
        return self._internal_api.get_data_path(object_id, sender=sender)

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='s', out_signature='a{sv}')
    def get_properties(self, uid):
        latest_versions = self._get_latest(uid)
        if not latest_versions:
            raise ValueError('Entry %s does not exist' % (uid, ))

        latest_versions[0]['uid'] = latest_versions[0].pop('tree_id')
        del latest_versions[0]['version_id']
        return latest_versions[0]

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='sa{sv}', out_signature='as')
    def get_uniquevaluesfor(self, propertyname, query=None):
        return self._internal_api.find_unique_values(query, propertyname)

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='s', out_signature='')
    def delete(self, uid):
        latest_versions = self._get_latest(uid)
        if not latest_versions:
            raise ValueError('Entry %s does not exist' % (uid, ))

        self._internal_api.delete((uid, latest_versions[0]['version_id']))

    @dbus.service.signal(DBUS_INTERFACE_SUGAR_V2, signature='s')
    def Deleted(self, uid):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.method(DBUS_INTERFACE_SUGAR_V2,
                         in_signature='', out_signature='aa{sv}')
    def mounts(self):
        return [{'id': 1}]

    @dbus.service.signal(DBUS_INTERFACE_SUGAR_V2, signature='a{sv}')
    def Mounted(self, descriptior):
        # pylint: disable-msg=C0103
        pass

    @dbus.service.signal(DBUS_INTERFACE_SUGAR_V2, signature='a{sv}')
    def Unmounted(self, descriptor):
        # pylint: disable-msg=C0103
        pass

    def _get_latest(self, uid):
        return self._internal_api.find({'tree_id': uid},
            {'limit': 1, 'order_by': ['+timestamp']})[0]

    def _check_identical(self, parent, child_data_path):
        """Check whether the new version contains the same data as the parent

        If child_data_path is empty, but the parent contains data, that's
        interpreted as wanting to do a metadata-only update (emulating
        sugar-datastore behaviour).
        """
        parent_object_id = (parent['tree_id'], parent['version_id'])
        parent_data_path = self._internal_api.get_data_path(parent_object_id)
        if not child_data_path:
            return True
        elif child_data_path and not parent_data_path:
            return False

        # TODO: compare checksums?
        return False

    def __change_metadata_cb(self, (tree_id, version_id), metadata):
        self.Updated(tree_id)

    def __delete_cb(self, (tree_id, version_id)):
        if self._get_latest(tree_id):
            self.Updated(tree_id)
        else:
            self.Deleted(tree_id)

    def __save_cb(self, tree_id, child_id, parent_id, metadata):
        if parent_id:
            self.Updated(tree_id)
        else:
            self.Created(tree_id)


class InternalApi(object):

    SIGNALS = ['change_metadata', 'delete', 'save']

    def __init__(self, base_dir):
        self._base_dir = base_dir
        self._callbacks = {}
        self._checkouts_dir = os.path.join(base_dir, 'checkouts')
        if not os.path.exists(self._checkouts_dir):
            os.makedirs(self._checkouts_dir)
        self._git_dir = os.path.join(base_dir, 'git')
        self._git_env = {}
        gconf_client = gconf.client_get_default()
        self._max_versions = gconf_client.get_int(
            '/desktop/sugar/datastore/max_versions')
        logging.debug('max_versions=%r', self._max_versions)
        self._index = Index(os.path.join(self._base_dir, 'index'))
        self._migrate()
        self._check_reindex()
        logging.info('ready')

    def add_callback(self, signal, callback):
        if signal not in InternalApi.SIGNALS:
            raise ValueError('Invalid signal %r' % (signal, ))

        self._callbacks.setdefault(signal, []).append(callback)

    def change_metadata(self, object_id, metadata):
        logging.debug('change_metadata(%r, %r)', object_id, metadata)
        metadata['tree_id'], metadata['version_id'] = object_id
        if 'creation_time' not in metadata:
            old_metadata = self._index.retrieve(object_id)
            metadata['creation_time'] = old_metadata['creation_time']

        self._index.store(object_id, metadata)
        self._invoke_callbacks('change_metadata', object_id, metadata)

    def delete(self, object_id):
        logging.debug('delete(%r)', object_id)
        self._index.delete(object_id)
        self._git_call('update-ref', ['-d', _format_ref(*object_id)])
        self._invoke_callbacks('delete', object_id)

    def get_data_path(self, (tree_id, version_id), sender=None):
        logging.debug('get_data_path((%r, %r), %r)', tree_id, version_id,
                      sender)
        metadata = self._index.retrieve((tree_id, version_id))
        ref_name = _format_ref(tree_id, version_id)
        top_level_entries = self._git_call('ls-tree',
                                           [ref_name]).splitlines()
        if len(top_level_entries) == 1 and \
           top_level_entries[0].endswith('\tdata'):
            blob_hash = top_level_entries[0].split('\t')[0].split(' ')[2]
            mime_type = metadata.get('mime_type', '')
            return self._checkout_file(blob_hash,
                                       suffix=_guess_extension(mime_type))

        return self._checkout_dir(ref_name)

    def find(self, query_dict, options, query_string=None):
        logging.debug('find(%r, %r, %r)', query_dict, options, query_string)
        entries, total_count = self._index.find(query_dict, query_string,
                                                options)
        #logging.debug('object_ids=%r', object_ids)
        property_names = options.pop('metadata', None)
        for entry in entries:
            for name in entry.keys():
                if property_names and name not in property_names:
                    del entry[name]
                elif isinstance(entry[name], str):
                    entry[name] = dbus.ByteArray(entry[name])

        return entries, total_count

    def find_unique_values(self, query, name):
        logging.debug('find_unique_values(%r, %r)', query, name)
        if query:
            raise NotImplementedError('non-empty query not supported yet')

        return self._index.find_unique_values(name)

    def get_properties(self, object_id):
        return self._index.retrieve(object_id)

    def save(self, tree_id, parent_id, metadata, path, delete_after, async_cb,
             async_err_cb, allow_new_parent=False):
        logging.debug('save(%r, %r, %r, %r, %r)', tree_id, parent_id,
                      metadata, path, delete_after)

        if path:
            path = os.path.realpath(path)
            if not os.access(path, os.R_OK):
                raise ValueError('Invalid path given.')

            if delete_after and not os.access(os.path.dirname(path), os.W_OK):
                raise ValueError('Deletion requested for read-only directory')

        if (not tree_id) and parent_id:
            raise ValueError('tree_id is empty but parent_id is not')

        if tree_id and not parent_id and not allow_new_parent:
            if self.find({'tree_id': tree_id}, {'limit': 1})[1]:
                raise ValueError('No parent_id given but tree_id already '
                                 'exists')

        if not tree_id:
            tree_id = self._gen_uuid()

        child_id = metadata.get('version_id')
        if not child_id:
            child_id = self._gen_uuid()
        elif not tree_id:
            raise ValueError('No tree_id given but metadata contains'
                             ' version_id')
        elif self._index.contains((tree_id, child_id)):
            raise ValueError('There is an existing entry with the same tree_id'
                             ' and version_id')

        if 'timestamp' not in metadata:
            metadata['timestamp'] = time.time()

        if 'creation_time' not in metadata:
            metadata['creation_time'] = metadata['timestamp']

        if os.path.isfile(path):
            metadata['filesize'] = str(os.stat(path).st_size)
        elif not path:
            metadata['filesize'] = '0'

        tree_id = str(tree_id)
        parent_id = str(parent_id)
        child_id = str(child_id)

        metadata['tree_id'] = tree_id
        metadata['version_id'] = child_id

        # TODO: check metadata for validity first (index?)
        self._log_store((tree_id, child_id))
        self._store_entry_in_git(tree_id, child_id, parent_id, path, metadata)
        self._index.store((tree_id, child_id), metadata)
        self._invoke_callbacks('save', tree_id, child_id, parent_id, metadata)

        if delete_after and path:
            os.remove(path)

        async_cb(tree_id, child_id)

    def stop(self):
        logging.debug('stop()')
        self._index.close()

    def _add_to_git_index(self, index_path, path):
        if os.path.isdir(path):
            self._git_call('add', ['-A'], work_dir=path, index_path=index_path)
        elif os.path.isfile(path):
            object_hash = self._git_call('hash-object', ['-w', path]).strip()
            mode = os.stat(path).st_mode
            self._git_call('update-index',
                           ['--add',
                            '--cacheinfo', oct(mode), object_hash, 'data'],
                           index_path=index_path)
        else:
            raise DataStoreError('Refusing to store special object %r' % (path, ))

    def _check_max_versions(self, tree_id):
        if not self._max_versions:
            return

        options = {'all_versions': True, 'offset': self._max_versions,
                   'metadata': ['tree_id', 'version_id', 'timestamp'],
                   'order_by': ['+timestamp']}
        old_versions = self.find({'tree_id': tree_id}, options)[0]
        logging.info('Deleting old versions: %r', old_versions)
        for entry in old_versions:
            self.delete((entry['tree_id'], entry['version_id']))

    def _checkout_file(self, blob_hash, suffix=''):
        fd, file_name = tempfile.mkstemp(dir=self._checkouts_dir, suffix=suffix)
        try:
            self._git_call('cat-file', ['blob', blob_hash], stdout_fd=fd)
        finally:
            os.close(fd)
        return file_name

    def _checkout_dir(self, ref_name):
        # FIXME
        return ''

    def _create_repo(self):
        os.makedirs(self._git_dir)
        self._git_call('init', ['-q', '--bare'])

    def _migrate(self):
        if not os.path.exists(self._git_dir):
            return self._create_repo()

    def _check_reindex(self):
        """Recreate or update index if necessary
        """
        last_object_id = self._get_last_object_id_from_log()
        # Non-existence of the log (i.e. last_object_id=None) does not
        # necessarily mean an empty data store: We could be upgrading
        # from a previous version that didn't write the file, a file
        # system corruption may have occured or the user may have
        # deleted the log to force reindexing. This operation is cheap
        # enough on empty data stores that we don't care about the
        # performance impact on valid, empty data stores.
        if not last_object_id or not self._index.contains(last_object_id):
            logging.info('Rebuilding index')
            self._reindex()

    def _reindex(self):
        """Recreate or update index from git repository

        Log the last object after finishing the rebuild.
        """
        last_object_id = None
        for object_id in self._get_object_ids_from_git():
            last_object_id = object_id
            logging.debug('reindex(): checking entry %r', object_id)
            if self._index.contains(object_id):
                continue

            logging.debug('reindex(): adding entry %r from git', object_id)
            metadata = self._get_metadata_from_git(object_id)
            self._index.store(object_id, metadata)

        if last_object_id:
            self._log_store(last_object_id)

    def _format_commit_message(self, metadata):
        return pprint.pformat(to_native(metadata))

    def _parse_commit_message(self, commit_message):
        try:
            return ast.literal_eval(commit_message)
        except ValueError:
            return self._parse_commit_message_dbus(commit_message)

    def _parse_commit_message_dbus(self, commit_message):
        # Compatibility work-around to parse commit messages
        # written by previous versions and containing dbus.Int()
        # instead of plain integer literals.
        num_subs = 1
        while num_subs:
            commit_message, num_subs = re.subn(_DBUS_METADATA_DICTIONARY_RE,
                                              '\g<value>', commit_message)
        num_subs = 1
        while num_subs:
            commit_message, num_subs = re.subn(_DBUS_METADATA_BASIC_RE,
                                              '\g<value>', commit_message)

        return ast.literal_eval(commit_message)

    def _gen_uuid(self):
        return str(uuid.uuid4())

    def _git_call(self, command, args=None, input=None, input_fd=None,
                  stdout_fd=None, work_dir=None, index_path=None):
        env = dict(self._git_env)
        if work_dir:
            env['GIT_WORK_TREE'] = work_dir
        if index_path:
            env['GIT_INDEX_FILE'] = index_path
        logging.debug('calling git %s, env=%r', ['git', command] + (args or []), env)
        pipe = Popen(['git', command] + (args or []), stdin=input_fd or PIPE,
                     stdout=stdout_fd or PIPE, stderr=PIPE, close_fds=True,
                     cwd=self._git_dir, env=env)
        stdout, stderr = pipe.communicate(input)
        if pipe.returncode:
            raise GitError(pipe.returncode, stderr)
        return stdout

    def _invoke_callbacks(self, signal, *args):
        for callback in self._callbacks.get(signal, []):
            callback(*args)

    def _store_entry_in_git(self, tree_id, version_id, parent_id, path, metadata):
        commit_message = self._format_commit_message(metadata)
        tree_hash = self._write_tree(path)
        commit_hash = self._git_call('commit-tree', [tree_hash],
                                     input=commit_message).strip()
        self._git_call('update-ref', [_format_ref(tree_id, version_id),
                                      commit_hash])

    def _write_tree(self, path):
        if not path:
            return self._git_call('hash-object',
                                  ['-w', '-t', 'tree', '--stdin'],
                                  input='').strip()

        index_dir = tempfile.mkdtemp(prefix='gdatastore-')
        index_path = os.path.join(index_dir, 'index')
        try:
            self._add_to_git_index(index_path, path)
            return self._git_call('write-tree', index_path=index_path).strip()
        finally:
            shutil.rmtree(index_dir)

    def _get_object_ids_from_git(self):
        args = ['--sort=committerdate', '--format=%(refname)',
                'refs/gdatastore/*/*']
        return [tuple(line.rsplit('/', 2)[1:])
                for line in self._git_call('for-each-ref', args).split()]

    def _get_metadata_from_git(self, object_id):
        args = ['commit', _format_ref(*object_id)]
        commit_message = self._git_call('cat-file', args).split('\n\n', 1)[1]
        return self._parse_commit_message(commit_message)

    def _log_store(self, object_id):
        """Record the fact that we tried to store the given object

        Make sure we know on next start-up that the object with the
        given object_id was the last one to be processed. Used for
        checking the index on start-up and triggering a rebuild if
        necessary.
        """
        log_name = os.path.join(self._base_dir, 'last_object_id')
        tmp_name = log_name + '.tmp'
        with open(tmp_name, 'w') as f:
            f.write(repr(tuple(object_id)))
            f.flush()
            os.fsync(f.fileno())

        os.rename(tmp_name, log_name)

    def _get_last_object_id_from_log(self):
        """Return the object_id saved by _log_store()

        Return the object_id of the last object to be processed, as
        written by _log_store(). If no such log exists, return None.
        """
        log_name = os.path.join(self._base_dir, 'last_object_id')
        if not os.path.exists(log_name):
            return None

        return ast.literal_eval(open(log_name).read())


def calculate_checksum(path):
    checksum = hashlib.sha1()
    f = file(path)
    while True:
        chunk = f.read(65536)
        if not chunk:
            return checksum.hexdigest()

        checksum.update(chunk)


def to_native(value):
    if isinstance(value, list):
        return [to_native(e) for e in value]
    elif isinstance(value, dict):
        return dict([(to_native(k), to_native(v)) for k, v in value.items()])
    elif isinstance(value, unicode):
        return unicode(value)
    elif isinstance(value, str):
        return str(value)
    elif isinstance(value, int):
        return int(value)
    elif isinstance(value, float):
        return float(value)
    else:
        raise TypeError('Unknown type: %s' % (type(value), ))


def _format_ref(tree_id, version_id):
    return 'refs/gdatastore/%s/%s' % (tree_id, version_id)


def _guess_extension(mime_type):
    if sugar_mime is None:
        return ''
    extension = sugar_mime.get_primary_extension(mime_type)
    if not extension:
        return ''
    return '.' + extension