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/>.
25 use File::Path qw(make_path);
29 use Dpkg::ErrorHandling;
32 use Dpkg::Arch qw(get_host_arch);
34 textdomain('dpkg-dev');
47 printf(g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION);
52 printf(g_("Usage: %s [<option>...] <file>...\n"), $Dpkg::PROGNAME);
56 -a, --no-architecture no architecture part in filename.
57 -o, --overwrite overwrite if file exists.
58 -k, --symlink don't create a new file, but a symlink.
59 -s, --subdir [dir] move file into subdirectory (use with care).
60 -c, --create-dir create target directory if not there (use with care).
61 -?, --help show this help message.
62 -v, --version show the version.
64 file.deb changes to <package>_<version>_<architecture>.<package_type>
65 according to the 'underscores convention'.
76 warning(g_("cannot find '%s'"), $filename);
87 # Same device and inode numbers.
88 return (@sta and @stb and $sta[0] == $stb[0] and $sta[1] == $stb[1]);
96 open(my $cdata_fh, '-|', 'dpkg-deb', '-f', '--', $filename)
97 or syserr(g_('cannot open %s'), $filename);
98 my $fields = Dpkg::Control->new(type => CTRL_PKG_DEB);
99 $fields->parse($cdata_fh, sprintf(g_('binary control file %s'), $filename));
107 my ($filename, $fields) = @_;
109 my $arch = $fields->{Architecture};
110 if (not $fields->{Architecture} and $options{architecture}) {
111 $arch = get_host_arch();
112 warning(g_("assuming architecture '%s' for '%s'"), $arch, $filename);
120 my ($filename, $fields, $arch) = @_;
122 my $pkg = $fields->{Package};
123 my $v = Dpkg::Version->new($fields->{Version});
124 my $version = $v->as_string(omit_epoch => 1);
125 my $type = $fields->{'Package-Type'} || 'deb';
128 if ($options{architecture}) {
129 $tname = "$pkg\_$version\_$arch.$type";
131 $tname = "$pkg\_$version.$type";
133 (my $name = $tname) =~ s/ //g;
134 if ($tname ne $name) { # control fields have spaces
135 warning(g_("bad package control information for '%s'"), $filename);
142 my ($filename, $fields, $arch) = @_;
145 if (!$options{destdir}) {
146 $dir = dirname($filename);
147 if ($options{subdir}) {
148 my $section = $fields->{Section};
150 $section = 'no-section';
151 warning(g_("assuming section '%s' for '%s'"), $section,
154 if ($section ne 'non-free' and $section ne 'contrib' and
155 $section ne 'no-section') {
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) {