Subject: | Provide a Win32-smart file deletion function |
OK, this is something of a two-part issue, and can probably be solved a
couple of ways.
The main problem stems from Win32 not allowing you to delete a file that
is in use.
This means that the following occurs.
1. Can't upgrade PathTools via CPAN.pm (Cmd.dll is locked)
2. Can't upgrade Term::ReadLine::Perl (readline.pm is locked)
Running make install
Cannot forceunlink
C:\vanilla-perl\perl\site\lib\Term\ReadLine\readline.pm: Perm
ission denied at C:/vanilla-perl/perl/lib/File/Find.pm line 918
3. Cannot always remove previous build directories, because sometimes
Windows holds a stale lock on the .dll (in my case, on IO.dll because
the IO::Socket::INET tests hung).
Basically, there's a whole bunch of reasons where you might have
problems deleting files containing code on Windows.
Thankfully, there seems to be a solution.
While you cannot delete a file outright if in use, you CAN move or
rename it!
So, to fix this problem for at least the cases that CPAN.pm is capable
of fixing itself, we need to essentially find an unused temporary file
name, and move the offending file well away from harm into the temp dir.
Next time the temp directory gets cleaned out, presumably after a reboot
down the track, then locked file gets cleared away.
So, what you probably want to do is implement a utility function called
remove or delete or something.
If not Win32, unlink as normal.
If Win32, try first to delete it normally inside an eval.
If the eval dies with a "Permission denied" error, then implement the
"move away" trick. If THAT fails, die as normal.
Simply implementing the delete function is step 1, and then presumably
changing the various bits of delete code to be use that abstracted
delete (instead of unlink directly) is step 2.