Index.pm 9.2 KB

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