hxtools/sadmin/filenameconv

76 lines
1.3 KiB
Perl
Executable File

#!/usr/bin/perl
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2004-2007 Jan Engelhardt
#
# filenameconv -
# convert filenames on the whole filesystem between two charsets.
# written by Jan Engelhardt, 2004-2007
use Cwd;
use Getopt::Long;
use IPC::Open2;
use strict;
my($FROM, $TO, $YES, $Depth);
&Getopt::Long::Configure(qw(bundling pass_through));
&GetOptions("f=s" => \$FROM, "t=s" => \$TO, "Y" => \$YES);
if ($FROM eq "" || $TO eq "") {
die "You need to specify both -f and -t\n";
}
$SIG{CHLD} = \&REAPER;
&convert(@ARGV);
sub REAPER ()
{
wait();
$SIG{CHLD} = \&REAPER;
return;
}
sub convert ()
{
local(*RD, *WD);
foreach my $src (@_) {
my $pid = open2(\*RD, \*WD, "iconv", "-f", $FROM, "-t", $TO);
if (!defined($pid)) {
print STDERR "Error starting conversion program!\n";
exit 1;
}
print WD $src;
close WD;
my $dest = join("", <RD>);
if (!-l $src && -d _) {
&dive($src);
}
if ($src eq $dest) {
next;
}
if ($YES) {
rename $src, $dest;
}
print "\tProcessing \"$src\" => \"$dest\"\n";
}
return;
}
sub dive ()
{
my $dir = shift @_;
my $prev = getcwd();
++$Depth;
print "[$Depth] Entering \"$prev/$dir\"\n";
chdir($dir) || return undef;
&convert(glob("*"));
chdir("..") || die "Got a problem chdir'ing back\n";
print "[$Depth] Leaving \"$prev/$dir\"\n";
--$Depth;
return;
}