Files.pm 3.4 KB

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