1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:58:11 +00:00

VirtualFileSystem class builds inside Kernel.

This commit is contained in:
Andreas Kling 2018-10-17 11:40:58 +02:00
parent 9171521752
commit d2425495ca
8 changed files with 147 additions and 97 deletions

View file

@ -1,12 +1,7 @@
#pragma once #pragma once
#ifdef SERENITY_KERNEL #ifdef SERENITY_KERNEL
inline time_t time(time_t* tloc) #include <Kernel/ktime.h>
{
if (tloc)
*tloc = 123;
return 123;
}
#else #else
#include <time.h> #include <time.h>
#define ktime time #define ktime time

View file

@ -28,7 +28,8 @@ VFS_OBJS = \
../VirtualFileSystem/RandomDevice.o \ ../VirtualFileSystem/RandomDevice.o \
../VirtualFileSystem/FileSystem.o \ ../VirtualFileSystem/FileSystem.o \
../VirtualFileSystem/DiskBackedFileSystem.o \ ../VirtualFileSystem/DiskBackedFileSystem.o \
../VirtualFileSystem/Ext2FileSystem.o ../VirtualFileSystem/Ext2FileSystem.o \
../VirtualFileSystem/VirtualFileSystem.o
AK_OBJS = \ AK_OBJS = \
../AK/String.o \ ../AK/String.o \

View file

@ -11,55 +11,50 @@ PRIVATE BYTE current_attr = 0x07;
PRIVATE volatile WORD soft_cursor; PRIVATE volatile WORD soft_cursor;
PRIVATE void print_num( DWORD ); template<typename PutChFunc> static int printNumber(PutChFunc, char*&, DWORD);
PRIVATE void print_hex( DWORD, BYTE fields ); template<typename PutChFunc> static int printHex(PutChFunc, char*&, DWORD, BYTE fields);
static void printSignedNumber(int); template<typename PutChFunc> static int printSignedNumber(PutChFunc, char*&, int);
PRIVATE void static void vga_putch(char*, char ch)
putch( char ch )
{ {
WORD row; WORD row;
switch( ch ) switch (ch) {
{ case '\n':
case '\n': row = soft_cursor / 80;
row = soft_cursor / 80; if (row == 23) {
if( row == 23 ) memcpy( vga_mem, vga_mem + 160, 160 * 23 );
{ memset( vga_mem + (160 * 23), 0, 160 );
memcpy( vga_mem, vga_mem + 160, 160 * 23 ); soft_cursor = row * 80;
memset( vga_mem + (160 * 23), 0, 160 ); } else {
soft_cursor = row * 80; soft_cursor = (row + 1) * 80;
} }
else return;
soft_cursor = (row + 1) * 80; default:
return; vga_mem[soft_cursor * 2] = ch;
default: vga_mem[soft_cursor * 2 + 1] = current_attr;
vga_mem[soft_cursor * 2] = ch; ++soft_cursor;
vga_mem[soft_cursor * 2 + 1] = current_attr;
soft_cursor++;
} }
row = soft_cursor / 80; row = soft_cursor / 80;
if ((row >= 24 && current->handle() != IPC::Handle::PanelTask)) { if ((row >= 24 && current->handle() != IPC::Handle::PanelTask)) {
memcpy( vga_mem, vga_mem + 160, 160 * 23 ); memcpy(vga_mem, vga_mem + 160, 160 * 23);
memset( vga_mem + (160 * 23), 0, 160 ); memset(vga_mem + (160 * 23), 0, 160);
soft_cursor = 23 * 80; soft_cursor = 23 * 80;
} }
} }
PUBLIC void template<typename PutChFunc>
kprintf( const char *fmt, ... ) int kprintfInternal(PutChFunc putch, char* buffer, const char*& fmt, char*& ap)
{ {
const char *p; const char *p;
va_list ap;
soft_cursor = vga_get_cursor(); soft_cursor = vga_get_cursor();
va_start( ap, fmt ); int ret = 0;
char* bufptr = buffer;
for( p = fmt; *p; ++p ) for (p = fmt; *p; ++p) {
{ if (*p == '%' && *(p + 1)) {
if( *p == '%' && *(p + 1) )
{
++p; ++p;
switch( *p ) switch( *p )
{ {
@ -68,105 +63,136 @@ kprintf( const char *fmt, ... )
const char* sp = va_arg(ap, const char*); const char* sp = va_arg(ap, const char*);
//ASSERT(sp != nullptr); //ASSERT(sp != nullptr);
if (!sp) { if (!sp) {
putch('<'); putch(bufptr, '<');
putch('N'); putch(bufptr, 'N');
putch('u'); putch(bufptr, 'u');
putch('L'); putch(bufptr, 'L');
putch('>'); putch(bufptr, '>');
ret += 5;
} else { } else {
for (; *sp; ++sp) for (; *sp; ++sp) {
putch(*sp); putch(bufptr, *sp);
++ret;
}
} }
} }
break; break;
case 'd': case 'd':
printSignedNumber(va_arg(ap, int)); ret += printSignedNumber(putch, bufptr, va_arg(ap, int));
break; break;
case 'u': case 'u':
print_num( va_arg( ap, DWORD )); ret += printNumber(putch, bufptr, va_arg(ap, DWORD));
break; break;
case 'x': case 'x':
print_hex( va_arg( ap, DWORD ), 8 ); ret += printHex(putch, bufptr, va_arg(ap, DWORD), 8);
break; break;
case 'b': case 'b':
print_hex( va_arg( ap, int ), 2 ); ret += printHex(putch, bufptr, va_arg(ap, int), 2);
break; break;
case 'c': case 'c':
putch( (char)va_arg( ap, int )); putch(bufptr, (char)va_arg(ap, int));
++ret;
break; break;
case 'p': case 'p':
putch( '0' ); putch(bufptr, '0');
putch( 'x' ); putch(bufptr, 'x');
print_hex( va_arg( ap, DWORD ), 8 ); ret += 2;
ret += printHex(putch, bufptr, va_arg(ap, DWORD), 8);
break; break;
} }
} }
else else {
{ putch(bufptr, *p);
putch( *p ); ++ret;
} }
} }
return ret;
/* va_arg( ap, type ); */
va_end( ap );
vga_set_cursor( soft_cursor );
} }
PRIVATE void int kprintf(const char* fmt, ...)
print_hex( DWORD number, BYTE fields ) {
va_list ap;
va_start(ap, fmt);
soft_cursor = vga_get_cursor();
int ret = kprintfInternal(vga_putch, nullptr, fmt, ap);
vga_set_cursor(soft_cursor);
va_end(ap);
return ret;
}
static void buffer_putch(char* bufptr, char ch)
{
*bufptr++ = ch;
}
int ksprintf(char* buffer, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = kprintfInternal(buffer_putch, buffer, fmt, ap);
va_end(ap);
return ret;
}
template<typename PutChFunc>
int printHex(PutChFunc putch, char*& bufptr, DWORD number, BYTE fields)
{ {
static const char h[] = { static const char h[] = {
'0','1','2','3','4','5','6','7', '0','1','2','3','4','5','6','7',
'8','9','a','b','c','d','e','f' '8','9','a','b','c','d','e','f'
}; };
int ret = 0;
BYTE shr_count = fields * 4; BYTE shr_count = fields * 4;
while( shr_count ) while (shr_count) {
{
shr_count -= 4; shr_count -= 4;
putch( h[(number >> shr_count) & 0x0F] ); putch(bufptr, h[(number >> shr_count) & 0x0F]);
++ret;
} }
return ret;
} }
PRIVATE void template<typename PutChFunc>
print_num( DWORD number ) int printNumber(PutChFunc putch, char*& bufptr, DWORD number)
{ {
DWORD divisor = 1000000000; DWORD divisor = 1000000000;
char ch; char ch;
char padding = 1; char padding = 1;
int ret = 0;
for( ;; ) for (;;) {
{
ch = '0' + (number / divisor); ch = '0' + (number / divisor);
number %= divisor; number %= divisor;
if( ch != '0' ) if (ch != '0')
padding = 0; padding = 0;
if( !padding || divisor == 1 ) if (!padding || divisor == 1) {
putch( ch ); putch(bufptr, ch);
++ret;
}
if( divisor == 1 ) if (divisor == 1)
break; break;
divisor /= 10; divisor /= 10;
} }
return ret;
} }
static void printSignedNumber(int number) template<typename PutChFunc>
static int printSignedNumber(PutChFunc putch, char*& bufptr, int number)
{ {
if (number < 0) { if (number < 0) {
putch('-'); putch(bufptr, '-');
print_num(0 - number); return printNumber(putch, bufptr, 0 - number) + 1;
} else {
print_num(number);
} }
return printNumber(putch, bufptr, number);
} }
PUBLIC void PUBLIC void

View file

@ -8,4 +8,6 @@ void vga_set_attr(BYTE);
void vga_set_cursor(WORD); void vga_set_cursor(WORD);
void vga_set_cursor(BYTE row, BYTE column); void vga_set_cursor(BYTE row, BYTE column);
WORD vga_get_cursor(); WORD vga_get_cursor();
void kprintf(const char *fmt, ...);
int kprintf(const char *fmt, ...);
int ksprintf(char* buf, const char *fmt, ...);

26
Kernel/ktime.h Normal file
View file

@ -0,0 +1,26 @@
#pragma once
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
inline time_t ktime(time_t* tloc)
{
if (tloc)
*tloc = 123;
return 123;
}
inline tm* klocaltime(time_t* t)
{
static tm timmy;
return &timmy;
}

View file

@ -948,7 +948,7 @@ InodeIdentifier Ext2FileSystem::createInode(InodeIdentifier parentInode, const S
else else
initialLinksCount = 1; initialLinksCount = 1;
auto timestamp = time(nullptr); auto timestamp = ktime(nullptr);
auto e2inode = make<ext2_inode>(); auto e2inode = make<ext2_inode>();
memset(e2inode.ptr(), 0, sizeof(ext2_inode)); memset(e2inode.ptr(), 0, sizeof(ext2_inode));
e2inode->i_mode = mode; e2inode->i_mode = mode;

View file

@ -24,7 +24,7 @@ typedef signed_qword off_t;
typedef dword blksize_t; typedef dword blksize_t;
typedef dword blkcnt_t; typedef dword blkcnt_t;
typedef long int time_t; typedef unsigned int time_t;
typedef dword size_t; typedef dword size_t;
typedef signed_dword ssize_t; typedef signed_dword ssize_t;

View file

@ -81,8 +81,8 @@ bool VirtualFileSystem::mount(RetainPtr<FileSystem>&& fileSystem, const String&
kprintf("mounting %s{%p} at %s (inode: %u)\n", fileSystem->className(), fileSystem.ptr(), path.characters(), inode.index()); kprintf("mounting %s{%p} at %s (inode: %u)\n", fileSystem->className(), fileSystem.ptr(), path.characters(), inode.index());
// FIXME: check that this is not already a mount point // FIXME: check that this is not already a mount point
auto mount = make<Mount>(inode, std::move(fileSystem)); auto mount = make<Mount>(inode, move(fileSystem));
m_mounts.append(std::move(mount)); m_mounts.append(move(mount));
return true; return true;
} }
@ -93,7 +93,7 @@ bool VirtualFileSystem::mountRoot(RetainPtr<FileSystem>&& fileSystem)
return false; return false;
} }
auto mount = make<Mount>(InodeIdentifier(), std::move(fileSystem)); auto mount = make<Mount>(InodeIdentifier(), move(fileSystem));
auto node = makeNode(mount->guest()); auto node = makeNode(mount->guest());
if (!node->inUse()) { if (!node->inUse()) {
@ -105,13 +105,13 @@ bool VirtualFileSystem::mountRoot(RetainPtr<FileSystem>&& fileSystem)
return false; return false;
} }
m_rootNode = std::move(node); m_rootNode = move(node);
kprintf("[VFS] mountRoot mounted %s{%p}\n", kprintf("[VFS] mountRoot mounted %s{%p}\n",
m_rootNode->fileSystem()->className(), m_rootNode->fileSystem()->className(),
m_rootNode->fileSystem()); m_rootNode->fileSystem());
m_mounts.append(std::move(mount)); m_mounts.append(move(mount));
return true; return true;
} }
@ -136,7 +136,7 @@ void VirtualFileSystem::freeNode(Node* node)
node->inode.fileSystem()->release(); node->inode.fileSystem()->release();
node->inode = InodeIdentifier(); node->inode = InodeIdentifier();
node->m_characterDevice = nullptr; node->m_characterDevice = nullptr;
m_nodeFreeList.append(std::move(node)); m_nodeFreeList.append(move(node));
} }
bool VirtualFileSystem::isDirectory(const String& path) bool VirtualFileSystem::isDirectory(const String& path)
@ -260,14 +260,14 @@ void VirtualFileSystem::listDirectory(const String& path)
if (metadata.isCharacterDevice() || metadata.isBlockDevice()) { if (metadata.isCharacterDevice() || metadata.isBlockDevice()) {
char buf[16]; char buf[16];
sprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice); ksprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice);
kprintf("%12s ", buf); kprintf("%12s ", buf);
} else { } else {
kprintf("%12lld ", metadata.size); kprintf("%12lld ", metadata.size);
} }
kprintf("\033[30;1m"); kprintf("\033[30;1m");
auto tm = *localtime(&metadata.mtime); auto tm = *klocaltime(&metadata.mtime);
kprintf("%04u-%02u-%02u %02u:%02u:%02u ", kprintf("%04u-%02u-%02u %02u:%02u:%02u ",
tm.tm_year + 1900, tm.tm_year + 1900,
tm.tm_mon + 1, tm.tm_mon + 1,
@ -306,7 +306,7 @@ void VirtualFileSystem::listDirectoryRecursively(const String& path)
if (metadata.isDirectory()) { if (metadata.isDirectory()) {
if (entry.name != "." && entry.name != "..") { if (entry.name != "." && entry.name != "..") {
char buf[4096]; char buf[4096];
sprintf(buf, "%s/%s", path.characters(), entry.name.characters()); ksprintf(buf, "%s/%s", path.characters(), entry.name.characters());
listDirectoryRecursively(buf); listDirectoryRecursively(buf);
} }
} else { } else {
@ -320,7 +320,7 @@ bool VirtualFileSystem::touch(const String& path)
auto inode = resolvePath(path); auto inode = resolvePath(path);
if (!inode.isValid()) if (!inode.isValid())
return false; return false;
return inode.fileSystem()->setModificationTime(inode, time(nullptr)); return inode.fileSystem()->setModificationTime(inode, ktime(nullptr));
} }
OwnPtr<FileHandle> VirtualFileSystem::open(const String& path) OwnPtr<FileHandle> VirtualFileSystem::open(const String& path)
@ -331,7 +331,7 @@ OwnPtr<FileHandle> VirtualFileSystem::open(const String& path)
auto vnode = getOrCreateNode(inode); auto vnode = getOrCreateNode(inode);
if (!vnode) if (!vnode)
return nullptr; return nullptr;
return make<FileHandle>(std::move(vnode)); return make<FileHandle>(move(vnode));
} }
OwnPtr<FileHandle> VirtualFileSystem::create(const String& path) OwnPtr<FileHandle> VirtualFileSystem::create(const String& path)
@ -354,7 +354,7 @@ InodeIdentifier VirtualFileSystem::resolveSymbolicLink(const String& basePath, I
if (!symlinkContents) if (!symlinkContents)
return { }; return { };
char buf[4096]; char buf[4096];
sprintf(buf, "/%s/%s", basePath.characters(), String((const char*)symlinkContents.pointer(), symlinkContents.size()).characters()); ksprintf(buf, "/%s/%s", basePath.characters(), String((const char*)symlinkContents.pointer(), symlinkContents.size()).characters());
return resolvePath(buf); return resolvePath(buf);
} }
@ -407,7 +407,7 @@ InodeIdentifier VirtualFileSystem::resolvePath(const String& path)
char buf[4096] = ""; char buf[4096] = "";
char* p = buf; char* p = buf;
for (unsigned j = 0; j < i; ++j) { for (unsigned j = 0; j < i; ++j) {
p += sprintf(p, "/%s", parts[j].characters()); p += ksprintf(p, "/%s", parts[j].characters());
} }
inode = resolveSymbolicLink(buf, inode); inode = resolveSymbolicLink(buf, inode);
if (!inode.isValid()) { if (!inode.isValid()) {
@ -436,7 +436,7 @@ void VirtualFileSystem::Node::release()
VirtualFileSystem::Mount::Mount(InodeIdentifier host, RetainPtr<FileSystem>&& guestFileSystem) VirtualFileSystem::Mount::Mount(InodeIdentifier host, RetainPtr<FileSystem>&& guestFileSystem)
: m_host(host) : m_host(host)
, m_guest(guestFileSystem->rootInode()) , m_guest(guestFileSystem->rootInode())
, m_fileSystem(std::move(guestFileSystem)) , m_fileSystem(move(guestFileSystem))
{ {
} }