Nucleus
Barry File object 015db5a (3 years, 3 months ago)
/*
* This file implements the Directory Entry object and associated operations.
*/
#include <string.h>
#include <nucleus/object.h>
#include <nucleus/vfs.h>
/* Information for find callback */
struct FindData {
const char *name;
DirEntry *result;
};
static void direntry_delete(Object *);
/* Directory Entry object type */
ObjectType dirEntryType = {
.size = sizeof(DirEntry),
.delete = direntry_delete,
};
/* Hash a file name */
static uint32_t
name_hash(const char *name)
{
uint32_t digest = 5381;
int c;
while (c = *name++)
digest = ((digest << 5) + digest) ^ c;
return digest;
}
/* Destroy a Directory Entry */
static void
direntry_delete(Object *obj)
{
DirEntry *entry = (void *) obj;
if (entry->inode)
put(entry->inode);
}
/* Callback for finding a directory entry by name */
static int
compare_direntry_name(void *addr, void *data)
{
DirEntry *entry = (void *) addr;
struct FindData *find = data;
uint32_t hash = name_hash(find->name);
if (entry->hash != hash)
return 0;
if (strcmp(entry->name, find->name))
return 0;
find->result = get(entry);
return 1;
}
/* Find a Directory Entry by name in a list */
DirEntry *
find_direntry(ObjectList *list, const char *name)
{
lock(list);
struct FindData data = {
.name = name,
.result = NULL,
};
iterate(list, compare_direntry_name, &data);
unlock(list);
return data.result;
}