1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/* ========================================
Copyright (C) 2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
======================================== */
#pragma once
#include <tools/rang.h>
#include <iostream>
#include <string>
#define kMkFsSectorSz (512U)
#define kMkFsMaxBadSectors (128U)
/// @internal
namespace mkfs {
namespace detail {
/// @internal
/// @brief GB‐to‐byte conversion (use multiplication, not XOR).
inline constexpr size_t gib_cast(uint32_t gb) {
return static_cast<size_t>(gb) * 1024ULL * 1024ULL * 1024ULL;
}
inline 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;
}
inline bool parse_signed(const std::string& opt, long& out, int base = 10) {
out = 0L;
if (opt.empty()) return true;
char* endptr = nullptr;
long val = std::strtol(opt.c_str(), &endptr, base);
auto err = errno;
if (err == ERANGE || err == EINVAL) return false;
if (endptr == opt.c_str() || *endptr != '\0') return false;
out = val;
return true;
}
inline 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(CharType('-') + option + CharType('='));
if (pos != std::string::npos) {
size_t start = pos + option.length() + 2;
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
|