Debian Codegraph @

scripts/get_signature

raw
1 #!/usr/bin/env perl
2 # Copyright (c) MediaTek USA Inc., 2022
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or (at
7 # your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, see
16 # <http://www.gnu.org/licenses/>.
17 #
18 #
19 # get_signature
20 #
21 # This is a sample script which uses uses md5sum to compare file versions
22 # If the checksum is not the same - then line numbers, etc. may be different
23 # and some very strange errors may occur.
24 # md5sum is not secure - so could use sha512sum or some other program, if we
25 # really wanted to
26
27 use POSIX qw(strftime);
28 use Getopt::Long;
29 use Cwd qw(abs_path);
30
31 sub usage
32 {
33 print(STDERR "usage: $0 --compare old_version new_version filename OR\n" .
34 " $0 [--allow-missing] filename\n");
35 }
36
37 my $compare;
38 my $allow_missing;
39 my $help;
40 if (!GetOptions("--compare" => \$compare,
41 '--allow-missing' => \$allow_missing,
42 '--help' => \$help) ||
43 $help ||
44 ($compare && scalar(@ARGV) != 3) ||
45 (!$compare && scalar(@ARGV) != 1)
46 ) {
47 usage();
48 exit(defined($help) ? 0 : 1);
49 }
50
51 my $filename = $ARGV[$compare ? 2 : 0];
52
53 if ($compare) {
54 my ($old, $new) = @ARGV;
55 exit($old ne $new); # for the moment, just look for exact match
56 }
57
58 unless (-e $filename) {
59 if ($allow_missing) {
60 print("\n"); # empty string
61 exit 0;
62 }
63 die("Error: $filename does not exist - perhaps you need the '--allow-missing' flag"
64 );
65 }
66 $pathname = abs_path($filename);
67
68 #my $sum = `sha512sum $pathname`;
69 my $sum = `md5sum $pathname`;
70 my $rtn = $?;
71 $sum =~ /^(\S+)/;
72 print($1 . "\n");
73 exit $rtn;
74