dpkg-ar: New internal ar implementation script
[dpkg.git] / scripts / dpkg-ar.pl
blob92cd8cf1a77e0f968bc83ce69b83d62060d7abbc
1 #!/usr/bin/perl
3 # dpkg-ar
5 # Copyright © 2024 Guillem Jover <guillem@debian.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 use warnings;
21 use strict;
23 use Dpkg ();
24 use Dpkg::Gettext;
25 use Dpkg::ErrorHandling;
26 use Dpkg::Archive::Ar;
28 textdomain('dpkg-dev');
30 my $action;
32 sub version()
34 printf(g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION);
37 sub usage()
39 printf(g_("Usage: %s [<option>...]\n"), $Dpkg::PROGNAME);
41 print(g_('
42 Commands:
43 --create <archive> <file>... create an ar archive.
44 --list <archive> list the contents of an ar archive.
45 --extract <archive> [<file>...] extract the contents of an ar archive.
46 -?, --help show this help message.
47 --version show the version.
48 '));
51 sub create
53 my ($archive, @files) = @_;
55 my $ar = Dpkg::Archive::Ar->new(
56 filename => $archive,
57 create => 1,
60 foreach my $file (@files) {
61 $ar->add_file($file);
65 sub list
67 my $archive = shift;
69 my $ar = Dpkg::Archive::Ar->new(filename => $archive);
71 foreach my $member (@{$ar->get_members()}) {
72 print "$member->{name}\n";
76 sub extract
78 my ($archive, @files) = @_;
79 my %file = map { $_ => 1 } @files;
81 my $ar = Dpkg::Archive::Ar->new(filename => $archive);
83 foreach my $member (@{$ar->get_members()}) {
84 next if @files && ! exists $file{$member->{name}};
86 $ar->extract_member($member);
90 my @files;
92 while (@ARGV) {
93 my $arg = shift @ARGV;
94 if ($arg eq '-?' or $arg eq '--help') {
95 usage();
96 exit(0);
97 } elsif ($arg eq '-v' or $arg eq '--version') {
98 version();
99 exit(0);
100 } elsif ($arg eq '--create') {
101 $action = 'create';
102 } elsif ($arg eq '--list') {
103 $action = 'list';
104 } elsif ($arg eq '--extract') {
105 $action = 'extract';
106 } elsif ($arg eq '--') {
107 push @files, @ARGV;
108 last;
109 } elsif ($arg =~ m/^-/) {
110 usageerr(g_("unknown option '%s'"), $arg);
111 } else {
112 push @files, $arg;
116 @files or usageerr(g_('need at least an archive filename'));
118 if ($action eq 'create') {
119 if (@files == 1) {
120 usageerr(g_('need at least a file to add into the archive'));
122 create(@files);
123 } elsif ($action eq 'list') {
124 list(@files);
125 } elsif ($action eq 'extract') {
126 extract(@files);