Version française
Home     About     Download     Resources     Contact us    
Browse thread
absolute_name ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] absolute_name ?
On Thu, 2007-08-02 at 16:36 +0200, Christophe Raffalli wrote:
> So my original code is not so bad:

yes it is:

> let absolute_name str =
>   let base = Filename.basename str in
>   let dir = Filename.dirname str in
>   let saved_dir = Sys.getcwd () in
>   try Sys.chdir dir;
>     let res = Filename.concat (Sys.getcwd ()) base in
>     Sys.chdir saved_dir;
>     res
>   with
>   | Sys_error _ -> str
> 

NEVER change the current directory in a program (unless the program
is specifically designed to do that, eg 'chdir' program .. :)

Doing so creates a side-effect, which will interfere with
multi-threading.

Even worse .. your error recovery routine forgets to reset
the current directory. You would have to be very sure that
the only failure possible is in the first Sys.chdir and it results
in Sys_error without changing the current directory ..

At least, you should write:

let absolute_name str =
  let base = Filename.basename str in
  let dir = Filename.dirname str in  
  let saved_dir = Sys.getcwd () in
  let res = 
    try Sys.chdir dir;
      `Some (Filename.concat (Sys.getcwd ()) base)
    with
    | Sys_error _ -> `Some str
    | x -> `Error x
  in
  begin try Sys.chdir saved_dir with _ -> () end;
  match res with
  | `Error x -> raise x
  | `Some filename -> filename

The guarantees the current directory is at least attempted to be
reset.

The result is still quite suspicious!

For a start Windows does not HAVE a current directory!
In fact it has a current directory for EVERY drive letter,
independently, and it isn't only possible for two letters
to map the same drive: it is stock standard practice to
do that with shared drives (a shared global letter and 
a local one).

Secondly, it doesn't account for LAN manager network mounts on Windows,
or just plain old mounts on Linux.

So what is the answer? There is one:

let absolute_name str = str

Simple. Two files are the same if, and only if, the user
spelled their names the same. You cannot solve the problem:
throw the responsibility back on the user.



-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net