Files.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright © 2014 Guillem Jover <guillem@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 <https://www.gnu.org/licenses/>.
  15. package Dpkg::Dist::Files;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use parent qw(Dpkg::Interface::Storable);
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. sub new {
  23. my ($this, %opts) = @_;
  24. my $class = ref($this) || $this;
  25. my $self = {
  26. options => [],
  27. files => {},
  28. order => [],
  29. };
  30. foreach my $opt (keys %opts) {
  31. $self->{$opt} = $opts{$opt};
  32. }
  33. bless $self, $class;
  34. return $self;
  35. }
  36. sub parse {
  37. my ($self, $fh, $desc) = @_;
  38. my $count = 0;
  39. local $_;
  40. binmode $fh;
  41. while (<$fh>) {
  42. chomp;
  43. my %file;
  44. if (m/^(([-+.0-9a-z]+)_([^_]+)_([-\w]+)\.([a-z0-9.]+)) (\S+) (\S+)$/) {
  45. $file{filename} = $1;
  46. $file{package} = $2;
  47. $file{version} = $3;
  48. $file{arch} = $4;
  49. $file{package_type} = $5;
  50. $file{section} = $6;
  51. $file{priority} = $7;
  52. } elsif (m/^([-+.,_0-9a-zA-Z]+) (\S+) (\S+)$/) {
  53. $file{filename} = $1;
  54. $file{section} = $2;
  55. $file{priority} = $3;
  56. } else {
  57. error(_g('badly formed line in files list file, line %d'), $.);
  58. }
  59. if (defined $self->{files}->{$file{filename}}) {
  60. warning(_g('duplicate files list entry for file %s (line %d)'),
  61. $file{filename}, $.);
  62. } else {
  63. $count++;
  64. $self->{files}->{$file{filename}} = \%file;
  65. push @{$self->{order}}, $file{filename};
  66. }
  67. }
  68. return $count;
  69. }
  70. sub get_files {
  71. my ($self) = @_;
  72. return map { $self->{files}->{$_} } @{$self->{order}};
  73. }
  74. sub get_file {
  75. my ($self, $filename) = @_;
  76. return $self->{files}->{$filename};
  77. }
  78. sub add_file {
  79. my ($self, $filename, $section, $priority) = @_;
  80. # XXX: Ideally we'd need to parse the filename, to match the behaviour
  81. # on parse(), and initialize the other attributes, although no code is
  82. # in need of this for now, at least in dpkg-dev.
  83. if (not defined $self->{files}->{$filename}) {
  84. push @{$self->{order}}, $filename;
  85. }
  86. $self->{files}->{$filename} = {
  87. filename => $filename,
  88. section => $section,
  89. priority => $priority,
  90. };
  91. }
  92. sub del_file {
  93. my ($self, $filename) = @_;
  94. delete $self->{files}->{$filename};
  95. @{$self->{order}} = grep { $_ ne $filename } @{$self->{order}};
  96. }
  97. sub output {
  98. my ($self, $fh) = @_;
  99. my $str = '';
  100. binmode $fh if defined $fh;
  101. foreach my $filename (@{$self->{order}}) {
  102. my $file = $self->{files}->{$filename};
  103. my $entry = "$filename $file->{section} $file->{priority}\n";
  104. print { $fh } $entry if defined $fh;
  105. $str .= $entry;
  106. }
  107. return $str;
  108. }
  109. 1;