diff --git a/kernel/include/errno.h b/kernel/include/errno.h index 8c9786df..e7d472e9 100644 --- a/kernel/include/errno.h +++ b/kernel/include/errno.h @@ -4,6 +4,7 @@ #define ENOENT 2 #define EBADF 9 #define ENOMEM 12 +#define EISDIR 21 #define EINVAL 22 #define EMFILE 24 #define ENOSYS 38 \ No newline at end of file diff --git a/kernel/include/fs/VFS.h b/kernel/include/fs/VFS.h index 7d35061f..943604da 100644 --- a/kernel/include/fs/VFS.h +++ b/kernel/include/fs/VFS.h @@ -4,6 +4,9 @@ typedef long ssize_t; +#define VFS_FILE 0x0 +#define VFS_DIRECTORY 0x1 + namespace VFS { struct Node; @@ -16,6 +19,7 @@ namespace VFS char name[64]; uint64_t inode; uint64_t length; + int type; node_read read_func; node_finddir find_func; }; diff --git a/kernel/src/fs/VFS.cpp b/kernel/src/fs/VFS.cpp index bbf997ed..c5646e4a 100644 --- a/kernel/src/fs/VFS.cpp +++ b/kernel/src/fs/VFS.cpp @@ -1,6 +1,7 @@ #define MODULE "vfs" #include "fs/VFS.h" +#include "errno.h" #include "log/Log.h" #include "std/stdlib.h" #include "std/string.h" @@ -14,6 +15,11 @@ ssize_t VFS::read(Node* node, size_t offset, size_t length, char* buffer) kwarnln("read() failed: trying to read from nullptr"); return -1; } + if (node->type == VFS_DIRECTORY) + { + kwarnln("read() failed: is a directory"); + return -EISDIR; + } if (!node->read_func) { kwarnln("read() failed: the chosen node doesn't support reading"); diff --git a/kernel/src/init/InitRD.cpp b/kernel/src/init/InitRD.cpp index ad7c5acb..c8bce851 100644 --- a/kernel/src/init/InitRD.cpp +++ b/kernel/src/init/InitRD.cpp @@ -8,7 +8,7 @@ #include "memory/MemoryManager.h" #include "misc/utils.h" #include "std/stdlib.h" -#include +#include "std/string.h" extern BOOTBOOT bootboot; @@ -227,6 +227,7 @@ static bool initrd_register_dir(InitRD::Directory& dir, uint64_t inode) node.inode = inode; node.find_func = initrd_scan_dir; node.length = 0; + node.type = VFS_DIRECTORY; strncpy(node.name, buffer, sizeof(node.name)); strncpy(dir.name, buffer, sizeof(dir.name)); @@ -284,6 +285,7 @@ static bool initrd_register_file(InitRD::File& f, uint64_t inode) node.inode = inode; node.read_func = initrd_read; node.length = f.size; + node.type = VFS_FILE; strncpy(node.name, buffer, sizeof(node.name)); strncpy(f.name, buffer, sizeof(f.name)); diff --git a/libs/libc/include/errno.h b/libs/libc/include/errno.h index 912dd606..ddff34c4 100644 --- a/libs/libc/include/errno.h +++ b/libs/libc/include/errno.h @@ -7,6 +7,7 @@ extern int errno; #define ENOENT 2 #define EBADF 9 #define ENOMEM 12 +#define EISDIR 21 #define EINVAL 22 #define EMFILE 24 #define ENOSYS 38 diff --git a/libs/libc/src/string.cpp b/libs/libc/src/string.cpp index d35c6689..ffbf07f2 100644 --- a/libs/libc/src/string.cpp +++ b/libs/libc/src/string.cpp @@ -124,6 +124,7 @@ extern "C" case ENOENT: return "No such file or directory"; case EBADF: return "Bad file descriptor"; case EMFILE: return "Too many open files"; + case EISDIR: return "Is a directory"; case 0: return "Success"; default: return (char*)(unsigned long int)errnum; }