Improve tracking of memory usage in BlueSky.
[bluesky.git] / bluesky / serialize.c
index 7f7f041..6eef425 100644 (file)
@@ -11,7 +11,7 @@
 #include <glib.h>
 #include <string.h>
 
-#include "bluesky.h"
+#include "bluesky-private.h"
 
 /* Serialization of in-memory filesystem data structures to bytestrings which
  * can be written to persistent storage.  All data is stored in little-endian
@@ -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);
+    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,11 @@ 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 = NULL;
+            if (b->type == BLUESKY_BLOCK_REF)
+                ref = b->ref;
+            bluesky_cloudlog_ref(ref);
+            g_array_append_val(cloudlog->links, ref);
         }
         break;
     }
@@ -124,27 +132,51 @@ void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
 
         break;
     }
+
+    case BLUESKY_SYMLINK:
+    {
+        g_string_append(out, inode->symlink_contents);
+        g_string_append_c(out, '\0');
+        break;
+    }
+
     default:
         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;
 }
 
-BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
+/* 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)
 {
+    if (bluesky_verbose) {
+        g_log("bluesky/serialize", G_LOG_LEVEL_DEBUG,
+              "Deserializing inode %lld...", (long long)inode->inum);
+    }
+
     struct serialized_inode *raw = (struct serialized_inode *)buf;
 
     if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
-        return NULL;
+        return FALSE;
+
+    if (inode->inum != GUINT64_FROM_LE(raw->inum))
+        return FALSE;
 
-    BlueSkyInode *inode = bluesky_new_inode(GUINT64_FROM_LE(raw->inum),
-                                            fs, GUINT32_FROM_LE(raw->type));
+    bluesky_init_inode(inode, GUINT32_FROM_LE(raw->type));
 
     inode->mode = GUINT32_FROM_LE(raw->mode);
     inode->uid = GUINT32_FROM_LE(raw->uid);
     inode->gid = GUINT32_FROM_LE(raw->gid);
     inode->nlink = GUINT32_FROM_LE(raw->nlink);
     inode->change_count = GUINT64_FROM_LE(raw->change_count);
+    inode->change_commit = inode->change_count;
     inode->atime = GINT64_FROM_LE(raw->atime);
     inode->ctime = GINT64_FROM_LE(raw->ctime);
     inode->mtime = GINT64_FROM_LE(raw->mtime);
@@ -152,8 +184,6 @@ BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
 
     buf += sizeof(struct serialized_inode);
 
-    g_print("Deserializing inode %lld...\n", (long long)inode->inum);
-
     /* TODO: Bounds checking */
     switch (inode->type) {
     case BLUESKY_REGULAR:
@@ -162,12 +192,15 @@ BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
         g_array_set_size(inode->blocks,
                          (inode->size + BLUESKY_BLOCK_SIZE - 1)
                           / BLUESKY_BLOCK_SIZE);
+        // TODO
+#if 0
         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;
         }
+#endif
         break;
 
     case BLUESKY_DIRECTORY:
@@ -192,18 +225,22 @@ BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
             g_hash_table_insert(inode->dirhash_folded, dirent->name_folded,
                                 dirent);
 
-            g_print("  dirent[%08x]: %s -> %"PRIu64"\n",
-                    dirent->cookie, dirent->name, dirent->inum);
-
             buf = strchr(d->name, '\0') + 1;
             d = (struct serialized_dirent *)buf;
         }
         break;
     }
+
+    case BLUESKY_SYMLINK:
+    {
+        inode->symlink_contents = g_strdup(buf);
+        break;
+    }
+
     default:
         g_warning("Deserialization for inode type %d not implemented!\n",
                   inode->type);
     }
 
-    return inode;
+    return TRUE;
 }