This file is indexed.

/usr/share/perl5/Dpkg/ErrorHandling.pm is in libdpkg-perl 1.18.25.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

package Dpkg::ErrorHandling;

use strict;
use warnings;

our $VERSION = '0.02';
our @EXPORT_OK = qw(
    REPORT_PROGNAME
    REPORT_COMMAND
    REPORT_STATUS
    REPORT_DEBUG
    REPORT_INFO
    REPORT_NOTICE
    REPORT_WARN
    REPORT_ERROR
    report_pretty
    report_color
    report
);
our @EXPORT = qw(
    report_options
    debug
    info
    notice
    warning
    error
    errormsg
    syserr
    printcmd
    subprocerr
    usageerr
);

use Exporter qw(import);
use Term::ANSIColor;

use Dpkg ();
use Dpkg::Gettext;

my $quiet_warnings = 0;
my $debug_level = 0;
my $info_fh = \*STDOUT;
my $use_color = 0;

sub setup_color
{
    my $mode = $ENV{'DPKG_COLORS'} // 'auto';

    if ($mode eq 'auto') {
        ## no critic (InputOutput::ProhibitInteractiveTest)
        $use_color = 1 if -t *STDOUT or -t *STDERR;
    } elsif ($mode eq 'always') {
        $use_color = 1;
    } else {
        $use_color = 0;
    }
}

setup_color();

use constant {
    REPORT_PROGNAME => 1,
    REPORT_COMMAND => 2,
    REPORT_STATUS => 3,
    REPORT_INFO => 4,
    REPORT_NOTICE => 5,
    REPORT_WARN => 6,
    REPORT_ERROR => 7,
    REPORT_DEBUG => 8,
};

my %report_mode = (
    REPORT_PROGNAME() => {
        color => 'bold',
    },
    REPORT_COMMAND() => {
        color => 'bold magenta',
    },
    REPORT_STATUS() => {
        color => 'clear',
        # We do not translate this name because the untranslated output is
        # part of the interface.
        name => 'status',
    },
    REPORT_DEBUG() => {
        color => 'clear',
        # We do not translate this name because it is a developer interface
        # and all debug messages are untranslated anyway.
        name => 'debug',
    },
    REPORT_INFO() => {
        color => 'green',
        name => g_('info'),
    },
    REPORT_NOTICE() => {
        color => 'yellow',
        name => g_('notice'),
    },
    REPORT_WARN() => {
        color => 'bold yellow',
        name => g_('warning'),
    },
    REPORT_ERROR() => {
        color => 'bold red',
        name => g_('error'),
    },
);

sub report_options
{
    my (%options) = @_;

    if (exists $options{quiet_warnings}) {
        $quiet_warnings = $options{quiet_warnings};
    }
    if (exists $options{debug_level}) {
        $debug_level = $options{debug_level};
    }
    if (exists $options{info_fh}) {
        $info_fh = $options{info_fh};
    }
}

sub report_name
{
    my $type = shift;

    return $report_mode{$type}{name} // '';
}

sub report_color
{
    my $type = shift;

    return $report_mode{$type}{color} // 'clear';
}

sub report_pretty
{
    my ($msg, $color) = @_;

    if ($use_color) {
        return colored($msg, $color);
    } else {
        return $msg;
    }
}

sub _progname_prefix
{
    return report_pretty("$Dpkg::PROGNAME: ", report_color(REPORT_PROGNAME));
}

sub _typename_prefix
{
    my $type = shift;

    return report_pretty(report_name($type), report_color($type));
}

sub report(@)
{
    my ($type, $msg) = (shift, shift);

    $msg = sprintf($msg, @_) if (@_);

    my $progname = _progname_prefix();
    my $typename = _typename_prefix($type);

    return "$progname$typename: $msg\n";
}

sub debug
{
    my $level = shift;
    print report(REPORT_DEBUG, @_) if $level <= $debug_level;
}

sub info($;@)
{
    print { $info_fh } report(REPORT_INFO, @_) if not $quiet_warnings;
}

sub notice
{
    warn report(REPORT_NOTICE, @_) if not $quiet_warnings;
}

sub warning($;@)
{
    warn report(REPORT_WARN, @_) if not $quiet_warnings;
}

sub syserr($;@)
{
    my $msg = shift;
    die report(REPORT_ERROR, "$msg: $!", @_);
}

sub error($;@)
{
    die report(REPORT_ERROR, @_);
}

sub errormsg($;@)
{
    print { *STDERR } report(REPORT_ERROR, @_);
}

sub printcmd
{
    my (@cmd) = @_;

    print { *STDERR } report_pretty(" @cmd\n", report_color(REPORT_COMMAND));
}

sub subprocerr(@)
{
    my ($p) = (shift);

    $p = sprintf($p, @_) if (@_);

    require POSIX;

    if (POSIX::WIFEXITED($?)) {
	error(g_('%s gave error exit status %s'), $p, POSIX::WEXITSTATUS($?));
    } elsif (POSIX::WIFSIGNALED($?)) {
	error(g_('%s died from signal %s'), $p, POSIX::WTERMSIG($?));
    } else {
	error(g_('%s failed with unknown exit code %d'), $p, $?);
    }
}

my $printforhelp = g_('Use --help for program usage information.');

sub usageerr(@)
{
    my ($msg) = (shift);

    $msg = sprintf($msg, @_) if (@_);
    warn report(REPORT_ERROR, $msg);
    warn "\n$printforhelp\n";
    exit(2);
}

1;