Encrypt data blocks being stored to S3.
[bluesky.git] / bluesky / inode.c
index 7ea8dbf..42b045c 100644 (file)
@@ -119,6 +119,20 @@ BlueSkyFS *bluesky_new_fs(gchar *name)
     return fs;
 }
 
+/* Inode reference counting. */
+void bluesky_inode_ref(BlueSkyInode *inode)
+{
+    g_atomic_int_inc(&inode->refcount);
+}
+
+void bluesky_inode_unref(BlueSkyInode *inode)
+{
+    if (g_atomic_int_dec_and_test(&inode->refcount)) {
+        g_error("Reference count for inode %lld dropped to zero!\n",
+                inode->inum);
+    }
+}
+
 /* Allocate a fresh inode number which has not been used before within a
  * filesystem. */
 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
@@ -139,6 +153,7 @@ BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFS *fs,
     BlueSkyInode *i = g_new0(BlueSkyInode, 1);
 
     i->lock = g_mutex_new();
+    i->refcount = 1;
     i->type = type;
     i->fs = fs;
     i->inum = inum;
@@ -337,12 +352,15 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block)
     if (block->type != BLUESKY_BLOCK_DIRTY)
         return;
 
+    BlueSkyRCStr *data = block->data;
+    data = bluesky_crypt_encrypt(data, fs->encryption_key);
+
     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
-    g_checksum_update(csum, block->data->data, block->data->len);
+    g_checksum_update(csum, data->data, data->len);
     gchar *name = g_strdup(g_checksum_get_string(csum));
 
     g_print("Flushing block as %s\n", name);
-    s3store_put(fs->store, name, block->data);
+    s3store_put(fs->store, name, data);
     g_free(block->ref);
     block->ref = name;
 
@@ -352,4 +370,5 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block)
     block->type = BLUESKY_BLOCK_REF;
 
     g_checksum_free(csum);
+    bluesky_string_unref(data);
 }