summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlejandro Sior <aho@sior.be>2022-06-18 14:43:46 +0200
committerAlejandro Sior <aho@sior.be>2022-06-18 14:43:46 +0200
commitfd2f5a94d6ad595e8bed354eca168a82b882e8b4 (patch)
treef30495b2ca343e1baf189e8a9f20f6234848fdea
parent6117a803ad717fd50003c0bd8fbe393db2dcac6b (diff)
gptman: change the way of how args are passed to subcommands
-rw-r--r--convert/convert.ha9
-rw-r--r--create/create.ha30
-rw-r--r--header/header.ha4
-rw-r--r--main.ha9
-rw-r--r--mkbackup/mkbackup.ha4
-rw-r--r--volume/volume.ha26
6 files changed, 68 insertions, 14 deletions
diff --git a/convert/convert.ha b/convert/convert.ha
index 5404bd8..dbf4ef4 100644
--- a/convert/convert.ha
+++ b/convert/convert.ha
@@ -11,10 +11,11 @@ use strings;
use errors;
use rt;
+use volume;
-export fn convert(vol: io::file, args: []str) void = {
+export fn convert(vol: str, args: []str) void = {
if (len(args) < 2) {
- fmt::fatalf("gptman.convert: needs disk");
+ fmt::fatalf("convert: needs disk");
};
let source = os::open(args[1], fs::flags::RDONLY)!;
@@ -22,13 +23,15 @@ export fn convert(vol: io::file, args: []str) void = {
const source = mbr::from(source)!;
+ const vol = volume::openvol(vol);
let vol = gpt::from(vol)!;
defer {
gpt::chksums(vol);
gpt::commit(vol)!;
gpt::finish(vol);
};
-
+
+ rt::memcpy(vol.mbr, source.bootsector, size(mbr::bootsector));
for (let i = 0z; i < 4; i += 1) {
const entry = &source.bootsector.entries[i];
if (entry.lba_begin == 0 && entry.length == 0)
diff --git a/create/create.ha b/create/create.ha
index 2c08eec..b7dc0f7 100644
--- a/create/create.ha
+++ b/create/create.ha
@@ -1,12 +1,38 @@
use fmt;
+use fs;
+use getopt;
use io;
+use os;
+use strconv;
use gpt;
+use volume;
-export fn create(vol: io::file, args: []str) void = {
+export fn create(vol: str, args: []str) void = {
// XXX this is a stub
- const vol = gpt::create(vol, 1000);
+ const cmd = getopt::parse(args,
+ "create a volume",
+ ('f', "file", "specify another partition file to fit"),
+ ('l', "length", "specify another length margin")
+ );
+ defer getopt::finish(&cmd);
+
+ let length = 0z;
+
+ for (let i = 0z; i < len(cmd.opts); i += 1) {
+ const opt = cmd.opts[i];
+ switch (opt.0) {
+ case 'l' =>
+ length += (strconv::stoi(opt.1)! / 512 + 1): size;
+ case 'f' =>
+ const stat = os::stat(opt.1)!;
+ length += stat.sz / 512 + 1;
+ };
+ };
+
+ const vol = volume::mkvol(vol);
+ const vol = gpt::create(vol, length);
defer {
gpt::chksums(vol);
gpt::commit(vol)!;
diff --git a/header/header.ha b/header/header.ha
index 9602071..0e93842 100644
--- a/header/header.ha
+++ b/header/header.ha
@@ -2,8 +2,10 @@ use fmt;
use io;
use gpt;
+use volume;
-export fn header(vol: io::file, args: []str) void = {
+export fn header(vol: str, args: []str) void = {
+ const vol = volume::openvol(vol);
const vol = match (gpt::from(vol)) {
case let g: *gpt::gpt =>
yield g;
diff --git a/main.ha b/main.ha
index 508bd0c..53f5148 100644
--- a/main.ha
+++ b/main.ha
@@ -12,18 +12,13 @@ use create;
use header;
use mkbackup;
+
export fn main() void = {
if (len(os::args) < 3) {
fmt::fatalf("{}: expected disk name", os::args[0]);
};
- const volume = match(os::create(os::args[1], fs::mode::USER_RWX | fs::mode::GROUP_RX | fs::mode::OTHER_RX, fs::flags::RDWR)) {
- case let v: io::file =>
- yield v;
- case =>
- fmt::fatalf("{}: cannot open file {}", os::args[0], os::args[1]);
- };
- defer io::close(volume)!;
+ const volume = os::args[1];
switch (os::args[2]) {
case "convert" => convert::convert(volume, os::args[2..]);
diff --git a/mkbackup/mkbackup.ha b/mkbackup/mkbackup.ha
index 210e00a..55f8464 100644
--- a/mkbackup/mkbackup.ha
+++ b/mkbackup/mkbackup.ha
@@ -1,9 +1,11 @@
use fmt;
use io;
+use volume;
use gpt;
-export fn mkbackup(vol: io::file, args: []str) void = {
+export fn mkbackup(vol: str, args: []str) void = {
+ const vol = volume::openvol(vol);
const vol = match(gpt::from(vol)) {
case let g: *gpt::gpt =>
yield g;
diff --git a/volume/volume.ha b/volume/volume.ha
new file mode 100644
index 0000000..d3bae12
--- /dev/null
+++ b/volume/volume.ha
@@ -0,0 +1,26 @@
+use fmt;
+use fs;
+use io;
+use os;
+
+export fn mkvol(vol: str) io::file = {
+ const vol = match (os::create(vol, fs::mode::USER_RWX | fs::mode::GROUP_RX | fs::mode::OTHER_RX, fs::flags::RDWR)) {
+ case let v: io::file =>
+ yield v;
+ case =>
+ fmt::fatalf("{}: cannot open file {}", os::args[0], os::args[1]);
+ };
+
+ return vol;
+};
+
+export fn openvol(vol: str) io::file = {
+ const vol = match (os::open(vol, fs::flags::RDWR)) {
+ case let v: io::file =>
+ yield v;
+ case =>
+ fmt::fatalf("{}: cannot open file {}", os::args[0], os::args[1]);
+ };
+
+ return vol;
+};