diff options
| author | Alejandro Sior <aho@sior.be> | 2022-06-17 14:43:27 +0200 |
|---|---|---|
| committer | Alejandro Sior <aho@sior.be> | 2022-06-17 14:43:27 +0200 |
| commit | 12577f3445df86023ba0cbe2462d55ea5dcafe20 (patch) | |
| tree | e72d03b6339084760d4c781cbbb5e7e4dd71385d /sector | |
| parent | 3c8602ee9f04991e1d60b8d6504e12296ca671d9 (diff) | |
gptman: now can allocate partitions
Diffstat (limited to 'sector')
| -rw-r--r-- | sector/README | 7 | ||||
| -rw-r--r-- | sector/sector.ha | 45 |
2 files changed, 32 insertions, 20 deletions
diff --git a/sector/README b/sector/README new file mode 100644 index 0000000..c47a6f1 --- /dev/null +++ b/sector/README @@ -0,0 +1,7 @@ +The sector module provides a facility to read regions of +sectors from a file and readily be able to keep them in sync +with their original location. + +Normally, one would use mmap for this, however for ease of +porting to other projects and due to the mmap offset alignment +limitation, we proceed like this for now.
\ No newline at end of file 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)?; }; |
