Implement basic full log replay.
[bluesky.git] / bluesky / serialize.c
index f90c63a..792b01a 100644 (file)
@@ -152,8 +152,11 @@ BlueSkyCloudLog *bluesky_serialize_inode(BlueSkyInode *inode)
 
 /* Deserialize an inode into an in-memory representation.  Returns a boolean
  * indicating whether the deserialization was successful. */
-gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf)
+gboolean bluesky_deserialize_inode(BlueSkyInode *inode, BlueSkyCloudLog *item)
 {
+    g_assert(item->data != NULL);
+    const char *buf = item->data->data;
+
     if (bluesky_verbose) {
         g_log("bluesky/serialize", G_LOG_LEVEL_DEBUG,
               "Deserializing inode %lld...", (long long)inode->inum);
@@ -190,15 +193,14 @@ gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf)
         g_array_set_size(inode->blocks,
                          (inode->size + BLUESKY_BLOCK_SIZE - 1)
                           / BLUESKY_BLOCK_SIZE);
-        // TODO
-#if 0
+        g_assert(inode->blocks->len == item->links->len);
         for (int i = 0; i < inode->blocks->len; i++) {
             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
             b->type = BLUESKY_BLOCK_REF;
-            b->ref = g_strdup(buf);
-            buf += strlen(b->ref) + 1;
+            b->ref = g_array_index(item->links, BlueSkyCloudLog *, i);
+            bluesky_cloudlog_ref(b->ref);
+            b->dirty = NULL;
         }
-#endif
         break;
 
     case BLUESKY_DIRECTORY:
@@ -242,3 +244,29 @@ gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf)
 
     return TRUE;
 }
+
+/* Convert an in-memory cloud log item to a more serialized form, suitable
+ * either for writing to the local journal or the the cloud. */
+void bluesky_serialize_cloudlog(BlueSkyCloudLog *log,
+                                GString *encrypted,     // Raw data payload
+                                GString *authenticated, // Block links
+                                GString *writable)      // Writable block links
+{
+    g_string_append_len(encrypted, log->data->data, log->data->len);
+    for (int i = 0; i < log->links->len; i++) {
+        BlueSkyCloudLog *ref = g_array_index(log->links, BlueSkyCloudLog *, i);
+        if (ref != NULL) {
+            g_string_append_len(authenticated,
+                                (const char *)&ref->id,
+                                sizeof(BlueSkyCloudID));
+            // TODO: Fix endianness of output
+            g_string_append_len(writable,
+                                (const char *)&ref->location,
+                                sizeof(ref->location));
+        } else {
+            BlueSkyCloudID id;
+            memset(&id, 0, sizeof(id));
+            g_string_append_len(authenticated, (const char *)&id, sizeof(id));
+        }
+    }
+}