From 94a2de3847ec9e7ef0943a1f788eee6c2894517f Mon Sep 17 00:00:00 2001 From: Nigel Date: Sun, 19 Feb 2023 22:14:58 +0100 Subject: [PATCH] Started on path resolution algorithm - The algorithm will work once I am of better mind to deal with raw C strings - The resolution should look at each entry divided by '/'. if the entry is not there then we can quit early, however for now I am mostly concerned with getting the names of directory entries we would need to look for. --- Makefile | 5 ++- source/kernel/kernel.cpp | 4 ++ source/kernel/vfs/Path.cpp | 83 ++++++++++++++++++++++++++++++++++++++ source/kernel/vfs/Path.h | 46 +++++++++++++++++++++ source/kernel/vfs/VFS.cpp | 57 +++++++++++++++++++++++++- source/kernel/vfs/VFS.h | 4 ++ source/lib/include/mem.h | 27 +++++++------ 7 files changed, 210 insertions(+), 16 deletions(-) create mode 100644 source/kernel/vfs/Path.cpp create mode 100644 source/kernel/vfs/Path.h diff --git a/Makefile b/Makefile index 84b3479..faad734 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CC = ${HOME}/opt/cross/bin/i686-elf-gcc CPP = ${HOME}/opt/cross/bin/i686-elf-g++ CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/VFS.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o +OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/Path.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/VFS.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o SRC_DIR = source BUILD_DIR = build @@ -133,10 +133,11 @@ $(BUILD_DIR)/KHeap.o: $(BUILD_DIR)/prekernel.o: $(CPP) -c $(SRC_DIR)/kernel/prekernel/prekernel.cpp -o $(BUILD_DIR)/prekernel.o $(CFLAGS) -fno-exceptions -fno-rtti - $(BUILD_DIR)/processor.o: $(CPP) -c $(SRC_DIR)/kernel/i386/processor.cpp -o $(BUILD_DIR)/processor.o $(CFLAGS) -fno-exceptions -fno-rtti +$(BUILD_DIR)/Path.o: + $(CPP) -c $(SRC_DIR)/kernel/vfs/Path.cpp -o $(BUILD_DIR)/Path.o $(CFLAGS) -fno-exceptions -fno-rtti # Assembly -> Object files $(BUILD_DIR)/boot.o: diff --git a/source/kernel/kernel.cpp b/source/kernel/kernel.cpp index 319b5a9..7bd0ca1 100644 --- a/source/kernel/kernel.cpp +++ b/source/kernel/kernel.cpp @@ -48,6 +48,10 @@ extern "C" void kernel () FileSystem::initialize(); + // Testing my path resolution functions + Path test = Path("/boot/myos.bin"); + FileSystem::ResolvePath(test); + #ifdef USERMODE_RELEASE // Lets jump into user mode diff --git a/source/kernel/vfs/Path.cpp b/source/kernel/vfs/Path.cpp new file mode 100644 index 0000000..3489836 --- /dev/null +++ b/source/kernel/vfs/Path.cpp @@ -0,0 +1,83 @@ +// +// Created by nigel on 19/02/23. +// +#include "Path.h" +#include "../memory/KernelHeap.h" +String::String(char* characters) +: chars(characters) +{ + +} + +char* String::str(){ + return chars; +} + +unsigned int String::length () +{ + int i = 0; + + while ( chars[i] != '\0'){ + i++; + } + + return i; +} + +// Returns a null character if size exceeds limits +char String::operator[] (size_t idx) +{ + if( idx > this->length()) + return '\0'; + + return chars[idx]; +} + +const char String::operator[](size_t idx) const { + return (const char) chars[idx]; +} + +StringView::StringView(String &string, unsigned int start, unsigned int end) +: String(string), begin(start), end(end) +{ + +} + +char* StringView::str(){ + char* str = (char*) malloc((this->length() * sizeof(char))+1); + + int index = 0; + for ( int i = begin; i < end ; i++){ + str[index] = chars[i]; + index++; + } + chars[index+1] ='\0'; + return str; +} + + +Path::Path(char *path) +: path(String(path)) +{ + +} + +Path::Path(String &path) +: path(path) +{ + +} + +StringView Path::getbasename() +{ + unsigned int path_length = path.length(); + int i = path_length; + while (path[i] != '/') + i--; + + return StringView(path,i +1, path_length); +} + +char* Path::str() { + return path.str(); +} \ No newline at end of file diff --git a/source/kernel/vfs/Path.h b/source/kernel/vfs/Path.h new file mode 100644 index 0000000..2d6de27 --- /dev/null +++ b/source/kernel/vfs/Path.h @@ -0,0 +1,46 @@ +// +// Created by nigel on 19/02/23. +// +#pragma once +#include + +class String { +public: + String(char* characters); + String(String&) = default; + unsigned int length(); + + char* str (); + char operator[](size_t index) ; + const char operator[](size_t idx) const; + +protected: + char* chars; + + +}; + +class StringView : String { +public: + StringView(String& string, unsigned int start, unsigned int end ); + char* str (); +private: + unsigned int begin; + unsigned int end; +}; + +class Path{ +public: + Path(String& path); + Path(char* path); + + StringView getbasename(); + + char* str(); + +private: + String path; + +}; + + diff --git a/source/kernel/vfs/VFS.cpp b/source/kernel/vfs/VFS.cpp index cdb787c..3f59fc2 100644 --- a/source/kernel/vfs/VFS.cpp +++ b/source/kernel/vfs/VFS.cpp @@ -4,7 +4,7 @@ #include "../drivers/ata/ataDevice.h" #include "../partitiontable/mbr/MasterBootRecord.h" #include "../memory/KernelHeap.h" -#include "../../lib/include/string.h" +#include "Path.h" #include "../filesystem/FAT/DirectoryEntry.h" MOUNT_INFO mountInfo; @@ -360,4 +360,59 @@ void FileSystem::initialize() */ +} + +char* FindNextEntryName (char* path ) +{ + int length = strlen(path); + + char* name = path; + int i = 0; + + if( name[0] == '/') + i++; + + while ( name[i] != '/' && i <= length) + i++; + + char* s = (char*) malloc(i + 1 * sizeof(char)); + for ( int a = 0; a < i; a++) + s[a] = path[a]; + + s[i + 1] = '\0'; + + return s; + +} + + + +void FileSystem::ResolvePath(Path &path) +{ + char* string_path = path.str(); + void* cpy = string_path; + + bool isAbsolutePath = string_path[0] == '/'; + if(isAbsolutePath) + { + // strip the first slash + string_path++; + } + + char* entry_name = FindNextEntryName(string_path); + printf("Look for entry with name: %s\n", entry_name); + int skip = strlen(entry_name); + free(entry_name); + + + entry_name = FindNextEntryName(string_path + skip); + printf("Look for entry with name: %s\n", entry_name); + skip = strlen(entry_name); + free(entry_name); + + + + + free(cpy); + } \ No newline at end of file diff --git a/source/kernel/vfs/VFS.h b/source/kernel/vfs/VFS.h index f7ec66d..17b92c0 100644 --- a/source/kernel/vfs/VFS.h +++ b/source/kernel/vfs/VFS.h @@ -1,5 +1,7 @@ #pragma once #include +#include "Path.h" + #define FS_FILE 0 #define FS_DIRECTORY 1 #define FS_INVALID 2 @@ -73,4 +75,6 @@ extern PFILESYSTEM _filesystems[DEVICE_MAX]; public: static void initialize(); + static void ResolvePath(Path& path); + }; \ No newline at end of file diff --git a/source/lib/include/mem.h b/source/lib/include/mem.h index 83216f6..e5ff1dd 100644 --- a/source/lib/include/mem.h +++ b/source/lib/include/mem.h @@ -1,6 +1,7 @@ #pragma once // NOTE: These should not be inline -inline void* memset (void* ptr, int value, size_t num){ +inline void* memset (void* ptr, int value, size_t num) +{ for( int i = 0; i < num; i++ ) { unsigned char* data = (unsigned char*)ptr+ i; @@ -12,19 +13,19 @@ inline void* memset (void* ptr, int value, size_t num){ inline int memcmp( const void* ptr1, const void* ptr2, size_t num) - { - const unsigned char * cs = (const unsigned char*) ptr1; - const unsigned char * ct = (const unsigned char*) ptr2; +{ + const unsigned char * cs = (const unsigned char*) ptr1; + const unsigned char * ct = (const unsigned char*) ptr2; - for (int i = 0 ; i < num ; i++, cs++, ct++ ){ - if( *cs < *ct){ - return -1; - } else if( *cs > *ct){ - return 1; - } - } + for (int i = 0 ; i < num ; i++, cs++, ct++ ){ + if( *cs < *ct){ + return -1; + } else if( *cs > *ct){ + return 1; + } + } - return 0; + return 0; - } \ No newline at end of file +} \ No newline at end of file