Nucleus
Barry File system type object 4f75471 (3 years, 3 months ago)
diff --git a/include/nucleus/vfs.h b/include/nucleus/vfs.h
index f20cd47..2439151 100644
--- a/include/nucleus/vfs.h
+++ b/include/nucleus/vfs.h
@@ -6,9 +6,12 @@
#include <nucleus/object.h>
#include <nucleus/memory.h>
+typedef struct FSType FSType;
typedef struct Inode Inode;
typedef struct InodeOps InodeOps;
+typedef Inode *(*mount_callback_t)(FSType *, int, const char *, void *);
+
/* Structure for an Inode */
struct Inode {
Object obj;
@@ -27,6 +30,10 @@ struct Inode {
struct InodeOps {
};
+extern ObjectType fstypeType;
extern ObjectType inodeType;
+void init_vfs(void);
+void register_fstype(const char *name, mount_callback_t mount);
+
#endif
diff --git a/lib/string.c b/lib/string.c
new file mode 100644
index 0000000..5b9506e
--- /dev/null
+++ b/lib/string.c
@@ -0,0 +1,51 @@
+#include <stddef.h>
+
+/* Compare two strings */
+int
+strcmp(char *s1, char *s2)
+{
+ for (; *s1 == *s2 && *s1 && *s2; s1++, s2++);
+ return *(unsigned char *) s1 - *(unsigned char *) s2;
+}
+
+/* Compare two limited strings */
+int
+strncmp(char *s1, char *s2, size_t n)
+{
+ if (!n--) return 0;
+ for (; *s1 == *s2 && *s1 && *s2 && n; s1++, s2++, n--);
+ return *(unsigned char *) s1 - *(unsigned char *) s2;
+}
+
+/* Copy a string */
+char *
+strcpy(char *dest, const char *src)
+{
+ char *ret = dest;
+ while (*src)
+ *dest++ = *src++;
+ *dest = '\0';
+ return ret;
+}
+
+/* Copy a limited string */
+char *
+strncpy(char *dest, const char *src, size_t n)
+{
+ char *ret = dest;
+ while (*src && n--)
+ *dest++ = *src++;
+ *dest = '\0';
+ return ret;
+}
+
+/* Find length of string */
+size_t
+strlen(const char *str)
+{
+ if (!str)
+ return 0;
+ size_t i;
+ for (i = 0; str[i]; i++);
+ return i;
+}
diff --git a/vfs/fstype.c b/vfs/fstype.c
new file mode 100644
index 0000000..73d28c8
--- /dev/null
+++ b/vfs/fstype.c
@@ -0,0 +1,65 @@
+/*
+ * This file implements the FSType object. Objects of this type represent
+ * different file-system types (e.g. ext2fs, tmpfs). They exist to be iterated
+ * by the VFS to keep track of the installed file systems.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <nucleus/object.h>
+#include <nucleus/vfs.h>
+
+/* Structure for a File System Type */
+struct FSType {
+ const char *name;
+ Inode *(*mount)(FSType *, int, const char *, void *);
+};
+
+/* Find callback data */
+struct FindData {
+ const char *name;
+ FSType *result;
+};
+
+/* File System Type object type */
+ObjectType fstypeType = {
+ .size = sizeof(FSType),
+};
+
+/* Callback for finding a file system type */
+static int
+compare_fstype_name(void *object, void *data)
+{
+ FSType *type = object;
+ struct FindData *find = data;
+ if (!strcmp(type->name, find->name)) {
+ find->result = type;
+ return 1;
+ }
+ return 0;
+}
+
+/* Create a new file system type */
+void
+register_fstype(const char *name, mount_callback_t mount)
+{
+ FSType *fstype = new(&fstypeType);
+ fstype->name = name;
+ fstype->mount = mount;
+}
+
+/* Mount file system type by name */
+int
+mount_fstype(const char *name, int flags, const char *src, void *data,
+ Inode **root)
+{
+ struct FindData find = {
+ .name = name,
+ .result = NULL,
+ };
+ iterate(fstypeType.objects, compare_fstype_name, &find);
+ if (!find.result)
+ return -ENODEV;
+ *root = find.result->mount(find.result, flags, src, data);
+ return 0;
+}