rename and case insensitive filing systems

Discussion in 'Perl Misc' started by Jon Ripley, Mar 12, 2005.

  1. Jon Ripley

    Jon Ripley Guest

    I while back I wrote a perl script which recursed through a directory
    structure and tidied up any non-preferred file names. It used the standard
    rename (oldfile, newfile); approach and I discovered, to my horror, some
    very unexpected behaviour when it was run on an operating system with a case
    insensitive filing system.

    Where filename 'aaa_bbb.ccc.ccc' would be correctly renamed to 'Aaa Bcc.ccc'
    on all systems. For cases where only the case of a filename changed - as in
    'Aaa bbb.ccc' becoming 'Aaa Bbb.ccc', perl sucessfully deleted all the
    files.

    It seems that perl was doing:

    copy oldfile newfile
    delete oldfile

    which on a case-insensitive filesystem has the effect of simply deleting
    oldfile.

    Is this, as it seems to be, standard perl behaviour or is it possible that
    it is a piece of very sloppy programming in the particular port I am using?

    If copy/delete is standard then there is a *major* data loss causing bug in
    perl which needs to be resolved.

    (Currently I am manually doing rename old TMP; rename TMP new which is just
    a hacky workaround.)

    Jon Ripley
    --
    http://jonripley.com/
    Why use CGI::pM, if you can't write secure code then don't bother?
    Jon Ripley, Mar 12, 2005
    #1
    1. Advertising

  2. Jon Ripley

    John Bokma Guest

    Jon Ripley wrote:

    [ rename ]
    > It seems that perl was doing:
    >
    > copy oldfile newfile
    > delete oldfile
    >
    > which on a case-insensitive filesystem has the effect of simply
    > deleting oldfile.


    ls -al index.html
    -rw-rw-rw- 1 user group 0 Feb 1 2004 index.html

    perl -e "rename('index.html', 'InNdeX.html')"

    ls -al InNdeX.html
    -rw-rw-rw- 1 user group 0 Feb 1 2004 InNdeX.html

    Windows XP, perl, v5.8.3

    Which OS, version of Perl, etc.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Mar 12, 2005
    #2
    1. Advertising

  3. Jon Ripley

    Andy Hassall Guest

    On 12 Mar 2005 08:22:10 GMT, John Bokma <> wrote:

    >Jon Ripley wrote:
    >
    >[ rename ]
    >> It seems that perl was doing:
    >>
    >> copy oldfile newfile
    >> delete oldfile
    >>
    >> which on a case-insensitive filesystem has the effect of simply
    >> deleting oldfile.

    >
    >ls -al index.html
    >-rw-rw-rw- 1 user group 0 Feb 1 2004 index.html
    >
    >perl -e "rename('index.html', 'InNdeX.html')"


    But you've changed the filename now - there's more N's in the second one.

    >Windows XP, perl, v5.8.3


    G:\temp>dir /b index.html
    index.html

    G:\temp>perl -e "rename('index.html', 'InDeX.html')"

    G:\temp>dir /b index.html
    InDeX.html

    G:\temp>perl -v

    This is perl, v5.8.6 built for MSWin32-x86-multi-thread

    (On Windows 2000)

    --
    Andy Hassall / <> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
    Andy Hassall, Mar 12, 2005
    #3
  4. Jon Ripley wrote:

    > Where filename 'aaa_bbb.ccc.ccc' would be correctly renamed to 'Aaa
    > Bcc.ccc' on all systems. For cases where only the case of a filename
    > changed - as in 'Aaa bbb.ccc' becoming 'Aaa Bbb.ccc', perl sucessfully
    > deleted all the files.
    >
    > It seems that perl was doing:


    Perl was doing exactly what the docs say it will do:

    rename OLDNAME,NEWNAME
    Changes the name of a file; an existing file NEWNAME will be
    clobbered. Returns true for success, false otherwise.

    If you try to rename a file, Perl first looks to see if the new name already
    exists. If so, it *must* delete the file first to allow the rename to take
    place; if it doesn't the rename will fail, because you can't have two files
    with the same name in the same directory.

    > which on a case-insensitive filesystem has the effect of simply deleting
    > oldfile.


    Yes. If your OS doesn't distinguish between "newname" and "NEWNAME", then
    rename("newname", "NEWNAME") will delete the file. Perl checks to see if
    "NEWNAME" exists, sees that it does, and deletes it to make room for the
    rename.

    > If copy/delete is standard then there is a *major* data loss causing bug
    > in perl which needs to be resolved.


    Not a bad idea. I've been playing with the idea of diving into the perl
    source for some time now - this looks like a reasonably small patch to
    start with. I'll have a look at it. (No promises, though!)

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 12, 2005
    #4
  5. Jon Ripley

    Andy Hassall Guest

    On Sat, 12 Mar 2005 06:57:47 -0500, Sherm Pendley <> wrote:

    >Jon Ripley wrote:
    >
    >> Where filename 'aaa_bbb.ccc.ccc' would be correctly renamed to 'Aaa
    >> Bcc.ccc' on all systems. For cases where only the case of a filename
    >> changed - as in 'Aaa bbb.ccc' becoming 'Aaa Bbb.ccc', perl sucessfully
    >> deleted all the files.
    >>
    >> It seems that perl was doing:

    >
    >Perl was doing exactly what the docs say it will do:
    >
    > rename OLDNAME,NEWNAME
    > Changes the name of a file; an existing file NEWNAME will be
    > clobbered. Returns true for success, false otherwise.
    >
    >If you try to rename a file, Perl first looks to see if the new name already
    >exists. If so, it *must* delete the file first to allow the rename to take
    >place; if it doesn't the rename will fail, because you can't have two files
    >with the same name in the same directory.
    >
    >> which on a case-insensitive filesystem has the effect of simply deleting
    >> oldfile.

    >
    >Yes. If your OS doesn't distinguish between "newname" and "NEWNAME", then
    >rename("newname", "NEWNAME") will delete the file. Perl checks to see if
    >"NEWNAME" exists, sees that it does, and deletes it to make room for the
    >rename.
    >
    >> If copy/delete is standard then there is a *major* data loss causing bug
    >> in perl which needs to be resolved.

    >
    >Not a bad idea. I've been playing with the idea of diving into the perl
    >source for some time now - this looks like a reasonably small patch to
    >start with. I'll have a look at it. (No promises, though!)


    Looks like Perl 5.8.6 already tries to cope with this under Windows;
    $PERL_SRC/win32/win32.c lines 3071 onwards, it works differently depending on
    whether you're on an NT-based system (NT, 2000, XP, 2003) or a Windows 95-based
    system.

    The NT branch uses the Windows MoveFileEx function, which is supposed to
    handle case changes, whereas the 95 version does a bit of trickery with
    temporary files, e.g. see the comments:

    3072 /* if newname exists, rename it to a temporary name so that we
    3073 * don't delete it in case oname happens to be the same file
    3074 * (but perhaps accessed via a different path)
    3075 */

    Maybe the OP is on an older Perl, a different operating system or using a
    different filesystem, etc.? Perhaps running from Linux and accessing a
    case-insensitive filesystem over a Samba share, so the code from above wouldn't
    be applicable?

    --
    Andy Hassall / <> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
    Andy Hassall, Mar 12, 2005
    #5
  6. Jon Ripley

    Jon Ripley Guest

    John Bokma wrote:
    > Which OS, version of Perl, etc.


    RISC OS 4.02, Perl 5.005_03, ADFS file system.
    Jon Ripley, Mar 12, 2005
    #6
  7. Jon Ripley

    John Bokma Guest

    John Bokma, Mar 13, 2005
    #7
  8. Jon Ripley

    John Bokma Guest

    John Bokma, Mar 13, 2005
    #8
    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. Tee
    Replies:
    3
    Views:
    7,773
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  2. Replies:
    1
    Views:
    2,461
    Mark P
    Apr 6, 2007
  3. bb
    Replies:
    4
    Views:
    573
    Juha Nieminen
    May 19, 2007
  4. Mosfet
    Replies:
    22
    Views:
    1,708
    kwikius
    Jul 23, 2007
  5. Xah Lee
    Replies:
    4
    Views:
    940
Loading...

Share This Page