summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlejandro Sior <aho@sior.be>2022-06-21 18:44:33 +0200
committerAlejandro Sior <aho@sior.be>2022-06-21 18:44:33 +0200
commit8fbfda70d7ea101d5a15ab8157910af22cd51d07 (patch)
tree1188b60c6e51f314d90974b81956bf18e2085474
parent68b9c04241ef433f346279fbc153928bcbd360e9 (diff)
gpt.part: change a few stuff and working new
-rw-r--r--cmd/part/part.ha92
-rw-r--r--cmd/util.ha4
-rw-r--r--main.ha2
3 files changed, 56 insertions, 42 deletions
diff --git a/cmd/part/part.ha b/cmd/part/part.ha
index 138c093..44f917d 100644
--- a/cmd/part/part.ha
+++ b/cmd/part/part.ha
@@ -1,4 +1,5 @@
use fmt;
+use getopt;
use io;
use os;
use strconv;
@@ -7,57 +8,78 @@ use cmd;
use gpt;
export fn part(args: []str) void = {
- if (len(args) <= 1) {
- partlist(args[1..]);
+ const fd = cmd::openfile();
+ defer io::close(fd)!;
+
+ const vol = cmd::opengpt(fd);
+ defer gpt::finish(vol);
+
+ if (len(args) == 1) {
+ partlist(vol);
return;
};
-
- match (strconv::stou(args[1])) {
- case let i: uint =>
- const fd = cmd::openfile();
- defer io::close(fd)!;
- const vol = cmd::opengpt(fd);
- defer gpt::finish(vol);
- partno(vol, i, args[2..]);
+ switch (args[1]) {
+ case "new" =>
+ partnew(vol, args[1..]);
return;
- case =>
- yield;
+ case "list" =>
+ partlist(vol);
+ return;
+ case => yield;
};
- switch (args[1]) {
- case "new" => partnew(args[1..]);
- case "list" => partlist(args[1..]);
+ const entid = match (strconv::stou(args[1])) {
+ case let i: uint =>
+ yield i;
+ case => fmt::fatalf("gpt.part: invalid number");
};
-};
-export fn partnew(args: []str) void = {
- const fd = cmd::openfile();
- defer io::close(fd)!;
+ partno(vol, entid, args[2..]);
+};
- const vol = cmd::opengpt(fd);
+export fn partnew(vol: *gpt::gpt, args: []str) void = {
defer {
gpt::chksums(vol);
gpt::commit(vol)!;
- gpt::finish(vol);
};
- const sz = 71z;
+ const c = getopt::parse(args,
+ ('f', "file", "specify a file that the partition must fit"),
+ ('b', "bytes", "specify another length margin (bytes)"),
+ ('s', "sectors", "specify another length margin (sectors, 512 bytes)")
+ );
+ defer getopt::finish(&c);
+
+ let length = 0z;
+
+ for (let i = 0z; i < len(c.opts); i += 1) {
+ const opt = c.opts[i];
+
+ // XXX incorrect calculations and handle incorrect input
+ switch (opt.0) {
+ case 'l' =>
+ length += (strconv::stoi64(opt.1)! / 512 + 1): size;
+ case 's' =>
+ length += strconv::stou64(opt.1)!: size;
+ case 'f' =>
+ const stat = os::stat(opt.1)!;
+ length += stat.sz / 512 + 1;
+ };
+ };
- const entry = match (gpt::allocate(vol, sz)) {
+ const entry = match (gpt::allocate(vol, length)) {
case let e: *gpt::entry =>
yield e;
case =>
- fmt::fatalf("gpt.part: could not allocate entry of size {}", sz);
+ fmt::fatalf("gpt.part: could not allocate entry of size {}", length);
};
-};
-export fn partlist(args: []str) void = {
- const fd = cmd::openfile();
- defer io::close(fd)!;
+ let id = (entry: uintptr - vol.primary.entries: uintptr): size / size(gpt::entry);
+ fmt::printfln("{}", id)!;
+};
- const vol = cmd::opengpt(fd);
- defer gpt::finish(vol);
+export fn partlist(vol: *gpt::gpt) void = {
const header = vol.primary.header;
const entries = vol.primary.entries;
@@ -103,10 +125,7 @@ fn partno(vol: *gpt::gpt, i: size, args: []str) void = {
};
fn partfill(vol: *gpt::gpt, i: size) void = {
- const entry = &vol.primary.entries[i];
-
- if (entry.lba_begin == 0 && entry.lba_end == 0)
- fmt::fatalf("gpt.part: partition {} is not allocated", i);
+ const entry = cmd::getgptentry(vol, i);
const writer = match (gpt::partstream_writer(vol, entry)) {
case let l: io::limitstream =>
@@ -119,10 +138,7 @@ fn partfill(vol: *gpt::gpt, i: size) void = {
};
fn partdump(vol: *gpt::gpt, i: size) void = {
- const entry = &vol.primary.entries[i];
-
- if (entry.lba_begin == 0 && entry.lba_end == 0)
- fmt::fatalf("gpt.part: partition {} is not allocated", i);
+ const entry = cmd::getgptentry(vol, i);
const reader = match (gpt::partstream_reader(vol, entry)) {
case let l: io::limitstream =>
diff --git a/cmd/util.ha b/cmd/util.ha
index 7bbeca8..5b41830 100644
--- a/cmd/util.ha
+++ b/cmd/util.ha
@@ -42,12 +42,12 @@ export fn getgptentry(vol: *gpt::gpt, i: size) *gpt::entry = {
const header = vol.primary.header;
if (i >= header.entries_len)
- fmt::fatalf("gpt: invalid entry index");
+ fmt::fatalf("gpt.part: invalid entry index");
const entry = &vol.primary.entries[i];
if (entry.lba_begin == 0 && entry.lba_end == 0)
- fmt::fatalf("gpt: entry {} is not allocated", i);
+ fmt::fatalf("gpt.part: entry {} is not allocated", i);
return entry;
};
diff --git a/main.ha b/main.ha
index 09c5bf7..731b9c4 100644
--- a/main.ha
+++ b/main.ha
@@ -19,8 +19,6 @@ export fn main() void = {
fmt::fatalf("gpt: expected disk name", os::args[0]);
};
- const volume = os::args[1];
-
switch (os::args[2]) {
case "convert" => convert::convert(os::args[2..]);
case "create" => create::create(os::args[2..]);