Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#32 unlink incorrectly returning success for locked files

closed-fixed
Olaf Barthel
None
5
2007-01-18
2006-08-24
Andy Broad
No

I've just found that the unistd function unlink will return success (0) when attempting to delete a file that is locked by another program.

This only seems to occur when linked with libunix.a

Take this example

#include <stdio.h>
#include <unistd.h>

int main ()
{
printf("%d\n",unlink("ram:test"));
printf("%d\n",unlink("/ram/test"));

}

compile with

> gcc -o unixy test.c -lunix
> gcc -o amigay test.c

> echo >ram:test "fooo";
> amigay
0
-1
>echo >ram:test "fooo";
>unixy
0
-1
>echo >ram:test "fooo";
[ open ram:test in multiview so that the file is locked an undeletable]

>amigay
-1
-1
>unixy
0
0
> list ram:test
Directory "ram:" on Thursday 24-Aug-06
test 5 ----rwed Today 12:40:48
1 file - 5 bytes - 2 blocks used

As you can see the "amiagy" returns the correct -1 result as the file could not be deleted.
But the "unixy" compile returns 0 (success) even the the file is not deleted, it doesn't matter if te file path has amiga or unix syntax.

andy@broad.ology.org.uk

Discussion

  • Andy Broad
    Andy Broad
    2006-08-24

    Logged In: YES
    user_id=1163218

    I forgot to mention that the unixy version returns the correct result (-1 failure) if the delete fails because file not found. It;s only if the file exists and delation fails that the error occurs.

     
  • Olaf Barthel
    Olaf Barthel
    2006-09-05

    • assigned_to: nobody --> obarthel
     
  • Olaf Barthel
    Olaf Barthel
    2006-09-05

    Logged In: YES
    user_id=1089675

    This is intentional for the Unix library version, since it's
    used in the Samba port. The Samba port keeps a bunch of
    files open and expects the unlink() operation to happen
    later when the files are closed. This is why the unlink
    pretends to work, and tags an open file for cleanup later.
    The cleanup may happen when the file is closed or the
    program exits.

    I'll have to look if this isn't working as intended.

     
  • Andy Broad
    Andy Broad
    2006-09-06

    Logged In: YES
    user_id=1163218

    Yes, I did some research, and now understand that the unix version of unlink() will cause files in use by another program / process to be deleted later. Later being on release of the file.

    So this is partly my misunderstanding of unlink().

    However under unix I guess that this unlinking is handled by the "kernal". But in this case it's handled by the program calling unlink (via the clib2 destructors?) . Understandably as AmigaOS doesn't behave like this.
    The issue then occurs if the unlinking program exits before the program with the file lock. The file will never get deleted.

    This is effectively what was happening to me, my apod perl script was calling "unlink" on a mp3 file locked by Tunenet (as a external player) apparently succeding and so updateing the iPod database incorrectly.
    There are ways I can work arround this, seperate from the unlink issue, but it illustrates the probelem quite well.

     
  • Olaf Barthel
    Olaf Barthel
    2006-09-27

    Logged In: YES
    user_id=1089675

    I have added a switch (see <dos.h>) to the current CVS
    version of clib2 which controls whether or not unlink() and
    remove() pretend that deletion has succeeded. This will be a
    feature in the next library release :-)

     
  • Olaf Barthel
    Olaf Barthel
    2006-11-13

    • status: open --> open-fixed
     
  • Olaf Barthel
    Olaf Barthel
    2007-01-18

    • status: open-fixed --> closed-fixed