Drop some debugging messages.
[bluesky.git] / bluesky / file.c
index 75768d1..f7e8fca 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdint.h>
 #include <glib.h>
 #include <string.h>
+#include <inttypes.h>
 
 #include "bluesky-private.h"
 
@@ -55,6 +56,8 @@ void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
     if (size == inode->size)
         return;
 
+    g_print("Truncating file to %"PRIi64" bytes\n", size);
+
     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
 
     if (blocks > inode->blocks->len) {
@@ -98,6 +101,8 @@ void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
                         const char *data, gint len)
 {
+    g_print("Write %d bytes at offset %"PRIi64"\n", len, offset);
+
     g_return_if_fail(inode->type == BLUESKY_REGULAR);
     g_return_if_fail(offset < inode->size);
     g_return_if_fail(len <= inode->size - offset);
@@ -128,6 +133,11 @@ void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
                        char *buf, gint len)
 {
+    g_print("Read %d bytes at offset %"PRIi64"\n", len, offset);
+
+    if (len == 0 && offset <= inode->size)
+        return;
+
     g_return_if_fail(inode->type == BLUESKY_REGULAR);
     g_return_if_fail(offset < inode->size);
     g_return_if_fail(len <= inode->size - offset);
@@ -172,6 +182,11 @@ void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block)
     block->type = BLUESKY_BLOCK_CACHED;
 }
 
+static void finished(gpointer a, gpointer b)
+{
+    g_print("Barrier completed!\n");
+}
+
 /* Write the given block to cloud-backed storage and mark it clean. */
 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block)
 {
@@ -184,10 +199,26 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block)
     g_checksum_update(csum, data->data, data->len);
     gchar *name = g_strdup(g_checksum_get_string(csum));
 
-    bluesky_store_put(fs->store, name, data);
+    /* Store the file data asynchronously, and don't bother waiting for a
+     * response. */
+    BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
+    async->op = STORE_OP_PUT;
+    async->key = g_strdup(name);
+    bluesky_string_ref(data);
+    async->data = data;
+    bluesky_store_async_submit(async);
+    bluesky_store_async_unref(async);
+
     g_free(block->ref);
     block->ref = name;
 
+    BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
+    barrier->op = STORE_OP_BARRIER;
+    bluesky_store_add_barrier(barrier, async);
+    bluesky_store_async_add_notifier(barrier, finished, NULL);
+    bluesky_store_async_submit(barrier);
+    bluesky_store_async_unref(barrier);
+
     /* block->type = BLUESKY_BLOCK_CACHED; */
     bluesky_string_unref(block->data);
     block->data = NULL;
@@ -196,3 +227,14 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block)
     g_checksum_free(csum);
     //bluesky_string_unref(data);
 }
+
+/* Flush all blocks in a file to stable storage. */
+void bluesky_file_flush(BlueSkyInode *inode)
+{
+    g_return_if_fail(inode->type == BLUESKY_REGULAR);
+
+    for (int i = 0; i < inode->blocks->len; i++) {
+        BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
+        bluesky_block_flush(inode->fs, b);
+    }
+}