[Mono-devel-list] Handling GetFullPath on Linux

Jonathan Gilbert 2a5gjx302 at sneakemail.com
Mon Nov 22 05:01:51 EST 2004

At 10:25 AM 22/11/2004 +0100, Jeroen wrote:
>Jonathan Gilbert wrote:
>> >This doesn't make sense. DirectorySeparatorChar and
>> >AltDirectorySeparatorChar should both be '/' on Linux. 
>> >Applications that hardcode a backslash are lame.
>> That doesn't stop people from writing them.
>Apps that can't even get this simple thing right are unlikely to work on
>Linux anyway. If Java taught us anything, it's that
>write-once-run-anywhere is doesn't work.

True enough.

>> >> I am not sure what to do about this. Which is more 
>> >> important, allowing UNIX escapes, or keeping compat?
>> >
>> >Surely allowing UNIX escapes is more important?
>> Aren't escape codes the job of the shell?
>Hmm. I didn't know that, but that would make sense. In any case, it
>would still be legal to use a backslash in a filename on Linux, by
>converting backslashes into slashes, that would be broken.

Path.GetFullPath() actually interacts with the underlying filesystem,
though, so the problem becomes more one of finding the closest
logically-consistent *existing* path leading up to the specified file. I
suggest that it would be best to first try for a path separated by '/'s
only. If that fails to produce ANY matching path components, try separating
by '\'s as well. If that DOES produce matching components, then use that
interpretation (i.e., translate the '\'s into '/'s for File.Open and such).
If it does not, however, I think it'd be best to make no assumptions about
the quality of the program and simply leave the string alone (that is,
append it to the current path as a relative subpath, using only the
platform's standard directory separator char(s)).

In pseudocode, this might be expressed as:

public static string GetFullPath(string path)
  if ('path' does not contain at least one existing component
      when only '/' is a separator)
    if ('path' *does* contain at least one existing component
        when both '/' and '\' are separators)
      return GetFullPath(path.Replace(@"\", "/");
    // else flow into the rest of the code

  attach 'path' onto current working directory;

  return the resulting absolute path;

Jonathan Gilbert

More information about the Mono-devel-list mailing list