summaryrefslogtreecommitdiffstats
path: root/scripts/Dpkg/Archive/Ar.pm
blob: 97d5711b1c5bde8498b7a2b10de7cfe4edfdd7eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Copyright © 2023-2024 Guillem Jover <guillem@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

=encoding utf8

=head1 NAME

Dpkg::Archive::Ar - Unix ar archive support

=head1 DESCRIPTION

This module provides a class to handle Unix ar archives.
It support the common format, with no GNU or BSD extensions.

B<Note>: This is a private module, its API can change at any time.

=cut

package Dpkg::Archive::Ar 0.01;

use strict;
use warnings;

use Carp;
use Fcntl qw(:seek);
use IO::File;

use Dpkg::ErrorHandling;
use Dpkg::Gettext;

my $AR_MAGIC = "!<arch>\n";
my $AR_MAGIC_LEN = 8;
my $AR_FMAG = "\`\n";
my $AR_HDR_LEN = 60;

=head1 METHODS

=over 8

=item $ar = Dpkg::Archive::Ar->new(%opts)

Create a new object to handle Unix ar archives.

Supported options are:

=over 8

=item filename

The filename for the archive to open or create.

=item create

A boolean denoting whether the archive should be created,
otherwise if it does not exist the constructor will not open, create or
scan it.

=back

=cut

sub new {
    my ($this, %opts) = @_;
    my $class = ref($this) || $this;
    my $self = {
        filename => undef,
        fh => undef,
        # XXX: If we promote this out from internal use, we should make this
        # default to the archive mtime, or be overridable like in libdpkg,
        # so that it can be initialized from SOURCE_DATE_EPOCH for example.
        time => 0,
        size => 0,
        members => [],
    };
    bless $self, $class;

    if ($opts{filename}) {
        if ($opts{create}) {
            $self->create_archive($opts{filename});
        } elsif (-e $opts{filename}) {
            $self->open_archive($opts{filename});
        }
        if (-e $opts{filename}) {
            $self->scan_archive();
        }
    }

    return $self;
}

sub init_archive {
    my $self = shift;

    $self->{fh}->binmode();
    $self->{fh}->stat
        or syserr(g_('cannot get archive %s size'), $self->{filename});
    $self->{size} = -s _;

    return;
}

=item $ar->create_archive($filename)

Create the archive.

=cut

sub create_archive {
    my ($self, $filename) = @_;

    if (defined $self->{fh}) {
        croak 'the object has already been initialized with another file';
    }

    $self->{filename} = $filename;
    $self->{fh} = IO::File->new($filename, '+>')
        or syserr(g_('cannot open or create archive %s'), $filename);
    $self->init_archive();
    $self->{fh}->write($AR_MAGIC, $AR_MAGIC_LEN)
        or syserr(g_('cannot write magic into archive %s'), $filename);

    return;
}

=item $ar->open_archive($filename)

Open the archive.

=cut

sub open_archive {
    my ($self, $filename) = @_;

    if (defined $self->{fh}) {
        croak 'the object has already been initialized with another file';
    }

    $self->{filename} = $filename;
    $self->{fh} = IO::File->new($filename, '+<')
        or syserr(g_('cannot open or create archive %s'), $filename);
    $self->init_archive();

    return;
}

sub _read_buf {
    my ($self, $subject, $size) = @_;

    my $buf;
    my $offs = $self->{fh}->tell();
    my $n = $self->{fh}->read($buf, $size);
    if (not defined $n) {
        # TRANSLATORS: The first %s string is either "archive magic" or
        # "file header".
        syserr(g_('cannot read %s at offset %d from archive %s'),
               $subject, $offs, $self->{filename});
    } elsif ($n == 0) {
        return;
    } elsif ($n != $size) {
        # TRANSLATORS: The first %s string is either "archive magic" or
        # "file header".
        error(g_('%s at offset %d in archive %s is truncated'),
              $subject, $offs, $self->{filename});
    }

    return $buf;
}

=item $ar->parse_magic()

Reads and parses the archive magic string, and validates it.

=cut

sub parse_magic {
    my $self = shift;

    my $magic = $self->_read_buf(g_('archive magic'), $AR_MAGIC_LEN)
        or error(g_('archive %s contains no magic'), $self->{filename});

    if ($magic ne $AR_MAGIC) {
        error(g_('archive %s contains bad magic'), $self->{filename});
    }

    return;
}

=item $ar->parse_member()

Reads and parses the archive member and tracks it for later handling.

=cut

sub parse_member {
    my $self = shift;

    my $offs = $self->{fh}->tell();

    my $hdr = $self->_read_buf(g_('file header'), $AR_HDR_LEN)
        or return;

    my $hdr_fmt = 'A16A12A6A6A8A10a2';
    my ($name, $time, $uid, $gid, $mode, $size, $fmag) = unpack $hdr_fmt, $hdr;

    if ($fmag ne $AR_FMAG) {
        error(g_('file header at offset %d in archive %s contains bad magic'),
              $offs, $self->{filename});
    }

    # Remove trailing spaces from the member name.
    $name =~ s{ *$}{};

    # Remove optional slash terminator (on GNU-style archives).
    $name =~ s{/$}{};

    my $member = {
        name => $name,
        time => int $time,
        uid => int $uid,
        gid => int $gid,
        mode => oct $mode,
        size => int $size,
        offs => $offs,
    };
    push @{$self->{members}}, $member;

    return $member;
}

=item $ar->skip_member($member)

Skip this member to the next one.
Get the value of a given substitution.

=cut

sub skip_member {
    my ($self, $member) = @_;

    my $size = $member->{size};
    my $offs = $member->{offs} + $AR_HDR_LEN + $size + ($size & 1);

    $self->{fh}->seek($offs, SEEK_SET)
        or syserr(g_('cannot seek into next file header at offset %d from archive %s'),
                  $offs, $self->{filename});

    return;
}

=item $ar->scan_archive()

Scan the archive for all its member files and metadata.

=cut

sub scan_archive {
    my $self = shift;

    $self->{fh}->seek(0, SEEK_SET)
        or syserr(g_('cannot seek into beginning of archive %s'),
                  $self->{filename});

    $self->parse_magic();

    while (my $member = $self->parse_member()) {
        $self->skip_member($member);
    }

    return;
}

=item $ar->get_members()

Get the list of members in the archive.

=cut

sub get_members {
    my $self = shift;

    return $self->{members};
}

sub _copy_fh_fh {
    my ($self, $if, $of, $size) = @_;

    while ($size > 0) {
        my $buf;
        my $buflen = $size > 4096 ? 4096 : $size;

        my $n = $if->{fh}->read($buf, $buflen)
            or syserr(g_('cannot read file %s'), $if->{name});

        $of->{fh}->write($buf, $buflen)
            or syserr(g_('cannot write file %s'), $of->{name});

        $size -= $buflen;
    }

    return;
}

=item $ar->extract_member($member)

Extract the specified member to the current directory.

=cut

sub extract_member {
    my ($self, $member) = @_;

    $self->{fh}->seek($member->{offs} + $AR_HDR_LEN, SEEK_SET);

    my $ofh = IO::File->new($member->{name}, '+>')
        or syserr(g_('cannot create file %s to extract from archive %s'),
                  $member->{name}, $self->{filename});

    $self->_copy_fh_fh({ fh => $self->{fh}, name => $self->{filename} },
                       { fh => $ofh, name => $member->{name} },
                      $member->{size});

    $ofh->close()
        or syserr(g_('cannot write file %s to the filesystem'),
                  $member->{name});

    return;
}

=item $ar->write_member($member)

Write the provided $member into the archive.

=cut

sub write_member {
    my ($self, $member) = @_;

    my $size = $member->{size};
    my $mode = sprintf '%o', $member->{mode};

    my $hdr_fmt = 'A16A12A6A6A8A10A2';
    my $data = pack $hdr_fmt, @{$member}{qw(name time uid gid)}, $mode, $size, $AR_FMAG;

    $self->{fh}->write($data, $AR_HDR_LEN, $member->{offs})
        or syserr(g_('cannot write file header into archive %s'),
                  $self->{filename});

    return;
}

=item $ar->add_file($filename)

Append the specified $filename into the archive.

=cut

sub add_file {
    my ($self, $filename) = @_;

    if (length $filename > 15) {
        error(g_('filename %s is too long'), $filename);
    }

    my $fh = IO::File->new($filename, '<')
        or syserr(g_('cannot open file %s to append to archive %s'),
                  $filename, $self->{filename});
    $fh->stat()
        or syserr(g_('cannot get file %s size'), $filename);
    my $size = -s _;

    my %member = (
        name => $filename,
        size => $size,
        time => $self->{time},
        mode => 0100644,
        uid => 0,
        gid => 0,
    );

    $self->write_member(\%member);
    $self->_copy_fh_fh({ fh => $fh, name => $filename },
                       { fh => $self->{fh}, name => $self->{filename} },
                       $size);
    if ($size & 1) {
        $self->{fh}->write("\n", 1)
            or syserr(g_('cannot write file %s padding to archive %s'),
                      $filename, $self->{filename});
    }

    return;
}

=item $ar->close_archive()

Close the archive and release any allocated resource.

=cut

sub close_archive {
    my $self = shift;

    $self->{fh}->close() if defined $self->{fh};
    $self->{fh} = undef;
    $self->{size} = 0;
    $self->{members} = [];

    return;
}

sub DESTROY {
    my $self = shift;

    $self->close_archive();

    return;
}

=back

=head1 CHANGES

=head2 Version 0.xx

This is a private module.

=cut

1;