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

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

Thread Previous | Thread Next
From:
Ben Morrow
Date:
August 9, 2010 16:53
Subject:
Re: What do we do about perl -pi on Windows?
Message ID:
20100809235323.GA11835@osiris.mauzo.dyndns.org
Quoth avar@cpan.org:
> On Mon, Aug 9, 2010 at 23:05, Jan Dubois <jand@activestate.com> wrote:
> >
> > The problem is that the monkeypatch is documented (in README.cygwin), so
> > just removing it doesn't feel quite right either. Ideally someone would
> > go in and update the patch to remove the backup file when it is being
> > closed. Then this patch could be enabled for other DOSISH platforms too
> > (maybe with a somewhat more obscure extension than .bak to avoid
> > conflicts with existing filenames).
> 
> If we're set on adding perl -pi emulation on Windows it would probably
> be best just to create a backup file with the equivalent of
> File::Temp, and unlink it when we're done with it.

IMHO -i on all platforms should open ARGVOUT to a tmpfile, and rename(2)
it over the original when it's done. This is a reversal of the usual
behaviour of -i, but it's enormously safer. (It's really annoying there
isn't a flink(2) syscall, which would let you unlink the tempfile right
away and then link it in place atomically at the end.)

> The only problem with that is that perl might run out of diskspace
> when doing -pi on large files,

This is no different from the situation on Unix. -i with no backup
unlinks the original file, but the disk space (obviously) isn't freed
until the filehandle is closed. -i in all cases requires enough space
for both old and new files to exist simultaneously.

> or slowly accumulate garbage if it's
> e.g. being killed before it gets around to the unlink().

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
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.)

Ben


Thread Previous | 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