File: BackupPC_tarCreate

package info (click to toggle)
backuppc 4.4.0-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,752 kB
  • sloc: perl: 37,523; sh: 607; javascript: 176; makefile: 38; ansic: 6
file content (704 lines) | stat: -rwxr-xr-x 23,859 bytes parent folder | download | duplicates (3)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#!/usr/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_tarCreate: create a tar archive of an existing dump
# for restore on a client.
#
# DESCRIPTION
#
#   Usage: BackupPC_tarCreate [options] files/directories...
#
#   Flags:
#     Required options:
#
#       -h host         Host from which the tar archive is created.
#       -n dumpNum      Dump number from which the tar archive is created.
#                       A negative number means relative to the end (eg -1
#                       means the most recent dump, -2 2nd most recent etc).
#       -s shareName    Share name from which the tar archive is created,
#                       or "*" to mean all shares.
#
#     Other options:
#       -t              print summary totals
#       -r pathRemove   path prefix that will be replaced with pathAdd
#       -p pathAdd      new path prefix
#       -b BLOCKS       output write buffer size in 512-byte blocks (default 20; same as tar)
#       -w readBufSz    buffer size for reading files (default 1048576 = 1MB)
#       -e charset      charset for encoding file names (default: value of
#                       $Conf{ClientCharset} when backup was done)
#       -l              just print a file listing; don't generate an archive
#       -L              just print a detailed file listing; don't generate an archive
#       -m              run even if a backup on this host is running
#                       (specifically, don't take the server host mutex)
#
#     The -h, -n and -s options specify which dump is used to generate
#     the tar archive.  The -r and -p options can be used to relocate
#     the paths in the tar archive so extracted files can be placed
#     in a location different from their original location.
#
# AUTHOR
#   Craig Barratt  <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
#   Copyright (C) 2001-2020  Craig Barratt
#
#   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 3 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 <http://www.gnu.org/licenses/>.
#
#========================================================================
#
# Version 4.4.0, released 20 Jun 2020.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================

use strict;
no utf8;

use lib "__INSTALLDIR__/lib";
use File::Path;
use Getopt::Std;
use Encode qw/from_to/;
use Data::Dumper;

use BackupPC::Lib;
use BackupPC::XS qw( :all );
use BackupPC::View;

die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
my %Conf = $bpc->Conf();

my %opts;

if ( !getopts("Lltme:h:n:p:r:s:b:w:", \%opts) || @ARGV < 1 ) {
    print STDERR <<EOF;
usage: $0 [options] files/directories...
  Required options:
     -h host         host from which the tar archive is created
     -n dumpNum      dump number from which the tar archive is created
                     A negative number means relative to the end (eg -1
                     means the most recent dump, -2 2nd most recent etc).
     -s shareName    share name from which the tar archive is created,
                     or "*" to mean all shares

  Other options:
     -t              print summary totals
     -r pathRemove   path prefix that will be replaced with pathAdd
     -p pathAdd      new path prefix
     -b BLOCKS       output write buffer size in 512-byte blocks (default 20; same as tar)
     -w readBufSz    buffer size for reading files (default 1048576 = 1MB)
     -e charset      charset for encoding file names (default: value of
                     \$Conf{ClientCharset} when backup was done)
     -l              just print a file listing; don't generate an archive
     -L              just print a detailed file listing; don't generate an archive
     -m              force running even if a backup on this host is running
                     (specifically, don't take the server host mutex)
EOF
    exit(1);
}

my $Host;
if ( $opts{h} !~ m{(^|/)\.\.(/|$)} && $opts{h} =~ /^([\w@.\s-]+)$/ ) {
    $Host = $1;
} else {
    print(STDERR "$0: bad host name '$opts{h}'\n");
    exit(1);
}

if ( $opts{n} !~ /^(-?\d+)$/ ) {
    print(STDERR "$0: bad dump number '$opts{n}'\n");
    exit(1);
}
if (   !$opts{m}
    && !defined($bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}))
    && (my $status = $bpc->ServerMesg("hostMutex $Host 1 BackupPC_tarCreate")) =~ /fail/ ) {
    print(STDERR "$0: $status (use -m option to force running)\n");
    exit(1);
}

my $Num = $opts{n};

my @Backups    = $bpc->BackupInfoRead($Host);
my $FileCnt    = 0;
my $ByteCnt    = 0;
my $DirCnt     = 0;
my $SpecialCnt = 0;
my $ErrorCnt   = 0;

my $i;

$Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
for ( $i = 0 ; $i < @Backups ; $i++ ) {
    last if ( $Backups[$i]{num} == $Num );
}
if ( $i >= @Backups ) {
    print(STDERR "$0: bad backup number $Num for host $Host\n");
    exit(1);
}

my $Charset = $Backups[$i]{charset};
$Charset = $opts{e} if ( $opts{e} ne "" );
my $PreV4 = ($Backups[$i]{version} eq "" || $Backups[$i]{version} =~ /^[23]\./) ? 1 : 0;

my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
my $PathAdd    = $1 if ( $opts{p} =~ /(.+)/ );
if ( $opts{s} =~ m{(^|/)\.\.(/|$)} ) {
    print(STDERR "$0: bad share name '$opts{s}'\n");
    exit(1);
}

our $ShareName = $opts{s};
our $view      = BackupPC::View->new($bpc, $Host, \@Backups);

#
# This constant and the line of code below that uses it are borrowed
# from Archive::Tar.  Thanks to Calle Dybedahl and Stephen Zander.
# See www.cpan.org.
#
# Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
#                 Copyright 1998 Stephen Zander. All rights reserved.
#
my $tar_pack_header   = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
my $tar_unpack_header = 'Z100 A8 A8 A8 a12 A12 A8 A1 Z100 A6 A2 Z32 Z32 A8 A8 A155 x12';
my $tar_header_length = 512;

my $BufSize    = $opts{w} || 1048576;                     # 1MB or 2^20
my $WriteBuf   = "";
my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;

my(%UidCache, %GidCache);
my(%HardLinkExtraFiles, @HardLinks, %Inode2File);

#
# Write out all the requested files/directories
#
binmode(STDOUT);
my $fh = *STDOUT;
if ( $ShareName eq "*" ) {
    my $PathRemoveOrig = $PathRemove;
    my $PathAddOrig    = $PathAdd;
    foreach $ShareName ( $view->shareList($Num) ) {
        #print(STDERR "Doing share ($ShareName)\n");
        $PathRemove = "/" if ( !defined($PathRemoveOrig) );
        ($PathAdd = "/$ShareName/$PathAddOrig") =~ s{//+}{/}g;
        foreach my $dir ( @ARGV ) {
            archiveWrite($fh, $dir);
        }
        archiveWriteHardLinks($fh);
    }
} else {
    foreach my $dir ( @ARGV ) {
        archiveWrite($fh, $dir);
    }
    archiveWriteHardLinks($fh);
}

if ( !$opts{l} && !$opts{L} ) {
    #
    # Finish with two null 512 byte headers, and then round out a full
    # block.
    #
    my $data = "\0" x ($tar_header_length * 2);
    TarWrite($fh, \$data);
    TarWrite($fh, undef);
}

#
# print out totals if requested
#
if ( $opts{t} ) {
    print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,", " $SpecialCnt specials, $ErrorCnt errors\n";
}
if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
    #
    # Got errors, with no files or directories; exit with non-zero
    # status
    #
    exit(1);
}
exit(0);

###########################################################################
# Subroutines
###########################################################################

sub archiveWrite
{
    my($fh, $dir, $tarPathOverride) = @_;

    if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
        print(STDERR "$0: bad directory '$dir'\n");
        $ErrorCnt++;
        return;
    }
    $dir = "/" if ( $dir eq "." );
    #print(STDERR "calling find with $Num, $ShareName, $dir\n");
    if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile, $fh, $tarPathOverride) < 0 ) {
        print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
        $ErrorCnt++;
        return;
    }
}

#
# Write out any hardlinks (if any); only for <= 3.x backups.
#
sub archiveWriteHardLinks
{
    my($fh) = @_;

    return if ( !$PreV4 );
    foreach my $hdr ( @HardLinks ) {
        $hdr->{size} = 0;
        my $name = $hdr->{linkname};
        $name =~ s{^\./}{/};
        if ( defined($HardLinkExtraFiles{$name}) ) {
            $hdr->{linkname} = $HardLinkExtraFiles{$name};
        }
        if ( defined($PathRemove)
            && substr($hdr->{linkname}, 0, length($PathRemove) + 1) eq ".$PathRemove" ) {
            substr($hdr->{linkname}, 0, length($PathRemove) + 1) = ".$PathAdd";
        }
        TarWriteFileInfo($fh, $hdr);
    }
    @HardLinks          = ();
    %HardLinkExtraFiles = ();
}

sub UidLookup
{
    my($uid) = @_;

    $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
    return $UidCache{$uid};
}

sub GidLookup
{
    my($gid) = @_;

    $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
    return $GidCache{$gid};
}

sub TarWrite
{
    my($fh, $dataRef) = @_;

    if ( !defined($dataRef) ) {
        #
        # do flush by padding to a full $WriteBufSz
        #
        my $data = "\0" x ($WriteBufSz - length($WriteBuf));
        $dataRef = \$data;
    }
    if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
        #
        # just buffer and return
        #
        $WriteBuf .= $$dataRef;
        return;
    }
    my $done = $WriteBufSz - length($WriteBuf);
    if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done)) != $WriteBufSz ) {
        print(STDERR "Unable to write to output file ($!)\n");
        exit(1);
    }
    while ( $done + $WriteBufSz <= length($$dataRef) ) {
        if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz)) != $WriteBufSz ) {
            print(STDERR "Unable to write to output file ($!)\n");
            exit(1);
        }
        $done += $WriteBufSz;
    }
    $WriteBuf = substr($$dataRef, $done);
}

sub TarWritePad
{
    my($fh, $size) = @_;

    if ( $size % $tar_header_length ) {
        my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
        TarWrite($fh, \$data);
    }
}

sub TarWriteHeader
{
    my($fh, $hdr) = @_;

    $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
    $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
    my $devmajor =
      defined($hdr->{devmajor})
      ? sprintf("%07o", $hdr->{devmajor})
      : "";
    my $devminor =
      defined($hdr->{devminor})
      ? sprintf("%07o", $hdr->{devminor})
      : "";
    my $data = pack(
        $tar_pack_header,
        substr($hdr->{name}, 0, 99),
        sprintf("%07o",  $hdr->{mode}),
        sprintf("%07o",  $hdr->{uid}),
        sprintf("%07o",  $hdr->{gid}),
        sprintf("%011o", $hdr->{size}),
        sprintf("%011o", $hdr->{mtime}),
        "",    #checksum field - space padded by pack("A8")
        $hdr->{type},
        substr($hdr->{linkname}, 0, 99),
        $hdr->{magic}   || 'ustar ',
        $hdr->{version} || ' ',
        $hdr->{uname},
        $hdr->{gname},
        $devmajor,
        $devminor,
        ""     # prefix is empty
    );
    #
    # now unpack it to see which fields weren't represented correctly,
    # and if there are any we generate a pax header
    #
    my @paxFlds;
    my(
        $name,        # string
        $mode,        # octal number
        $uid,         # octal number
        $gid,         # octal number
        $size,        # octal number
        $mtime,       # octal number
        $chksum,      # octal number
        $type,        # character
        $linkname,    # string
        $magic,       # string
        $version,     # two bytes
        $uname,       # string
        $gname,       # string
        $devmajor,    # octal number
        $devminor,    # octal number
        $prefix
    ) = unpack($tar_unpack_header, $data);
    push(@paxFlds, "path=$hdr->{name}")         if ( $name ne $hdr->{name} );
    push(@paxFlds, "linkpath=$hdr->{linkname}") if ( $linkname ne $hdr->{linkname} );
    push(@paxFlds, "size=$hdr->{size}")         if ( oct($size) != $hdr->{size} );
    push(@paxFlds, "mtime=$hdr->{mtime}")       if ( oct($mtime) != $hdr->{mtime} );
    push(@paxFlds, "uid=$hdr->{uid}")           if ( oct($uid) != $hdr->{uid} );
    push(@paxFlds, "gid=$hdr->{gid}")           if ( oct($uid) != $hdr->{gid} );
    push(@paxFlds, "uname=$hdr->{uname}")       if ( $uname ne $hdr->{uname} );
    push(@paxFlds, "gname=$hdr->{gname}")       if ( $gname ne $hdr->{gname} );

    if ( ref($hdr->{xattr}) eq 'HASH' ) {

        # include xattr and acl using gnu tar naming convention
        foreach my $name ( keys(%{$hdr->{xattr}}) ) {

            # Skip rsync acls; should try to figure out binary rsync acls, and map
            # then to ascii version used by tar...
            next if ( $name eq "user.rsync.%aacl" || $name eq "user.rsync.%dacl" );
            if ( $name eq "user.gtar.%aacl" ) {
                push(@paxFlds, "SCHILY.acl.access=" . $hdr->{xattr}{$name});
            } elsif ( $name eq "user.gtar.%dacl" ) {
                push(@paxFlds, "SCHILY.acl.default=" . $hdr->{xattr}{$name});
            } else {
                push(@paxFlds, "SCHILY.xattr.$name=" . $hdr->{xattr}{$name});
            }
        }
    }
    if ( @paxFlds ) {
        #
        # Some fields don't match - we need to generate a pax header
        #
        my $paxData;
        foreach my $fld ( @paxFlds ) {

            # the length includes the string length...
            my $len  = sprintf("%d", length($fld) + 3);                   # at least 1 digit + space + \n
            my $len2 = sprintf("%d", length($fld) + length($len) + 2);    # + space + \n
            if ( length($len2) != length($len) ) {

                # rollover: adding length requires one more digit in length
                $len2 = sprintf("%d", length($fld) + length($len2) + 2);
            }
            $paxData .= "$len2 " . $fld . "\n";
        }
        my $paxHdrData = pack(
            $tar_pack_header,
            substr("./PaxHeaders/$hdr->{name}", 0, 99),
            sprintf("%07o",  $hdr->{mode}),
            sprintf("%07o",  $hdr->{uid}),
            sprintf("%07o",  $hdr->{gid}),
            sprintf("%011o", length($paxData)),
            sprintf("%011o", $hdr->{mtime}),
            "",    #checksum field - space padded by pack("A8")
            "x",
            substr($hdr->{linkname}, 0, 99),
            $hdr->{magic}   || 'ustar ',
            $hdr->{version} || ' ',
            $hdr->{uname},
            $hdr->{gname},
            $devmajor,
            $devminor,
            ""     # prefix is empty
        );
        substr($paxHdrData, 148, 7) = sprintf("%06o\0", unpack("%16C*", $paxHdrData));
        TarWrite($fh, \$paxHdrData);
        TarWrite($fh, \$paxData);
        TarWritePad($fh, length($paxData));
    }
    substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*", $data));
    TarWrite($fh, \$data);
}

sub TarWriteFileInfo
{
    my($fh, $hdr) = @_;

    #
    # Convert path names to requested (eg: client) charset
    #
    if ( $Charset ne "" ) {
        from_to($hdr->{name},     "utf8", $Charset);
        from_to($hdr->{linkname}, "utf8", $Charset);
    }

    if ( $opts{l} ) {
        print($hdr->{name} . "\n");
        return;
    } elsif ( $opts{L} ) {
        my $owner = "$hdr->{uid}/$hdr->{gid}";

        my $name = $hdr->{name};

        if ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
            $name .= " -> $hdr->{linkname}";
        }
        $name =~ s/\n/\\n/g;

        printf("%6o %9s %11.0f %s\n", $hdr->{mode}, $owner, $hdr->{size}, $name);
        return;
    }

    TarWriteHeader($fh, $hdr);
}

sub TarWriteFile
{
    my($hdr, $fh, $tarPathOverride) = @_;

    my $tarPath = $hdr->{relPath};
    $tarPath = $tarPathOverride if ( defined($tarPathOverride) );

    $tarPath =~ s{//+}{/}g;
    if ( defined($PathRemove)
        && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
        substr($tarPath, 0, length($PathRemove)) = $PathAdd;
    }
    $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
    $tarPath =~ s{//+}{/}g;
    $hdr->{name} = $tarPath;

    if ( !$PreV4 && $hdr->{nlinks} > 0 && defined($hdr->{inode}) ) {
        if ( defined($Inode2File{$hdr->{inode}}) ) {
            #
            # Later inodes: emit a hardlink to an existing file in the archive
            # TODO: do path rewrite on link path?
            #
            $hdr->{size}     = 0;
            $hdr->{type}     = BPC_FTYPE_HARDLINK;
            $hdr->{linkname} = $Inode2File{$hdr->{inode}}{name};
            TarWriteFileInfo($fh, $hdr);
            return;
        } else {
            #
            # First time: remember the data for this inode and dump
            # the file in its original form.
            #
            $Inode2File{$hdr->{inode}} = {%$hdr};
        }
    }

    if ( $hdr->{type} == BPC_FTYPE_DIR ) {
        #
        # Directory: just write the header
        #
        $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
        TarWriteFileInfo($fh, $hdr);
        $DirCnt++;
    } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
        my($data, $size);
        #
        # Regular file: write the header and file
        #
        my $f = BackupPC::XS::FileZIO::open($hdr->{fullPath}, 0, $hdr->{compress});
        if ( !defined($f) ) {
            print(STDERR "Unable to open file $hdr->{fullPath} (for $hdr->{name})\n");
            $ErrorCnt++;
            return;
        }
        TarWriteFileInfo($fh, $hdr);
        if ( $opts{l} || $opts{L} ) {
            $size = $hdr->{size};
        } else {
            while ( $f->read(\$data, $BufSize) > 0 ) {
                if ( $size + length($data) > $hdr->{size} ) {
                    print(STDERR "Error: truncating $hdr->{fullPath} to $hdr->{size} bytes (for $hdr->{name})\n");
                    $data = substr($data, 0, $hdr->{size} - $size);
                    $ErrorCnt++;
                }
                TarWrite($fh, \$data);
                $size += length($data);
            }
            $f->close;
            if ( $size != $hdr->{size} ) {
                print(STDERR "Error: padding $hdr->{fullPath} to $hdr->{size}"
                      . " bytes from $size bytes (for $hdr->{name})\n");
                $ErrorCnt++;
                while ( $size < $hdr->{size} ) {
                    my $len = $hdr->{size} - $size;
                    $len  = $BufSize if ( $len > $BufSize );
                    $data = "\0" x $len;
                    TarWrite($fh, \$data);
                    $size += $len;
                }
            }
            TarWritePad($fh, $size);
        }
        $FileCnt++;
        $ByteCnt += $size;
    } elsif ( $PreV4 && $hdr->{type} == BPC_FTYPE_HARDLINK ) {
        #
        # Note: the meaning of this type changed between BackupPC <= v3.x
        # and >= 4.x.
        #
        # In 3.x a hardlink is stored like a symlink: the contents
        # of the "file" is the path to the linked-to file.
        #
        # In 4.x+ a hardlink's attributes are stored with the
        # inode, and the real attributes are stored by inode
        # for all files with nlinks >= 1.
        #
        # The 4.x case is handled above.
        #
        # TODO: do path rewrite on link path?
        #
        # Hardlink file: either write a hardlink or the complete file
        # depending upon whether the linked-to file will be written
        # to the archive.
        #
        # Start by reading the contents of the link.
        #
        my $f = BackupPC::XS::FileZIO::open($hdr->{fullPath}, 0, $hdr->{compress});
        if ( !defined($f) ) {
            print(STDERR "Unable to open file $hdr->{fullPath} (for $hdr->{name})\n");
            $ErrorCnt++;
            return;
        }
        my $data;
        while ( $f->read(\$data, $BufSize) > 0 ) {
            $hdr->{linkname} .= $data;
        }
        $f->close;
        #
        # Check @ARGV and the list of hardlinked files we have explicitly
        # dumped to see if we have dumped this file or not
        #
        my $done = 0;
        my $name = $hdr->{linkname};
        $name =~ s{^\.?/+}{/};
        $name = "/$name" if ( $name !~ m{^/} );
        if ( defined($HardLinkExtraFiles{$name}) ) {
            $done = 1;
        } else {
            foreach my $arg ( @ARGV ) {
                $arg = "/" if ( $arg eq "." );
                $arg =~ s{^\.?/+}{/};
                $arg =~ s{/+$}{};
                $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// || $arg eq "" );
            }
        }
        if ( $done ) {
            #
            # Target file will be or was written, so just remember
            # the hardlink so we can dump it later.
            #
            push(@HardLinks, $hdr);
            $SpecialCnt++;
        } else {
            #
            # Have to dump the original file.  Just call the top-level
            # routine, so that we save the hassle of dealing with
            # mangling, merging and attributes.
            #
            my $name = $hdr->{linkname};
            $name =~ s{^\./}{/};
            $HardLinkExtraFiles{$name} = $hdr->{name};
            archiveWrite($fh, $name, $hdr->{name});
        }
    } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
        #
        # Symbolic link: read the symbolic link contents into the header
        # and write the header.
        # TODO: do path rewrite on link path?
        #
        my $f = BackupPC::XS::FileZIO::open($hdr->{fullPath}, 0, $hdr->{compress});
        if ( !defined($f) ) {
            print(STDERR "Unable to open symlink file $hdr->{fullPath} (for $hdr->{name})\n");
            $ErrorCnt++;
            return;
        }
        my $data;
        while ( $f->read(\$data, $BufSize) > 0 ) {
            $hdr->{linkname} .= $data;
        }
        $f->close;
        $hdr->{size} = 0;
        TarWriteFileInfo($fh, $hdr);
        $SpecialCnt++;
    } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
        || $hdr->{type} == BPC_FTYPE_BLOCKDEV
        || $hdr->{type} == BPC_FTYPE_FIFO ) {
        #
        # Special files: for char and block special we read the
        # major and minor numbers from a plain file.
        #
        if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
            my $f = BackupPC::XS::FileZIO::open($hdr->{fullPath}, 0, $hdr->{compress});
            my $data;
            if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
                print(STDERR "Unable to open/read char/block special file $hdr->{fullPath} (for $hdr->{name})\n");
                $f->close if ( defined($f) );
                $ErrorCnt++;
                return;
            }
            $f->close;
            if ( $data =~ /(\d+),(\d+)/ ) {
                $hdr->{devmajor} = $1;
                $hdr->{devminor} = $2;
            }
        }
        $hdr->{size} = 0;
        TarWriteFileInfo($fh, $hdr);
        $SpecialCnt++;
    } elsif ( $hdr->{type} == BPC_FTYPE_SOCKET || $hdr->{type} == BPC_FTYPE_UNKNOWN ) {
        #
        # ignore these two file types - these are dynamic file types created
        # by applications as needed
        #
    } else {
        print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
        $ErrorCnt++;
    }
}