On Fri Nov 22 16:26:52 2013, MJD wrote:
Show quoted text> I suppose I'm probably missing some important edge case, and I
> recognize that this won't work on VMF, MS-DOS, etc., but I have not
> been able to find anything wrong with the following implementation:
>
> sub is_upwards {
> my ($path) = @_;
> grep $_ eq "..", split m{/}, $path;
> }
>
> sub no_upwards { grep ! is_upwards($_), @_ }
The question I have is what the intended behavior is? Eliminate any path with any parent directory component? Or eliminate paths that go above the root? E.g. ".." is bad; "./foo/bar/.." is OK because it refers to "./foo" (ignoring symlinks at least).
If *any* parent directory component is bad, then I think is_upwards has to be more generic like this:
sub is_upwards {
my ($path) = @_;
my ($vol, $dir) = File::Spec->splitpath($path, 1);
my $updir = File::Spec->updir;
return ! scalar grep { $_ eq $updir } File::Spec->splitdir($dir);
}
That would allow "." (which isn't a parent directory anyway), but I don't think that's a real problem.
That might not be sufficient if a platform has multiple representations for a parent directory (VMS?) E.g. apparently some versions of Windows might treat "..." as "..\..".