Skip to content

Commit b18d270

Browse files
committed
Make collection of non-existent files more clear
(DIS-962)
1 parent 0f8c210 commit b18d270

File tree

2 files changed

+111
-14
lines changed

2 files changed

+111
-14
lines changed

acquire/collector.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ def collect_path(
341341
path = self.target.fs.path(path)
342342

343343
try:
344+
# If a path does not exist, is_dir(), is_file() and is_symlink() will return False (and not raise an
345+
# exception), so we need to explicitly check for this.
346+
exists = self.target.fs.lexists(str(path))
344347
is_dir = path.is_dir()
345348
is_file = path.is_file()
346349
is_symlink = path.is_symlink()
@@ -360,19 +363,23 @@ def collect_path(
360363
self.report.add_path_failed(module_name, path)
361364
return
362365

363-
if is_dir:
364-
self.collect_dir(path, seen_paths=seen_paths, module_name=module_name)
365-
elif is_file:
366-
self.collect_file(path, module_name=module_name)
367-
elif is_symlink:
368-
log.error(
369-
"- Can't collect %s (symlink to %s) in module %s",
370-
path,
371-
path.get().readlink(),
372-
module_name,
373-
)
366+
if exists:
367+
if is_dir:
368+
self.collect_dir(path, seen_paths=seen_paths, module_name=module_name)
369+
elif is_file:
370+
self.collect_file(path, module_name=module_name)
371+
elif is_symlink:
372+
log.error(
373+
"- Can't collect %s (symlink to %s) in module %s",
374+
path,
375+
path.get().readlink(),
376+
module_name,
377+
)
378+
else:
379+
log.error("- Don't know how to collect %s in module %s", path, module_name)
374380
else:
375-
log.error("- Don't know how to collect %s in module %s", path, module_name)
381+
self.report.add_path_missing(module_name, path)
382+
log.error("- Path %s is not found", path)
376383

377384
def collect_command_output(
378385
self,

tests/test_collector.py

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from unittest.mock import Mock
1+
from unittest.mock import Mock, patch
22

3+
import pytest
34
from dissect.target import Target
4-
from dissect.target.filesystem import VirtualFilesystem
5+
from dissect.target.filesystem import VirtualFile, VirtualFilesystem, VirtualSymlink
56

67
from acquire.collector import Collector
78

@@ -23,3 +24,92 @@ def test_collector():
2324
collector.collect_file("$MFT", module_name="test")
2425

2526
assert not collector.report.was_path_seen(fs_2.get("$MFT"))
27+
28+
29+
@pytest.fixture
30+
def mock_file():
31+
return Mock()
32+
33+
34+
@pytest.fixture
35+
def mock_fs(mock_file):
36+
fs = VirtualFilesystem(case_sensitive=False)
37+
fs.makedirs("/foo/bar")
38+
fs.map_file_entry("/foo/bar/some-file", VirtualFile(fs, "some-file", mock_file))
39+
fs.map_file_entry("/foo/bar/some-symlink", VirtualSymlink(fs, "some-symlink", "/foo/bar/some_file"))
40+
return fs
41+
42+
43+
@pytest.fixture
44+
def mock_target(mock_fs):
45+
target = Target()
46+
target.fs.mount("/", mock_fs)
47+
target.filesystems.add(mock_fs)
48+
return target
49+
50+
51+
@pytest.fixture
52+
def mock_collector(mock_target):
53+
collector = Collector(mock_target, Mock())
54+
return collector
55+
56+
57+
def test_collector_collect_path_no_module_name(mock_collector):
58+
with pytest.raises(ValueError):
59+
mock_collector.collect_path("/some/path")
60+
61+
62+
def test_collector_collect_path(mock_fs, mock_target, mock_collector):
63+
mock_seen_paths = set()
64+
mock_module_name = "DUMMY"
65+
66+
with patch("acquire.collector.log") as mock_log:
67+
with patch.object(mock_collector, "report"):
68+
with patch.object(mock_collector, "collect_dir"):
69+
with patch.object(mock_collector, "collect_file"):
70+
path = mock_target.fs.path("/foo/bar")
71+
mock_collector.collect_path(
72+
path,
73+
seen_paths=mock_seen_paths,
74+
module_name=mock_module_name,
75+
)
76+
mock_collector.collect_dir.assert_called()
77+
78+
mock_collector.collect_path(
79+
"/foo/bar",
80+
seen_paths=mock_seen_paths,
81+
module_name=mock_module_name,
82+
)
83+
mock_collector.collect_dir.assert_called()
84+
85+
mock_collector.collect_path(
86+
"/foo/bar/some-file",
87+
seen_paths=mock_seen_paths,
88+
module_name=mock_module_name,
89+
)
90+
mock_collector.collect_file.assert_called()
91+
92+
mock_collector.collect_path(
93+
"/foo/bar/some-symlink",
94+
seen_paths=mock_seen_paths,
95+
module_name=mock_module_name,
96+
)
97+
mock_log.error.assert_called()
98+
assert mock_log.error.call_args.args[0] == "- Can't collect %s (symlink to %s) in module %s"
99+
100+
mock_collector.collect_path(
101+
"/foo/bar/non-existing-file",
102+
seen_paths=mock_seen_paths,
103+
module_name=mock_module_name,
104+
)
105+
mock_log.error.assert_called()
106+
assert mock_log.error.call_args.args[0] == "- Path %s is not found"
107+
108+
with patch.object(mock_target.fs, "lexists", return_value=True):
109+
mock_collector.collect_path(
110+
"/foo/bar/non-existing-file",
111+
seen_paths=mock_seen_paths,
112+
module_name=mock_module_name,
113+
)
114+
mock_log.error.assert_called()
115+
assert mock_log.error.call_args.args[0] == "- Don't know how to collect %s in module %s"

0 commit comments

Comments
 (0)