You probably just want to run the results through $self->canonpath() rather
than redoing all that code yourself.
catpath() should do that for you now, so the patch below should be
redundant. Try rel2abs in the latest development snapshot from
ftp://ftp.funet.fi/pub/languages/perl/snap/
> --- Win32.pm.orig 2001-09-04 15:24:00.000000000 +0400
> +++ Win32.pm 2002-09-27 07:46:46.000000000 +0400
> @@ -334,9 +334,22 @@
> my ( $base_volume, $base_directories ) =
> $self->splitpath( $base, 1 ) ;
>
> + my @path_directories = $self->splitdir($path_directories);
> + my @base_directories = $self->splitdir($base_directories);
> +
> + # for each .. in @path_directories pop one item from
> + # @base_directories
> + while (my $dir = shift @path_directories){
> + unless ($dir eq $self->updir){
> + unshift @path_directories, $dir;
> + last;
> + }
> + pop @base_directories;
> + }
> +
> $path = $self->catpath(
> $base_volume,
> - $self->catdir( $base_directories, $path_directories ),
> + $self->catdir( @base_directories, @path_directories ),
> $path_file
> ) ;
> }
>
>
--
Michael G. Schwern <sch...@pobox.com> http://www.pobox.com/~schwern/
Perl Quality Assurance <per...@perl.org> Kwalitee Is Job One
"His plagiarism was limited only by this faulty technique."
-- Peter Schickele
Michael G Schwern wrote:
>
> On Wed, Oct 02, 2002 at 01:04:57PM +0400, Information Service wrote:
> > File::Spec::Win32::rel2abs('../../dir') returns
> >
> > cwd/../../dir
> >
> > which is IMHO wrong as this result is good for shell only and is not
> > good for other uses. AFAIK, this is intended behavior in 5.6.1 & 5.8.0
> > according to filespec.t. Below is the patch to File::Spec::Win32.pm of
> > 5.6.1.
>
> You probably just want to run the results through $self->canonpath() rather
> than redoing all that code yourself.
here is what $self->canonpath() does, in a sense:
$path =~ s|([^\\])\\+|$1\\|g; # xx\\\\xx -> xx\xx
$path =~ s|(\\\.)+\\|\\|g; # xx\.\.\xx -> xx\xx
$path =~ s|^(\.\\)+||s unless $path eq ".\\"; # .\xx -> xx
$path =~ s|\\\Z(?!\n)||
unless $path =~ m#^([A-Z]:)?\\\Z(?!\n)#s; # xx\ ->
xx
what I need:
given
../../dir
and cwd like /dir1/dir2/dir3
to have
/dir1/dir
rather than
/dir1/dir2/dir3/../../dir
which is now the case.
Ok, I see what you mean, but canonpath() should be extended rather than
rel2abs() to do the .. cleanup. That way, all the methods get the benefit.
--
Michael G. Schwern <sch...@pobox.com> http://www.pobox.com/~schwern/
Perl Quality Assurance <per...@perl.org> Kwalitee Is Job One
"In my day, we didn't have none of yer new-fangled token-pasting
operators! We stuck the tokens together with our BARE HANDS and we
LIKED IT."
-- Mark-Jason Dominus fondly remembers perl 1.0
in <2002030412132...@plover.com>
Michael G Schwern wrote:
> Ok, I see what you mean, but canonpath() should be extended rather than
> rel2abs() to do the .. cleanup. That way, all the methods get the > benefit.
Sounds good to me. I would make a patch against
File::Spec::Win32::canonpath() in the development snapshot you've
mentioned (ftp://ftp.funet.fi/pub/languages/perl/snap/, I've downloaded
and installed it) but without using s///. Should I? What else whould I
do?
---
Ruslan Shvedov <rus...@lingo.kiev.ua>
Lingo Translation Agency, Translator/IT Consultant
*shrug* Whatever works and works fast. If you're not comfortable with s///
and want to do it with splitpath and arrays, go ahead. If someone doesn't
like it, they'll patch it just like you did. :)
--
Michael G. Schwern <sch...@pobox.com> http://www.pobox.com/~schwern/
Perl Quality Assurance <per...@perl.org> Kwalitee Is Job One
I knew right away that my sock and your eyebrows could be best friends.
Michael G Schwern wrote:
>
> On Thu, Oct 03, 2002 at 12:05:56PM +0400, Information Service wrote:
> > Michael G Schwern wrote:
> > > Ok, I see what you mean, but canonpath() should be extended rather than
> > > rel2abs() to do the .. cleanup. That way, all the methods get the > benefit.
> >
> > Sounds good to me. I would make a patch against
> > File::Spec::Win32::canonpath() in the development snapshot you've
> > mentioned (ftp://ftp.funet.fi/pub/languages/perl/snap/, I've downloaded
> > and installed it) but without using s///. Should I? What else whould I
> > do?
>
> *shrug* Whatever works and works fast. If you're not comfortable with s///
> and want to do it with splitpath and arrays, go ahead. If someone doesn't
> like it, they'll patch it just like you did. :)
So, here are the diff -u patches to
lib/File/Spec/Win32.pm (Win32.pm.canonpath.patch)
lib/File/Spec/t/Spec.t (Spec.t.canonpath.patch)
against
ftp://ftp.funet.fi/pub/languages/perl/snap/pe...@17882.tbz
Bug:
on Win32,
given
dir1/dir2/dir3/../../dir4
File::Spec::Win32::canonpath does not clean up it to
dir1/dir4
which would be IMHO a good thing.
--
Ruslan Shvedov, rus...@lingo.kiev.ua
I vote against these patches, agreed that File::Spec::Win32::canonpath()
didn't DWIM, but it didn't give the *wrong* answer, these patches make it do
that in the case of UNC-paths.
> --- Spec.t.orig 2002-10-04 13:32:28.000000000 +0400
> +++ Spec.t.patched 2002-10-04 15:57:24.000000000 +0400
> @@ -202,8 +202,9 @@
> [ "Win32->canonpath('////')", '\\\\\\' ],
> [ "Win32->canonpath('//')", '\\' ],
> [ "Win32->canonpath('/.')", '\\.' ],
> -[ "Win32->canonpath('//a/b/../../c')", '\\\\a\\b\\..\\..\\c' ],
> -[ "Win32->canonpath('//a/../../c')", '\\\\a\\..\\..\\c' ],
> +[ "Win32->canonpath('//a/b/../../c')", '\\\\c' ],
if that is an UNC-path, the answer is wrong, Windows translates it as:
//a/b/../../c => \\a\b\c
Windows doesn't allow you to change shares (and server) via UpDir. splitpath()
does the right thing for separating $volume and $directory, perhaps
canonpath() should use that $directory.
> +[ "Win32->canonpath('//a/../../c')", '\\c' ],
That should be an invalid UNC-path, leave it as-is.
> +[ "Win32->canonpath('//a/b/c/../../d')", '\\\\a\\d' ],
Which (as UNC-path) translates:
//a/b/c/../../d => \\a\b\d
good luck,
Abe
--
Amsterdam Perl Mongers http://amsterdam.pm.org
perl -wle '$_=q.dtr aJfagne wexrshuctahovkjhcskem nPoeprlritqlbu.;
print@{{split//}}{"a".."x"}'
Regards,
Ruslan
I would like it to work for both UNC-paths _and_ normal paths. Can't you use
the fact that splitpath() gives you the right directory to canonicalise?
I just found that Win32::GetFullPathName( filename ) does the right thing, but
is only available for $^O eq 'MSWin32' (you won't need lib-win32 for it, but
it won't work crossplatform)
good luck,
Abe
--
Amsterdam Perl Mongers http://amsterdam.pm.org
perl -wle '$_=q@Just\@another\@Perl\@hacker@;print qq@\@{[split/\@/]}@'
Abe Timmerman wrote:
>
> Op wereld dierendag (Friday 04 October 2002 16:06), schreef Information
> Service:
>
> > Abe Timmerman wrote:
> > > I vote against these patches, agreed that File::Spec::Win32::canonpath()
> > > didn't DWIM, but it didn't give the *wrong* answer, these patches make it
> > > do that in the case of UNC-paths.
> >
> > well, I could easily leave UNC-paths as is by checking for double slash
> > in the beginning. That given, will the patches look better for you?
>
> I would like it to work for both UNC-paths _and_ normal paths. Can't you use
> the fact that splitpath() gives you the right directory to canonicalise?
You're right. I will try.
>
> I just found that Win32::GetFullPathName( filename ) does the right thing, but
Depends upon what you call the right thing :). .. cleanup or
\\node\share sanity?
> is only available for $^O eq 'MSWin32' (you won't need lib-win32 for it, > but it won't work crossplatform)
Patching File::Spec::Win32 do I need to be crossplatform? The problem is
that notorious Win98 command.com does wrong things with
dir1\dir2\..\..\dir3.
Originally I needed rel2abs() to return path with .. cleaned up because
I later used it to store file information. I patched rel2abs() but
Michael Schwern hinted me to patch canonpath() so that other functions
used the feature, which I'm doing.
Take care,
Ruslan
> Abe Timmerman wrote:
> > Op wereld dierendag (Friday 04 October 2002 16:06), schreef Information
> >
> > Service:
> > > Abe Timmerman wrote:
> > > > I vote against these patches, agreed that
> > > > File::Spec::Win32::canonpath() didn't DWIM, but it didn't give the
> > > > *wrong* answer, these patches make it do that in the case of
> > > > UNC-paths.
> > >
> > > well, I could easily leave UNC-paths as is by checking for double slash
> > > in the beginning. That given, will the patches look better for you?
> >
> > I would like it to work for both UNC-paths _and_ normal paths. Can't you
> > use the fact that splitpath() gives you the right directory to
> > canonicalise?
>
> You're right. I will try.
>
> > I just found that Win32::GetFullPathName( filename ) does the right
> > thing, but
>
> Depends upon what you call the right thing :). .. cleanup or
> \\node\share sanity?
If I understand the code right:
Both, it deals with $volume, $directory separation and makes Windows's
FindFirstFile() do the '.' and '..' interpolation.
See: win32/win32.c::win32_longpath()
> > is only available for $^O eq 'MSWin32' (you won't need lib-win32 for it,
> > > but it won't work crossplatform)
>
> Patching File::Spec::Win32 do I need to be crossplatform?
IMO, yes! File::Spec::Win32 should be able to produce a Win32 canonical path
on _any_ platform (and you have nearly made it do so :)
> The problem is
> that notorious Win98 command.com does wrong things with
> dir1\dir2\..\..\dir3.
Hmmm, that would seem - even in M$ terms - a bug in command.com (Sorry I can't
verify this, I don't do toy-windows anymore)
> Originally I needed rel2abs() to return path with .. cleaned up because
> I later used it to store file information. I patched rel2abs() but
> Michael Schwern hinted me to patch canonpath() so that other functions
> used the feature, which I'm doing.
I agree with Schwern, the problem is most generally solved in canonpath().
I also agree with the intent of your patch.
The only thing I objected to, was returning potentially "invalid" UNC-paths
(where the answer used to be a "valid" path, althoug not canonicalised) [for
certain values of "valid" and "invalid"]
good luck,
Abe
--
Amsterdam Perl Mongers http://amsterdam.pm.org
perl -we '$_="Just another Perl hacker";print$1while/(.)/g'
We need to be a little careful where we put this though the simple prune
_may_ suffice on Win32 - but is not safely correct on a file system with
symlinks (I think WinNT+ on NTFS can do those to some extent).
--
Nick Ing-Simmons
http://www.ni-s.u-net.com/
If possible. Some of us manipulated Win32 pathnames on Linux
(and vice versa).
Ruslan