From 8b7216aaf2dacad7ce67d734d07b047fed8ec01e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 29 Dec 2024 09:57:48 +0100 Subject: IMPL: NeFS: Add file lock feature and validation for it. - It is added to hide files from indexing. - And will be used to hide system files. IMPL: Swap: Add SwapDiskDelegate class, will also work on a swap packet structure. Signed-off-by: Amlal El Mahrouss --- dev/Kernel/FSKit/NeFS.h | 16 ++++++-- dev/Kernel/KernelKit/LPC.h | 1 + dev/Kernel/KernelKit/Timer.h | 1 - dev/Kernel/KernelKit/UserProcessScheduler.h | 5 +-- dev/Kernel/SystemKit/Scheduler.h | 20 ++++++++++ dev/Kernel/SystemKit/Swap.h | 18 +++++++++ dev/Kernel/SystemKit/SwapMgr.h | 18 --------- dev/Kernel/SystemKit/ThreadMgr.h | 20 ---------- dev/Kernel/src/FS/NeFS.cc | 23 +++++++++--- dev/Kernel/src/Mgr/SwapMgr.cc | 28 -------------- dev/Kernel/src/System/SwapDiskDelegate.cc | 57 +++++++++++++++++++++++++++++ 11 files changed, 128 insertions(+), 79 deletions(-) create mode 100644 dev/Kernel/SystemKit/Scheduler.h create mode 100644 dev/Kernel/SystemKit/Swap.h delete mode 100644 dev/Kernel/SystemKit/SwapMgr.h delete mode 100644 dev/Kernel/SystemKit/ThreadMgr.h delete mode 100644 dev/Kernel/src/Mgr/SwapMgr.cc create mode 100644 dev/Kernel/src/System/SwapDiskDelegate.cc (limited to 'dev/Kernel') diff --git a/dev/Kernel/FSKit/NeFS.h b/dev/Kernel/FSKit/NeFS.h index 6d0b6bfe..c33ffaf7 100644 --- a/dev/Kernel/FSKit/NeFS.h +++ b/dev/Kernel/FSKit/NeFS.h @@ -131,25 +131,33 @@ enum kNeFSDriveCount = 7, }; +enum +{ + kNeFSStatusUnlocked, + kNeFSStatusLocked, + kNeFSStatusError, + kNeFSStatusInvalid, +}; + /// @brief Catalog type. struct PACKED NFS_CATALOG_STRUCT final { Kernel::Char Name[kNeFSNodeNameLen]; Kernel::Char Mime[kNeFSMimeNameLen]; - /// Catalog status flag. + /// Catalog flags. Kernel::UInt16 Flags; + Kernel::UInt16 Status; /// Custom catalog flags. Kernel::UInt16 FilkMMFlags; /// Catalog kind. Kernel::Int32 Kind; - /// Size of the data fork. Kernel::Lba DataForkSize; - /// Size of all resource forks. Kernel::Lba ResourceForkSize; + /// Forks LBA. Kernel::Lba DataFork; Kernel::Lba ResourceFork; @@ -264,7 +272,7 @@ namespace Kernel _Output Void CloseFork(_Input NFS_FORK_STRUCT* fork); - _Output NFS_CATALOG_STRUCT* FindCatalog(_Input const Char* catalogName, Lba& outLba); + _Output NFS_CATALOG_STRUCT* FindCatalog(_Input const Char* catalogName, Lba& outLba, Bool searchHidden = YES); _Output NFS_CATALOG_STRUCT* GetCatalog(_Input const Char* name); diff --git a/dev/Kernel/KernelKit/LPC.h b/dev/Kernel/KernelKit/LPC.h index 6596d77f..c73c9da7 100644 --- a/dev/Kernel/KernelKit/LPC.h +++ b/dev/Kernel/KernelKit/LPC.h @@ -57,6 +57,7 @@ namespace Kernel inline constexpr HError kErrorInvalidCreds = 61; inline constexpr HError kErrorCDTrayBroken = 62; inline constexpr HError kErrorUnrecoverableDisk = 63; + inline constexpr HError kErrorFileLocked = 64; inline constexpr HError kErrorUnimplemented = 0; /// @brief Raises a bug check stop code. diff --git a/dev/Kernel/KernelKit/Timer.h b/dev/Kernel/KernelKit/Timer.h index b95dc9ca..59c21d40 100644 --- a/dev/Kernel/KernelKit/Timer.h +++ b/dev/Kernel/KernelKit/Timer.h @@ -7,7 +7,6 @@ #pragma once #include -#include #include namespace Kernel diff --git a/dev/Kernel/KernelKit/UserProcessScheduler.h b/dev/Kernel/KernelKit/UserProcessScheduler.h index 73780dc3..b79985dd 100644 --- a/dev/Kernel/KernelKit/UserProcessScheduler.h +++ b/dev/Kernel/KernelKit/UserProcessScheduler.h @@ -7,7 +7,6 @@ #ifndef INC_PROCESS_SCHEDULER_H #define INC_PROCESS_SCHEDULER_H -#include "NewKit/Defines.h" #include #include #include @@ -155,14 +154,14 @@ namespace Kernel ZKA_COPY_DEFAULT(UserProcess); public: - Char Name[kProcessNameLen] = {"Process (Unnamed)"}; + Char Name[kProcessNameLen] = {"Process (Unnamed, No Subsystem)"}; ProcessSubsystem SubSystem{ProcessSubsystem::kProcessSubsystemInvalid}; User* Owner{nullptr}; HAL::StackFramePtr StackFrame{nullptr}; AffinityKind Affinity{AffinityKind::kStandard}; ProcessStatusKind Status{ProcessStatusKind::kFinished}; UInt8* StackReserve{nullptr}; - UserProcessImage Image; + UserProcessImage Image{}; SizeT StackSize{kSchedMaxStackSz}; IDLLObject* DylibDelegate{nullptr}; SizeT MemoryCursor{0UL}; diff --git a/dev/Kernel/SystemKit/Scheduler.h b/dev/Kernel/SystemKit/Scheduler.h new file mode 100644 index 00000000..5b6c8719 --- /dev/null +++ b/dev/Kernel/SystemKit/Scheduler.h @@ -0,0 +1,20 @@ + +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Corp, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include + +/// @file Scheduler.h +/// @brief This file takes care of creating processes/threads from a subsystem context. + +namespace Kernel +{ + class UserSubsystem; + class UserEnvVar; + class UserEnv; +} // namespace Kernel \ No newline at end of file diff --git a/dev/Kernel/SystemKit/Swap.h b/dev/Kernel/SystemKit/Swap.h new file mode 100644 index 00000000..5d9d1ade --- /dev/null +++ b/dev/Kernel/SystemKit/Swap.h @@ -0,0 +1,18 @@ + +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Corp, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include + +#define kSwapMgrBlockMaxSize mib_cast(16) +#define KSwapMgrBlockMagic "SWEP " + +#define kSwapMgrPageFile "/System/pagefile.sys" + +/// @file Swap.h +/// @brief Virtual memory swap API. diff --git a/dev/Kernel/SystemKit/SwapMgr.h b/dev/Kernel/SystemKit/SwapMgr.h deleted file mode 100644 index 9f1ecc3e..00000000 --- a/dev/Kernel/SystemKit/SwapMgr.h +++ /dev/null @@ -1,18 +0,0 @@ - -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Corp, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include - -#define kSwapMgrBlockMaxSize mib_cast(16) -#define KSwapMgrBlockMagic "SWEP " - -#define kSwapMgrPageFile "/System/pagefile.sys" - -/// @file SwapMgr.h -/// @brief Virtual memory swap manager. diff --git a/dev/Kernel/SystemKit/ThreadMgr.h b/dev/Kernel/SystemKit/ThreadMgr.h deleted file mode 100644 index 27a9707a..00000000 --- a/dev/Kernel/SystemKit/ThreadMgr.h +++ /dev/null @@ -1,20 +0,0 @@ - -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Corp, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include - -/// @file ThreadMgr.h -/// @brief This file takes care of creating processes/threads from a subsystem context. - -namespace Kernel -{ - class UserSubsystem; - class UserEnvVar; - class UserEnv; -} // namespace Kernel \ No newline at end of file diff --git a/dev/Kernel/src/FS/NeFS.cc b/dev/Kernel/src/FS/NeFS.cc index 27a027cd..bf39f7e6 100644 --- a/dev/Kernel/src/FS/NeFS.cc +++ b/dev/Kernel/src/FS/NeFS.cc @@ -720,7 +720,8 @@ bool NeFileSystemParser::WriteCatalog(_Input _Output NFS_CATALOG_STRUCT* catalog /// @param catalog_name the catalog name. /// @return the newly found catalog. _Output NFS_CATALOG_STRUCT* NeFileSystemParser::FindCatalog(_Input const Char* catalog_name, - Lba& out_lba) + Lba& out_lba, + Bool search_hidden) { if (!catalog_name || *catalog_name == 0) @@ -798,7 +799,7 @@ _Output NFS_CATALOG_STRUCT* NeFileSystemParser::FindCatalog(_Input const Char* c } } - kcout << "Fetching catalog...\r"; + kcout << "Finding catalog...\r"; NeFSSearchThroughCatalogList: while (drive.fPacket.fPacketGood) @@ -813,6 +814,14 @@ NeFSSearchThroughCatalogList: if (StringBuilder::Equals(catalog_name, catalog->Name)) { + /// ignore it, it's locked. + if (catalog->Status == kNeFSStatusLocked && + !search_hidden) + { + err_local_get() = kErrorFileLocked; + goto NeFSContinueSearch; + } + /// ignore unallocated catalog, break if (!(catalog->Flags & kNeFSFlagCreated)) { @@ -822,8 +831,8 @@ NeFSSearchThroughCatalogList: NFS_CATALOG_STRUCT* catalogPtr = new NFS_CATALOG_STRUCT(); rt_copy_memory(catalog, catalogPtr, sizeof(NFS_CATALOG_STRUCT)); - kcout << "Found catalog at: " << hex_number(start_catalog_lba) << endl; - kcout << "Found catalog at: " << catalog->Name << endl; + kcout << "Found available catalog at: " << hex_number(start_catalog_lba) << endl; + kcout << "Found available catalog at: " << catalog->Name << endl; out_lba = start_catalog_lba; return catalogPtr; @@ -844,6 +853,8 @@ NeFSSearchThroughCatalogList: goto NeFSSearchThroughCatalogList; } + err_local_get() = kErrorFileNotFound; + out_lba = 0UL; return nullptr; } @@ -854,7 +865,7 @@ NeFSSearchThroughCatalogList: _Output NFS_CATALOG_STRUCT* NeFileSystemParser::GetCatalog(_Input const Char* name) { Lba unused = 0; - return this->FindCatalog(name, unused); + return this->FindCatalog(name, unused, true); } /// @brief Closes a catalog, (frees it). @@ -923,6 +934,8 @@ Boolean NeFileSystemParser::RemoveCatalog(_Input const Char* catalog_name) } delete catalog; + catalog = nullptr; + return false; } diff --git a/dev/Kernel/src/Mgr/SwapMgr.cc b/dev/Kernel/src/Mgr/SwapMgr.cc deleted file mode 100644 index a909846f..00000000 --- a/dev/Kernel/src/Mgr/SwapMgr.cc +++ /dev/null @@ -1,28 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Corp, all rights reserved. - -------------------------------------------- */ - -#include -#include - -namespace Kernel -{ - class SwapMgrDiskMgr; - - class SwapMgrDiskMgr final - { - static BOOL DumpToDisk(const Char* fork_name, const SizeT fork_name_len, VoidPtr data, const SizeT data_len) - { - if (!fork_name || !fork_name_len) - return NO; - - FileStream file(kSwapMgrPageFile, "wb"); - - file.Write(fork_name, data, data_len); - - return YES; - } - } -} // namespace Kernel \ No newline at end of file diff --git a/dev/Kernel/src/System/SwapDiskDelegate.cc b/dev/Kernel/src/System/SwapDiskDelegate.cc new file mode 100644 index 00000000..ffe8812d --- /dev/null +++ b/dev/Kernel/src/System/SwapDiskDelegate.cc @@ -0,0 +1,57 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Corp, all rights reserved. + +------------------------------------------- */ + +#include +#include + +namespace Kernel::Detail +{ + class SwapDiskDelegate; + + class SwapDiskDelegate final + { + public: + explicit SwapDiskDelegate() = default; + ~SwapDiskDelegate() = default; + + ZKA_COPY_DEFAULT(SwapDiskDelegate); + + BOOL Write(const Char* fork_name, const SizeT fork_name_len, VoidPtr data, const SizeT data_len) + { + if (!fork_name || !fork_name_len) + return NO; + + if (data_len > mib_cast(16)) + return NO; + + if (!data) + return NO; + + FileStream file(kSwapMgrPageFile, "wb"); + + if (file.Write(fork_name, data, data_len).Error()) + { + return NO; + } + + return YES; + } + + VoidPtr Read(const Char* fork_name, const SizeT fork_name_len, const SizeT data_len) + { + if (!fork_name || !fork_name_len) + return nullptr; + + if (data_len > mib_cast(16)) + return nullptr; + + FileStream file(kSwapMgrPageFile, "rb"); + + voidPtr blob = file.Read(fork_name, data_len); + return blob; + } + }; +} // namespace Kernel::Detail \ No newline at end of file -- cgit v1.2.3