std.path.getName(): Screwy by design?

Jonathan M Davis jmdavisProg at gmx.com
Tue Mar 1 08:47:31 PST 2011


On Tuesday 01 March 2011 08:15:35 Lars T. Kyllingstad wrote:
> On Tue, 01 Mar 2011 11:07:15 -0500, Steven Schveighoffer wrote:
> > On Tue, 01 Mar 2011 11:04:52 -0500, Lars T. Kyllingstad
> > 
> > <public at kyllingen.nospamnet> wrote:
> >> On Tue, 01 Mar 2011 10:55:57 -0500, Steven Schveighoffer wrote:
> >>> The point of this whole discussion is how should phobos' std.path deal
> >>> with filenames.  I thought that was implied.
> >> 
> >> But std.path doesn't have to deal with these issues.  std.path is
> >> basically a bunch of functions that search for '/', '\' or '.' in some
> >> string.  The only special case it needs to worry about is that '\' is
> >> an ordinary character on POSIX and a dir separator on Windows.
> >> 
> >> If you want to disallow creating files named "-rf *", that'll have to
> >> be done in std.stdio and std.file.
> > 
> > Well, then that's probably where it should be disallowed then.  You are
> > right, path shouldn't care about the contents, because looking at a path
> > does not cause problem, it's only creating a file based on the given
> > path that causes problems.
> 
> std.path *could*, however, contain an isSafePath() function that checks
> whether a given path contains special characters.  And if we define
> "special characters" as "characters not allowed in Windows filenames" it
> would double as a Windows filename validator.

I previously suggested that we have a function which converts a file name so that 
it's valid on Windows (replacing invalid characters with valid ones), and IIRC 
Andrei was against it, because it was file system specific or somesuch. I 
definitely do that in at least one of my programs though. Another function that I 
find highly useful is one which escapes file names so that they're safe to use in 
a shell (the one I have wraps the file name in ' and then turn and 's in the file 
into '\''), but that could easily be shell-specific and might not be reasonable 
either. I do like the idea of both functions however. I use them all the time.

- Jonathan M Davis


More information about the Digitalmars-d mailing list