1 # Copyright © 2014-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
16 package Dpkg::Dist::Files;
21 our $VERSION = '0.01';
26 use Dpkg::ErrorHandling;
28 use parent qw(Dpkg::Interface::Storable);
31 my ($this, %opts) = @_;
32 my $class = ref($this) || $this;
38 foreach my $opt (keys %opts) {
39 $self->{$opt} = $opts{$opt};
57 if ($fn =~ m/^(([-+:.0-9a-z]+)_([^_]+)_([-\w]+)\.([a-z0-9.]+))$/) {
58 $file->{filename} = $1;
59 $file->{package} = $2;
60 $file->{version} = $3;
62 $file->{package_type} = $5;
63 } elsif ($fn =~ m/^([-+:.,_0-9a-zA-Z~]+)$/) {
64 $file->{filename} = $1;
73 my ($self, $fh, $desc) = @_;
84 if (m/^(\S+) (\S+) (\S+)$/) {
85 $file = $self->parse_filename($1);
86 error(g_('badly formed package name in files list file, line %d'), $.)
88 $file->{section} = $2;
89 $file->{priority} = $3;
91 error(g_('badly formed line in files list file, line %d'), $.);
94 if (defined $self->{files}->{$file->{filename}}) {
95 warning(g_('duplicate files list entry for file %s (line %d)'),
96 $file->{filename}, $.);
99 $self->{files}->{$file->{filename}} = $file;
107 my ($self, $dir) = @_;
110 my $dh = IO::Dir->new($dir) or syserr(g_('cannot open directory %s'), $dir);
112 while (defined(my $file = $dh->read)) {
113 my $pathname = "$dir/$file";
114 next unless -f $pathname;
115 $count += $self->load($pathname);
124 return map { $self->{files}->{$_} } sort keys %{$self->{files}};
128 my ($self, $filename) = @_;
130 return $self->{files}->{$filename};
134 my ($self, $filename, $section, $priority) = @_;
136 my $file = $self->parse_filename($filename);
137 error(g_('invalid filename %s'), $filename) unless defined $file;
138 $file->{section} = $section;
139 $file->{priority} = $priority;
141 $self->{files}->{$filename} = $file;
147 my ($self, $filename) = @_;
149 delete $self->{files}->{$filename};
153 my ($self, %opts) = @_;
154 my $remove = $opts{remove} // sub { 0 };
155 my $keep = $opts{keep} // sub { 1 };
157 foreach my $filename (keys %{$self->{files}}) {
158 my $file = $self->{files}->{$filename};
160 if (not &$keep($file) or &$remove($file)) {
161 delete $self->{files}->{$filename};
167 my ($self, $fh) = @_;
170 binmode $fh if defined $fh;
172 foreach my $filename (sort keys %{$self->{files}}) {
173 my $file = $self->{files}->{$filename};
174 my $entry = "$filename $file->{section} $file->{priority}\n";
176 print { $fh } $entry if defined $fh;