arcthis 0.5.1

A command-line tool for reading and managing compressed files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::io::{self, Write};

use globset::{Glob, GlobMatcher};
use serde::Serialize;
use sha2::{Digest, Sha256, Sha512};

use crate::archive::Archive;
use crate::error::{ArcthisError, Result};
use crate::model::{ArchiveEntry, EntryKind};

const BINARY_PROBE_BYTES: usize = 8 * 1024;
const MAX_LINE_BYTES: usize = 1024 * 1024;

#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "mcp", derive(rmcp::schemars::JsonSchema))]
pub struct FindResult {
    pub glob: String,
    pub matched: u64,
    pub entries: Vec<ArchiveEntry>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "mcp", derive(rmcp::schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum HashAlgorithm {
    Sha256,
    Sha512,
}

impl HashAlgorithm {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Sha256 => "sha256",
            Self::Sha512 => "sha512",
        }
    }
}

#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "mcp", derive(rmcp::schemars::JsonSchema))]
pub struct HashResult {
    pub entry: String,
    pub algorithm: HashAlgorithm,
    pub digest: String,
    pub bytes_hashed: u64,
}

#[derive(Debug, Clone)]
pub struct GrepOptions {
    pub glob: Option<String>,
    pub max_entry_size: u64,
    pub max_matches: u64,
    pub scan_binary: bool,
    /// Maximum serialized matching-line bytes retained across all files.
    pub max_result_bytes: u64,
    /// Maximum actual decoded bytes scanned across all files.
    pub max_total_size: u64,
}

impl Default for GrepOptions {
    fn default() -> Self {
        Self {
            glob: None,
            max_entry_size: 16 * 1024 * 1024,
            max_matches: 10_000,
            scan_binary: false,
            max_result_bytes: 16 * 1024 * 1024,
            max_total_size: u64::MAX,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "mcp", derive(rmcp::schemars::JsonSchema))]
pub struct GrepMatch {
    pub path: String,
    pub line_number: u64,
    pub text: String,
    pub line_truncated: bool,
}

#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "mcp", derive(rmcp::schemars::JsonSchema))]
pub struct GrepResult {
    pub pattern: String,
    pub glob: Option<String>,
    pub files_scanned: u64,
    pub binary_files_skipped: u64,
    pub oversized_files_skipped: u64,
    pub bytes_scanned: u64,
    pub matches_truncated: bool,
    pub matches: Vec<GrepMatch>,
}

pub fn find(archive: &Archive, pattern: &str) -> Result<FindResult> {
    let matcher = build_matcher(pattern)?;
    let entries = archive
        .entries()?
        .into_iter()
        .filter(|entry| matcher.is_match(&entry.path))
        .collect::<Vec<_>>();
    Ok(FindResult {
        glob: pattern.to_owned(),
        matched: u64::try_from(entries.len()).unwrap_or(u64::MAX),
        entries,
    })
}

pub fn hash(archive: &Archive, entry: &str, algorithm: HashAlgorithm) -> Result<HashResult> {
    let mut writer = HashWriter::new(algorithm);
    archive.copy_entry_to(entry, &mut writer)?;
    let bytes_hashed = writer.bytes;
    Ok(HashResult {
        entry: entry.to_owned(),
        algorithm,
        digest: writer.finish(),
        bytes_hashed,
    })
}

pub fn grep(archive: &Archive, pattern: &str, options: &GrepOptions) -> Result<GrepResult> {
    let matcher = options.glob.as_deref().map(build_matcher).transpose()?;
    let candidates = archive.entries()?.into_iter().filter(|entry| {
        entry.kind == EntryKind::File
            && matcher
                .as_ref()
                .is_none_or(|matcher| matcher.is_match(&entry.path))
    });
    let mut result = GrepResult {
        pattern: pattern.to_owned(),
        glob: options.glob.clone(),
        files_scanned: 0,
        binary_files_skipped: 0,
        oversized_files_skipped: 0,
        bytes_scanned: 0,
        matches_truncated: false,
        matches: Vec::new(),
    };
    let mut result_bytes = 0_u64;
    for entry in candidates {
        if entry.size > options.max_entry_size {
            result.oversized_files_skipped += 1;
            continue;
        }
        let remaining = options
            .max_matches
            .saturating_sub(u64::try_from(result.matches.len()).unwrap_or(u64::MAX));
        if remaining == 0 {
            result.matches_truncated = true;
            break;
        }
        let mut scanner = GrepWriter::new(
            &entry.path,
            pattern,
            remaining,
            options,
            options.max_result_bytes.saturating_sub(result_bytes),
            options.max_total_size.saturating_sub(result.bytes_scanned),
        );
        if let Err(error) = archive.copy_entry_to(&entry.path, &mut scanner) {
            if scanner.limit_exceeded {
                return Err(ArcthisError::ResourceLimit {
                    message: "grep decoded bytes exceed the per-entry or total scan budget"
                        .to_owned(),
                });
            }
            return Err(error);
        }
        let scan = scanner.finish();
        result_bytes = result_bytes.saturating_add(scan.result_bytes);
        result.files_scanned += 1;
        result.bytes_scanned = result.bytes_scanned.saturating_add(scan.bytes);
        if scan.binary_skipped {
            result.binary_files_skipped += 1;
        } else {
            result.matches.extend(scan.matches);
        }
        if scan.truncated {
            result.matches_truncated = true;
            break;
        }
    }
    Ok(result)
}

fn build_matcher(pattern: &str) -> Result<GlobMatcher> {
    Glob::new(pattern)
        .map(|glob| glob.compile_matcher())
        .map_err(|error| ArcthisError::UnsupportedOperation {
            message: format!("invalid glob `{pattern}`: {error}"),
        })
}

enum HashState {
    Sha256(Sha256),
    Sha512(Sha512),
}

struct HashWriter {
    state: HashState,
    bytes: u64,
}

impl HashWriter {
    fn new(algorithm: HashAlgorithm) -> Self {
        let state = match algorithm {
            HashAlgorithm::Sha256 => HashState::Sha256(Sha256::new()),
            HashAlgorithm::Sha512 => HashState::Sha512(Sha512::new()),
        };
        Self { state, bytes: 0 }
    }

    fn finish(self) -> String {
        match self.state {
            HashState::Sha256(state) => hex(&state.finalize()),
            HashState::Sha512(state) => hex(&state.finalize()),
        }
    }
}

impl Write for HashWriter {
    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
        match &mut self.state {
            HashState::Sha256(state) => state.update(buffer),
            HashState::Sha512(state) => state.update(buffer),
        }
        self.bytes = self.bytes.saturating_add(buffer.len() as u64);
        Ok(buffer.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

struct GrepScan {
    bytes: u64,
    binary_skipped: bool,
    truncated: bool,
    matches: Vec<GrepMatch>,
    result_bytes: u64,
}

#[allow(clippy::struct_excessive_bools)] // Independent streaming scan facts are clearer than an artificial state enum.
struct GrepWriter<'a> {
    path: &'a str,
    pattern: &'a [u8],
    scan_binary: bool,
    binary: bool,
    probe_complete: bool,
    pending: Vec<u8>,
    line: Vec<u8>,
    line_number: u64,
    line_truncated: bool,
    max_matches: u64,
    truncated: bool,
    matches: Vec<GrepMatch>,
    bytes: u64,
    result_bytes: u64,
    max_result_bytes: u64,
    max_scan_bytes: u64,
    limit_exceeded: bool,
}

impl<'a> GrepWriter<'a> {
    fn new(
        path: &'a str,
        pattern: &'a str,
        max_matches: u64,
        options: &GrepOptions,
        max_result_bytes: u64,
        max_total_size: u64,
    ) -> Self {
        Self {
            path,
            pattern: pattern.as_bytes(),
            scan_binary: options.scan_binary,
            binary: false,
            probe_complete: false,
            pending: Vec::new(),
            line: Vec::new(),
            line_number: 1,
            line_truncated: false,
            max_matches,
            truncated: false,
            matches: Vec::new(),
            bytes: 0,
            result_bytes: 0,
            max_result_bytes,
            max_scan_bytes: max_total_size.min(options.max_entry_size),
            limit_exceeded: false,
        }
    }

    fn consume(&mut self, bytes: &[u8]) {
        if self.truncated || self.binary && !self.scan_binary {
            return;
        }
        for byte in bytes {
            if *byte == b'\n' {
                self.finish_line();
                if self.truncated {
                    break;
                }
            } else if self.line.len() < MAX_LINE_BYTES {
                self.line.push(*byte);
            } else {
                self.line_truncated = true;
            }
        }
    }

    fn finish_line(&mut self) {
        if !self.truncated && contains_bytes(&self.line, self.pattern) {
            let matched = GrepMatch {
                path: self.path.to_owned(),
                line_number: self.line_number,
                text: String::from_utf8_lossy(&self.line).into_owned(),
                line_truncated: self.line_truncated,
            };
            // Count escaped UTF-8 JSON and path overhead, not just raw line bytes.
            let remaining = self.max_result_bytes.saturating_sub(self.result_bytes);
            match crate::budget::json_bytes(&matched, remaining.saturating_sub(1)) {
                Ok(size) if size < remaining => {
                    self.result_bytes += size + 1; // Reserve the array comma too.
                    self.matches.push(matched);
                }
                _ => {
                    self.truncated = true;
                }
            }
            if u64::try_from(self.matches.len()).unwrap_or(u64::MAX) >= self.max_matches {
                self.truncated = true;
            }
        }
        self.line.clear();
        self.line_truncated = false;
        self.line_number = self.line_number.saturating_add(1);
    }

    fn finish(mut self) -> GrepScan {
        if !self.probe_complete {
            self.binary = self.pending.contains(&0);
            let pending = std::mem::take(&mut self.pending);
            self.consume(&pending);
        }
        if !self.line.is_empty() && (!self.binary || self.scan_binary) {
            self.finish_line();
        }
        GrepScan {
            bytes: self.bytes,
            binary_skipped: self.binary && !self.scan_binary,
            truncated: self.truncated,
            matches: self.matches,
            result_bytes: self.result_bytes,
        }
    }
}

impl Write for GrepWriter<'_> {
    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
        let bytes = u64::try_from(buffer.len()).unwrap_or(u64::MAX);
        if bytes > self.max_scan_bytes.saturating_sub(self.bytes) {
            self.limit_exceeded = true;
            return Err(io::Error::other("grep scan budget exceeded"));
        }
        self.bytes += bytes;
        if self.probe_complete {
            self.consume(buffer);
            return Ok(buffer.len());
        }
        let probe_bytes = (BINARY_PROBE_BYTES - self.pending.len()).min(buffer.len());
        self.pending.extend_from_slice(&buffer[..probe_bytes]);
        if self.pending.len() >= BINARY_PROBE_BYTES {
            self.binary = self.pending[..BINARY_PROBE_BYTES].contains(&0);
            self.probe_complete = true;
            let pending = std::mem::take(&mut self.pending);
            self.consume(&pending);
            self.consume(&buffer[probe_bytes..]);
        }
        Ok(buffer.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

fn contains_bytes(haystack: &[u8], needle: &[u8]) -> bool {
    needle.is_empty()
        || haystack
            .windows(needle.len())
            .any(|window| window == needle)
}

fn hex(bytes: &[u8]) -> String {
    const DIGITS: &[u8; 16] = b"0123456789abcdef";
    let mut result = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        result.push(char::from(DIGITS[usize::from(byte >> 4)]));
        result.push(char::from(DIGITS[usize::from(byte & 0x0f)]));
    }
    result
}