#include <stdint.h>
#include <glib.h>
-#include "inode.h"
+#include "bluesky.h"
/* Core filesystem. Different proxies, such as the NFSv3 one, interface to
* this, but the core actually tracks the data which is stored. So far we just
* implement an in-memory filesystem, but eventually this will be state which
* is persisted to the cloud. */
-
-/* Hash a filename for a directory lookup. The string is hashed to a 64-bit
- * value. */
-uint64_t bluesky_directory_hash(gchar *name)
-{
- GChecksum *csum = g_checksum_new(G_CHECKSUM_MD5);
- g_checksum_update(csum, (guchar *)name, -1);
-
- guint64 hashbytes[2];
- gsize hashsize = sizeof(hashbytes);
- g_checksum_get_digest(csum, (guint8 *)hashbytes, &hashsize);
- return GUINT64_FROM_LE(hashbytes[0]);
-}
-
/* Return the current time, in microseconds since the epoch. */
int64_t bluesky_get_current_time()
{
return i;
}
-
-void bluesky_dirent_destroy(BlueSkyDirent *dirent)
-{
- g_free(dirent->name);
- g_free(dirent);
-}
-
-gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
- gpointer unused)
-{
- /* We can't simply subtract the hash values, since they are 64-bit and the
- * result could overflow when converted to a gint. */
- uint64_t hash1 = ((const BlueSkyDirent *)a)->hash;
- uint64_t hash2 = ((const BlueSkyDirent *)b)->hash;
-
- if (hash1 < hash2)
- return -1;
- else if (hash1 > hash2)
- return 1;
- else
- return 0;
-}
-
-/* Perform a lookup for a file name within a directory. Returns the inode
- * number if found, or 0 if not (0 is never a valid inode number). Should be
- * called with the inode lock already held. */
-uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name)
-{
- 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;
-}