summaryrefslogtreecommitdiff
path: root/cmd/part/part.ha
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/part/part.ha')
-rw-r--r--cmd/part/part.ha92
1 files changed, 54 insertions, 38 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 =>