commit: 030870764fba625c14ee5203bc72985283f807c0
parent 114a21559c4060c73b3824e9b54a0e545a501c64
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 31 Oct 2024 04:33:44 +0100
Add -c option, returning non-zero when blobs were found
Diffstat:
2 files changed, 32 insertions(+), 4 deletions(-)
diff --git a/deblob.1 b/deblob.1
@@ -1,6 +1,6 @@
.\" SPDX-FileCopyrightText: 2019 deblob Authors <https://hacktivis.me/projects/deblob>
.\" SPDX-License-Identifier: BSD-3-Clause
-.Dd 2024-03-19
+.Dd 2024-10-31
.Dt DEBLOB 1
.Os
.Sh NAME
@@ -8,7 +8,7 @@
.Nd remove binary executables ("blobs") from a directory
.Sh SYNOPSIS
.Nm
-.Op Fl n
+.Op Fl cn
.Op Fl e Ar excluded path ...
.Op Fl d Ar working directory
.Sh DESCRIPTION
@@ -23,6 +23,8 @@ already exists.
.Pp
The options are as follows:
.Bl -tag -width Ds
+.It Fl c
+Return error if any non-excluded blobs were found.
.It Fl n
Scan the files but do not actually delete them.
.It Fl e Ar excluded path
@@ -31,7 +33,23 @@ Pass the option multiple times to do multiple exclusions.
.It Fl d Ar working directory
Directory to be scanned rather than the current working directory on execution.
.El
-.Ss SUPPORTED FORMATS
+.Sh EXIT STATUS
+The
+.Nm
+utility exits with the following statuses:
+.Bl -tag -width _
+.It 0
+No errors happened and if
+.Fl c
+was passed, no matching blobs were found.
+.It 1
+Failed scanning for blobs, for example due to an I/O error.
+.It 2
+Option
+.Fl c
+passed and matching blobs were found.
+.El
+.Sh SUPPORTED FORMATS
Blobs scanned against are the following:
.Bl -dash -compact
.It
diff --git a/main.ha b/main.ha
@@ -14,6 +14,7 @@ use strings;
let excludes: []str = [];
let noop: bool = false;
+let check: bool = false;
const beam: []u8 = ['F', 'O', 'R', '1']; // Erlang BEAM
const magic: [_][]u8 = [
@@ -86,6 +87,8 @@ const zip: []u8 = ['P', 'K', 0x03, 0x04];
const jar: []u8 = [0xFE, 0xCA, 0, 0];
const shebang: []u8 = ['#', '!'];
+let found: bool = false;
+
fn is_blob(filename: str) (bool | fs::error | io::error) = {
static let buffer: [4096]u8 = [0...];
@@ -289,6 +292,8 @@ fn check_dir(dirname: str) (void | errors::invalid | io::error) = {
continue;
};
+ found = true;
+
if (noop) {
fmt::printfln("detected: {}", filename)!;
continue;
@@ -336,6 +341,7 @@ fn check_dir(dirname: str) (void | errors::invalid | io::error) = {
export fn main() void = {
const cmd = getopt::parse(os::args,
"Remove binary executable files",
+ ('c', "Return error if any non-excluded blobs were found"),
('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"),
@@ -346,6 +352,8 @@ export fn main() void = {
for (let i = 0z; i < len(cmd.opts); i += 1) {
const opt = cmd.opts[i];
switch (opt.0) {
+ case 'c' =>
+ check = true;
case 'e' =>
append(excludes, opt.1);
case 'd' =>
@@ -371,11 +379,13 @@ export fn main() void = {
match (ret) {
case void =>
+ if(check && found) os::exit(2);
+
os::exit(0);
case errors::invalid =>
os::exit(1);
case let e: io::error =>
fmt::errorfln("deblob: I/O error while traversing directories: {}", io::strerror(e))!;
- os::exit(2);
+ os::exit(1);
};
};