[Sugar-devel] [PATCH sugar-datastore] Ensure we return valid internal / calculated properties

Sascha Silbe silbe at activitycentral.com
Wed Nov 2 18:21:52 EDT 2011


The copy in the metadata storage can get corrupted, e.g. due to low level
crashes or running out of battery (see OLPC#11372 [1] for a real-life
example).

This is especially problematic for the uid property, since without it the
caller (i.e. the Journal) can't even figure out which entry to delete.

[1] https://dev.laptop.org/ticket/11372

Reported-by: Gary Martin <garycmartin at googlemail.com>
Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---

Passes the test suite - at least after fixing the latter to accept decimal
strings instead of just integers for the 'filesize' property. No difference
in performance noticeable with three runs of the test suite (default
settings, on my desktop machine).

 src/carquinyol/datastore.py |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/src/carquinyol/datastore.py b/src/carquinyol/datastore.py
index 4f3faba..eafc8e1 100644
--- a/src/carquinyol/datastore.py
+++ b/src/carquinyol/datastore.py
@@ -333,6 +333,7 @@ class DataStore(dbus.service.Object):
                 return self._find_all(query, properties)

             metadata = self._metadata_store.retrieve(uid, properties)
+            self._fill_internal_props(metadata, uid, properties)
             entries.append(metadata)

         logger.debug('find(): %r', time.time() - t)
@@ -350,10 +351,28 @@ class DataStore(dbus.service.Object):
         entries = []
         for uid in uids:
             metadata = self._metadata_store.retrieve(uid, properties)
+            self._fill_internal_props(metadata, uid, properties)
             entries.append(metadata)

         return entries, count

+    def _fill_internal_props(self, metadata, uid, names=None):
+        """Fill in internal / computed properties in metadata
+
+        Properties are only set if they appear in names or if names is
+        empty.
+        """
+        if not names or 'uid' in names:
+            metadata['uid'] = uid
+
+        if not names or 'filesize' in names:
+            file_path = self._file_store.get_file_path(uid)
+            if os.path.exists(file_path):
+                stat = os.stat(file_path)
+                metadata['filesize'] = str(stat.st_size)
+            else:
+                metadata['filesize'] = '0'
+
     @dbus.service.method(DS_DBUS_INTERFACE,
              in_signature='s',
              out_signature='s',
@@ -376,6 +395,7 @@ class DataStore(dbus.service.Object):
     def get_properties(self, uid):
         logging.debug('datastore.get_properties %r', uid)
         metadata = self._metadata_store.retrieve(uid)
+        self._fill_internal_props(metadata, uid)
         return metadata

     @dbus.service.method(DS_DBUS_INTERFACE,
--
1.7.6.3



More information about the Sugar-devel mailing list