Files.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 IO::Dir;
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. use parent qw(Dpkg::Interface::Storable);
  23. sub new {
  24. my ($this, %opts) = @_;
  25. my $class = ref($this) || $this;
  26. my $self = {
  27. options => [],
  28. files => {},
  29. };
  30. foreach my $opt (keys %opts) {
  31. $self->{$opt} = $opts{$opt};
  32. }
  33. bless $self, $class;
  34. return $self;
  35. }
  36. sub reset {
  37. my $self = shift;
  38. $self->{files} = {};
  39. }
  40. sub parse_filename {
  41. my ($self, $fn) = @_;
  42. my $file;
  43. if ($fn =~ m/^(([-+:.0-9a-z]+)_([^_]+)_([-\w]+)\.([a-z0-9.]+))$/) {
  44. $file->{filename} = $1;
  45. $file->{package} = $2;
  46. $file->{version} = $3;
  47. $file->{arch} = $4;
  48. $file->{package_type} = $5;
  49. } elsif ($fn =~ m/^([-+:.,_0-9a-zA-Z~]+)$/) {
  50. $file->{filename} = $1;
  51. } else {
  52. $file = undef;
  53. }
  54. return $file;
  55. }
  56. sub parse {
  57. my ($self, $fh, $desc) = @_;
  58. my $count = 0;
  59. local $_;
  60. binmode $fh;
  61. while (<$fh>) {
  62. chomp;
  63. my $file;
  64. if (m/^(\S+) (\S+) (\S+)$/) {
  65. $file = $self->parse_filename($1);
  66. error(g_('badly formed package name in files list file, line %d'), $.)
  67. unless defined $file;
  68. $file->{section} = $2;
  69. $file->{priority} = $3;
  70. } else {
  71. error(g_('badly formed line in files list file, line %d'), $.);
  72. }
  73. if (defined $self->{files}->{$file->{filename}}) {
  74. warning(g_('duplicate files list entry for file %s (line %d)'),
  75. $file->{filename}, $.);
  76. } else {
  77. $count++;
  78. $self->{files}->{$file->{filename}} = $file;
  79. }
  80. }
  81. return $count;
  82. }
  83. sub load_dir {
  84. my ($self, $dir) = @_;
  85. my $count = 0;
  86. my $dh = IO::Dir->new($dir) or syserr(g_('cannot open directory %s'), $dir);
  87. while (defined(my $file = $dh->read)) {
  88. my $pathname = "$dir/$file";
  89. next unless -f $pathname;
  90. $count += $self->load($pathname);
  91. }
  92. return $count;
  93. }
  94. sub get_files {
  95. my $self = shift;
  96. return map { $self->{files}->{$_} } sort keys %{$self->{files}};
  97. }
  98. sub get_file {
  99. my ($self, $filename) = @_;
  100. return $self->{files}->{$filename};
  101. }
  102. sub add_file {
  103. my ($self, $filename, $section, $priority) = @_;
  104. my $file = $self->parse_filename($filename);
  105. error(g_('invalid filename %s'), $filename) unless defined $file;
  106. $file->{section} = $section;
  107. $file->{priority} = $priority;
  108. $self->{files}->{$filename} = $file;
  109. return $file;
  110. }
  111. sub del_file {
  112. my ($self, $filename) = @_;
  113. delete $self->{files}->{$filename};
  114. }
  115. sub filter {
  116. my ($self, %opts) = @_;
  117. my $remove = $opts{remove} // sub { 0 };
  118. my $keep = $opts{keep} // sub { 1 };
  119. foreach my $filename (keys %{$self->{files}}) {
  120. my $file = $self->{files}->{$filename};
  121. if (not &$keep($file) or &$remove($file)) {
  122. delete $self->{files}->{$filename};
  123. }
  124. }
  125. }
  126. sub output {
  127. my ($self, $fh) = @_;
  128. my $str = '';
  129. binmode $fh if defined $fh;
  130. foreach my $filename (sort keys %{$self->{files}}) {
  131. my $file = $self->{files}->{$filename};
  132. my $entry = "$filename $file->{section} $file->{priority}\n";
  133. print { $fh } $entry if defined $fh;
  134. $str .= $entry;
  135. }
  136. return $str;
  137. }
  138. 1;