See createFile() and CreateFile() from Win32API::File for more information on much of this.

In Win32, when you open a file you have to specify what type of sharing you want to allow. For C programs that use open() or fopen(), the default is to allow read- and write-sharing but not delete-sharing (note that delete-sharing is ignored prior to WinNT).

If you use Win32's CreateFile(), then you have to specify the type of sharing allowed. Unfortunately, it is very easy to go "Um, 'sharing'? What am I supposed to pass in there? Ah, '0' works, so I'll just do that since I don't want to spend time trying to figure that out." And '0' means "no sharing". So a lot of Win32 programs end up not allowing any sharing of the files that they open.

Now, if you open a file and don't specify that read-sharing is allowed, then any other open against that file that requests read access will fail (and your open will fail if the file is already open for read access). Similar for write-sharing and write access.

So if you can't read the file, then it (probably) was opened w/o allowing read-sharing (or you could be trying to open it w/o allowing some for of sharing, but that seems unlikely given the C RTL defaults that are also used by Perl). If that is the case, then nothing can open the file for read-sharing.

I'd had hoped that (under WinNT and later) a program that has a special privilege designated for making backups would be able to override this, but testing shows otherwise:

#!/usr/bin/perl -w use strict; use Win32API::Registry qw( AllowPriv ); use Win32API::File qw( createFile FILE_FLAG_BACKUP_SEMANTICS OsFHandleOpen ); AllowPriv( "SeBackupPrivilege", 1 ) or warn "Can't enable 'backup' privilege: $^E\n"; my $h= createFile( "lockedFile.txt", "re", "rwd", { Flags => FILE_FLAG_BACKUP_SEMANTICS() } ) or die "Can't read lockedFile.txt: $^E\n"; OsFHandleOpen( \*FILE, $h, "r" ) or die "Can't open FILE handle: $! ($^E)\n"; while(<FILE>) { print; }

So it looks to me like this program you are using is "broken" in this respect and there is no work-around other than modifying that program. ):

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

In reply to (tye)Re: Opening a locked file for reading by tye
in thread Opening a locked file for reading by entropy

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.