From d82afec5debe0c12ed9ab82e2db30ae91ddc8dca Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Thu, 28 Jul 2022 16:58:47 +0200 Subject: [PATCH] error out if stdout is a tty --- coverage.txt | 2 ++ mmdebstrap | 9 +++++++-- tests/error-if-stdout-is-tty | 12 ++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/error-if-stdout-is-tty diff --git a/coverage.txt b/coverage.txt index e28d525..ab05c30 100644 --- a/coverage.txt +++ b/coverage.txt @@ -322,3 +322,5 @@ Modes: fakechroot Test: dev-ptmx Modes: root unshare Needs-QEMU: true + +Test: error-if-stdout-is-tty diff --git a/mmdebstrap b/mmdebstrap index 5db65bb..f997ac4 100755 --- a/mmdebstrap +++ b/mmdebstrap @@ -37,7 +37,8 @@ use Cwd qw(abs_path getcwd); require "syscall.ph"; ## no critic (Modules::RequireBarewordIncludes) use Fcntl qw(S_IFCHR S_IFBLK FD_CLOEXEC F_GETFD F_SETFD); use List::Util qw(any none); -use POSIX qw(SIGINT SIGHUP SIGPIPE SIGTERM SIG_BLOCK SIG_UNBLOCK strftime); +use POSIX + qw(SIGINT SIGHUP SIGPIPE SIGTERM SIG_BLOCK SIG_UNBLOCK strftime isatty); use Carp; use Term::ANSIColor; use Socket; @@ -5126,7 +5127,11 @@ sub main() { $options->{sourceslists} = $sourceslists; } - if ($options->{target} ne '-') { + if ($options->{target} eq '-') { + if (POSIX::isatty STDOUT) { + error "stdout is a an interactive tty"; + } + } else { my $abs_path = abs_path($options->{target}); if (!defined $abs_path) { error "unable to get absolute path of target directory" diff --git a/tests/error-if-stdout-is-tty b/tests/error-if-stdout-is-tty new file mode 100644 index 0000000..b4f6923 --- /dev/null +++ b/tests/error-if-stdout-is-tty @@ -0,0 +1,12 @@ +#!/bin/sh + +set -eu + +export LC_ALL=C.UTF-8 + +ret=0 +script -qfec "{{ CMD }} --mode={{ MODE }} --variant=apt {{ DIST }} - {{ MIRROR }}" /dev/null || ret=$? +if [ "$ret" = 0 ]; then + echo expected failure but got exit $ret >&2 + exit 1 +fi