1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
13 #include "bluesky-private.h"
15 /* Core filesystem: handling of directories. */
17 void bluesky_dirent_destroy(gpointer data)
19 BlueSkyDirent *dirent = (BlueSkyDirent *)data;
21 g_free(dirent->name_folded);
25 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
28 uint32_t hash1 = ((const BlueSkyDirent *)a)->cookie;
29 uint32_t hash2 = ((const BlueSkyDirent *)b)->cookie;
33 else if (hash1 > hash2)
39 /* Perform a lookup for a file name within a directory. Returns the inode
40 * number if found, or 0 if not (0 is never a valid inode number). Should be
41 * called with the inode lock already held. */
42 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name)
44 g_return_val_if_fail(inode->type == BLUESKY_DIRECTORY, 0);
45 g_return_val_if_fail(inode->dirhash != NULL, 0);
47 BlueSkyDirent *d = g_hash_table_lookup(inode->dirhash, name);
54 /* Case-insensitive lookup. */
55 uint64_t bluesky_directory_ilookup(BlueSkyInode *inode, gchar *name)
57 g_return_val_if_fail(inode->type == BLUESKY_DIRECTORY, 0);
58 g_return_val_if_fail(inode->dirhash_folded != NULL, 0);
60 name = bluesky_lowercase(name);
61 BlueSkyDirent *d = g_hash_table_lookup(inode->dirhash_folded, name);
70 /* Insert a new entry into a directory. Should be called with the inode lock
72 gboolean bluesky_directory_insert(BlueSkyInode *dir,
73 const gchar *name, uint64_t inum)
75 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
77 /* Check that no entry with the given name already exists. */
78 if (g_hash_table_lookup(dir->dirhash, name) != NULL)
81 BlueSkyDirent *d = g_new(BlueSkyDirent, 1);
82 d->name = g_strdup(name);
83 d->name_folded = bluesky_lowercase(name);
86 GSequence *dirents = dir->dirents;
88 /* Pick an unused cookie value for the directory at random. Restrict
89 * ourselves to positive 32-bit values (even if treated as signed), and
90 * keep the first four slots free. */
93 d->cookie = g_random_int() & 0x7fffffff;
94 } while (d->cookie < 4);
96 /* If the directory is empty, we can't possibly have a collision, so
97 * just go with the first key chosen. */
98 if (g_sequence_get_length(dirents) == 0)
101 /* Otherwise, try looking up the generated cookie. If we do not find a
102 * match, we can use this cookie value, otherwise we need to generate a
103 * new one and try again. Because of the check above for an empty
104 * directory, we know that the lookup will return something so no need
105 * to worry about NULL. */
106 GSequenceIter *i = g_sequence_search(dir->dirents, d,
107 bluesky_dirent_compare, NULL);
108 i = g_sequence_iter_prev(i);
109 if (((BlueSkyDirent *)g_sequence_get(i))->cookie != d->cookie)
113 /* Add the directory entry to both indices. */
114 g_sequence_insert_sorted(dirents, d, bluesky_dirent_compare, NULL);
115 g_hash_table_insert(dir->dirhash, d->name, d);
116 g_hash_table_insert(dir->dirhash_folded, d->name_folded, d);
118 bluesky_inode_update_ctime(dir, 1);
119 bluesky_inode_flush(dir->fs, dir);
124 /* Remove an entry from a directory. Should be called with the inode lock
126 gboolean bluesky_directory_remove(BlueSkyInode *dir, gchar *name)
128 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
130 BlueSkyDirent *d = g_hash_table_lookup(dir->dirhash, name);
135 g_hash_table_remove(dir->dirhash, d->name);
136 g_hash_table_remove(dir->dirhash_folded, d->name_folded);
138 GSequenceIter *i = g_sequence_search(dir->dirents, d,
139 bluesky_dirent_compare, NULL);
140 i = g_sequence_iter_prev(i);
142 /* Assertion check, this ought to succeed */
143 g_return_val_if_fail(g_sequence_get(i) == d, FALSE);
145 g_sequence_remove(i);
147 bluesky_inode_update_ctime(dir, 1);
148 bluesky_inode_flush(dir->fs, dir);
153 /* Rename a file. If desired (if overwrite is true) and if the target already
154 * exists, it will be unlinked first. */
155 gboolean bluesky_rename(BlueSkyInode *dir1, gchar *name1,
156 BlueSkyInode *dir2, gchar *name2,
157 gboolean case_sensitive,
160 g_return_val_if_fail(dir1->type == BLUESKY_DIRECTORY, FALSE);
161 g_return_val_if_fail(dir2->type == BLUESKY_DIRECTORY, FALSE);
163 BlueSkyDirent *d1, *d2;
165 d1 = g_hash_table_lookup(case_sensitive ? dir1->dirhash
166 : dir1->dirhash_folded, name1);
167 d2 = g_hash_table_lookup(case_sensitive ? dir2->dirhash
168 : dir2->dirhash_folded, name2);
173 /* Check that this rename does not cause a directory to be moved into one
174 * of its descendants, as that would create a loop of directories
175 * disconnected from the root. */
182 bluesky_directory_remove(dir2, name2);
187 /* Dump the contents of a directory to stdout. Debugging only. */
188 void bluesky_directory_dump(BlueSkyInode *dir)
190 g_print("Directory dump:\n");
192 GSequenceIter *i = g_sequence_get_begin_iter(dir->dirents);
194 while (!g_sequence_iter_is_end(i)) {
195 BlueSkyDirent *d = g_sequence_get(i);
196 g_print(" 0x%08x [inum=%"PRIu64"] %s\n",
197 d->cookie, d->inum, d->name);
198 i = g_sequence_iter_next(i);