commit 9d4cd6b0cdcf898445dd52b250b7cda2dd8f0167
parent 4397f94bbe1dc6b5d6377f9a9ddc5b0d5b76ed71
Author: illiliti <illiliti@dimension.sh>
Date: Mon, 13 Feb 2023 15:38:32 +0300
hare/module: use buffered io
Fix trillion single-byte read/write calls + fix numerous fd leaks
Signed-off-by: illiliti <illiliti@dimension.sh>
Diffstat:
2 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/hare/module/manifest.ha b/hare/module/manifest.ha
@@ -59,7 +59,7 @@ export fn manifest_load(ctx: *context, ident: ast::ident) (manifest | error) = {
let mpath = path::join(cachedir, "manifest");
defer free(mpath);
- let file = match (fs::open(ctx.fs, mpath, fs::flags::RDONLY)) {
+ let truefile = match (fs::open(ctx.fs, mpath, fs::flags::RDONLY)) {
case errors::noentry =>
return manifest;
case let err: fs::error =>
@@ -67,17 +67,17 @@ export fn manifest_load(ctx: *context, ident: ast::ident) (manifest | error) = {
case let file: io::handle =>
yield file;
};
+ defer io::close(truefile)!;
let inputs: []input = [], versions: []version = [];
let buf: [4096]u8 = [0...];
- let file = bufio::buffered(file, buf, []);
+ let file = bufio::buffered(truefile, buf, []);
for (true) {
let line = match (bufio::scanline(&file)) {
case io::EOF =>
break;
case let err: io::error =>
- io::close(&file): void;
return err;
case let line: []u8 =>
yield line;
@@ -269,7 +269,6 @@ export fn manifest_load(ctx: *context, ident: ast::ident) (manifest | error) = {
};
};
- io::close(&file)?;
manifest.inputs = inputs;
manifest.versions = versions;
return manifest;
@@ -323,10 +322,13 @@ export fn manifest_write(ctx: *context, man: *manifest) (void | error) = {
let mpath = path::join(cachedir, "manifest");
defer free(mpath);
- let (file, name) = temp::named(ctx.fs, cachedir, io::mode::WRITE, 0o644)?;
+ let (truefile, name) = temp::named(ctx.fs, cachedir, io::mode::WRITE, 0o644)?;
+ let wbuf: [os::BUFSIZ]u8 = [0...];
+ let file = &bufio::buffered(truefile, [], wbuf);
defer {
+ bufio::flush(file)!;
fs::remove(ctx.fs, name): void;
- io::close(file)!;
+ io::close(truefile)!;
};
let ident = unparse::identstr(man.ident);
diff --git a/hare/module/scan.ha b/hare/module/scan.ha
@@ -17,6 +17,8 @@ use path;
use sort;
use strings;
use strio;
+use bufio;
+use os;
def ABI_VERSION: u8 = 5;
@@ -343,7 +345,10 @@ fn scan_file(
ftype: filetype,
deps: *[]ast::ident,
) ([]u8 | error) = {
- let f = fs::open(ctx.fs, path)?;
+ let truef = fs::open(ctx.fs, path)?;
+ defer io::close(truef)!;
+ let rbuf: [os::BUFSIZ]u8 = [0...];
+ let f = &bufio::buffered(truef, rbuf, []);
let sha = sha256::sha256();
hash::write(&sha, strings::toutf8(path));
hash::write(&sha, [ABI_VERSION]);
@@ -355,7 +360,6 @@ fn scan_file(
case let im: []ast::import =>
yield im;
case let err: parse::error =>
- io::close(f): void;
return err;
};
for (let i = 0z; i < len(imports); i += 1) {
@@ -367,18 +371,15 @@ fn scan_file(
match (io::copy(io::empty, &tee)) {
case size => void;
case let err: io::error =>
- io::close(f): void;
return err;
};
} else {
match (io::copy(&sha, f)) {
case size => void;
case let err: io::error =>
- io::close(f): void;
return err;
};
};
- io::close(f)?;
let tmp: [sha256::SIZE]u8 = [0...];
hash::sum(&sha, tmp);