caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Christophe Raffalli <Christophe.Raffalli@univ-savoie.fr>
Cc: "Correnson Loïc" <Loic.Correnson@trusted-labs.fr>, caml-list@inria.fr
Subject: Re: [Caml-list] absolute_name ?
Date: Fri, 03 Aug 2007 02:21:04 +1000	[thread overview]
Message-ID: <1186071664.6017.50.camel@rosella.wigram> (raw)
In-Reply-To: <46B1EC05.9060508@univ-savoie.fr>

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


      reply	other threads:[~2007-08-02 16:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-20 16:55 Christophe Raffalli
2007-07-20 21:23 ` Sylvain Le Gall
     [not found] ` <46B1BCD1.9030004@trusted-labs.fr>
2007-08-02 14:36   ` [Caml-list] " Christophe Raffalli
2007-08-02 16:21     ` skaller [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1186071664.6017.50.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=Christophe.Raffalli@univ-savoie.fr \
    --cc=Loic.Correnson@trusted-labs.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).