logo

deblob

remove binary executables from a directory git clone https://hacktivis.me/git/deblob.git
commit: af06251e4d87bec3ce78ea1178ee71f0c87cc857
parent 73ef3d3bfc1d8ef309008781f0d06194f63afdeb
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 18 Feb 2022 10:17:56 +0100

Style fixes

- Space between if/match/for and the (
- "case" uses same alignment as match/switch
- Each case in match/switch gets a new block
- 80 column soft limit
- Use const where possible
- Sorted imports
- Space between | in tagged union types

Diffstat:

Mmain.ha131+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 74 insertions(+), 57 deletions(-)

diff --git a/main.ha b/main.ha @@ -1,14 +1,13 @@ // Copyright © 2019-2022 Haelwenn (lanodan) Monnier <contact+deblob-notice@hacktivis.me> // SPDX-License-Identifier: BSD-3-Clause - -use getopt; +use bytes; use fmt; -use os; +use fnmatch; use fs; +use getopt; use io; +use os; use path; -use bytes; -use fnmatch; type invalid = !void; type ok = !void; @@ -16,22 +15,24 @@ type ok = !void; let excludes: []str = []; let noop: bool = false; -fn is_blob(filename: str) (bool|invalid) = { +fn is_blob(filename: str) (bool | invalid) = { static let buffer: [512]u8 = [0...]; static const elf: []u8 = [0x7F, 'E', 'L', 'F']; static const ar: []u8 = ['!', '<', 'a', 'r', 'c', 'h', '>', '\n']; // <arch>! static const bios: []u8 = [0x55, 0xAA]; - let file = match(os::open(filename)) { - case let f: io::file => yield f; + 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 invalid; }; defer io::close(file); - const n = match(io::read(file, buffer)) { - case let s: size => yield s; + const n = match (io::read(file, buffer)) { + case let s: size => + yield s; case io::EOF => // empty file //fmt::errorf("EOF reading: {}\n", filename)!; @@ -41,7 +42,9 @@ fn is_blob(filename: str) (bool|invalid) = { return invalid; }; - return bytes::hasprefix(buffer, elf) || bytes::hasprefix(buffer, ar) || bytes::hasprefix(buffer, bios); + return bytes::hasprefix(buffer, elf) + || bytes::hasprefix(buffer, ar) + || bytes::hasprefix(buffer, bios); }; @test fn is_blob() void = { @@ -54,10 +57,10 @@ fn is_blob(filename: str) (bool|invalid) = { //("test/fixtures/option.rom", true), ]; - for(let i = 0z; i < len(tests); i += 1) { - match(is_blob(tests[i].0)) { + for (let i = 0z; i < len(tests); i += 1) { + match (is_blob(tests[i].0)) { case let got: bool => - if(got != tests[i].1) { + if (got != tests[i].1) { fmt::fatal("is_blob({}) was incorrect, got {}, expected {}", tests[i].0, got, tests[i].1); }; case invalid => @@ -74,60 +77,66 @@ fn is_excluded(filename: str) bool = { return true; }; }; - return false; }; -fn check_dir(dirname: str, ignoring: bool) (ok|invalid) = { - let dir_ls = match(os::readdir(dirname)) { - case let d: []fs::dirent => yield d; - case let e: fs::error => - fmt::errorf("os::readdir({}): {}\n", dirname, fs::strerror(e))!; - return invalid; +fn check_dir(dirname: str, ignoring: bool) (ok | invalid) = { + const dir_ls = match (os::readdir(dirname)) { + case let d: []fs::dirent => + yield d; + case let e: fs::error => + fmt::errorf("os::readdir({}): {}\n", dirname, fs::strerror(e))!; + return invalid; }; for (let i = 0z; i < len(dir_ls); i += 1) { - let file = dir_ls[i]; - let filename = path::join(dirname, file.name); + const file = dir_ls[i]; + const filename = path::join(dirname, file.name); - if(file.name == "." || file.name == "..") { + if (file.name == "." || file.name == "..") { continue; }; - if(!ignoring) { + if (!ignoring) { ignoring = is_excluded(filename); }; if(fs::isdir(file.ftype)) { - match(check_dir(filename, ignoring)) { - case => continue; - }; + check_dir(filename, ignoring): void; } else if(fs::isfile(file.ftype)) { - let is_blob = match(is_blob(filename)) { - case let b: bool => yield b; - case invalid => continue; + const is_blob = match (is_blob(filename)) { + case let b: bool => + yield b; + case invalid => + continue; }; if(!is_blob) { continue; }; - if(ignoring) { + if (!is_blob) { + continue; + }; + + if (ignoring) { fmt::printf("ignoring: {}\n", filename)!; continue; }; - if(noop) { + if (noop) { fmt::printf("detected: {}\n", filename)!; continue; }; fmt::printf("removing: {}\n", filename)!; - match(os::remove(filename)) { - case void => continue; + match (os::remove(filename)) { + case void => + continue; case let e: fs::error => - fmt::errorf("os::remove({}): {}\n", filename, fs::strerror(e))!; + fmt::errorf("os::remove({}): {}\n", + filename, fs::strerror(e))!; }; } else { // ignore non-(dir/regular-file) like symlinks, blocks, fifo, … @@ -141,19 +150,21 @@ fn check_dir(dirname: str, ignoring: bool) (ok|invalid) = { @test fn check_dir() void = { const dirname = "test/check_dir-fixtures"; - let files_before = match(os::readdir(dirname)) { - case let d: []fs::dirent => yield d; - case let e: fs::error => - fmt::fatal("os::readdir({}): {}", dirname, fs::strerror(e)); + const files_before = match (os::readdir(dirname)) { + case let d: []fs::dirent => + yield d; + case let e: fs::error => + fmt::fatal("os::readdir({}): {}", dirname, fs::strerror(e)); }; - let ret = check_dir(dirname, false); + const ret = check_dir(dirname, false); assert(ret is ok); - let files_after = match(os::readdir(dirname)) { - case let d: []fs::dirent => yield d; - case let e: fs::error => - fmt::fatal("os::readdir({}): {}", dirname, fs::strerror(e)); + const files_after = match (os::readdir(dirname)) { + case let d: []fs::dirent => + yield d; + case let e: fs::error => + fmt::fatal("os::readdir({}): {}", dirname, fs::strerror(e)); }; assert(len(files_after) == 5); @@ -171,33 +182,39 @@ export fn main() void = { "Remove binary executable files", ('e', "NAME", "Exclude filename from removal (defaults to none)"), ('d', "PATH", "Set working directory (default to current dir)"), - ('n', "No actual removal, only scan and log") + ('n', "No actual removal, only scan and log"), ); defer getopt::finish(&cmd); for (let i = 0z; i < len(cmd.opts); i += 1) { - let opt = cmd.opts[i]; - switch(opt.0) { - case 'e' => append(excludes, opt.1); - case 'd' => workdir = opt.1; - case 'n' => noop = true; + const opt = cmd.opts[i]; + switch (opt.0) { + case 'e' => + append(excludes, opt.1); + case 'd' => + workdir = opt.1; + case 'n' => + noop = true; }; }; - match(os::chdir(workdir)) { - case void => fn_noop; + match (os::chdir(workdir)) { + case void => + fn_noop; // TODO case let e: fs::error => fmt::fatal("os::chdir({}): {}", workdir, fs::strerror(e)); }; fmt::println(":: Checking for blobs")!; - let ret = check_dir(".", false); + const ret = check_dir(".", false); fmt::println(":: Done checking for blobs")!; - match(ret) { - case ok => os::exit(0); - case invalid => os::exit(1); + match (ret) { + case ok => + os::exit(0); + case invalid => + os::exit(1); }; };