5 # Copyright © 1995,1996 Erick Branderhorst <branderh@debian.org>.
6 # Copyright © 2006-2010, 2012-2015 Guillem Jover <guillem@debian.org>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
24 use List
::Util
qw(none);
26 use File
::Path
qw(make_path);
30 use Dpkg
::ErrorHandling
;
33 use Dpkg
::Arch
qw(get_host_arch);
35 textdomain
('dpkg-dev');
48 printf(g_
("Debian %s version %s.\n"), $Dpkg::PROGNAME
, $Dpkg::PROGVERSION
);
53 printf(g_
("Usage: %s [<option>...] <file>...\n"), $Dpkg::PROGNAME
);
57 -a, --no-architecture no architecture part in filename.
58 -o, --overwrite overwrite if file exists.
59 -k, --symlink don't create a new file, but a symlink.
60 -s, --subdir [dir] move file into subdirectory (use with care).
61 -c, --create-dir create target directory if not there (use with care).
62 -?, --help show this help message.
63 -v, --version show the version.
65 file.deb changes to <package>_<version>_<architecture>.<package_type>
66 according to the 'underscores convention'.
77 warning
(g_
("cannot find '%s'"), $filename);
88 # Same device and inode numbers.
89 return (@sta and @stb and $sta[0] == $stb[0] and $sta[1] == $stb[1]);
97 open(my $cdata_fh, '-|', 'dpkg-deb', '-f', '--', $filename)
98 or syserr
(g_
('cannot open %s'), $filename);
99 my $fields = Dpkg
::Control
->new(type
=> CTRL_PKG_DEB
);
100 $fields->parse($cdata_fh, sprintf(g_
('binary control file %s'), $filename));
108 my ($filename, $fields) = @_;
110 my $arch = $fields->{Architecture
};
111 if (not $fields->{Architecture
} and $options{architecture
}) {
112 $arch = get_host_arch
();
113 warning
(g_
("assuming architecture '%s' for '%s'"), $arch, $filename);
121 my ($filename, $fields, $arch) = @_;
123 my $pkg = $fields->{Package
};
124 my $v = Dpkg
::Version
->new($fields->{Version
});
125 my $version = $v->as_string(omit_epoch
=> 1);
126 my $type = $fields->{'Package-Type'} || 'deb';
129 if ($options{architecture
}) {
130 $tname = "$pkg\_$version\_$arch.$type";
132 $tname = "$pkg\_$version.$type";
134 (my $name = $tname) =~ s/ //g;
135 if ($tname ne $name) { # control fields have spaces
136 warning
(g_
("bad package control information for '%s'"), $filename);
143 my ($filename, $fields, $arch) = @_;
146 if (!$options{destdir
}) {
147 $dir = dirname
($filename);
148 if ($options{subdir
}) {
149 my $section = $fields->{Section
};
151 $section = 'no-section';
152 warning
(g_
("assuming section '%s' for '%s'"), $section,
155 if (none
{ $section eq $_ } qw(no-section contrib non-free)) {
156 $dir = "unstable/binary-$arch/$section";
158 $dir = "$section/binary-$arch";
162 $dir = $options{destdir
};
170 my $filename = shift;
172 if (fileexists
($filename)) {
173 my $fields = getfields
($filename);
175 unless (exists $fields->{Package
}) {
176 warning
(g_
("no Package field found in '%s', skipping package"),
181 my $arch = getarch
($filename, $fields);
183 my $name = getname
($filename, $fields, $arch);
185 my $dir = getdir
($filename, $fields, $arch);
187 if ($options{createdir
}) {
188 if (make_path
($dir)) {
189 info
(g_
("created directory '%s'"), $dir);
191 error
(g_
("cannot create directory '%s'"), $dir);
194 error
(g_
("no such directory '%s', try --create-dir (-c) option"),
199 my $newname = "$dir/$name";
202 if ($options{symlink}) {
203 @command = qw(ln -s --);
205 @command = qw(mv --);
208 if (filesame
($newname, $filename)) {
209 warning
(g_
("skipping '%s'"), $filename);
210 } elsif (-f
$newname and not $options{overwrite
}) {
211 warning
(g_
("cannot move '%s' to existing file"), $filename);
212 } elsif (system(@command, $filename, $newname) == 0) {
213 info
(g_
("moved '%s' to '%s'"), basename
($filename), $newname);
215 error
(g_
('mkdir can be used to create directory'));
224 if (m/^-\?|--help$/) {
227 } elsif (m/^-v|--version$/) {
230 } elsif (m/^-c|--create-dir$/) {
231 $options{createdir
} = 1;
232 } elsif (m/^-s|--subdir$/) {
233 $options{subdir
} = 1;
235 $options{destdir
} = shift(@ARGV);
237 } elsif (m/^-o|--overwrite$/) {
238 $options{overwrite
} = 1;
239 } elsif (m/^-k|--symlink$/) {
240 $options{symlink} = 1;
241 } elsif (m/^-a|--no-architecture$/) {
242 $options{architecture
} = 0;
247 usageerr
(g_
("unknown option '%s'"), $_);
253 @files or usageerr
(g_
('need at least a filename'));
255 foreach my $file (@files) {