- g_return_val_if_fail(inode->type != BLUESKY_DIRECTORY, 0);
- g_return_val_if_fail(inode->dirents != NULL, 0);
-
- /* First, construct a hash of the file name. Search the directory for a
- * match, then check to see if it does really match. */
- uint64_t hash = bluesky_directory_hash(name);
-
- BlueSkyDirent d = {name, hash, 0};
- GSequenceIter *i = g_sequence_search(inode->dirents, &d,
- bluesky_dirent_compare, NULL);
-
- if (g_sequence_iter_is_end(i))
- return 0;
- BlueSkyDirent *dirent = g_sequence_get(i);
- if (dirent->hash != hash)
- return 0;
- if (g_strcmp0(name, dirent->name) != 0)
- return 0;
-
- return dirent->inum;
+ g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
+
+ if (size == inode->size)
+ return;
+
+ uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_MAX_FILE_SIZE;
+
+ if (blocks > inode->blocks->len) {
+ /* Need to add new blocks to the end of a file. New block structures
+ * are automatically zeroed, which initializes them to be pointers to
+ * zero blocks so we don't need to do any more work. */
+ g_array_set_size(inode->blocks, blocks);
+ } else if (blocks < inode->blocks->len) {
+ /* Delete blocks from a file. Must reclaim memory. */
+ for (guint i = inode->blocks->len; i < blocks; i++) {
+ BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
+ g_free(b->ref);
+ g_free(b->data);
+ }
+ g_array_set_size(inode->blocks, blocks);
+ }
+
+ /* TODO: Zero out partial blocks if needed? */
+
+ inode->size = size;
+ bluesky_inode_update_ctime(inode, 1);