Implement basic full log replay.
[bluesky.git] / bluesky / serialize.c
index e453f49..792b01a 100644 (file)
@@ -67,10 +67,16 @@ BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf)
     return fs;
 }
 
-void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
+BlueSkyCloudLog *bluesky_serialize_inode(BlueSkyInode *inode)
 {
+    BlueSkyFS *fs = inode->fs;
+    GString *out = g_string_new("");
     struct serialized_inode buf;
 
+    BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs, NULL);
+    cloudlog->type = LOGTYPE_INODE;
+    cloudlog->inum = inode->inum;
+
     buf.signature = GUINT64_TO_LE(INODE_MAGIC);
     buf.type = GUINT32_TO_LE(inode->type);
     buf.mode = GUINT32_TO_LE(inode->mode);
@@ -93,9 +99,9 @@ void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
         g_string_append_len(out, (gchar *)&size, sizeof(uint64_t));
         for (int i = 0; i < inode->blocks->len; i++) {
             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
-            if (b->ref != NULL)
-                g_string_append(out, b->ref);
-            g_string_append_c(out, '\0');
+            BlueSkyCloudLog *ref = (b->type == BLUESKY_BLOCK_REF ? b->ref : NULL);
+            bluesky_cloudlog_ref(ref);
+            g_array_append_val(cloudlog->links, ref);
         }
         break;
     }
@@ -136,12 +142,21 @@ void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
         g_warning("Serialization for inode type %d not implemented!\n",
                   inode->type);
     }
+
+    cloudlog->data = bluesky_string_new_from_gstring(out);
+    bluesky_cloudlog_insert(cloudlog);
+    bluesky_cloudlog_stats_update(cloudlog, 1);
+
+    return cloudlog;
 }
 
 /* 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);
@@ -178,11 +193,13 @@ gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf)
         g_array_set_size(inode->blocks,
                          (inode->size + BLUESKY_BLOCK_SIZE - 1)
                           / BLUESKY_BLOCK_SIZE);
+        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;
         }
         break;
 
@@ -227,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));
+        }
+    }
+}