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 /* Iterate through a directory listing. This returns one directory entry at a
71 * time, finding the first entry with a directory cookie value larger than the
72 * supplied one. Use a cookie of 0 to start reading from the start of a
74 BlueSkyDirent *bluesky_directory_read(BlueSkyInode *dir, uint32_t cookie)
76 BlueSkyDirent start = {NULL, NULL, cookie, 0};
77 GSequenceIter *i = g_sequence_search(dir->dirents, &start,
78 bluesky_dirent_compare, NULL);
80 if (g_sequence_iter_is_end(i))
83 return g_sequence_get(i);
86 /* Insert a new entry into a directory. Should be called with the inode lock
88 gboolean bluesky_directory_insert(BlueSkyInode *dir,
89 const gchar *name, uint64_t inum)
91 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
93 /* Check that no entry with the given name already exists. */
94 if (g_hash_table_lookup(dir->dirhash, name) != NULL)
97 BlueSkyDirent *d = g_new(BlueSkyDirent, 1);
98 d->name = g_strdup(name);
99 d->name_folded = bluesky_lowercase(name);
102 GSequence *dirents = dir->dirents;
104 /* Pick an unused cookie value for the directory at random. Restrict
105 * ourselves to positive 32-bit values (even if treated as signed), and
106 * keep the first four slots free. */
109 d->cookie = g_random_int() & 0x7fffffff;
110 } while (d->cookie < 4);
112 /* If the directory is empty, we can't possibly have a collision, so
113 * just go with the first key chosen. */
114 if (g_sequence_get_length(dirents) == 0)
117 /* Otherwise, try looking up the generated cookie. If we do not find a
118 * match, we can use this cookie value, otherwise we need to generate a
119 * new one and try again. Because of the check above for an empty
120 * directory, we know that the lookup will return something so no need
121 * to worry about NULL. */
122 GSequenceIter *i = g_sequence_search(dir->dirents, d,
123 bluesky_dirent_compare, NULL);
124 i = g_sequence_iter_prev(i);
125 if (((BlueSkyDirent *)g_sequence_get(i))->cookie != d->cookie)
129 /* Add the directory entry to both indices. */
130 g_sequence_insert_sorted(dirents, d, bluesky_dirent_compare, NULL);
131 g_hash_table_insert(dir->dirhash, d->name, d);
132 g_hash_table_insert(dir->dirhash_folded, d->name_folded, d);
134 bluesky_inode_update_ctime(dir, 1);
135 //bluesky_inode_do_sync(dir); // TODO: Needed?
140 /* Remove an entry from a directory. Should be called with the inode lock
142 gboolean bluesky_directory_remove(BlueSkyInode *dir, gchar *name)
144 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
146 BlueSkyDirent *d = g_hash_table_lookup(dir->dirhash, name);
151 g_hash_table_remove(dir->dirhash, d->name);
152 g_hash_table_remove(dir->dirhash_folded, d->name_folded);
154 GSequenceIter *i = g_sequence_search(dir->dirents, d,
155 bluesky_dirent_compare, NULL);
156 i = g_sequence_iter_prev(i);
158 /* Assertion check, this ought to succeed */
159 g_return_val_if_fail(g_sequence_get(i) == d, FALSE);
161 g_sequence_remove(i);
163 bluesky_inode_update_ctime(dir, 1);
168 /* Rename a file. If desired (if overwrite is true) and if the target already
169 * exists, it will be unlinked first. */
170 gboolean bluesky_rename(BlueSkyInode *dir1, gchar *name1,
171 BlueSkyInode *dir2, gchar *name2,
172 gboolean case_sensitive,
175 g_return_val_if_fail(dir1->type == BLUESKY_DIRECTORY, FALSE);
176 g_return_val_if_fail(dir2->type == BLUESKY_DIRECTORY, FALSE);
178 BlueSkyDirent *d1, *d2;
180 d1 = g_hash_table_lookup(case_sensitive ? dir1->dirhash
181 : dir1->dirhash_folded, name1);
182 d2 = g_hash_table_lookup(case_sensitive ? dir2->dirhash
183 : dir2->dirhash_folded, name2);
188 uint64_t inum = d1->inum;
190 /* Check that this rename does not cause a directory to be moved into one
191 * of its descendants, as that would create a loop of directories
192 * disconnected from the root. */
199 bluesky_directory_remove(dir2, name2);
201 // TODO: Drop inode reference
204 bluesky_directory_remove(dir1, name1);
205 bluesky_directory_insert(dir2, name2, inum);
210 /* Dump the contents of a directory to stdout. Debugging only. */
211 void bluesky_directory_dump(BlueSkyInode *dir)
213 g_print("Directory dump:\n");
215 GSequenceIter *i = g_sequence_get_begin_iter(dir->dirents);
217 while (!g_sequence_iter_is_end(i)) {
218 BlueSkyDirent *d = g_sequence_get(i);
219 g_print(" 0x%08x [inum=%"PRIu64"] %s\n",
220 d->cookie, d->inum, d->name);
221 i = g_sequence_iter_next(i);