Skip to content

Commit

Permalink
Create file_filter inside the collector
Browse files Browse the repository at this point in the history
This is only executed in collect_file.
  • Loading branch information
Miauwkeru committed Jul 18, 2023
1 parent 052efe7 commit 8d0498e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion acquire/collector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
from contextlib import contextmanager

import dataclasses
import errno
Expand All @@ -9,7 +10,7 @@
from dataclasses import dataclass
from itertools import groupby
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterable, Optional, Sequence, Type, Union
from typing import TYPE_CHECKING, Any, Iterable, Optional, Sequence, Type, Union, Callable

from dissect.target import Target
from dissect.target.exceptions import (
Expand Down Expand Up @@ -184,6 +185,7 @@ def __init__(self, target: Target, output: Output, base: str = "fs", skip_list:

self.report = CollectionReport()
self.bound_module_name = None
self.filter: Callable[[fsutil.TargetPath], bool] = None

self.output.init(self.target)

Expand All @@ -193,6 +195,14 @@ def __enter__(self) -> Collector:
def __exit__(self, *args, **kwargs) -> None:
self.close()

@contextmanager
def file_filter(self, filter: Callable[[fsutil.TargetPath], bool]) -> Collector:
try:
self.filter = filter
yield self
finally:
self.filter = None

def bind(self, module: Type) -> None:
self.bound_module_name = module.__name__

Expand Down Expand Up @@ -263,6 +273,10 @@ def collect_file(
if not isinstance(path, fsutil.TargetPath):
path = self.target.fs.path(path)

if self.filter and self.filter(path) is False:
log.info("- Collecting file %s: Skipped (filtered out)", path)
return

if self.report.was_path_seen(path):
log.info("- Collecting file %s: Skipped (DEDUP)", path)
return
Expand Down

0 comments on commit 8d0498e

Please sign in to comment.