Signature.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Signature.pm,v 1.9 2015/05/10 08:14:14 espie Exp $
  3. #
  4. # Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. use strict;
  18. use warnings;
  19. use OpenBSD::LibSpec;
  20. use OpenBSD::LibSpec::Build;
  21. package DPB::Signature::Dir;
  22. sub best
  23. {
  24. my ($h, $lib) = @_;
  25. if ($lib->is_static) {
  26. $h->{a}->{$lib->stem} = 1;
  27. return;
  28. }
  29. my $old = $h->{$lib->stem} //= $lib;
  30. return if $old eq $lib;
  31. return if $old->major > $lib->major;
  32. return if $old->major == $lib->major && $old->minor > $lib->minor;
  33. $h->{$lib->stem} = $lib;
  34. }
  35. sub is_empty
  36. {
  37. my $h = shift;
  38. return keys %$h == 0;
  39. }
  40. sub new
  41. {
  42. my $class = shift;
  43. bless {}, $class;
  44. }
  45. sub compare1
  46. {
  47. my ($s1, $s2, $h1, $h2) = @_;
  48. my $r = '';
  49. while (my ($stem, $lib) = each %$s1) {
  50. next if $stem eq 'la';
  51. next if $stem eq 'a';
  52. if (!defined $s2->{$stem}) {
  53. $r .= "Can't find ".$lib->to_string." on $h2\n";
  54. } elsif ($s2->{$stem}->to_string ne $lib->to_string) {
  55. $r .= "versions don't match: ".
  56. $s2->{$stem}->to_string." on $h2 vs ".
  57. $lib->to_string. " on $h1\n";
  58. }
  59. }
  60. for my $k (keys %{$s1->{la}}) {
  61. if (!defined $s2->{la}{$k}) {
  62. $r .= "$h2 does not have $k.la (from $h1)\n";
  63. }
  64. }
  65. for my $k (keys %{$s1->{a}}) {
  66. if (!defined $s2->{a}{$k}) {
  67. $r .= "$h2 does not have lib$k.a (from $h1)\n";
  68. }
  69. }
  70. return $r;
  71. }
  72. sub print_out
  73. {
  74. my ($self, $dir, $fh) = @_;
  75. for my $k (sort keys %$self) {
  76. next if $k eq 'la';
  77. next if $k eq 'a';
  78. next if !defined $self->{$k};
  79. print $fh "\t", $self->{$k}->to_string, "\n";
  80. }
  81. if (defined $self->{la}) {
  82. for my $v (sort keys %{$self->{la}}) {
  83. print $fh "\t$dir/$v.la\n";
  84. }
  85. }
  86. if (defined $self->{a}) {
  87. for my $v (sort keys %{$self->{a}}) {
  88. print $fh "\t$dir/lib$v.a\n";
  89. }
  90. }
  91. }
  92. sub compare
  93. {
  94. my ($s1, $s2, $h1, $h2) = @_;
  95. return compare1($s1, $s2, $h1, $h2) . compare1($s2, $s1, $h2, $h1);
  96. }
  97. package DPB::Signature::Task;
  98. our @ISA = qw(DPB::Task::Pipe);
  99. sub new
  100. {
  101. my ($class, $o, $base) = @_;
  102. my $repo = $o->{$base} = DPB::Signature::Dir->new;
  103. bless {repo => $repo, dir => "$base/lib"}, $class;
  104. }
  105. sub run
  106. {
  107. my ($self, $core) = @_;
  108. $core->shell->exec("/bin/ls", $self->{dir});
  109. }
  110. sub process
  111. {
  112. my ($self, $core) = @_;
  113. my $fh = $core->fh;
  114. my $repo = $self->{repo};
  115. while (<$fh>) {
  116. if ($_ =~ m/(.*).la/) {
  117. $repo->{la}->{$1} = 1;
  118. } else {
  119. my $lib = OpenBSD::Library::Build->from_string("$self->{dir}/$_");
  120. next unless $lib->is_valid;
  121. $repo->best($lib);
  122. }
  123. }
  124. }
  125. package DPB::Signature;
  126. sub new
  127. {
  128. my ($class, $xenocara) = @_;
  129. bless {xenocara => $xenocara}, $class;
  130. }
  131. sub library_dirs
  132. {
  133. my $self = shift;
  134. if ($self->{xenocara}) {
  135. return ("/usr");
  136. } else {
  137. return OpenBSD::Paths->library_dirs;
  138. }
  139. }
  140. sub add_tasks
  141. {
  142. my ($class, $xenocara, $job) = @_;
  143. $job->{signature} = $class->new($xenocara);
  144. for my $base ($job->{signature}->library_dirs) {
  145. $job->add_tasks(
  146. DPB::Signature::Task->new($job->{signature}, $base));
  147. }
  148. }
  149. sub compare
  150. {
  151. my ($s1, $s2) = @_;
  152. my $r = '';
  153. for my $dir ($s1->library_dirs) {
  154. $r .= $s1->{$dir}->compare($s2->{$dir},
  155. $s1->{host}, $s2->{host});
  156. }
  157. if ($r) {
  158. DPB::Reporter->myprint("Error between $s1->{host} and $s2->{host}: $r");
  159. }
  160. return $r;
  161. }
  162. my $ref;
  163. sub matches
  164. {
  165. my ($self, $core, $logger) = @_;
  166. $self->{host} = $core->hostname;
  167. if (!defined $ref) {
  168. # couldn't read system dir, can't possibly be okay.
  169. if ($self->{'/usr'}->is_empty) {
  170. return 0;
  171. }
  172. $ref = $self;
  173. return 1;
  174. } else {
  175. my $r = $self->compare($ref);
  176. if ($r ne '') {
  177. my $log = $logger->append('signature');
  178. print $log "$r\n";
  179. return 0;
  180. close $log;
  181. } else {
  182. return 1;
  183. }
  184. }
  185. }
  186. sub print_out
  187. {
  188. my ($self, $core, $logger) = @_;
  189. my $log = $logger->create($core->hostname.".sig");
  190. for my $dir ($self->library_dirs) {
  191. print $log "$dir: \n";
  192. $self->{$dir}->print_out("$dir/lib", $log);
  193. }
  194. }
  195. 1;