Subject: | "File exists" error in Windows |
I encountered this peculiar issue in a Windows server using 0.27,
although I haven't been able to duplicate it in other Windows servers. I
suspect that this may be a fairly rare issue, but I'm submitting a
suggested change to cover this case.
This particular server reported directories above the web root as not
existing (returned false for -d or -e on those directories). I've never
seen this before but I'm guessing it's some type of security measure.
In other words, even though -d is true here:
C:\path\to\webroot
... -d returns false here:
C:\path\to
When F::C::R needs to create a new directory, its behavior (via pathmk)
is to descend from the top of the directory tree, building the missing
directories as it goes. However, in this particular configuration, the
server misreports that the directories at the top of the tree do not
exist. pathmk tries to create the "missing" directories, but this throws
a "File exists" exception since the directory does in fact exist.
Weird, huh?
The following patch fixes the problem. Instead of looking for missing
directories from the top of the directory tree, it looks for them from
the bottom. Then, when it finds the first missing directory, it goes
down from there.
Here's my suggested replacement for pathmk:
sub pathmk {
my @parts = File::Spec->splitdir( shift() );
my $nofatal = shift;
my $pth;
#backup and find the last directory
my $base = $parts[0] ? 0 : 1;
my $zer = @parts - 1;
while ( $zer >= $base ) {
$pth = File::Spec->catdir( @parts[0 .. $zer] );
last if -d $pth;
$zer--;
}
$zer = $base if $zer < $base;
#go forward from last directory and build missing dirs
for ( $zer .. @parts - 1 ) {
$pth = File::Spec->catdir( @parts[0 .. $_] );
mkdir $pth or return if !-d $pth && !$nofatal;
mkdir $pth if !-d $pth && $nofatal;
}
1;
}
Thanks once again for a very useful module.
All best,
Josh Clark