File exists on Mac OS X

Discussion in 'Perl Misc' started by Peter Hickman, Jul 12, 2004.

  1. I have a generic file renaming code that lowercases the filename but before it
    does the renaming it checks if the lowercased version of the filename already
    exists. This works fine on Linux however on Max OS X the filesystem is case
    agnostic and so if the file ZXC exists then perl will report that zxc, zxC, zXc,
    zXC, Zxc, ZxC and ZXc all also exist even if they don't.

    Is there a way to keep my code portable but not get this problem?
    Peter Hickman, Jul 12, 2004
    #1
    1. Advertising

  2. Peter Hickman

    Anno Siegel Guest

    Peter Hickman <> wrote in comp.lang.perl.misc:
    > I have a generic file renaming code that lowercases the filename but before it
    > does the renaming it checks if the lowercased version of the filename already
    > exists. This works fine on Linux however on Max OS X the filesystem is case
    > agnostic


    Not necessarily, but usually it is.

    > and so if the file ZXC exists then perl will report that zxc,
    > zxC, zXc,
    > zXC, Zxc, ZxC and ZXc all also exist even if they don't.


    It's worse than that. rename() will fail on a case-insensitive system
    when the only difference in the new name is in case.

    > Is there a way to keep my code portable but not get this problem?


    You need an intermediate name (or an auxiliary directory) to do this
    kind of renaming. Look at File::Temp for a reasonably safe way of
    creating unique file names.

    Anno
    Anno Siegel, Jul 12, 2004
    #2
    1. Advertising

  3. Anno Siegel wrote:
    > It's worse than that. rename() will fail on a case-insensitive system
    > when the only difference in the new name is in case.
    >
    >
    >>Is there a way to keep my code portable but not get this problem?

    >
    >
    > You need an intermediate name (or an auxiliary directory) to do this
    > kind of renaming. Look at File::Temp for a reasonably safe way of
    > creating unique file names.
    >
    > Anno


    Thanks for the information, I will look into File::Temp, it seems a mite
    wasteful but if it works who cares.
    Peter Hickman, Jul 12, 2004
    #3
  4. Peter Hickman

    Bo Lindbergh Guest

    In article <cctqlk$14e$-Berlin.DE>,
    -berlin.de (Anno Siegel) wrote:

    > It's worse than that. rename() will fail on a case-insensitive system
    > when the only difference in the new name is in case.


    On what version of what OS? It works just fine on Mac OS X 10.3.4.

    One possible approach is to lstat the old and new names and compare
    the device and inode numbers for equality. If they match, you have
    either two links to the same file or two case variants of the same
    name; in either case, a rename would be non-destructive.

    =cut

    use Errno qw(EEXIST);

    sub safer_rename ($$)
    {
    my($oldname,$newname)=@_;
    my($olddev,$oldino,$newdev,$newino);

    ($olddev,$oldino) = lstat($oldname) or return;
    if (($newdev,$newino) = lstat($newname)) {
    if ($newdev!=$olddev || $oldino!=$newino) {
    $! = EEXIST;
    return;
    }
    }
    return rename($oldname,$newname);
    }
    Bo Lindbergh, Jul 13, 2004
    #4
  5. Bo Lindbergh wrote:
    >>It's worse than that. rename() will fail on a case-insensitive system
    >>when the only difference in the new name is in case.

    >
    > On what version of what OS? It works just fine on Mac OS X 10.3.4.


    The problem , for me at least, is that I don't want to rename a file that does
    not require renaming if a file with the lowercase version of the filename
    already exists. However if I have ZXC as a file and do an -e zxc it will match
    the ZXC and claim the file exists. Hence no renaming.

    I'm going for the intermediate temporary file approach at the moment.
    Peter Hickman, Jul 13, 2004
    #5
  6. Peter Hickman

    Anno Siegel Guest

    Bo Lindbergh <> wrote in comp.lang.perl.misc:
    > In article <cctqlk$14e$-Berlin.DE>,
    > -berlin.de (Anno Siegel) wrote:
    >
    > > It's worse than that. rename() will fail on a case-insensitive system
    > > when the only difference in the new name is in case.

    >
    > On what version of what OS? It works just fine on Mac OS X 10.3.4.


    Indeed it does. That wasn't always the case, but I can't pinpoint
    the version when it changed.

    Anno
    Anno Siegel, Jul 13, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad Crowder
    Replies:
    7
    Views:
    21,534
    hemant.yadav
    Oct 24, 2009
  2. Totan
    Replies:
    0
    Views:
    937
    Totan
    Apr 17, 2006
  3. Max
    Replies:
    4
    Views:
    10,923
    Kevin Spencer
    Aug 11, 2006
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,141
  5. Help Needed
    Replies:
    1
    Views:
    269
    Jason M. Murray
    Sep 19, 2003
Loading...

Share This Page