summaryrefslogtreecommitdiff
path: root/sector/sector.ha
diff options
context:
space:
mode:
Diffstat (limited to 'sector/sector.ha')
-rw-r--r--sector/sector.ha45
1 files changed, 25 insertions, 20 deletions
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)?;
};