hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 680ad341ca06ba932ac1442eab68004c196e28fc
parent e9e1dfedf92145aab0ed87d97cb1669cebc348e7
Author: Drew DeVault <sir@cmpwn.com>
Date:   Wed,  5 Oct 2022 10:44:46 +0200

format::tar: implement seek within entries

Diffstat:
Mformat/tar/reader.ha | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/format/tar/reader.ha b/format/tar/reader.ha @@ -113,6 +113,7 @@ export fn skip(ent: *entry) (void | io::error) = { const file_vtable: io::vtable = io::vtable { reader = &file_read, + seeker = &file_seek, ... }; @@ -154,6 +155,38 @@ fn file_read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = { return z; }; +fn file_seek( + s: *io::stream, + off: io::off, + w: io::whence, +) (io::off | io::error) = { + let ent = s: *ent_reader; + assert(ent.vtable == &file_vtable); + + const orig = ent.orig: io::off; + const cur = (ent.orig - ent.remain): io::off; + let new = switch (w) { + case io::whence::SET => + yield off; + case io::whence::CUR => + yield cur + off; + case io::whence::END => + yield orig + off; + }; + + if (new < 0) { + new = 0; + } else if (new > orig) { + new = orig; + }; + + const rel = new - cur; + io::seek(ent.src, rel, io::whence::CUR)?; + + ent.remain = (orig - new): size; + return new; +}; + fn readstr(rd: *bufio::memstream, ln: size) str = { const buf = match (bufio::borrowedread(rd, ln)) { case let buf: []u8 =>