File: urlencode-fixer.pl

package info (click to toggle)
apt-cacher-ng 0.7.27-1~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 1,740 kB
  • sloc: cpp: 13,987; sh: 519; perl: 442; ansic: 414; makefile: 77
file content (72 lines) | stat: -rwxr-xr-x 1,633 bytes parent folder | download | duplicates (17)
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
#!/usr/bin/perl
use strict;

my $fd;
my $path=shift(@ARGV);
my $dryrun=0;

if ($path eq "-n")
{
   $dryrun=1;
   $path=shift(@ARGV);
}

if (! $path)
{
   die "Usage: $0 [ -n ] directory

Converts some URL encoded filenames to their native version, attempts to
merge directories in the new location if conflicts exist.

WARNING: use with care! Create a backup of your data, at least a tree of
hard links (\"cp -la /var/cache/apt-cacher-ng backupDir\").

Option(s):
   -n     Dry run, only search and report suggested actions
";
}

if(open $fd, "-|", "find", $path, "-type", "f", "-print0")
{
   local $/="\0";
   my @paths=<$fd>;
   for(@paths)
   {
      next if( ! /%(7|5)(b|d)/i);

      s/\0//;
      #print "$_\n";
      my $origpath=$_;
      my @comps=split("/", $_);
      next if(!@comps);

      my $dirnew;
      for(my $i=0; $i<=$#comps; $i++)
      {
         my $part=$comps[$i];
         my $hits=0;

         $hits += $part=~s/%7E/~/g;
         $hits += $part=~s/%5B/]/g;
         $hits += $part=~s/%5D/[/g;

         #print "i: $i, $part\n";
         if($i<$#comps)
         {
            #print "I: is dir\n";
            $dirnew.="/" if defined($dirnew);
            $dirnew.=$part;
            mkdir $dirnew if !$dryrun; # and if it already exists, don't care...
            #print "md: $dirnew\n";
            die "Cannot create target directory: $dirnew\n" if( ! -d $dirnew && ! $dryrun);
         }
         else
         {
            #print "I: is file\n";
            my $tgt="$dirnew/$part";
            print "$origpath -> $tgt\n";
            rename $origpath, $tgt if !$dryrun;
         }
      }
   }
}