+ bluesky_inode_update_ctime(inode, 1);
+}
+
+void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
+ const char *data, gint len)
+{
+ g_return_if_fail(inode->type == BLUESKY_REGULAR);
+ g_return_if_fail(offset < inode->size);
+ g_return_if_fail(len <= inode->size - offset);
+
+ if (len == 0)
+ return;
+
+ 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);
+
+ bluesky_block_touch(inode, block_num);
+ BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
+ block_num);
+ memcpy(&b->data[block_offset], data, bytes);
+ bluesky_block_flush(b);
+
+ offset += bytes;
+ data += bytes;
+ len -= bytes;
+ }
+
+ bluesky_inode_update_ctime(inode, 1);
+}
+
+void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
+ char *buf, gint len)
+{
+ 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:
+ /* TODO: Pull in data first */
+ memset(buf, 0, bytes);
+ break;
+ case BLUESKY_BLOCK_CACHED:
+ case BLUESKY_BLOCK_DIRTY:
+ memcpy(buf, &b->data[block_offset], bytes);
+ break;
+ }
+
+ offset += bytes;
+ buf += bytes;
+ len -= bytes;
+ }
+}
+
+/* Write the given block to cloud-backed storage and mark it clean. */
+void bluesky_block_flush(BlueSkyBlock *block)
+{
+ if (block->type != BLUESKY_BLOCK_DIRTY)
+ return;
+
+ BlueSkyRCStr *data = bluesky_string_new(g_memdup(block->data,
+ BLUESKY_BLOCK_SIZE),
+ BLUESKY_BLOCK_SIZE);
+
+ GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
+ g_checksum_update(csum, data->data, data->len);
+ const gchar *name = g_checksum_get_string(csum);
+
+ g_print("Flushing block as %s\n", name);
+ //memstore_put(store, name, data);
+ g_free(block->ref);
+ block->ref = g_strdup(name);
+ block->type = BLUESKY_BLOCK_CACHED;
+
+ g_checksum_free(csum);
+ bluesky_string_unref(data);