Debian Codegraph @

sadmin/filenameconv

raw
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: MIT
3 # SPDX-FileCopyrightText: 2004-2007 Jan Engelhardt
4 #
5 # filenameconv -
6 # convert filenames on the whole filesystem between two charsets.
7 # written by Jan Engelhardt, 2004-2007
8
9 use Cwd;
10 use Getopt::Long;
11 use IPC::Open2;
12 use strict;
13
14 my($FROM, $TO, $YES, $Depth);
15 &Getopt::Long::Configure(qw(bundling pass_through));
16 &GetOptions("f=s" => \$FROM, "t=s" => \$TO, "Y" => \$YES);
17
18 if ($FROM eq "" || $TO eq "") {
19 die "You need to specify both -f and -t\n";
20 }
21
22 $SIG{CHLD} = \&REAPER;
23 &convert(@ARGV);
24
25 sub REAPER ()
26 {
27 wait();
28 $SIG{CHLD} = \&REAPER;
29 return;
30 }
31
32 sub convert ()
33 {
34 local(*RD, *WD);
35
36 foreach my $src (@_) {
37 my $pid = open2(\*RD, \*WD, "iconv", "-f", $FROM, "-t", $TO);
38 if (!defined($pid)) {
39 print STDERR "Error starting conversion program!\n";
40 exit 1;
41 }
42
43 print WD $src;
44 close WD;
45
46 my $dest = join("", <RD>);
47 if (!-l $src && -d _) {
48 &dive($src);
49 }
50 if ($src eq $dest) {
51 next;
52 }
53 if ($YES) {
54 rename $src, $dest;
55 }
56 print "\tProcessing \"$src\" => \"$dest\"\n";
57 }
58 return;
59 }
60
61 sub dive ()
62 {
63 my $dir = shift @_;
64 my $prev = getcwd();
65
66 ++$Depth;
67 print "[$Depth] Entering \"$prev/$dir\"\n";
68 chdir($dir) || return undef;
69 &convert(glob("*"));
70 chdir("..") || die "Got a problem chdir'ing back\n";
71
72 print "[$Depth] Leaving \"$prev/$dir\"\n";
73 --$Depth;
74 return;
75 }