develooper Front page | perl.perl5.porters | Postings from August 2010

RE: What do we do about perl -pi on Windows?

Thread Previous | Thread Next
Jan Dubois
August 9, 2010 17:59
RE: What do we do about perl -pi on Windows?
Message ID:
On Mon, 09 Aug 2010, Ben Morrow wrote:
> IMHO -i on all platforms should open ARGVOUT to a tmpfile, and rename(2)
> it over the original when it's done.

Sounds sensible.  You would still want to find a non-conflicting filename
in the same directory as the original filename to make sure they are on
the same device.


> IIRC the equivalent of Unix' close-behind on Win32 is a flag that will
> cause the OS to delete the file when it gets closed. That sounds exactly
> right for this situation (better, in fact, than close-behind, since the
> file needs a name to get renamed). The flag would have to be cleared
> just before renaming, of course, which still leaves a small race

I believe there is no API that lets you revert the FILE_FLAG_DELETE_ON_CLOSE
bit.  But I'm not sure why you would need to do this.  You close all handles
to the file, hope nobody else has it open at that moment, so it actually
goes away, and then rename the temp file back to the original name.

You may want to Sleep() and retry a few times to deal with on-access
virus scanners that keep files around until they manage to scan them.

It is kind of a stupid thing: the virus scanner keeps the file alive,
just to either delete it silently later, or telling you that it found
a virus and then deleting it anyways. :)

> condition. (Taking a quick look at MSDN, it seems to me that -i should
> in any case be using ReplaceFile, which handles making a backup while
> also preserving the attributes of the replaced file.)

Yes ReplaceFile() would be a good idea to keep ACLs etc.  But if we really
cared about this, wouldn't we also try to preserve soft- and hard-links?


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About