From d2425495ca7c24d09613649978678027f91a140d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 17 Oct 2018 11:40:58 +0200 Subject: [PATCH] VirtualFileSystem class builds inside Kernel. --- AK/ktime.h | 7 +- Kernel/Makefile | 3 +- Kernel/VGA.cpp | 172 ++++++++++++++---------- Kernel/VGA.h | 4 +- Kernel/ktime.h | 26 ++++ VirtualFileSystem/Ext2FileSystem.cpp | 2 +- VirtualFileSystem/UnixTypes.h | 2 +- VirtualFileSystem/VirtualFileSystem.cpp | 28 ++-- 8 files changed, 147 insertions(+), 97 deletions(-) create mode 100644 Kernel/ktime.h diff --git a/AK/ktime.h b/AK/ktime.h index b5f5e639a88..f082def89e5 100644 --- a/AK/ktime.h +++ b/AK/ktime.h @@ -1,12 +1,7 @@ #pragma once #ifdef SERENITY_KERNEL -inline time_t time(time_t* tloc) -{ - if (tloc) - *tloc = 123; - return 123; -} +#include #else #include #define ktime time diff --git a/Kernel/Makefile b/Kernel/Makefile index 330b6582148..63041a0b5b1 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -28,7 +28,8 @@ VFS_OBJS = \ ../VirtualFileSystem/RandomDevice.o \ ../VirtualFileSystem/FileSystem.o \ ../VirtualFileSystem/DiskBackedFileSystem.o \ - ../VirtualFileSystem/Ext2FileSystem.o + ../VirtualFileSystem/Ext2FileSystem.o \ + ../VirtualFileSystem/VirtualFileSystem.o AK_OBJS = \ ../AK/String.o \ diff --git a/Kernel/VGA.cpp b/Kernel/VGA.cpp index c9916a3b79a..06d02339dc7 100644 --- a/Kernel/VGA.cpp +++ b/Kernel/VGA.cpp @@ -11,55 +11,50 @@ PRIVATE BYTE current_attr = 0x07; PRIVATE volatile WORD soft_cursor; -PRIVATE void print_num( DWORD ); -PRIVATE void print_hex( DWORD, BYTE fields ); -static void printSignedNumber(int); +template static int printNumber(PutChFunc, char*&, DWORD); +template static int printHex(PutChFunc, char*&, DWORD, BYTE fields); +template static int printSignedNumber(PutChFunc, char*&, int); -PRIVATE void -putch( char ch ) +static void vga_putch(char*, char ch) { WORD row; - switch( ch ) - { - case '\n': - row = soft_cursor / 80; - if( row == 23 ) - { - memcpy( vga_mem, vga_mem + 160, 160 * 23 ); - memset( vga_mem + (160 * 23), 0, 160 ); - soft_cursor = row * 80; - } - else - soft_cursor = (row + 1) * 80; - return; - default: - vga_mem[soft_cursor * 2] = ch; - vga_mem[soft_cursor * 2 + 1] = current_attr; - soft_cursor++; + switch (ch) { + case '\n': + row = soft_cursor / 80; + if (row == 23) { + memcpy( vga_mem, vga_mem + 160, 160 * 23 ); + memset( vga_mem + (160 * 23), 0, 160 ); + soft_cursor = row * 80; + } else { + soft_cursor = (row + 1) * 80; + } + return; + default: + vga_mem[soft_cursor * 2] = ch; + vga_mem[soft_cursor * 2 + 1] = current_attr; + ++soft_cursor; } row = soft_cursor / 80; if ((row >= 24 && current->handle() != IPC::Handle::PanelTask)) { - memcpy( vga_mem, vga_mem + 160, 160 * 23 ); - memset( vga_mem + (160 * 23), 0, 160 ); + memcpy(vga_mem, vga_mem + 160, 160 * 23); + memset(vga_mem + (160 * 23), 0, 160); soft_cursor = 23 * 80; } } -PUBLIC void -kprintf( const char *fmt, ... ) +template +int kprintfInternal(PutChFunc putch, char* buffer, const char*& fmt, char*& ap) { const char *p; - va_list ap; soft_cursor = vga_get_cursor(); - va_start( ap, fmt ); + int ret = 0; + char* bufptr = buffer; - for( p = fmt; *p; ++p ) - { - if( *p == '%' && *(p + 1) ) - { + for (p = fmt; *p; ++p) { + if (*p == '%' && *(p + 1)) { ++p; switch( *p ) { @@ -68,105 +63,136 @@ kprintf( const char *fmt, ... ) const char* sp = va_arg(ap, const char*); //ASSERT(sp != nullptr); if (!sp) { - putch('<'); - putch('N'); - putch('u'); - putch('L'); - putch('>'); + putch(bufptr, '<'); + putch(bufptr, 'N'); + putch(bufptr, 'u'); + putch(bufptr, 'L'); + putch(bufptr, '>'); + ret += 5; } else { - for (; *sp; ++sp) - putch(*sp); + for (; *sp; ++sp) { + putch(bufptr, *sp); + ++ret; + } } } break; case 'd': - printSignedNumber(va_arg(ap, int)); + ret += printSignedNumber(putch, bufptr, va_arg(ap, int)); break; case 'u': - print_num( va_arg( ap, DWORD )); + ret += printNumber(putch, bufptr, va_arg(ap, DWORD)); break; case 'x': - print_hex( va_arg( ap, DWORD ), 8 ); + ret += printHex(putch, bufptr, va_arg(ap, DWORD), 8); break; case 'b': - print_hex( va_arg( ap, int ), 2 ); + ret += printHex(putch, bufptr, va_arg(ap, int), 2); break; case 'c': - putch( (char)va_arg( ap, int )); + putch(bufptr, (char)va_arg(ap, int)); + ++ret; break; case 'p': - putch( '0' ); - putch( 'x' ); - print_hex( va_arg( ap, DWORD ), 8 ); + putch(bufptr, '0'); + putch(bufptr, 'x'); + ret += 2; + ret += printHex(putch, bufptr, va_arg(ap, DWORD), 8); break; } } - else - { - putch( *p ); + else { + putch(bufptr, *p); + ++ret; } } - - /* va_arg( ap, type ); */ - va_end( ap ); - - vga_set_cursor( soft_cursor ); + return ret; } -PRIVATE void -print_hex( DWORD number, BYTE fields ) +int kprintf(const char* fmt, ...) +{ + 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 +int printHex(PutChFunc putch, char*& bufptr, DWORD number, BYTE fields) { static const char h[] = { '0','1','2','3','4','5','6','7', '8','9','a','b','c','d','e','f' }; + int ret = 0; BYTE shr_count = fields * 4; - while( shr_count ) - { + while (shr_count) { shr_count -= 4; - putch( h[(number >> shr_count) & 0x0F] ); + putch(bufptr, h[(number >> shr_count) & 0x0F]); + ++ret; } + return ret; } -PRIVATE void -print_num( DWORD number ) +template +int printNumber(PutChFunc putch, char*& bufptr, DWORD number) { DWORD divisor = 1000000000; char ch; char padding = 1; + int ret = 0; - for( ;; ) - { + for (;;) { ch = '0' + (number / divisor); number %= divisor; - if( ch != '0' ) + if (ch != '0') padding = 0; - if( !padding || divisor == 1 ) - putch( ch ); + if (!padding || divisor == 1) { + putch(bufptr, ch); + ++ret; + } - if( divisor == 1 ) + if (divisor == 1) break; divisor /= 10; } + return ret; } -static void printSignedNumber(int number) +template +static int printSignedNumber(PutChFunc putch, char*& bufptr, int number) { if (number < 0) { - putch('-'); - print_num(0 - number); - } else { - print_num(number); + putch(bufptr, '-'); + return printNumber(putch, bufptr, 0 - number) + 1; } + return printNumber(putch, bufptr, number); } PUBLIC void diff --git a/Kernel/VGA.h b/Kernel/VGA.h index 747e064509b..7d5ff358f4e 100644 --- a/Kernel/VGA.h +++ b/Kernel/VGA.h @@ -8,4 +8,6 @@ void vga_set_attr(BYTE); void vga_set_cursor(WORD); void vga_set_cursor(BYTE row, BYTE column); WORD vga_get_cursor(); -void kprintf(const char *fmt, ...); + +int kprintf(const char *fmt, ...); +int ksprintf(char* buf, const char *fmt, ...); diff --git a/Kernel/ktime.h b/Kernel/ktime.h new file mode 100644 index 00000000000..75655fd4e05 --- /dev/null +++ b/Kernel/ktime.h @@ -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; +} diff --git a/VirtualFileSystem/Ext2FileSystem.cpp b/VirtualFileSystem/Ext2FileSystem.cpp index c42e929bc01..19b150848cf 100644 --- a/VirtualFileSystem/Ext2FileSystem.cpp +++ b/VirtualFileSystem/Ext2FileSystem.cpp @@ -948,7 +948,7 @@ InodeIdentifier Ext2FileSystem::createInode(InodeIdentifier parentInode, const S else initialLinksCount = 1; - auto timestamp = time(nullptr); + auto timestamp = ktime(nullptr); auto e2inode = make(); memset(e2inode.ptr(), 0, sizeof(ext2_inode)); e2inode->i_mode = mode; diff --git a/VirtualFileSystem/UnixTypes.h b/VirtualFileSystem/UnixTypes.h index ca873e40a30..92eb9bc5a48 100644 --- a/VirtualFileSystem/UnixTypes.h +++ b/VirtualFileSystem/UnixTypes.h @@ -24,7 +24,7 @@ typedef signed_qword off_t; typedef dword blksize_t; typedef dword blkcnt_t; -typedef long int time_t; +typedef unsigned int time_t; typedef dword size_t; typedef signed_dword ssize_t; diff --git a/VirtualFileSystem/VirtualFileSystem.cpp b/VirtualFileSystem/VirtualFileSystem.cpp index b1884f7c4b4..630b9204b37 100644 --- a/VirtualFileSystem/VirtualFileSystem.cpp +++ b/VirtualFileSystem/VirtualFileSystem.cpp @@ -81,8 +81,8 @@ bool VirtualFileSystem::mount(RetainPtr&& fileSystem, const String& 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 - auto mount = make(inode, std::move(fileSystem)); - m_mounts.append(std::move(mount)); + auto mount = make(inode, move(fileSystem)); + m_mounts.append(move(mount)); return true; } @@ -93,7 +93,7 @@ bool VirtualFileSystem::mountRoot(RetainPtr&& fileSystem) return false; } - auto mount = make(InodeIdentifier(), std::move(fileSystem)); + auto mount = make(InodeIdentifier(), move(fileSystem)); auto node = makeNode(mount->guest()); if (!node->inUse()) { @@ -105,13 +105,13 @@ bool VirtualFileSystem::mountRoot(RetainPtr&& fileSystem) return false; } - m_rootNode = std::move(node); + m_rootNode = move(node); kprintf("[VFS] mountRoot mounted %s{%p}\n", m_rootNode->fileSystem()->className(), m_rootNode->fileSystem()); - m_mounts.append(std::move(mount)); + m_mounts.append(move(mount)); return true; } @@ -136,7 +136,7 @@ void VirtualFileSystem::freeNode(Node* node) node->inode.fileSystem()->release(); node->inode = InodeIdentifier(); node->m_characterDevice = nullptr; - m_nodeFreeList.append(std::move(node)); + m_nodeFreeList.append(move(node)); } bool VirtualFileSystem::isDirectory(const String& path) @@ -260,14 +260,14 @@ void VirtualFileSystem::listDirectory(const String& path) if (metadata.isCharacterDevice() || metadata.isBlockDevice()) { char buf[16]; - sprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice); + ksprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice); kprintf("%12s ", buf); } else { kprintf("%12lld ", metadata.size); } kprintf("\033[30;1m"); - auto tm = *localtime(&metadata.mtime); + auto tm = *klocaltime(&metadata.mtime); kprintf("%04u-%02u-%02u %02u:%02u:%02u ", tm.tm_year + 1900, tm.tm_mon + 1, @@ -306,7 +306,7 @@ void VirtualFileSystem::listDirectoryRecursively(const String& path) if (metadata.isDirectory()) { if (entry.name != "." && entry.name != "..") { char buf[4096]; - sprintf(buf, "%s/%s", path.characters(), entry.name.characters()); + ksprintf(buf, "%s/%s", path.characters(), entry.name.characters()); listDirectoryRecursively(buf); } } else { @@ -320,7 +320,7 @@ bool VirtualFileSystem::touch(const String& path) auto inode = resolvePath(path); if (!inode.isValid()) return false; - return inode.fileSystem()->setModificationTime(inode, time(nullptr)); + return inode.fileSystem()->setModificationTime(inode, ktime(nullptr)); } OwnPtr VirtualFileSystem::open(const String& path) @@ -331,7 +331,7 @@ OwnPtr VirtualFileSystem::open(const String& path) auto vnode = getOrCreateNode(inode); if (!vnode) return nullptr; - return make(std::move(vnode)); + return make(move(vnode)); } OwnPtr VirtualFileSystem::create(const String& path) @@ -354,7 +354,7 @@ InodeIdentifier VirtualFileSystem::resolveSymbolicLink(const String& basePath, I if (!symlinkContents) return { }; 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); } @@ -407,7 +407,7 @@ InodeIdentifier VirtualFileSystem::resolvePath(const String& path) char buf[4096] = ""; char* p = buf; 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); if (!inode.isValid()) { @@ -436,7 +436,7 @@ void VirtualFileSystem::Node::release() VirtualFileSystem::Mount::Mount(InodeIdentifier host, RetainPtr&& guestFileSystem) : m_host(host) , m_guest(guestFileSystem->rootInode()) - , m_fileSystem(std::move(guestFileSystem)) + , m_fileSystem(move(guestFileSystem)) { }