diff options
| -rw-r--r-- | convert/convert.ha | 9 | ||||
| -rw-r--r-- | create/create.ha | 30 | ||||
| -rw-r--r-- | header/header.ha | 4 | ||||
| -rw-r--r-- | main.ha | 9 | ||||
| -rw-r--r-- | mkbackup/mkbackup.ha | 4 | ||||
| -rw-r--r-- | volume/volume.ha | 26 |
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; @@ -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; +}; |
