Changes to the semantics of basename/dirname?

A

Austin Ziegler

There are some issues with the way that File.basename and
File.dirname work, at least on Windows. I also have an issue with
File.expand_path related to some of the issues of File.basename and
File.dirname. This may be present in more than just these, but these
are what I've seen over the past few weeks.

The first problem is that basename, dirname, and expand_path convert
\\ to /. This shouldn't be problematic in the general case (where
the path is then used in a Ruby command), but it would be useful to
indicate that conversion should not be done.

# Current, invalid behaviour
p = "a\\b\\c\\d.txt"
basename(p) #=> "d.txt"
p1 = dirname(p) #=> "a/b/c" (should be "a\\b\\c")
basename(p1) #=> "c"
p2 = dirname(p1) #=> "a/b" (should be "a\\b")
basename(p2) #=> "b"
p3 = dirname(p2) #=> "a"
basename(p3) #=> "a"
p4 = dirname(p3) #=> "."
basename(p4) #=> "."
dirname(p4) #=> "."

p = "\\a\\b\\c\\d.txt"
basename(p) #=> "d.txt"
p1 = dirname(p) #=> "/a/b/c" (should be "\\a\\b\\c")
basename(p1) #=> "c"
p2 = dirname(p1) #=> "/a/b" (should be "\\a\\b")
basename(p2) #=> "b"
p3 = dirname(p2) #=> "/a" (should be "\\a")
basename(p3) #=> "a"
p4 = dirname(p3) #=> "/" (should be "\\")
basename(p4) #=> "/" (should be "\\")
dirname(p4) #=> "/" (should be "\\")

The second has to do with UNC/SMB share names.

p = "\\\\host\\a\\b\\c\\d.txt"
basename(p) #=> "d.txt"
p1 = dirname(p) #=> "\\\\host\\a\\b\\c"
basename(p1) #=> "c"
p2 = dirname(p1) #=> "\\\\host\\a\\b"
basename(p2) #=> "b"
p3 = dirname(p2) #=> "\\\\host\\a"
basename(p3) #=> "a" (should be "\\\\host\\a")
p4 = dirname(p3) #=> "\\\\host" (should be "\\\\host\\a")
basename(p4) #=> "host" (should be "\\\\host\\a")
dirname(p4) #=> "\\\\" (should be "\\\\host\\a")

In this specification, the base directory (the "device", if you
will), is \\host\a, not \\ or \\host. Just as basename can never go
above "/" in Unix, it should never go above \\hostname\sharename on
Windows UNC shares.

I'm not sure if URI special cases should be built in, but
ftp://ruby-lang.org/pub/ruby would ultimately become "ftp:", which
isn't desired -- but if it doesn't do that, then NetWare device
support ("SYS1:\a\b\c\d.txt") would probably break.

-austin
 
R

Robert Klemme

Austin Ziegler said:
There are some issues with the way that File.basename and
File.dirname work, at least on Windows. I also have an issue with
File.expand_path related to some of the issues of File.basename and
File.dirname. This may be present in more than just these, but these
are what I've seen over the past few weeks.

The first problem is that basename, dirname, and expand_path convert
\\ to /. This shouldn't be problematic in the general case (where
the path is then used in a Ruby command), but it would be useful to
indicate that conversion should not be done.

What Ruby are you using? AFAIK windows compiled Ruby handles that according
to Win conventions. Cygwin ruby handles it according to *nix style. But
then there is cygpath...

Regards

robert
 
A

Austin Ziegler

What Ruby are you using? AFAIK windows compiled Ruby handles that according
to Win conventions. Cygwin ruby handles it according to *nix style. But
then there is cygpath...

irb(main):003:0> [RUBY_PLATFORM, RUBY_VERSION, RUBY_RELEASE_DATE].join(" ")
=> "i386-mswin32 1.8.2 2004-07-29"

Ruby converts \ to / and doesn't handle UNC names well at all.

-austin
 
R

Robert Klemme

Austin Ziegler said:
What Ruby are you using? AFAIK windows compiled Ruby handles that according
to Win conventions. Cygwin ruby handles it according to *nix style. But
then there is cygpath...

irb(main):003:0> [RUBY_PLATFORM, RUBY_VERSION, RUBY_RELEASE_DATE].join(" ")
=> "i386-mswin32 1.8.2 2004-07-29"

Ruby converts \ to / and doesn't handle UNC names well at all.

Darn! I was sure my old 1.6.8 did at least handle "\"... Maybe it has
changed or my memory fooled me. Sorry for the noise.

Regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top