Actually, it is possible. Kinda. If you set the right share permission, the file can be deleted. Actually, it only gets flagged as being deleted. The file still appears in the directory until all file handles to it are closed. Open file handles still function. New handles can't be created. ("Access denied".)

For example,

use Win32API::File qw( createFile ReadFile CloseHandle ); $|=1; my $share = $ARGV[0]; print("share: $share\n"); { open(my $fh, '>', 'deleteme') or die("Unable to create file: $!\n"); print $fh ("foo\n"); } { my $h = createFile('deleteme', 'r', $share) or die("Unable to open file: $^E\n"); { ReadFile($h, my $buf, 1, [], []) or die("Unable to read from file: $^E\n"); print("Read $buf\n"); } print("pre unlink dir: "); system('dir /b deleteme'); print("pre unlink type: "); system('type deleteme'); if (unlink('deleteme')) { print("File deleted\n"); } else { print("Unable to delete file\n"); } print("post unlink dir: "); system('dir /b deleteme'); print("post unlink type: "); system('type deleteme'); { ReadFile($h, my $buf, 1, [], []) or die("Unable to read from file: $^E\n"); print("Read $buf\n"); } print("pre CloseHandle dir: "); system('dir /b deleteme'); print("pre CloseHandle type: "); system('type deleteme'); CloseHandle($h); print("post CloseHandle dir: "); system('dir /b deleteme'); print("post CloseHandle type: "); system('type deleteme'); }

outputs

>perl 549202.pl rw share: rw Read f pre unlink dir: deleteme pre unlink type: foo Unable to delete file <--- post unlink dir: deleteme post unlink type: foo Read o pre CloseHandle dir: deleteme pre CloseHandle type: foo post CloseHandle dir: deleteme post CloseHandle type: foo >perl 549202.pl rwd share: rwd Read f pre unlink dir: deleteme pre unlink type: foo File deleted <--- post unlink dir: deleteme post unlink type: Access is denied. Read o pre CloseHandle dir: deleteme pre CloseHandle type: Access is denied. post CloseHandle dir: File Not Found post CloseHandle type: The system cannot find the file specified.

In reply to Re^2: What if FILE deleted during WHILE read-in loop? by ikegami
in thread What if FILE deleted during WHILE read-in loop? by punch_card_don

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.