File: makelinks.pl

package info (click to toggle)
doc-rfc 20170121-1
  • links: PTS, VCS
  • area: non-free
  • in suites: stretch
  • size: 541,932 kB
  • ctags: 32
  • sloc: xml: 267,963; sh: 101; python: 90; perl: 42; makefile: 13
file content (43 lines) | stat: -rw-r--r-- 1,068 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w

# Optimized ln that doesn't read the directory N thousand times

use strict;
use File::Path 'make_path';

my $tgdir = $ARGV[0];
die "Missing argument" unless defined($tgdir);

# read the existing rfcs
opendir(my $dh, '..') || die "Can't opendir ..: $!";
my @entries = readdir($dh);
closedir $dh;

# compute a hash rfc to target directory, to avoid list scanning later
open(my $fh, '<', "rfc-status.txt") or die $!;
my %equiv;
while(<$fh>) {
    my ($rfc, $dir) = split;
    $equiv{int($rfc)} = $dir;
}

# create needed dirs, once
my %paths;
foreach my $path (values %equiv) {
    $paths{$path} = 1;
}
foreach my $dir (keys %paths) {
    make_path("$tgdir/$dir");
}

# and now create the links, scanning the existing rfc list only once
foreach my $src (@entries) {
    if(! ($src =~ /^rfc(\d+)\./o)) {
        next;
    }
    my $rfcid = int($1);
    my $dir = $equiv{$rfcid};
    next unless(defined($dir));
    #print "will make link for $src ($rfcid) to '$tgdir/$dir'\n";
    link("../$src", "$tgdir/$dir/$src") || die "Can't create link: $!";
}