Index.pm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Index;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "1.00";
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Control;
  22. use Dpkg::Compression::FileHandle;
  23. use base qw(Dpkg::Interface::Storable);
  24. use overload
  25. '@{}' => sub { return $_[0]->{'order'} },
  26. fallback => 1;
  27. =encoding utf8
  28. =head1 NAME
  29. Dpkg::Index - generic index of control information
  30. =head1 DESCRIPTION
  31. This object represent a set of Dpkg::Control objects.
  32. =head1 FUNCTIONS
  33. =over 4
  34. =item my $index = Dpkg::Index->new(%opts)
  35. Creates a new empty index. See set_options() for more details.
  36. =cut
  37. sub new {
  38. my ($this, %opts) = @_;
  39. my $class = ref($this) || $this;
  40. my $self = {
  41. items => {},
  42. order => [],
  43. get_key_func => sub { return $_[0]->{Package} },
  44. type => CTRL_UNKNOWN,
  45. };
  46. bless $self, $class;
  47. $self->set_options(%opts);
  48. if (exists $opts{'load'}) {
  49. $self->load($opts{'load'});
  50. }
  51. return $self;
  52. }
  53. =item $index->set_options(%opts)
  54. The "type" option is checked first to define default values for other
  55. options. Here are the relevant options: "get_key_func" is a function
  56. returning a key for the item passed in parameters. The index can only
  57. contain one item with a given key. The function used depend on the
  58. type: for CTRL_INFO_PKG, CTRL_INDEX_SRC, CTRL_INDEX_PKG and CTRL_PKG_DEB
  59. it's simply the Package field; for CTRL_PKG_SRC and CTRL_INFO_SRC, it's
  60. the Source field; for CTRL_CHANGELOG it's the Source and the Version
  61. fields (concatenated with an intermediary "_"); for CTRL_FILE_CHANGES it's
  62. the Source, Version and Architecture fields (concatenated with "_");
  63. for CTRL_FILE_VENDOR it's the Vendor field; for CTRL_FILE_STATUS it's the
  64. Package and Architecture fields (concatenated with "_"). Otherwise it's
  65. the Package field by default.
  66. =cut
  67. sub set_options {
  68. my ($self, %opts) = @_;
  69. # Default values based on type
  70. if (exists $opts{'type'}) {
  71. my $t = $opts{'type'};
  72. if ($t == CTRL_INFO_PKG or $t == CTRL_INDEX_SRC or
  73. $t == CTRL_INDEX_PKG or $t == CTRL_PKG_DEB) {
  74. $self->{get_key_func} = sub { return $_[0]->{Package}; };
  75. } elsif ($t == CTRL_PKG_SRC or $t == CTRL_INFO_SRC) {
  76. $self->{get_key_func} = sub { return $_[0]->{Source}; };
  77. } elsif ($t == CTRL_CHANGELOG) {
  78. $self->{get_key_func} = sub {
  79. return $_[0]->{Source} . "_" . $_[0]->{Version};
  80. };
  81. } elsif ($t == CTRL_FILE_CHANGES) {
  82. $self->{get_key_func} = sub {
  83. return $_[0]->{Source} . "_" . $_[0]->{Version} . "_" .
  84. $_[0]->{Architecture};
  85. };
  86. } elsif ($t == CTRL_FILE_VENDOR) {
  87. $self->{get_key_func} = sub { return $_[0]->{Vendor}; };
  88. } elsif ($t == CTRL_FILE_STATUS) {
  89. $self->{get_key_func} = sub {
  90. return $_[0]->{Package} . "_" . $_[0]->{Architecture};
  91. };
  92. }
  93. }
  94. # Options set by the user override default values
  95. $self->{$_} = $opts{$_} foreach keys %opts;
  96. }
  97. =item $index->get_type()
  98. Returns the type of control information stored. See the type parameter
  99. set during new().
  100. =cut
  101. sub get_type {
  102. my ($self) = @_;
  103. return $self->{'type'};
  104. }
  105. =item $index->add($item, [$key])
  106. Add a new item in the index. If the $key parameter is omitted, the key
  107. will be generated with the get_key_func function (see set_options() for
  108. details).
  109. =cut
  110. sub add {
  111. my ($self, $item, $key) = @_;
  112. unless (defined $key) {
  113. $key = $self->{'get_key_func'}($item);
  114. }
  115. if (not exists $self->{'items'}{$key}) {
  116. push @{$self->{'order'}}, $key;
  117. }
  118. $self->{'items'}{$key} = $item;
  119. }
  120. =item $index->load($file)
  121. Reads the file and creates all items parsed. Returns the number of items
  122. parsed. Handles compressed files transparently based on their extensions.
  123. =item $index->parse($fh, $desc)
  124. Reads the filehandle and creates all items parsed. Returns the number of
  125. items parsed.
  126. =cut
  127. sub parse {
  128. my ($self, $fh, $desc) = @_;
  129. my $item = $self->new_item();
  130. my $i = 0;
  131. while ($item->parse($fh, $desc)) {
  132. $self->add($item);
  133. $item = $self->new_item();
  134. $i++;
  135. }
  136. return $i;
  137. }
  138. =item $index->save($file)
  139. Writes the content of the index in a file. Auto-compresses files
  140. based on their extensions.
  141. =item my $item = $index->new_item()
  142. Creates a new item. Mainly useful for derived objects that would want
  143. to override this method to return something else than a Dpkg::Control
  144. object.
  145. =cut
  146. sub new_item {
  147. my ($self) = @_;
  148. return Dpkg::Control->new(type => $self->{'type'});
  149. }
  150. =item my $item = $index->get_by_key($key)
  151. Returns the item identified by $key or undef.
  152. =cut
  153. sub get_by_key {
  154. my ($self, $key) = @_;
  155. return $self->{'items'}{$key} if exists $self->{'items'}{$key};
  156. return undef;
  157. }
  158. =item my @keys = $index->get_keys(%criteria)
  159. Returns the keys of items that matches all the criteria. The key of the
  160. %criteria hash is a field name and the value is either a regexp that needs
  161. to match the field value, or a reference to a function that must return
  162. true and that receives the field value as single parameter, or a scalar
  163. that must be equal to the field value.
  164. =cut
  165. sub get_keys {
  166. my ($self, %crit) = @_;
  167. my @selected = @{$self->{order}};
  168. foreach my $s_crit (keys %crit) { # search criteria
  169. if (ref($crit{$s_crit}) eq "Regexp") {
  170. @selected = grep {
  171. $self->{'items'}{$_}{$s_crit} =~ $crit{$s_crit}
  172. } @selected;
  173. } elsif (ref($crit{$s_crit}) eq "CODE") {
  174. @selected = grep {
  175. &{$crit{$s_crit}}($self->{'items'}{$_}{$s_crit});
  176. } @selected;
  177. } else {
  178. @selected = grep {
  179. $self->{'items'}{$_}{$s_crit} eq $crit{$s_crit}
  180. } @selected;
  181. }
  182. }
  183. return @selected;
  184. }
  185. =item my @items = $index->get(%criteria)
  186. Returns all the items that matches all the criteria.
  187. =cut
  188. sub get {
  189. my ($self, %crit) = @_;
  190. return map { $self->{'items'}{$_} } $self->get_keys(%crit);
  191. }
  192. =item $index->remove_by_key($key)
  193. Remove the item identified by the given key.
  194. =cut
  195. sub remove_by_key {
  196. my ($self, $key) = @_;
  197. @{$self->{'order'}} = grep { $_ ne $key } @{$self->{'order'}};
  198. return delete $self->{'items'}{$key};
  199. }
  200. =item my @items = $index->remove(%criteria)
  201. Returns and removes all the items that matches all the criteria.
  202. =cut
  203. sub remove {
  204. my ($self, %crit) = @_;
  205. my @keys = $self->get_keys(%crit);
  206. my (%keys, @ret);
  207. foreach my $key (@keys) {
  208. $keys{$key} = 1;
  209. push @ret, $self->{'items'}{$key} if defined wantarray;
  210. delete $self->{'items'}{$key};
  211. }
  212. @{$self->{'order'}} = grep { not exists $keys{$_} } @{$self->{'order'}};
  213. return @ret;
  214. }
  215. =item $index->merge($other_index, %opts)
  216. Merge the entries of the other index. While merging, the keys of the merged
  217. index are used, they are not re-computed (unless you have set the options
  218. "keep_keys" to "0"). It's your responsibility to ensure that they have been
  219. computed with the same function.
  220. =cut
  221. sub merge {
  222. my ($self, $other, %opts) = @_;
  223. $opts{'keep_keys'} = 1 unless exists $opts{'keep_keys'};
  224. foreach my $key ($other->get_keys()) {
  225. $self->add($other->get_by_key($key), $opts{'keep_keys'} ? $key : undef);
  226. }
  227. }
  228. =item $index->sort(\&sortfunc)
  229. Sort the index with the given sort function. If no function is given, an
  230. alphabetic sort is done based on the keys. The sort function receives the
  231. items themselves as parameters and not the keys.
  232. =cut
  233. sub sort {
  234. my ($self, $func) = @_;
  235. if (defined $func) {
  236. @{$self->{'order'}} = sort {
  237. &$func($self->{'items'}{$a}, $self->{'items'}{$b})
  238. } @{$self->{'order'}};
  239. } else {
  240. @{$self->{'order'}} = sort @{$self->{'order'}};
  241. }
  242. }
  243. =item my $str = $index->output()
  244. =item "$index"
  245. Get a string representation of the index. The Dpkg::Control objects are
  246. output in the order which they have been read or added except if the order
  247. hae been changed with sort().
  248. =item $index->output($fh)
  249. Print the string representation of the index to a filehandle.
  250. =cut
  251. sub output {
  252. my ($self, $fh) = @_;
  253. my $str = "";
  254. foreach my $key ($self->get_keys()) {
  255. if (defined $fh) {
  256. print $fh $self->get_by_key($key) . "\n";
  257. }
  258. if (defined wantarray) {
  259. $str .= $self->get_by_key($key) . "\n";
  260. }
  261. }
  262. return $str;
  263. }
  264. =back
  265. =head1 AUTHOR
  266. Raphaël Hertzog <hertzog@debian.org>.
  267. =cut
  268. 1;