logo

deblob

remove binary executables from a directory git clone https://hacktivis.me/git/deblob.git
commit: 89ef0d14a8d3784e3a1027fbd215f75bf4abadb5
parent 2d9e78f676d24490740605cb65184f8ecf5d5f28
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 18 Feb 2022 10:34:21 +0100

is_blob: improve error handling

Note that this updates the functionality to return "not a blob" instead
of an error for empty files (which I think is more correct).

Diffstat:

Mmain.ha43++++++++++++++++---------------------------
1 file changed, 16 insertions(+), 27 deletions(-)

diff --git a/main.ha b/main.ha @@ -20,28 +20,18 @@ const magic: [_]str = [ "\x55\xAA", ]; -fn is_blob(filename: str) (bool | errors::invalid) = { +fn is_blob(filename: str) (bool | fs::error | io::error) = { static let buffer: [512]u8 = [0...]; - const file = match (os::open(filename)) { - case let f: io::file => - yield f; - case let e: fs::error => - fmt::errorf("os::open({}): {}\n", filename, fs::strerror(e))!; - return errors::invalid; - }; + const file = os::open(filename)?; defer io::close(file); - const n = match (io::read(file, buffer)) { + const n = match (io::read(file, buffer)?) { case let s: size => yield s; case io::EOF => // empty file - //fmt::errorf("EOF reading: {}\n", filename)!; - return errors::invalid; - case let e: io::error => - fmt::errorf("Error reading `{}`: {}\n", filename, io::strerror(e))!; - return errors::invalid; + return false; }; for (let i = 0z; i < len(magic); i += 1) { @@ -65,17 +55,14 @@ fn is_blob(filename: str) (bool | errors::invalid) = { ]; for (let i = 0z; i < len(tests); i += 1) { - match (is_blob(tests[i].0)) { - case let got: bool => - if (got != tests[i].1) { - fmt::fatal("is_blob({}) was incorrect, got {}, expected {}", tests[i].0, got, tests[i].1); - }; - case errors::invalid => - fmt::fatal("is_blob({}) was incorrect, got invalid, expected {}", tests[i].0, tests[i].1); + const result = is_blob(tests[i].0)!; + if (result != tests[i].1) { + fmt::fatal("is_blob({}) was incorrect, got {}, expected {}", + tests[i].0, result, tests[i].1); }; }; - assert(is_blob("test/fixtures/empty") is errors::invalid); + assert(is_blob("test/fixtures/empty")! == false); }; fn is_excluded(filename: str) bool = { @@ -115,16 +102,18 @@ fn check_dir(dirname: str, ignoring: bool) (void | errors::invalid) = { }; if (fs::isdir(ent.ftype)) { - check_dir(filename, ignoring): void; + check_dir(filename, ignoring)?; } else if(fs::isfile(ent.ftype)) { const is_blob = match (is_blob(filename)) { case let b: bool => yield b; - case errors::invalid => + case let err: fs::error => + fmt::errorln("Error opening {}: {}", + filename, fs::strerror(err))!; continue; - }; - - if(!is_blob) { + case let err: io::error => + fmt::errorln("Error reading {}: {}", + filename, io::strerror(err))!; continue; };