Switch to 31-bit directory cookies and a separate hashtable for lookups.
[bluesky.git] / inode.c
diff --git a/inode.c b/inode.c
index 4437c04..aa822b7 100644 (file)
--- a/inode.c
+++ b/inode.c
@@ -24,6 +24,18 @@ int64_t bluesky_get_current_time()
     return (int64_t)t.tv_sec * 1000000 + t.tv_usec;
 }
 
+/* Update an inode to indicate that a modification was made.  This increases
+ * the change counter, updates the ctime to the current time, and optionally
+ * updates the mtime. */
+void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime)
+{
+    int64_t now = bluesky_get_current_time();
+    inode->change_count++;
+    inode->ctime = now;
+    if (update_mtime)
+        inode->mtime = now;
+}
+
 /* Unfortunately a glib hash table is only guaranteed to be able to store
  * 32-bit keys if we use the key directly.  If we want 64-bit inode numbers,
  * we'll have to allocate memory to store the 64-bit inumber, and use a pointer
@@ -81,9 +93,12 @@ BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
 
     switch (type) {
     case BLUESKY_REGULAR:
+        i->blocks = g_array_new(FALSE, TRUE, sizeof(BlueSkyBlock));
         break;
     case BLUESKY_DIRECTORY:
         i->dirents = g_sequence_new(bluesky_dirent_destroy);
+        i->dirhash = g_hash_table_new(g_str_hash, g_str_equal);
+        break;
     case BLUESKY_BLOCK:
     case BLUESKY_CHARACTER:
     case BLUESKY_SYMLINK:
@@ -116,3 +131,35 @@ void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
     g_hash_table_insert(fs->inodes, &inode->inum, inode);
     g_mutex_unlock(fs->lock);
 }
+
+/* Set the size of a file.  This will truncate or extend the file as needed.
+ * Newly-allocated bytes are zeroed. */
+void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
+{
+    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;
+    inode->change_count++;
+}