summaryrefslogtreecommitdiffhomepage
path: root/tooling
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-07-04 15:43:44 +0200
committerAmlal El Mahrouss <amlal@nekernel.org>2025-07-04 15:43:44 +0200
commit32edc6508c91e51316d67b92eb86b4e72d0a41a4 (patch)
treedfdd7eda6c5d88ca9b9b0a4a34beb8d9962409bb /tooling
parent13e1137757626d274e7754d54e7db828da1e7fa8 (diff)
feat: libmkfs: a new library to format filesystem and utilities for
them. Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'tooling')
-rw-r--r--tooling/fsck.hefs.cc30
-rw-r--r--tooling/libmkfs/hefs.h (renamed from tooling/hefs.h)0
-rw-r--r--tooling/libmkfs/mkfs.h72
-rw-r--r--tooling/mkfs.h36
-rw-r--r--tooling/mkfs.hefs.cc60
5 files changed, 111 insertions, 87 deletions
diff --git a/tooling/fsck.hefs.cc b/tooling/fsck.hefs.cc
index 37dfbdd7..ce386152 100644
--- a/tooling/fsck.hefs.cc
+++ b/tooling/fsck.hefs.cc
@@ -4,18 +4,40 @@
------------------------------------------- */
-#include <tooling/hefs.h>
-#include <tooling/mkfs.h>
+#include <tooling/libmkfs/hefs.h>
+#include <tooling/libmkfs/mkfs.h>
#include <cstdlib>
+#include <fstream>
int main(int argc, char** argv) {
if (argc < 2) {
- mkfs::console_out() << "fsck: hefs: usage: fsck.hefs i <input_device>"
+ mkfs::console_out() << "fsck: hefs: usage: fsck.hefs -i=<input_device>"
<< "\n";
return EXIT_FAILURE;
}
- (void) (argv);
+ auto args = mkfs::detail::build_args(argc, argv);
+
+ auto opt_disk = mkfs::get_option<char>(args, "-i");
+
+ if (opt_disk.empty()) {
+ mkfs::console_out() << "fsck: hefs: error: HeFS is empty! Exiting..."
+ << "\n";
+ return EXIT_FAILURE;
+ }
+
+
+ std::ifstream output_device(opt_disk, std::ios::binary);
+
+ if (!output_device.good()) {
+ mkfs::console_out() << "hefs: error: Unable to open output device: " << opt_disk << "\n";
+ return EXIT_FAILURE;
+ }
+
+ mkfs::hefs::BootNode boot_node;
+ std::memset(&boot_node, 0, sizeof(boot_node));
+
+ mkfs::console_out() << "hefs: HeFS partition is is healthy, exiting...\r";
return EXIT_SUCCESS;
} \ No newline at end of file
diff --git a/tooling/hefs.h b/tooling/libmkfs/hefs.h
index d0da516b..d0da516b 100644
--- a/tooling/hefs.h
+++ b/tooling/libmkfs/hefs.h
diff --git a/tooling/libmkfs/mkfs.h b/tooling/libmkfs/mkfs.h
new file mode 100644
index 00000000..d954624c
--- /dev/null
+++ b/tooling/libmkfs/mkfs.h
@@ -0,0 +1,72 @@
+/* -------------------------------------------
+
+ Copyright (C) 2025, Amlal El Mahrouss, all rights reserved.
+
+------------------------------------------- */
+
+#pragma once
+
+#include <tooling/rang.h>
+#include <iostream>
+#include <string>
+
+/// @internal
+namespace mkfs {
+
+namespace detail {
+ /// @internal
+ /// @brief GB‐to‐byte conversion (use multiplication, not XOR).
+ static constexpr size_t gib_cast(uint32_t gb) {
+ return static_cast<size_t>(gb) * 1024ULL * 1024ULL * 1024ULL;
+ }
+
+ static bool parse_decimal(const std::string& opt, unsigned long long& out) {
+ if (opt.empty()) return false;
+ char* endptr = nullptr;
+ unsigned long long val = std::strtoull(opt.c_str(), &endptr, 10);
+ if (endptr == opt.c_str() || *endptr != '\0') return false;
+ out = val;
+ return true;
+ }
+
+ static bool parse_signed(const std::string& opt, long& out, int base = 10) {
+ if (opt.empty()) return false;
+ char* endptr = nullptr;
+ long val = std::strtol(opt.c_str(), &endptr, base);
+ if (endptr == opt.c_str() || *endptr != '\0' || val < 0) return false;
+ out = val;
+ return true;
+ }
+
+ static std::string build_args(int argc, char** argv) {
+ std::string combined;
+ for (int i = 1; i < argc; ++i) {
+ combined += argv[i];
+ combined += ' ';
+ }
+ return combined;
+ }
+} // namespace detail
+
+/// @brief Helper function to get the option value from command line arguments.
+template <typename CharType>
+inline std::basic_string<CharType> get_option(const std::basic_string<CharType>& args,
+ const std::basic_string<CharType>& option) {
+ size_t pos = args.find(option + CharType('='));
+
+ if (pos != std::string::npos) {
+ size_t start = pos + option.length() + 1;
+ size_t end = args.find(' ', start);
+ return args.substr(start, end - start);
+ }
+
+ return std::basic_string<CharType>{};
+}
+
+inline auto console_out() -> std::ostream& {
+ std::ostream& conout = std::cout;
+ conout << rang::fg::red << "mkfs: " << rang::style::reset;
+
+ return conout;
+}
+} // namespace mkfs \ No newline at end of file
diff --git a/tooling/mkfs.h b/tooling/mkfs.h
deleted file mode 100644
index 7180b179..00000000
--- a/tooling/mkfs.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/* -------------------------------------------
-
- Copyright (C) 2025, Amlal El Mahrouss, all rights reserved.
-
-------------------------------------------- */
-
-#pragma once
-
-#include <tooling/rang.h>
-#include <iostream>
-#include <string>
-
-/// @internal
-namespace mkfs {
-/// @brief Helper function to get the option value from command line arguments.
-template <typename CharType>
-inline std::basic_string<CharType> get_option(const std::basic_string<CharType>& args,
- const std::basic_string<CharType>& option) {
- size_t pos = args.find(option + CharType('='));
-
- if (pos != std::string::npos) {
- size_t start = pos + option.length() + 1;
- size_t end = args.find(' ', start);
- return args.substr(start, end - start);
- }
-
- return std::basic_string<CharType>{};
-}
-
-inline auto console_out() -> std::ostream& {
- std::ostream& conout = std::cout;
- conout << rang::fg::red << "mkfs: " << rang::style::reset;
-
- return conout;
-}
-} // namespace mkfs \ No newline at end of file
diff --git a/tooling/mkfs.hefs.cc b/tooling/mkfs.hefs.cc
index 3960fa5e..c1cf9bca 100644
--- a/tooling/mkfs.hefs.cc
+++ b/tooling/mkfs.hefs.cc
@@ -4,54 +4,19 @@
------------------------------------------- */
-#include <tooling/hefs.h>
-#include <tooling/mkfs.h>
+#include <tooling/libmkfs/hefs.h>
+#include <tooling/libmkfs/mkfs.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <limits>
-namespace detail {
-/// @internal
-/// @brief GB‐to‐byte conversion (use multiplication, not XOR).
-static constexpr size_t gib_cast(uint32_t gb) {
- return static_cast<size_t>(gb) * 1024ULL * 1024ULL * 1024ULL;
-}
-} // namespace detail
-
-static size_t kDiskSize = detail::gib_cast(4UL);
+static size_t kDiskSize = mkfs::detail::gib_cast(4UL);
static uint16_t kVersion = kHeFSVersion;
static std::u8string kLabel;
static size_t kSectorSize = 512;
-static bool parse_decimal(const std::string& opt, unsigned long long& out) {
- if (opt.empty()) return false;
- char* endptr = nullptr;
- unsigned long long val = std::strtoull(opt.c_str(), &endptr, 10);
- if (endptr == opt.c_str() || *endptr != '\0') return false;
- out = val;
- return true;
-}
-
-static bool parse_signed(const std::string& opt, long& out, int base = 10) {
- if (opt.empty()) return false;
- char* endptr = nullptr;
- long val = std::strtol(opt.c_str(), &endptr, base);
- if (endptr == opt.c_str() || *endptr != '\0' || val < 0) return false;
- out = val;
- return true;
-}
-
-static std::string build_args(int argc, char** argv) {
- std::string combined;
- for (int i = 1; i < argc; ++i) {
- combined += argv[i];
- combined += ' ';
- }
- return combined;
-}
-
int main(int argc, char** argv) {
if (argc < 2) {
mkfs::console_out()
@@ -62,7 +27,7 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}
- std::string args = build_args(argc, argv);
+ std::string args = mkfs::detail::build_args(argc, argv);
auto output_path = mkfs::get_option<char>(args, "-o");
if (output_path.empty()) {
@@ -72,7 +37,7 @@ int main(int argc, char** argv) {
auto opt_s = mkfs::get_option<char>(args, "-s");
long parsed_s = 0;
- if (!parse_signed(opt_s, parsed_s, 10) || parsed_s == 0) {
+ if (!mkfs::detail::parse_signed(opt_s, parsed_s, 10) || parsed_s == 0) {
mkfs::console_out() << "hefs: error: Invalid sector size \"" << opt_s
<< "\". Must be a positive integer.\n";
return EXIT_FAILURE;
@@ -98,7 +63,7 @@ int main(int argc, char** argv) {
auto opt_S = mkfs::get_option<char>(args, "-S");
unsigned long long gb = 0;
- if (!parse_decimal(opt_S, gb) || gb == 0ULL) {
+ if (!mkfs::detail::parse_decimal(opt_S, gb) || gb == 0ULL) {
mkfs::console_out() << "hefs: error: Invalid disk size \"" << opt_S
<< "\". Must be a positive integer.\n";
return EXIT_FAILURE;
@@ -121,27 +86,27 @@ int main(int argc, char** argv) {
long start_block = 0, end_block = 0;
long start_in = 0, end_in = 0;
- if (!parse_signed(opt_b, start_ind, 16)) {
+ if (!mkfs::detail::parse_signed(opt_b, start_ind, 16)) {
mkfs::console_out() << "hefs: error: Invalid -b <hex> argument.\n";
return EXIT_FAILURE;
}
- if (!parse_signed(opt_e, end_ind, 16) || end_ind <= start_ind) {
+ if (!mkfs::detail::parse_signed(opt_e, end_ind, 16) || end_ind <= start_ind) {
mkfs::console_out() << "hefs: error: Invalid or out-of-range -e <hex> argument.\n";
return EXIT_FAILURE;
}
- if (!parse_signed(opt_bs, start_block, 16)) {
+ if (!mkfs::detail::parse_signed(opt_bs, start_block, 16)) {
mkfs::console_out() << "hefs: error: Invalid -bs <hex> argument.\n";
return EXIT_FAILURE;
}
- if (!parse_signed(opt_be, end_block, 16) || end_block <= start_block) {
+ if (!mkfs::detail::parse_signed(opt_be, end_block, 16) || end_block <= start_block) {
mkfs::console_out() << "hefs: error: Invalid or out-of-range -be <hex> argument.\n";
return EXIT_FAILURE;
}
- if (!parse_signed(opt_is, start_in, 16)) {
+ if (!mkfs::detail::parse_signed(opt_is, start_in, 16)) {
mkfs::console_out() << "hefs: error: Invalid -is <hex> argument.\n";
return EXIT_FAILURE;
}
- if (!parse_signed(opt_ie, end_in, 16) || end_in <= start_in) {
+ if (!mkfs::detail::parse_signed(opt_ie, end_in, 16) || end_in <= start_in) {
mkfs::console_out() << "hefs: error: Invalid or out-of-range -ie <hex> argument.\n";
return EXIT_FAILURE;
}
@@ -153,6 +118,7 @@ int main(int argc, char** argv) {
}
std::ofstream output_device(output_path, std::ios::binary);
+
if (!output_device.good()) {
mkfs::console_out() << "hefs: error: Unable to open output device: " << output_path << "\n";
return EXIT_FAILURE;