From 12577f3445df86023ba0cbe2462d55ea5dcafe20 Mon Sep 17 00:00:00 2001 From: Alejandro Sior Date: Fri, 17 Jun 2022 14:43:27 +0200 Subject: gptman: now can allocate partitions --- sector/sector.ha | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'sector/sector.ha') diff --git a/sector/sector.ha b/sector/sector.ha index 50f417b..ae1133b 100644 --- a/sector/sector.ha +++ b/sector/sector.ha @@ -1,48 +1,53 @@ +use errors; use io; use types; -use fmt; - -export const length: u64 = 512; +export const sector_length: u64 = 512; export fn lba(no: u64) size = { - return no * length; + return no * sector_length; }; +// Type that represents a region of a file mapped to memory +// which can be fetched or committed to. export type sector = struct { buf: []u8, fd: io::file, - lba_begin: u64, - lba_end: u64, + offs: u64, + length: u64, }; -export fn map(fd: io::file, lba_begin: u64, lba_end: u64) sector = { +// Creates a sector based on a file and a region +export fn map(fd: io::file, offs: u64, length: u64) sector = { let self = sector { - buf = alloc([0...], (lba_end - lba_begin + 1)*length), + buf = alloc([0...], length * sector_length), fd = fd, - lba_begin = lba_begin, - lba_end = lba_end + offs = offs, + length = length }; return self; }; +// Finishes a sector and deallocates its related resources export fn finish(self: *sector) void = { free(self.buf); }; -export fn fetch(self: *sector) void = { - // XXX proper error - io::seek(self.fd, (self.lba_begin * length): io::off, io::whence::SET)!; - io::read(self.fd, self.buf)!; +// Fetches the content of a sector into the buffer +export fn fetch(self: *sector) (void | io::error) = { + io::seek(self.fd, (self.offs * sector_length): io::off, io::whence::SET)?; + io::read(self.fd, self.buf)?; }; -export fn commit(self: *sector) void = { - io::seek(self.fd, (self.lba_begin * length): io::off, io::whence::SET)!; - io::write(self.fd, self.buf)!; +// Commits the content of a sector into the file +export fn commit(self: *sector) (void | io::error) = { + io::seek(self.fd, (self.offs * sector_length): io::off, io::whence::SET)?; + io::write(self.fd, self.buf)?; }; -export fn commit_at(self: *sector, fd: io::file, lba: u64) void = { - io::seek(fd, (lba * length): io::off, io::whence::SET)!; - io::write(fd, self.buf)!; +// Commits the contents of a sector to another location in another file +export fn commit_at(self: *sector, fd: io::file, lba: u64) (void | io::error) = { + io::seek(fd, (lba * sector_length): io::off, io::whence::SET)?; + io::write(fd, self.buf)?; }; -- cgit v1.2.3