-
Notifications
You must be signed in to change notification settings - Fork 52
/
pkgdiff.pl
4141 lines (3672 loc) · 111 KB
/
pkgdiff.pl
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
###########################################################################
# PkgDiff - Package Changes Analyzer 1.8
# A tool for visualizing changes in Linux software packages
#
# Copyright (C) 2012-2022 Andrey Ponomarenko's ABI Laboratory
#
# Written by Andrey Ponomarenko
#
# PLATFORMS
# =========
# GNU/Linux, FreeBSD, Mac OS X
#
# PACKAGE FORMATS
# ===============
# RPM, DEB, TAR.GZ, JAR, etc.
#
# REQUIREMENTS
# ============
# Perl 5 (5.8 or newer)
# GNU Diff
# GNU Wdiff
# GNU Awk
# GNU Binutils (readelf)
# Perl-File-LibMagic
# RPM (rpm, rpmbuild, rpm2cpio) for analysis of RPM-packages
# DPKG (dpkg, dpkg-deb) for analysis of DEB-packages
#
# SUGGESTIONS
# ===========
# ABI Compliance Checker (1.99.1 or newer)
# ABI Dumper (0.97 or newer)
#
# 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.
#
# 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 <http://www.gnu.org/licenses/>.
###########################################################################
use strict;
use Getopt::Long;
Getopt::Long::Configure ("posix_default", "no_ignore_case", "permute");
use File::Path qw(mkpath rmtree);
use File::Temp qw(tempdir);
use File::Copy qw(move copy);
use File::Compare;
use Cwd qw(abs_path cwd);
use Config;
use Fcntl;
my $TOOL_VERSION = "1.8";
my $ORIG_DIR = cwd();
# Internal modules
my $MODULES_DIR = getModules();
push(@INC, getDirname($MODULES_DIR));
my $DIFF = $MODULES_DIR."/Internals/Tools/rfcdiff-1.41-CUSTOM.sh";
my $JAVA_DUMP = $MODULES_DIR."/Internals/Tools/java-dump.sh";
my $ACC = "abi-compliance-checker";
my $ACC_VER = "1.99.1";
my $ABI_DUMPER = "abi-dumper";
my $ABI_DUMPER_VER = "0.97";
my ($Help, $ShowVersion, $DumpVersion, $GenerateTemplate, %Descriptor,
$CheckUsage, $PackageManager, $OutputReportPath, $ShowDetails, $Debug,
$SizeLimit, $QuickMode, $DiffWidth, $DiffLines, $Minimal, $NoWdiff,
$IgnoreSpaceChange, $IgnoreAllSpace, $IgnoreBlankLines, $ExtraInfo,
$CustomTmpDir, $HideUnchanged, $TargetName, $TargetTitle, %TargetVersion,
$CompareDirs, $ListAddedRemoved, $SkipSubArchives, $LinksTarget,
$SkipPattern, $AllText, $CheckByteCode, $FullMethodDiffs, $TrackUnchanged);
my $CmdName = getFilename($0);
my %ERROR_CODE = (
# Unchanged verdict
"Unchanged"=>0,
# Changed verdict
"Changed"=>1,
# Undifferentiated error code
"Error"=>2,
# System command is not found
"Not_Found"=>3,
# Cannot access input files
"Access_Error"=>4,
# Cannot find a module
"Module_Error"=>9
);
my $HomePage = "https://lvc.github.io/pkgdiff/";
my $ShortUsage = "Package Changes Analyzer (PkgDiff) $TOOL_VERSION
A tool for visualizing changes in Linux software packages
Copyright (C) 2022 Andrey Ponomarenko's ABI Laboratory
License: GNU GPL
Usage: $CmdName PKG1 PKG2 [options]
Example: $CmdName OLD.rpm NEW.rpm
More info: $CmdName --help\n";
if($#ARGV==-1)
{
printMsg("INFO", $ShortUsage);
exit(0);
}
GetOptions("h|help!" => \$Help,
"v|version!" => \$ShowVersion,
"dumpversion!" => \$DumpVersion,
# arguments
"old=s" => \$Descriptor{1},
"new=s" => \$Descriptor{2},
# options
"check-usage!" => \$CheckUsage,
"pkg-manager=s" => \$PackageManager,
"template!" => \$GenerateTemplate,
"report-path=s" => \$OutputReportPath,
"details!" => \$ShowDetails,
"size-limit=s" => \$SizeLimit,
"width=s" => \$DiffWidth,
"prelines=s" => \$DiffLines,
"ignore-space-change" => \$IgnoreSpaceChange,
"ignore-all-space" => \$IgnoreAllSpace,
"ignore-blank-lines" => \$IgnoreBlankLines,
"quick!" => \$QuickMode,
"minimal!" => \$Minimal,
"no-wdiff!" => \$NoWdiff,
"extra-info=s" => \$ExtraInfo,
"tmp-dir=s" => \$CustomTmpDir,
"hide-unchanged!" => \$HideUnchanged,
"debug!" => \$Debug,
"v1|vnum1=s" => \$TargetVersion{1},
"v2|vnum2=s" => \$TargetVersion{2},
"name=s" => \$TargetName,
"title=s" => \$TargetTitle,
"d|directories!" => \$CompareDirs,
"list-added-removed!" => \$ListAddedRemoved,
"skip-subarchives!" => \$SkipSubArchives,
"skip-pattern=s" => \$SkipPattern,
"all-text!" => \$AllText,
"links-target=s" => \$LinksTarget,
"check-byte-code!" => \$CheckByteCode,
"full-method-diffs!" => \$FullMethodDiffs,
"track-unchanged!" => \$TrackUnchanged
) or errMsg();
my $TMP_DIR = undef;
if($CustomTmpDir)
{
printMsg("INFO", "using custom temp directory: $CustomTmpDir");
$TMP_DIR = abs_path($CustomTmpDir);
mkpath($TMP_DIR);
cleanTmp();
}
else {
$TMP_DIR = tempdir(CLEANUP=>1);
}
sub cleanTmp()
{
foreach my $E ("null", "error",
"unpack", "output", "fmt",
"content1", "content2",
"xcontent1", "xcontent2")
{
if(-f $TMP_DIR."/".$E) {
unlink($TMP_DIR."/".$E);
}
elsif(-d $TMP_DIR."/".$E) {
rmtree($TMP_DIR."/".$E);
}
}
}
if(@ARGV)
{
if($#ARGV==1)
{ # pkgdiff OLD.pkg NEW.pkg
$Descriptor{1} = $ARGV[0];
$Descriptor{2} = $ARGV[1];
}
else {
errMsg();
}
}
sub errMsg()
{
printMsg("INFO", "\n".$ShortUsage);
exit($ERROR_CODE{"Error"});
}
my $HelpMessage="
NAME:
Package Changes Analyzer
A tool for visualizing changes in Linux software packages
DESCRIPTION:
Package Changes Analyzer (PkgDiff) is a tool for visualizing
changes in Linux software packages (RPM, DEB, TAR.GZ, etc).
The tool can compare directories as well (with the help of
the -d option).
The tool is intended for Linux maintainers who are interested
in ensuring compatibility of old and new versions of packages.
This tool is free software: you can redistribute it and/or
modify it under the terms of the GNU GPL.
USAGE:
$CmdName PKG1 PKG2 [options]
$CmdName -d DIR1/ DIR2/ [options]
EXAMPLES:
$CmdName OLD.rpm NEW.rpm
$CmdName OLD.deb NEW.deb
$CmdName OLD.tar.gz NEW.tar.gz
ARGUMENTS:
PKG1
Path to the old version of a package (RPM, DEB, TAR.GZ, etc).
If you need to analyze a group of packages then you can
pass an XML-descriptor of this group (VERSION.xml file):
<version>
/* Group version */
</version>
<group>
/* Group name */
</group>
<packages>
/path1/to/package(s)
/path2/to/package(s)
...
</packages>
PKG2
Path to the new version of a package (RPM, DEB, TAR.GZ, etc).
INFORMATION OPTIONS:
-h|-help
Print this help.
-v|-version
Print version information.
-dumpversion
Print the tool version ($TOOL_VERSION) and don't do anything else.
GENERAL OPTIONS:
-report-path PATH
Path to the report.
Default:
pkgdiff_reports/<pkg>/<v1>_to_<v2>/changes_report.html
-details
Try to create detailed reports.
-size-limit SIZE
Don't analyze files larger than SIZE in kilobytes.
-width WIDTH
Width of the Visual Diff.
Default: 80
-prelines NUM
Size of the context in the Visual Diff.
Default: 10
-ignore-space-change
Ignore changes in the amount of white space.
-ignore-all-space
Ignore all white space.
-ignore-blank-lines
Ignore changes whose lines are all blank.
-quick
Quick mode without creating of Visual Diffs for files.
-minimal
Try to find a smaller set of changes.
-no-wdiff
Do not use GNU Wdiff for analysis of changes.
This may be two times faster, but produces lower
quality reports.
OTHER OPTIONS:
-check-usage
Check if package content is used by other
packages in the repository.
-pkg-manager NAME
Specify package management tool.
Supported:
urpm - Mandriva URPM
-template
Create XML-descriptor template ./VERSION.xml
-extra-info DIR
Dump extra info to DIR.
-tmp-dir DIR
Use custom temp directory.
-hide-unchanged
Don't show unchanged files in the report.
-debug
Show debug info.
-name NAME
Set name of the package to NAME.
-title TITLE
Set name of the package in the title of the report to TITLE.
-vnum1 NUM
Set version number of the old package to NUM.
-vnum2 NUM
Set version number of the new package to NUM.
-links-target TARGET
Set target attribute for links in the report:
_self (default)
_blank
-list-added-removed
Show content of added and removed text files.
-skip-subarchives
Skip checking of archives inside the input packages.
-skip-pattern REGEX
Don't check files matching REGEX.
-d|-directories
Compare directories instead of packages.
-all-text
Treat all files in the archive as text files.
-check-byte-code
When comparing Java classes, also check for byte code changes.
-full-method-diffs
Perform a full diff of method bodies when -check-byte-code is specified.
-track-unchanged
Track unchanged files in extra info.
REPORT:
Report will be generated to:
pkgdiff_reports/<pkg>/<v1>_to_<v2>/changes_report.html
EXIT CODES:
0 - Unchanged. The tool has run without any errors.
non-zero - Changed or the tool has run with errors.
MORE INFORMATION:
".$HomePage."\n";
sub helpMsg() {
printMsg("INFO", $HelpMessage."\n");
}
my $DescriptorTemplate = "
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<descriptor>
/* Primary sections */
<version>
/* Version of a group of packages */
</version>
<group>
/* Name of a group of packages */
</group>
<packages>
/* The list of paths to packages and/or
directories with packages, one per line */
</packages>
<skip_files>
/* The list of files that should
not be analyzed, one per line */
</skip_files>
</descriptor>";
# Settings
my $RENAME_FILE_MATCH = 0.55;
my $RENAME_CONTENT_MATCH = 0.85;
my $MOVE_CONTENT_MATCH = 0.90;
my $MOVE_DEPTH = 4;
my $DEFAULT_WIDTH = 80;
my $DIFF_PRE_LINES = 10;
my $EXACT_DIFF_SIZE = 256*1024;
my $EXACT_DIFF_RATE = 0.1;
my $USE_LIBMAGIC = 0;
my %Group = (
"Count1"=>0,
"Count2"=>0
);
my %FormatInfo = ();
my %FileFormat = ();
my %TermFormat = ();
my %DirFormat = ();
my %BytesFormat = ();
# Cache
my %Cache;
# Modes
my $CheckMode = "Single";
# Packages
my %TargetPackages;
my %PackageFiles;
my %PathName;
my %FileChanges;
my %PackageInfo;
my %InfoChanges;
my %PackageUsage;
my %TotalUsage;
my %RemovePrefix;
# Deps
my %PackageDeps;
my %TotalDeps;
my %DepChanges;
# Files
my %AddedFiles;
my %RemovedFiles;
my %ChangedFiles;
my %UnchangedFiles;
my %StableFiles;
my %RenamedFiles;
my %RenamedFiles_R;
my %MovedFiles;
my %MovedFiles_R;
my %ChangeRate;
my %SkipFiles;
# Symbols
my %AddedSymbols;
my %RemovedSymbols;
# Report
my $REPORT_PATH;
my $REPORT_DIR;
my %RESULT;
my $STAT_LINE;
# ABI
my %ABI_Change;
# Other
my %ArchiveFormats = (
"TAR.GZ" => ["tar.gz", "tgz",
"tar.Z", "taz"],
"TAR.XZ" => ["tar.xz", "txz"],
"TAR.BZ2" => ["tar.bz2", "tbz2",
"tbz", "tb2"],
"TAR.LZMA" => ["tar.lzma",
"tlzma"],
"TAR.LZ" => ["tar.lz", "tlz"],
"ZIP" => ["zip", "zae"],
"TAR" => ["tar"],
"LZMA" => ["lzma"],
"GZ" => ["gz"],
"XZ" => ["xz"],
"JAR" => ["jar", "war",
"ear", "aar"],
"APK" => ["apk"]
);
my $ARCHIVE_EXT = getArchivePattern();
sub getModules()
{
my $TOOL_DIR = getDirname($0);
if(not $TOOL_DIR) {
$TOOL_DIR = ".";
}
my @SEARCH_DIRS = (
# tool's directory
abs_path($TOOL_DIR),
# relative path to modules
abs_path($TOOL_DIR)."/../share/pkgdiff",
# system directory
'MODULES_INSTALL_PATH'
);
foreach my $DIR (@SEARCH_DIRS)
{
if($DIR!~/\A\//)
{ # relative path
$DIR = abs_path($TOOL_DIR)."/".$DIR;
}
if(-d $DIR."/modules") {
return $DIR."/modules";
}
}
exitStatus("Module_Error", "can't find modules");
}
sub readModule($$)
{
my ($Module, $Name) = @_;
my $Path = $MODULES_DIR."/Internals/$Module/".$Name;
if(not -f $Path) {
exitStatus("Module_Error", "can't access \'$Path\'");
}
return readFile($Path);
}
sub readBytes($)
{ # ELF: 7f454c46
sysopen(FILE, $_[0], O_RDONLY);
sysread(FILE, my $Header, 4);
close(FILE);
my @Bytes = map { sprintf('%02x', ord($_)) } split (//, $Header);
return join("", @Bytes);
}
sub readSymbols($)
{
my $Path = $_[0];
my %Symbols = ();
open(LIB, "readelf -WhlSsdA \"$Path\" 2>\"$TMP_DIR/null\" |");
my $symtab = undef; # indicates that we are processing 'symtab' section of 'readelf' output
while(<LIB>)
{
if(defined $symtab)
{ # do nothing with symtab
if(index($_, "'.dynsym'")!=-1)
{ # dynamic table
$symtab = undef;
}
}
elsif(index($_, "'.symtab'")!=-1)
{ # symbol table
$symtab = 1;
}
elsif(my @Info = readline_ELF($_))
{
my ($Bind, $Ndx, $Symbol) = ($Info[3], $Info[5], $Info[6]);
if($Ndx ne "UND"
and $Bind ne "WEAK")
{ # only imported symbols
$Symbols{$Symbol} = 1;
}
}
}
close(LIB);
return %Symbols;
}
my %ELF_BIND = map {$_=>1} (
"WEAK",
"GLOBAL"
);
my %ELF_TYPE = map {$_=>1} (
"FUNC",
"IFUNC",
"OBJECT",
"COMMON"
);
my %ELF_VIS = map {$_=>1} (
"DEFAULT",
"PROTECTED"
);
sub readline_ELF($)
{ # read the line of 'readelf' output corresponding to the symbol
my @Info = split(/\s+/, $_[0]);
# Num: Value Size Type Bind Vis Ndx Name
# 3629: 000b09c0 32 FUNC GLOBAL DEFAULT 13 _ZNSt12__basic_fileIcED1Ev@@GLIBCXX_3.4
shift(@Info); # spaces
shift(@Info); # num
if($#Info!=6)
{ # other lines
return ();
}
return () if(not defined $ELF_TYPE{$Info[2]});
return () if(not defined $ELF_BIND{$Info[3]});
return () if(not defined $ELF_VIS{$Info[4]});
if($Info[5] eq "ABS" and $Info[0]=~/\A0+\Z/)
{ # 1272: 00000000 0 OBJECT GLOBAL DEFAULT ABS CXXABI_1.3
return ();
}
if(index($Info[2], "0x") == 0)
{ # size == 0x3d158
$Info[2] = hex($Info[2]);
}
return @Info;
}
sub compareSymbols($$)
{
my ($P1, $P2) = @_;
my %Symbols1 = readSymbols($P1);
my %Symbols2 = readSymbols($P2);
my $Changed = 0;
foreach my $Symbol (keys(%Symbols1))
{
if(not defined $Symbols2{$Symbol})
{
$Changed = 1;
if(defined $AddedSymbols{$Symbol})
{ # moved
delete($AddedSymbols{$Symbol});
}
else
{ # removed
$RemovedSymbols{$Symbol} = 1;
}
}
}
foreach my $Symbol (keys(%Symbols2))
{
if(not defined $Symbols1{$Symbol})
{
$Changed = 1;
if(defined $RemovedSymbols{$Symbol})
{ # moved
delete($RemovedSymbols{$Symbol})
}
else
{ # added
$AddedSymbols{$Symbol} = 1;
}
}
}
return $Changed;
}
sub compareFiles($$$$)
{
my ($P1, $P2, $N1, $N2) = @_;
if(not -f $P1
or not -f $P2)
{
if(not -l $P1)
{ # broken symlinks
return (0, "", "", 0, {});
}
}
my $Format = getFormat($P1);
if($Format ne getFormat($P2)) {
return (0, "", "", 0, {});
}
if(getSize($P1) == getSize($P2))
{ # equal size
if(compare($P1, $P2)==0)
{ # equal content
return (-1, "", "", 0, {});
}
}
if($QuickMode)
{ # --quick
return (3, "", "", 1, {});
}
if(skipFileCompare($P1, 1))
{ # <skip_files>
return (2, "", "", 1, {});
}
if(defined $SizeLimit)
{
if(getSize($P1) > $SizeLimit*1024
or getSize($P2) > $SizeLimit*1024)
{
return (2, "", "", 1, {});
}
}
my ($Changed, $DLink, $RLink, $Rate, $Adv) = (0, "", "", 0, {});
if(not $ShowDetails)
{
if($Format eq "SHARED_OBJECT"
or $Format eq "KERNEL_MODULE"
or $Format eq "DEBUG_INFO")
{
if(not compareSymbols($P1, $P2))
{ # equal sets of symbols
return (0, "", "", 0, {});
}
}
}
if(defined $FormatInfo{$Format}{"Format"}
and $FormatInfo{$Format}{"Format"} eq "Text") {
($DLink, $Rate) = diffFiles($P1, $P2, getRPath("diffs", $N1));
}
elsif($Format eq "LICENSE"
or $Format eq "CHANGELOG"
or $Format eq "README"
or $Format eq "INFORM")
{
if($P1=~/\.($ARCHIVE_EXT)\Z/i)
{ # changelog.Debian.gz
my $Page1 = showFile($P1, "ARCHIVE", 1);
my $Page2 = showFile($P2, "ARCHIVE", 2);
($DLink, $Rate) = diffFiles($Page1, $Page2, getRPath("diffs", $N1));
# clean space
unlink($Page1);
unlink($Page2);
}
else
{
($DLink, $Rate) = diffFiles($P1, $P2, getRPath("diffs", $N1));
}
}
elsif($Format eq "SHARED_OBJECT"
or $Format eq "KERNEL_MODULE"
or $Format eq "DEBUG_INFO"
or $Format eq "STATIC_LIBRARY"
or $Format eq "COMPILED_OBJECT"
or $Format eq "SHARED_LIBRARY"
or $Format eq "EXE"
or $Format eq "MANPAGE"
or $Format eq "INFODOC"
or $Format eq "SYMLINK"
or $Format eq "JAVA_CLASS"
or $Format eq "JAVASCRIPT"
or $Format eq "GETTEXT_MO")
{
my $Page1 = showFile($P1, $Format, 1);
my $Page2 = showFile($P2, $Format, 2);
if($Format eq "SYMLINK")
{
if(readFile($Page1) eq readFile($Page2))
{
# clean space
unlink($Page1);
unlink($Page2);
return (0, "", "", 0, {});
}
}
($DLink, $Rate) = diffFiles($Page1, $Page2, getRPath("diffs", $N1));
# clean space
unlink($Page1);
unlink($Page2);
}
else
{
$Changed = 1;
$Rate = checkDiff($P1, $P2);
}
if($DLink or $Changed)
{
if($ShowDetails)
{ # --details
if($ACC and $ABI_DUMPER)
{
if($Format eq "SHARED_OBJECT"
or $Format eq "KERNEL_MODULE"
or $Format eq "DEBUG_INFO"
or $Format eq "STATIC_LIBRARY") {
($RLink, $Adv) = compareABIs($P1, $P2, $N1, $N2, getRPath("details", $N1));
}
}
}
$DLink=~s/\A\Q$REPORT_DIR\E\///;
$RLink=~s/\A\Q$REPORT_DIR\E\///;
return (1, $DLink, $RLink, $Rate, $Adv);
}
return (0, "", "", 0, {});
}
sub hexDump($)
{
my $Path = $_[0];
my ($Hex, $Byte) = ();
open(FILE, "<", $Path);
while(my $Size = read(FILE, $Byte, 16*1024))
{
foreach my $Pos (0 .. $Size-1) {
$Hex .= sprintf('%02x', ord(substr($Byte, $Pos, 1)))."\n";
}
}
close(FILE);
return $Hex;
}
sub checkDiff($$)
{
my ($P1, $P2) = @_;
my $Size1 = getSize($P1);
if(not $Size1)
{ # empty
return 1;
}
my $Size2 = getSize($P2);
my $Rate = abs($Size1 - $Size2)/$Size1;
my $AvgSize = ($Size1 + $Size2)/2;
if($AvgSize<$EXACT_DIFF_SIZE
and $Rate<$EXACT_DIFF_RATE)
{
my $TmpFile = $TMP_DIR."/null";
if(-T $P1)
{ # Text
my $TDiff = $TMP_DIR."/txtdiff";
qx/diff -Bw \"$P1\" \"$P2\" >$TDiff 2>$TmpFile/;
$Rate = getRate($P1, $P2, $TDiff);
unlink($TDiff);
}
else
{ # Binary
my $TDiff = $TMP_DIR."/txtdiff";
my $T1 = $TMP_DIR."/tmp1.txt";
my $T2 = $TMP_DIR."/tmp2.txt";
writeFile($T1, hexDump($P1));
writeFile($T2, hexDump($P2));
qx/diff -Bw \"$T1\" \"$T2\" >$TDiff 2>$TmpFile/;
unlink($T1);
unlink($T2);
$Rate = getRate($P1, $P2, $TDiff);
unlink($TDiff);
}
}
if($Rate>1) {
$Rate=1;
}
return $Rate;
}
sub showFile($$$)
{
my ($Path, $Format, $Version) = @_;
my ($Dir, $Name) = sepPath($Path);
my $Cmd = undef;
if($Format eq "MANPAGE")
{
$Name=~s/\.(gz|bz2|xz)\Z//;
$Cmd = "man \"$Path\" 2>&1|col -bfx";
}
elsif($Format eq "INFODOC")
{
$Name=~s/\.(gz|bz2|xz)\Z//;
$Path=~s/\.(gz|bz2|xz)\Z//;
$Cmd = "info \"$Path\"";
}
elsif($Format eq "ARCHIVE")
{
my $Unpack = $TMP_DIR."/unpack/";
rmtree($Unpack);
unpackArchive($Path, $Unpack);
my @Contents = listDir($Unpack);
if($#Contents==0) {
$Cmd = "cat \"$Unpack/".$Contents[0]."\"";
}
else {
return undef;
}
}
elsif($Format eq "SHARED_OBJECT"
or $Format eq "KERNEL_MODULE"
or $Format eq "DEBUG_INFO"
or $Format eq "EXE"
or $Format eq "COMPILED_OBJECT"
or $Format eq "STATIC_LIBRARY")
{
$Cmd = "readelf -Wa \"$Path\"";
}
elsif($Format eq "SHARED_LIBRARY")
{
$Cmd = "otool -TVL \"$Path\"";
}
elsif($Format eq "SYMLINK")
{
$Cmd = "file -b \"$Path\"";
}
elsif($Format eq "JAVA_CLASS")
{
if(not checkCmd("javap")) {
return undef;
}
$Name=~s/\.class\Z//;
$Name=~s/\$/./;
$Path = $Name;
if ($CheckByteCode) {
if ($FullMethodDiffs) {
$Cmd = "$JAVA_DUMP \"$Path\"";
} else {
$Cmd = "$JAVA_DUMP -s \"$Path\"";
}
} else {
$Cmd = "javap \"$Path\""; # -c -private -verbose
}
chdir($Dir);
}
elsif($Format eq "JAVASCRIPT")
{
if(not checkCmd("js-beautify")) {
return undef;
}
$Cmd = "js-beautify \"$Path\"";
}
elsif($Format eq "GETTEXT_MO")
{
if(not checkCmd("msgunfmt")) {
return undef;
}
$Cmd = "msgunfmt \"$Path\"";
}
else
{ # error
return undef;
}
my $SPath = $TMP_DIR."/fmt/".$Format."/".$Version."/".$Name;
mkpath(getDirname($SPath));
my $TmpFile = $TMP_DIR."/null";
qx/$Cmd >"$SPath" 2>$TmpFile/;
if($Format eq "JAVA_CLASS") {
chdir($ORIG_DIR);
}
if($Format eq "SHARED_OBJECT"
or $Format eq "KERNEL_MODULE"
or $Format eq "DEBUG_INFO"
or $Format eq "EXE"
or $Format eq "COMPILED_OBJECT"
or $Format eq "STATIC_LIBRARY")
{
my $Content = readFile($SPath);
# 00bf608c 00000008 R_386_RELATIVE
# 00bf608c 00000008 R_386_NONE
$Content=~s/[0-9a-f]{8}\s+[0-9a-f]{8}\s+R_386_(RELATIVE|NONE)\s*//g;
# 0000000000210028 0000000000000008 R_X86_64_RELATIVE 0000000000210028
$Content=~s/[0-9a-f]{16}\s+[0-9a-f]{16}\s+R_X86_64_RELATIVE\s+[0-9a-f]{16}\s*//g;
# 00be77ec 0001d507 R_386_JUMP_SLOT 00000000 dlclose
# 0000000000210348 0000001800000007 R_X86_64_JUMP_SLOT 0000000000000000 mq_receive + 0
$Content=~s/\n([0-9a-f]{8}|[0-9a-f]{16})\s+([0-9a-f]{8}|[0-9a-f]{16}) /\nXXX YYY /g;
$Content=~s/ [0-9a-f]{16} / ZZZ /g;
# 563: 00000000 0 FUNC GLOBAL DEFAULT UND FT_New_Face
# 17: 0000000000000000 0 FUNC GLOBAL DEFAULT UND sem_trywait@GLIBC_2.2.5 (2)
$Content=~s/\n\s*\d+:(\s+[0-9a-f]{8}|\s+[0-9a-f]{16})\s+\d+\s+/\nN: XXX W /g;
$Content=~s/\Q$Dir\E\///g;
# Build ID: 88a916b3973e1a27b027706385af41c553a94061
$Content=~s/\s+Build ID: \w+\s+//g;
writeFile($SPath, uniqStr($Content));
}
return $SPath;
}
sub uniqStr($)
{
my $Str = $_[0];