BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / master / vfs / direntry.c

// Related

Nucleus

Barry System headers (remove libc dependency) 18495cf (3 years, 2 months ago)
/*
 * This file implements the Directory Entry object and associated operations.
 */

#include <nucleus/lib.h>
#include <nucleus/object.h>
#include <nucleus/vfs.h>

static void direntry_delete(Object *);

/* Directory Entry object type */
ObjectType dirEntryType = {
	.name = "DIRECTORY ENTRY",
	.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);
}

/* Find a Directory Entry by name in a list */
DirEntry *
find_direntry(ObjectList *list, const char *name)
{
	DirEntry *entry;
	uint32_t hash = name_hash(name);
	foreach (list, entry) {
		if (__builtin_expect(!(entry->hash), 0))
			entry->hash = name_hash(entry->name);
		if (entry->hash != hash)
			continue;
		if (strcmp(entry->name, name))
			continue;
		break;
	}
	return entry;
}