- g_return_if_fail(inode->type == BLUESKY_REGULAR);
- g_return_if_fail(offset < inode->size);
- g_return_if_fail(len <= inode->size - offset);
-
- while (len > 0) {
- uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
- gint block_offset = offset % BLUESKY_BLOCK_SIZE;
- gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
-
- BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
- block_num);
- switch (b->type) {
- case BLUESKY_BLOCK_ZERO:
- memset(buf, 0, bytes);
- break;
- case BLUESKY_BLOCK_REF:
- bluesky_block_fetch(inode->fs, b);
- /* Fall through */
- case BLUESKY_BLOCK_CACHED:
- case BLUESKY_BLOCK_DIRTY:
- memcpy(buf, &b->data->data[block_offset], bytes);
- break;
- }
-
- offset += bytes;
- buf += bytes;
- len -= bytes;
- }
-}
-
-/* Read the given block from cloud-backed storage if the data is not already
- * cached. */
-void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block)
-{
- if (block->type != BLUESKY_BLOCK_REF)
- return;
-
- g_print("Fetching block from %s\n", block->ref);
- BlueSkyRCStr *string = s3store_get(fs->store, block->ref);
-
- bluesky_string_unref(block->data);
- block->data = bluesky_crypt_decrypt(string, fs->encryption_key);
- block->type = BLUESKY_BLOCK_CACHED;
- bluesky_string_unref(string);
-}
-
-/* Write the given block to cloud-backed storage and mark it clean. */
-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, 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, data);
- g_free(block->ref);
- block->ref = name;
-
- /* block->type = BLUESKY_BLOCK_CACHED; */
- bluesky_string_unref(block->data);
- block->data = NULL;
- block->type = BLUESKY_BLOCK_REF;
-
- g_checksum_free(csum);
- bluesky_string_unref(data);
+ GString *buf = g_string_new("");
+ bluesky_serialize_superblock(buf, fs);
+ BlueSkyRCStr *data = bluesky_string_new_from_gstring(buf);
+
+ BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
+ async->op = STORE_OP_PUT;
+ async->key = g_strdup("superblock");
+ async->data = data;
+ bluesky_store_async_submit(async);
+ bluesky_store_async_unref(async);
+
+ bluesky_store_sync(fs->store);