Update kernel code

This commit is contained in:
Alex
2023-08-22 06:21:17 +03:00
parent ef3b761d4f
commit 8898791257
49 changed files with 3389 additions and 2313 deletions

View File

@ -26,7 +26,7 @@ namespace VirtualFileSystem
{
ReadFSFunction(USTAR_Read)
{
if (!Size)
if (Size <= 0)
Size = node->Length;
if (RefOffset > node->Length)

View File

@ -19,6 +19,7 @@
#include <smart_ptr.hpp>
#include <convert.h>
#include <stropts.h>
#include <task.hpp>
#include <printf.h>
#include <lock.hpp>
@ -30,7 +31,7 @@ namespace VirtualFileSystem
{
ReadFSFunction(fd_Read)
{
if (!Size)
if (Size <= 0)
Size = node->Length;
if (RefOffset > node->Length)
@ -45,7 +46,7 @@ namespace VirtualFileSystem
WriteFSFunction(fd_Write)
{
if (!Size)
if (Size <= 0)
Size = node->Length;
if (RefOffset > node->Length)
@ -64,15 +65,32 @@ namespace VirtualFileSystem
// .Write = fd_Write,
};
FileDescriptorTable::FileDescriptor
FileDescriptorTable::Fildes
FileDescriptorTable::GetFileDescriptor(int FileDescriptor)
{
foreach (auto fd in FileDescriptors)
{
if (fd.Descriptor == FileDescriptor)
{
debug("Found file descriptor %d", FileDescriptor);
return fd;
}
}
return {.Descriptor = -1};
return {};
}
FileDescriptorTable::DupFildes
FileDescriptorTable::GetDupFildes(int FileDescriptor)
{
foreach (auto fd in FildesDuplicates)
{
if (fd.Descriptor == FileDescriptor)
{
debug("Found duplicated file descriptor %d", FileDescriptor);
return fd;
}
}
return {};
}
int FileDescriptorTable::ProbeMode(mode_t Mode, int Flags)
@ -118,9 +136,8 @@ namespace VirtualFileSystem
if (!n)
{
error("Failed to create file %s: %d",
AbsolutePath, errno);
return -1;
debug("%s: File already exists, continuing...",
AbsolutePath);
}
}
@ -132,23 +149,25 @@ namespace VirtualFileSystem
if (!File)
{
errno = EEXIST;
error("Failed to open file %s: %d",
AbsolutePath, errno);
return -1;
}
}
if (Flags & O_TRUNC)
{
fixme("Implement O_TRUNC");
fixme("O_TRUNC");
}
if (Flags & O_APPEND)
{
fixme("Implement O_APPEND");
fixme("O_APPEND");
}
if (Flags & O_CLOEXEC)
{
fixme("Implement O_CLOEXEC");
fixme("O_CLOEXEC");
}
RefNode *File = vfs->Open(AbsolutePath,
@ -161,8 +180,7 @@ namespace VirtualFileSystem
return -1;
}
FileDescriptorTable::FileDescriptor fd;
fd.Descriptor = GetFreeFileDescriptor();
Fildes fd = {.Descriptor = GetFreeFileDescriptor()};
if (fd.Descriptor < 0)
{
@ -205,6 +223,19 @@ namespace VirtualFileSystem
}
}
forItr(itr, FildesDuplicates)
{
if (itr->Descriptor == FileDescriptor)
{
FildesDuplicates.erase(itr);
char FileName[64];
sprintf(FileName, "%d", FileDescriptor);
vfs->Delete(FileName, false, this->fdDir);
return 0;
}
}
errno = EBADF;
return -1;
}
@ -223,6 +254,19 @@ namespace VirtualFileSystem
break;
}
}
if (!Found)
{
foreach (auto fd in FildesDuplicates)
{
if (fd.Descriptor == i)
{
Found = true;
break;
}
}
}
if (!Found)
return i;
i++;
@ -234,12 +278,20 @@ namespace VirtualFileSystem
std::string FileDescriptorTable::GetAbsolutePath(int FileDescriptor)
{
FileDescriptorTable::FileDescriptor fd =
this->GetFileDescriptor(FileDescriptor);
if (fd.Descriptor == -1)
Fildes fd = this->GetFileDescriptor(FileDescriptor);
DupFildes dfd = this->GetDupFildes(FileDescriptor);
if (fd.Descriptor == -1 &&
dfd.Descriptor == -1)
return "";
Node *node = fd.Handle->node;
RefNode *hnd = nullptr;
if (fd.Descriptor != -1)
hnd = fd.Handle;
else
hnd = dfd.Handle;
Node *node = hnd->node;
std::string absolutePath = vfs->GetPathFromNode(node);
std::string path = absolutePath.c_str();
return path;
@ -264,36 +316,52 @@ namespace VirtualFileSystem
ssize_t FileDescriptorTable::_read(int fd, void *buf, size_t count)
{
FileDescriptor fdesc;
fdesc = this->GetFileDescriptor(fd);
if (fdesc.Descriptor < 0)
Fildes fdesc = this->GetFileDescriptor(fd);
DupFildes dfdesc = this->GetDupFildes(fd);
if (fdesc.Descriptor < 0 &&
dfdesc.Descriptor < 0)
{
errno = EBADF;
return -1;
}
return fdesc.Handle->Read((uint8_t *)buf, count);
RefNode *hnd = nullptr;
if (fdesc.Descriptor != -1)
hnd = fdesc.Handle;
else
hnd = dfdesc.Handle;
return hnd->Read((uint8_t *)buf, count);
}
ssize_t FileDescriptorTable::_write(int fd, const void *buf,
size_t count)
{
FileDescriptor fdesc;
fdesc = this->GetFileDescriptor(fd);
if (fdesc.Descriptor < 0)
Fildes fdesc = this->GetFileDescriptor(fd);
DupFildes dfdesc = this->GetDupFildes(fd);
if (fdesc.Descriptor < 0 &&
dfdesc.Descriptor < 0)
{
errno = EBADF;
return -1;
}
return fdesc.Handle->Write((uint8_t *)buf, count);
RefNode *hnd = nullptr;
if (fdesc.Descriptor != -1)
hnd = fdesc.Handle;
else
hnd = dfdesc.Handle;
return hnd->Write((uint8_t *)buf, count);
}
int FileDescriptorTable::_close(int fd)
{
FileDescriptor fdesc;
fdesc = this->GetFileDescriptor(fd);
Fildes fdesc = this->GetFileDescriptor(fd);
DupFildes dfdesc = this->GetDupFildes(fd);
if (fdesc.Descriptor < 0)
if (fdesc.Descriptor < 0 &&
dfdesc.Descriptor < 0)
{
errno = EBADF;
return -1;
@ -305,22 +373,61 @@ namespace VirtualFileSystem
return -1;
}
delete fdesc.Handle;
/* If the file descriptor is a duplicate,
we don't need to close the handle,
because it's a duplicate of another
file descriptor. */
bool Found = false;
RefNode *hnd = nullptr;
if (fdesc.Descriptor != -1)
hnd = fdesc.Handle;
else
hnd = dfdesc.Handle;
foreach (auto dfd in FileDescriptors)
{
if (dfd.Handle == hnd)
{
Found = true;
break;
}
}
foreach (auto dfd in FildesDuplicates)
{
if (dfd.Handle == hnd)
{
Found = true;
break;
}
}
if (!Found)
delete hnd;
return 0;
}
off_t FileDescriptorTable::_lseek(int fd, off_t offset, int whence)
{
FileDescriptor fdesc;
fdesc = this->GetFileDescriptor(fd);
Fildes fdesc = this->GetFileDescriptor(fd);
DupFildes dfdesc = this->GetDupFildes(fd);
if (fdesc.Descriptor < 0)
if (fdesc.Descriptor < 0 &&
dfdesc.Descriptor < 0)
{
errno = EBADF;
return -1;
}
return fdesc.Handle->Seek(offset, whence);
RefNode *hnd = nullptr;
if (fdesc.Descriptor != -1)
hnd = fdesc.Handle;
else
hnd = dfdesc.Handle;
return hnd->Seek(offset, whence);
}
int FileDescriptorTable::_stat(const char *pathname,
@ -353,22 +460,29 @@ namespace VirtualFileSystem
statbuf->st_size = node->Length;
statbuf->st_blksize = 0; /* FIXME: stub */
statbuf->st_blocks = 0; /* FIXME: stub */
statbuf->st_attr = 0; /* FIXME: stub */
statbuf->st_attr = 0; /* FIXME: stub */
return 0;
}
int FileDescriptorTable::_fstat(int fd, struct stat *statbuf)
{
FileDescriptor fdesc;
fdesc = this->GetFileDescriptor(fd);
Fildes fdesc = this->GetFileDescriptor(fd);
DupFildes dfdesc = this->GetDupFildes(fd);
if (fdesc.Descriptor < 0)
if (fdesc.Descriptor < 0 &&
dfdesc.Descriptor < 0)
{
errno = EBADF;
return -1;
}
Node *node = fdesc.Handle->node;
RefNode *hnd = nullptr;
if (fdesc.Descriptor != -1)
hnd = fdesc.Handle;
else
hnd = dfdesc.Handle;
Node *node = hnd->node;
statbuf->st_dev = 0; /* FIXME: stub */
statbuf->st_ino = node->IndexNode;
statbuf->st_mode = node->Flags | node->Mode;
@ -379,7 +493,7 @@ namespace VirtualFileSystem
statbuf->st_size = node->Length;
statbuf->st_blksize = 0; /* FIXME: stub */
statbuf->st_blocks = 0; /* FIXME: stub */
statbuf->st_attr = 0; /* FIXME: stub */
statbuf->st_attr = 0; /* FIXME: stub */
return 0;
}
@ -413,7 +527,112 @@ namespace VirtualFileSystem
statbuf->st_size = node->Length;
statbuf->st_blksize = 0; /* FIXME: stub */
statbuf->st_blocks = 0; /* FIXME: stub */
statbuf->st_attr = 0; /* FIXME: stub */
statbuf->st_attr = 0; /* FIXME: stub */
return 0;
}
int FileDescriptorTable::_dup(int oldfd)
{
Fildes fdesc = this->GetFileDescriptor(oldfd);
DupFildes dfdesc = this->GetDupFildes(oldfd);
if (fdesc.Descriptor < 0 &&
dfdesc.Descriptor < 0)
{
errno = EBADF;
return -1;
}
int newfd = this->GetFreeFileDescriptor();
if (newfd < 0)
{
errno = EMFILE;
return -1;
}
DupFildes new_dfd{};
if (fdesc.Descriptor != -1)
{
new_dfd.Handle = fdesc.Handle;
new_dfd.Mode = fdesc.Mode;
}
else
{
new_dfd.Handle = dfdesc.Handle;
new_dfd.Mode = dfdesc.Mode;
}
new_dfd.Descriptor = newfd;
this->FildesDuplicates.push_back(new_dfd);
debug("Duplicated file descriptor %d to %d",
oldfd, newfd);
return newfd;
}
int FileDescriptorTable::_dup2(int oldfd, int newfd)
{
Fildes fdesc = this->GetFileDescriptor(oldfd);
DupFildes dfdesc = this->GetDupFildes(oldfd);
if (fdesc.Descriptor < 0 &&
dfdesc.Descriptor < 0)
{
errno = EBADF;
return -1;
}
if (newfd < 0)
{
errno = EBADF;
return -1;
}
if (newfd == oldfd)
return newfd;
/* Even if it's not valid
we ignore it. */
this->_close(newfd);
DupFildes new_dfd{};
if (fdesc.Descriptor != -1)
{
new_dfd.Handle = fdesc.Handle;
new_dfd.Mode = fdesc.Mode;
}
else
{
new_dfd.Handle = dfdesc.Handle;
new_dfd.Mode = dfdesc.Mode;
}
new_dfd.Descriptor = newfd;
this->FildesDuplicates.push_back(new_dfd);
debug("Duplicated file descriptor %d to %d",
oldfd, newfd);
return newfd;
}
int FileDescriptorTable::_ioctl(int fd, unsigned long request, void *argp)
{
struct winsize *ws = (struct winsize *)argp;
Video::ScreenBuffer *sb = Display->GetBuffer(0);
Video::FontInfo fi = Display->GetCurrentFont()->GetInfo();
switch (request)
{
case TIOCGWINSZ:
fixme("TIOCGWINSZ: stub");
ws->ws_xpixel = uint16_t(sb->Width);
ws->ws_ypixel = uint16_t(sb->Height);
ws->ws_col = uint16_t(sb->Width / fi.Width);
ws->ws_row = uint16_t(sb->Height / fi.Height);
break;
default:
fixme("Unknown request %#lx", request);
errno = ENOSYS;
return -1;
}
return 0;
}

View File

@ -17,6 +17,13 @@
#include <filesystem.hpp>
#ifdef DEBUG
const char *SeekStrings[] =
{"SEEK_SET",
"SEEK_CUR",
"SEEK_END"};
#endif
namespace VirtualFileSystem
{
ReferenceNode *Node::CreateReference()
@ -83,10 +90,10 @@ namespace VirtualFileSystem
return -1;
}
off_t ReferenceNode::Seek(off_t Offset, int Whence)
off_t ReferenceNode::Seek(off_t _Offset, int Whence)
{
if (this->SymlinkTo)
return this->SymlinkTo->Seek(Offset, Whence);
return this->SymlinkTo->Seek(_Offset, Whence);
if (!this->node->Operator)
{
@ -99,25 +106,40 @@ namespace VirtualFileSystem
if (this->node->Operator->Seek)
{
off_t RefOffset = off_t(this->Offset.load());
return this->node->Operator->Seek(this->node, Offset, Whence, RefOffset);
debug("The node has a seek function");
return this->node->Operator->Seek(this->node, _Offset, Whence, RefOffset);
}
debug("Current offset is %d", this->Offset.load());
switch (Whence)
{
case SEEK_SET:
{
if (Offset > this->node->Length)
if (_Offset > this->node->Length)
{
errno = EINVAL;
return -1;
}
this->Offset.store(Offset);
if (_Offset < 0)
{
fixme("Negative offset %d is not implemented", _Offset);
_Offset = 0;
}
if (_Offset > this->node->Length)
{
fixme("Offset %d is bigger than file size %d",
_Offset, this->node->Length);
_Offset = this->node->Length;
}
this->Offset.store(_Offset);
break;
}
case SEEK_CUR:
{
off_t NewOffset = off_t(this->Offset.load()) + Offset;
off_t NewOffset = off_t(this->Offset.load()) + _Offset;
if (NewOffset > this->node->Length ||
NewOffset < 0)
{
@ -130,7 +152,7 @@ namespace VirtualFileSystem
}
case SEEK_END:
{
off_t NewOffset = this->node->Length + Offset;
off_t NewOffset = this->node->Length + _Offset;
if (NewOffset > this->node->Length ||
NewOffset < 0)
{
@ -149,7 +171,12 @@ namespace VirtualFileSystem
}
}
return (off_t)this->Offset.load();
off_t RetOffset = off_t(this->Offset.load());
debug("( %d %ld %s[%d] ) -> %d",
_Offset, this->Offset.load(),
SeekStrings[Whence], Whence,
RetOffset);
return RetOffset;
}
ReferenceNode::ReferenceNode(Node *node)

View File

@ -0,0 +1,49 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <filesystem.hpp>
#include <errno.h>
#include "../../kernel.h"
using namespace VirtualFileSystem;
ReadFSFunction(Null_Read)
{
if (Size <= 0)
return 0;
memset(Buffer, 0, Size);
return Size;
}
ReadFSFunction(Null_Write)
{
return Size;
}
FileSystemOperations null_op = {
.Name = "Null",
.Read = Null_Read,
.Write = Null_Write,
};
void Init_Null(Virtual *vfs_ctx)
{
Node *n = vfs_ctx->Create("null", CHARDEVICE, DevFS);
n->Operator = &null_op;
}

View File

@ -0,0 +1,52 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <filesystem.hpp>
#include <rand.hpp>
#include <errno.h>
#include "../../kernel.h"
using namespace VirtualFileSystem;
ReadFSFunction(Random_Read)
{
if (Size <= 0)
return 0;
uint64_t *buf = (uint64_t *)Buffer;
for (size_t i = 0; i < Size / sizeof(uint64_t); i++)
buf[i] = Random::rand64();
return Size;
}
ReadFSFunction(Random_Write)
{
return Size;
}
FileSystemOperations random_op = {
.Name = "Random",
.Read = Random_Read,
.Write = Random_Write,
};
void Init_Random(Virtual *vfs_ctx)
{
Node *n = vfs_ctx->Create("random", CHARDEVICE, DevFS);
n->Operator = &random_op;
}

View File

@ -0,0 +1,43 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <filesystem.hpp>
#include <errno.h>
#include "../../kernel.h"
using namespace VirtualFileSystem;
ReadFSFunction(tty_Write)
{
for (size_t i = 0; i < Size; i++)
putchar(((char *)Buffer)[i]);
Display->SetBuffer(0);
return Size;
}
FileSystemOperations tty_op = {
.Name = "tty",
.Write = tty_Write,
};
void Init_Teletype(Virtual *vfs_ctx)
{
Node *n = vfs_ctx->Create("tty", CHARDEVICE, DevFS);
n->Operator = &tty_op;
}

View File

@ -0,0 +1,49 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <filesystem.hpp>
#include <errno.h>
#include "../../kernel.h"
using namespace VirtualFileSystem;
ReadFSFunction(Zero_Read)
{
if (Size <= 0)
return 0;
memset(Buffer, 0, Size);
return Size;
}
ReadFSFunction(Zero_Write)
{
return Size;
}
FileSystemOperations zero_op = {
.Name = "Zero",
.Read = Zero_Read,
.Write = Zero_Write,
};
void Init_Zero(Virtual *vfs_ctx)
{
Node *n = vfs_ctx->Create("zero", CHARDEVICE, DevFS);
n->Operator = &zero_op;
}