summaryrefslogtreecommitdiff
path: root/sector/sector.ha
diff options
context:
space:
mode:
Diffstat (limited to 'sector/sector.ha')
-rw-r--r--sector/sector.ha48
1 files changed, 48 insertions, 0 deletions
diff --git a/sector/sector.ha b/sector/sector.ha
new file mode 100644
index 0000000..50f417b
--- /dev/null
+++ b/sector/sector.ha
@@ -0,0 +1,48 @@
+use io;
+use types;
+
+use fmt;
+
+export const length: u64 = 512;
+
+export fn lba(no: u64) size = {
+ return no * length;
+};
+
+export type sector = struct {
+ buf: []u8,
+ fd: io::file,
+ lba_begin: u64,
+ lba_end: u64,
+};
+
+export fn map(fd: io::file, lba_begin: u64, lba_end: u64) sector = {
+ let self = sector {
+ buf = alloc([0...], (lba_end - lba_begin + 1)*length),
+ fd = fd,
+ lba_begin = lba_begin,
+ lba_end = lba_end
+ };
+
+ return self;
+};
+
+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)!;
+};
+
+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)!;
+};
+
+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)!;
+};