in reply to Re: (tye)Re: Self Deletion
in thread Self Deletion

You mean opening $0 for writing will overwrite the existing file's contents, and although Perl may still have it open, the way it was opened doesn't prevent it from being opened for writing (at least by the same process or same security context).

Yes. And, no, some other process in a different security context could do exactly the same thing unless you went out of your way to prevent it.

But, in Win32, the deleting of a file is prohibited if it's open at all, regardless of what sharing modes are specified.

No, the C RTL opens files in such a way that no one (not even you) is allowed to delete them until you close them. You can easily open files such that they can be deleted but you have to not use C's fopen() if you want that.

It should be enough, unless spawned tasks are still using it. It's enough for that script.

Actually, I was thinking that if one managed to launch the script then in most cases one would have either a shell or Explorer with that directory open. But if the script (or the automated process that launched the script) was also what created the directory, then you have better odds.

The mechanism is totally different for NT/2000 than it is for Win 3.1/95/98/ME. The function in Win32API::File is only present on the former.

True. Thanks, I'd forgotten that.

Are you sure [deleting an open file(?) is] possible on Win32? If it's just a matter of the defaults passed to the underlying functions, you could change the Perl source.

Yes, I'm sure. See Win32API::File for more on how. You have to modify Win32 Perl to not use the C RTL's fopen(). Win32 Perl already replaces several bits of the C RTL with its own (because of bugs in common C RTLs) so it could roll its own fopen() as well in order to "fix" this. Hmm...

I just noticed that Win32 Perl's implementation of utime() won't work on a file if anyone has it open for writing (perhaps there would be no point?). It looks like Win32 Perl's stat won't work reliably on files that are currently opened by anyone. I blame these problems on Microsoft making you type FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE in order to be sharing but only 0 in order to be stingy.

        - tye (but my friends call me "Tye")