Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions dissect/target/tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,17 +799,18 @@ def cmd_ls(self, args: argparse.Namespace, stdout: TextIO) -> bool:
print("can't specify -c and -u at the same time")
return False

if not path or not self.check_dir(path):
if not path:
return False

if path.is_file():
print(args.path) # mimic ls behaviour
return False
if not path.exists():
print(f"ls: cannot access {path}: No such file or directory")

# Disable color if output is redirected to a file
use_color = False
if hasattr(stdout, "isatty") and stdout.isatty():
use_color = True
if os.getenv("NO_COLOR") in ["1", "True", "true"]:
use_color = False

print_ls(
path,
Expand Down
13 changes: 13 additions & 0 deletions tests/tools/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,19 @@ def test_redirect_simple_ls(tmp_path: Path, target_win: Target, monkeypatch: pyt
assert "sysvol" in content


def test_target_cli_ls_file(capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
# disable colorful output in `target-shell`
monkeypatch.setattr(fs, "LS_COLORS", {})

out, _ = run_target_shell(
monkeypatch,
capsys,
str(absolute_path("_data/filesystems/squashfs/gzip.sqfs")),
"ls -l small-file",
)
assert "1000 1000 9 2022-12-05T18:53:05.000000+00:00" in out


@pytest.mark.skipif(platform.system() == "Windows", reason="Unix-specific test")
def test_redirect_pipe(tmp_path: Path, target_win: Target, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(fs, "LS_COLORS", {"di": "\033[34m", "fi": "\033[0m"})
Expand Down
Loading