summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/src
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-06-04 14:34:18 +0200
committerGitHub <noreply@github.com>2025-06-04 14:34:18 +0200
commitb395c279875cdca523279128de42fb0c472ba40e (patch)
treee45134289017787fea8ebed48dd224720b8cc6f9 /dev/kernel/src
parent9828f432fe1ec678a5a78ad841b4f4d6beb3795b (diff)
parentf64668b1cca66565df06f59e1e68381ecbfef217 (diff)
Merge pull request #37 from 0xf00sec/0xf00sec-patch-4
Fix mParser
Diffstat (limited to 'dev/kernel/src')
-rw-r--r--dev/kernel/src/FS/NeFS+FileMgr.cc261
1 files changed, 165 insertions, 96 deletions
diff --git a/dev/kernel/src/FS/NeFS+FileMgr.cc b/dev/kernel/src/FS/NeFS+FileMgr.cc
index 978a43a8..fffb1e80 100644
--- a/dev/kernel/src/FS/NeFS+FileMgr.cc
+++ b/dev/kernel/src/FS/NeFS+FileMgr.cc
@@ -16,78 +16,94 @@
namespace Kernel {
/// @brief C++ constructor
NeFileSystemMgr::NeFileSystemMgr() {
- NeFileSystemParser* mParser = new NeFileSystemParser();
- MUST_PASS(mParser);
+ mParser = new NeFileSystemParser();
+ MUST_PASS(mParser);
- kout << "We are done allocating NeFileSystemParser...\r";
+ kout << "We are done allocating NeFileSystemParser...\n";
}
NeFileSystemMgr::~NeFileSystemMgr() {
- if (mParser) {
- kout << "Destroying NeFileSystemParser...\r";
- mm_delete_class(&mParser);
- }
+ if (mParser) {
+ kout << "Destroying NeFileSystemParser...\n";
+ delete mParser;
+ mParser = nullptr;
+ }
}
/// @brief Removes a node from the filesystem.
/// @param path The filename
/// @return If it was deleted or not.
bool NeFileSystemMgr::Remove(_Input const Char* path) {
- if (path == nullptr || *path == 0) return false;
-
- return mParser->RemoveCatalog(path);
+ if (path == nullptr || *path == 0) {
+ kout << "NeFS: Remove called with null or empty path\n";
+ return false;
+ }
+ return mParser->RemoveCatalog(path);
}
/// @brief Creates a node with the specified.
/// @param path The filename path.
/// @return The Node pointer.
NodePtr NeFileSystemMgr::Create(_Input const Char* path) {
- return rtl_node_cast(mParser->CreateCatalog(path));
+ if (!path || *path == 0) {
+ kout << "NeFS: Create called with null or empty path\n";
+ return nullptr;
+ }
+ return rtl_node_cast(mParser->CreateCatalog(path));
}
-/// @brief Creates a node with is a directory.
+/// @brief Creates a node which is a directory.
/// @param path The filename path.
/// @return The Node pointer.
NodePtr NeFileSystemMgr::CreateDirectory(const Char* path) {
- return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindDir));
+ if (!path || *path == 0) {
+ kout << "NeFS: CreateDirectory called with null or empty path\n";
+ return nullptr;
+ }
+ return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindDir));
}
-/// @brief Creates a node with is a alias.
+/// @brief Creates a node which is an alias.
/// @param path The filename path.
/// @return The Node pointer.
NodePtr NeFileSystemMgr::CreateAlias(const Char* path) {
- return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindAlias));
+ if (!path || *path == 0) {
+ kout << "NeFS: CreateAlias called with null or empty path\n";
+ return nullptr;
+ }
+ return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindAlias));
}
-/// @brief Creates a node with is a page file.
-/// @param path The filename path.
-/// @return The Node pointer.
NodePtr NeFileSystemMgr::CreateSwapFile(const Char* path) {
- return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindPage));
+ if (!path || *path == 0) {
+ kout << "NeFS: CreateSwapFile called with null or empty path\n";
+ return nullptr;
+ }
+ return rtl_node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindPage));
}
/// @brief Gets the root directory.
/// @return
const Char* NeFileSystemHelper::Root() {
- return kNeFSRoot;
+ return kNeFSRoot;
}
/// @brief Gets the up-dir directory.
/// @return
const Char* NeFileSystemHelper::UpDir() {
- return kNeFSUpDir;
+ return kNeFSUpDir;
}
/// @brief Gets the separator character.
/// @return
Char NeFileSystemHelper::Separator() {
- return kNeFSSeparator;
+ return kNeFSSeparator;
}
/// @brief Gets the metafile character.
/// @return
Char NeFileSystemHelper::MetaFile() {
- return kNeFSMetaFilePrefix;
+ return kNeFSMetaFilePrefix;
}
/// @brief Opens a new file.
@@ -95,109 +111,162 @@ Char NeFileSystemHelper::MetaFile() {
/// @param r
/// @return
_Output NodePtr NeFileSystemMgr::Open(_Input const Char* path, _Input const Char* r) {
- if (!path || *path == 0) return nullptr;
-
- if (!r || *r == 0) return nullptr;
-
- auto catalog = mParser->GetCatalog(path);
-
- return rtl_node_cast(catalog);
+ if (!path || *path == 0) {
+ kout << "NeFS: Open called with null or empty path\n";
+ return nullptr;
+ }
+ if (!r || *r == 0) {
+ kout << "NeFS: Open called with null or empty mode string\n";
+ return nullptr;
+ }
+ auto catalog = mParser->GetCatalog(path);
+ if (!catalog) {
+ kout << "NeFS: Open could not find catalog for path\n";
+ return nullptr;
+ }
+ return rtl_node_cast(catalog);
}
-/// @brief Writes to a catalog's fork.
-/// @param node the node ptr.
-/// @param data the data.
-/// @param flags the size.
-/// @return
Void NeFileSystemMgr::Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags,
_Input SizeT size) {
- if (!node) return;
- if (!size) return;
-
- constexpr auto kDataForkName = kNeFSDataFork;
- this->Write(kDataForkName, node, data, flags, size);
+ if (!is_valid_nefs_catalog(node)) {
+ kout << "NeFS: Write called with invalid node pointer\n";
+ return;
+ }
+ if (!data) {
+ kout << "NeFS: Write called with null data pointer\n";
+ return;
+ }
+ if (!size || size > kNeFSForkSize) {
+ kout << "NeFS: Write called with invalid size: " << size << "\n";
+ return;
+ }
+ constexpr auto kDataForkName = kNeFSDataFork;
+ this->Write(kDataForkName, node, data, flags, size);
}
-/// @brief Read from filesystem fork.
-/// @param node the catalog node.
-/// @param flags the flags with it.
-/// @param sz the size to read.
-/// @return
_Output VoidPtr NeFileSystemMgr::Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT size) {
- if (!node) return nullptr;
- if (!size) return nullptr;
-
- constexpr auto kDataForkName = kNeFSDataFork;
- return this->Read(kDataForkName, node, flags, size);
+ if (!is_valid_nefs_catalog(node)) {
+ kout << "NeFS: Read called with invalid node pointer\n";
+ return nullptr;
+ }
+ if (!size || size > kNeFSForkSize) {
+ kout << "NeFS: Read called with invalid size: " << size << "\n";
+ return nullptr;
+ }
+ constexpr auto kDataForkName = kNeFSDataFork;
+ return this->Read(kDataForkName, node, flags, size);
}
Void NeFileSystemMgr::Write(_Input const Char* name, _Input NodePtr node, _Input VoidPtr data,
_Input Int32 flags, _Input SizeT size) {
- if (!size || size > kNeFSForkSize) return;
-
- if (!data) return;
-
- NE_UNUSED(flags);
-
- if ((reinterpret_cast<NEFS_CATALOG_STRUCT*>(node))->Kind == kNeFSCatalogKindFile)
- mParser->WriteCatalog(reinterpret_cast<NEFS_CATALOG_STRUCT*>(node)->Name,
- (flags & kFileFlagRsrc ? true : false), data, size, name);
+ if (!is_valid_nefs_catalog(node)) {
+ kout << "NeFS: Write(fork) called with invalid node pointer\n";
+ return;
+ }
+ if (!name || *name == 0) {
+ kout << "NeFS: Write(fork) called with null or empty fork name\n";
+ return;
+ }
+ if (!data) {
+ kout << "NeFS: Write(fork) called with null data pointer\n";
+ return;
+ }
+ if (!size || size > kNeFSForkSize) {
+ kout << "NeFS: Write(fork) called with invalid size: " << size << "\n";
+ return;
+ }
+ NE_UNUSED(flags);
+ auto cat = reinterpret_cast<NEFS_CATALOG_STRUCT*>(node);
+ if (cat->Kind == kNeFSCatalogKindFile) {
+ mParser->WriteCatalog(cat->Name,
+ (flags & kFileFlagRsrc ? true : false),
+ data,
+ size,
+ name);
+ }
}
_Output VoidPtr NeFileSystemMgr::Read(_Input const Char* name, _Input NodePtr node,
_Input Int32 flags, _Input SizeT sz) {
- if (sz > kNeFSForkSize) return nullptr;
-
- if (!sz) return nullptr;
-
- NE_UNUSED(flags);
-
- if ((reinterpret_cast<NEFS_CATALOG_STRUCT*>(node))->Kind == kNeFSCatalogKindFile)
- return mParser->ReadCatalog(reinterpret_cast<NEFS_CATALOG_STRUCT*>(node),
- (flags & kFileFlagRsrc ? true : false), sz, name);
-
- return nullptr;
+ if (!is_valid_nefs_catalog(node)) {
+ kout << "NeFS: Read(fork) called with invalid node pointer\n";
+ return nullptr;
+ }
+ if (!name || *name == 0) {
+ kout << "NeFS: Read(fork) called with null or empty fork name\n";
+ return nullptr;
+ }
+ if (!sz || sz > kNeFSForkSize) {
+ kout << "NeFS: Read(fork) called with invalid size: " << sz << "\n";
+ return nullptr;
+ }
+ NE_UNUSED(flags);
+ auto cat = reinterpret_cast<NEFS_CATALOG_STRUCT*>(node);
+ if (cat->Kind == kNeFSCatalogKindFile) {
+ return mParser->ReadCatalog(cat,
+ (flags & kFileFlagRsrc ? true : false),
+ sz,
+ name);
+ }
+ return nullptr;
}
-/// @brief Seek from Catalog.
-/// @param node
-/// @param off
-/// @retval true always returns false, this is unimplemented.
-/// @retval false always returns this, it is unimplemented.
-
_Output Bool NeFileSystemMgr::Seek(NodePtr node, SizeT off) {
- if (!node || off == 0) return false;
-
- return mParser->Seek(reinterpret_cast<NEFS_CATALOG_STRUCT*>(node), off);
+ if (!is_valid_nefs_catalog(node)) {
+ kout << "NeFS: Seek called with invalid node pointer\n";
+ return false;
+ }
+ // Allow off == 0
+ return mParser->Seek(reinterpret_cast<NEFS_CATALOG_STRUCT*>(node), off);
}
-/// @brief Tell where the catalog is.
+/// @brief Tell current offset within catalog.
/// @param node
-/// @retval true always returns false, this is unimplemented.
-/// @retval false always returns this, it is unimplemented.
-
+/// @return kFileMgrNPos if invalid, else current offset.
_Output SizeT NeFileSystemMgr::Tell(NodePtr node) {
- if (!node) return kFileMgrNPos;
-
- return mParser->Tell(reinterpret_cast<NEFS_CATALOG_STRUCT*>(node));
+ if (!is_valid_nefs_catalog(node)) {
+ kout << "NeFS: Tell called with invalid node pointer\n";
+ return kFileMgrNPos;
+ }
+ return mParser->Tell(reinterpret_cast<NEFS_CATALOG_STRUCT*>(node));
}
-/// @brief Rewinds the catalog.
+/// @brief Rewinds the catalog
/// @param node
-/// @retval true always returns false, this is unimplemented.
-/// @retval false always returns this, it is unimplemented.
-
+/// @return False if invalid, nah? calls Seek(node, 0).
_Output Bool NeFileSystemMgr::Rewind(NodePtr node) {
- if (!node) return false;
-
- return this->Seek(node, 0);
+ if (!is_valid_nefs_catalog(node)) {
+ kout << "NeFS: Rewind called with invalid node pointer\n";
+ return false;
+ }
+ return this->Seek(node, 0);
}
-/// @brief Returns the filesystem parser.
-/// @return the Filesystem parser class.
_Output NeFileSystemParser* NeFileSystemMgr::GetParser() noexcept {
- return mParser;
+ return mParser;
}
+
+static inline bool is_valid_nefs_catalog(NodePtr node) {
+ if (!node) return false;
+ auto cat = reinterpret_cast<NEFS_CATALOG_STRUCT*>(node);
+ switch (cat->Kind) {
+ case kNeFSCatalogKindFile:
+ case kNeFSCatalogKindDir:
+ case kNeFSCatalogKindAlias:
+ case kNeFSCatalogKindPage:
+ break;
+ default:
+ return false;
+ }
+ bool null_found = false;
+ for (int i = 0; i < kNeFSCatalogNameLen; ++i) {
+ if (cat->Name[i] == 0) { null_found = true; break; }
+ }
+ if (!null_found) return false;
+ return true;
+}
+
} // namespace Kernel
#endif // ifdef __FSKIT_INCLUDES_NEFS__