develooper Front page | perl.perl5.porters | Postings from October 2005

Inconsistent behaviour when removing files on Cygwin

Thread Next
From:
=?iso-8859-1?b?U+liYXN0aWVu?= Aperghis-Tramoni
Date:
October 26, 2005 08:28
Subject:
Inconsistent behaviour when removing files on Cygwin
Message ID:
1130340510.435fa09e7ca06@imp5-g19.free.fr
Hello,

While correcting a bug in Net::Pcap Makefile.PL with a Windows developer,
we stumbled upon the strange behaviour of the unlink() function under
Cygwin. It looks like it tries to emulates the Unix behaviour but fails
to achieve it.

    # create a file
    my $file = 'test.txt';
    open(FILE, '>', $file) or die "can't write '$file': $!\n";

    # then try to unlink it
    unlink($file) or warn "can't unlink '$file': $!\n";

    # check whether it's still present
    print "'$file' ", (-f $file ? "does" : "doesn't"),
      " exist on the filesystem\n";

    # check whether it's listed
    opendir(DIR, '.');
    print "in dir: ", (grep {/$file/} readdir(DIR)), $/;
    closedir(DIR);

    # close the file and check again
    close(FILE);
    print "'$file' ", (-f $file ? "does" : "doesn't"),
      " exist on the filesystem\n";

    opendir(DIR, '.');
    print "in dir: ", (grep {/$file/} readdir(DIR)), $/;
    closedir(DIR);

When executed under a random Unix/Unix-like (here on Linux), it outputs:

    'test.txt' doesn't exist on the filesystem
    in dir:
    'test.txt' doesn't exist on the filesystem
    in dir:

which is what I expect. Now when executed on Cygwin, here is the output:

    'test.txt' doesn't exist on the filesystem
    in dir: test.txt
    'test.txt' doesn't exist on the filesystem
    in dir:

which is wrong because if unlink() doesn't return an error, the file is
supposed to be deleted. -f indeed says that the file is not present, but
readdir() still lists it. Who should I believe?

I can't test but based on previous experiments, ActivePerl is more
consistent as unlink() fails and the file is not removed.

...

Ok, the equivalent program in C produces the same results, so the error is
likely to be inside Cygwin API, but shouldn't this be documented somewhere?
Maybe in perlport/"DOS and Derivatives"?

--
S├ębastien Aperghis-Tramoni

Close the world, txEn eht nepO.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About