File locking in Win 95/Win NT 
Author Message
 File locking in Win 95/Win NT

I would like to be able to check to see whether some application has
finished writing to a file, so my little python program can safely read it
and delete it.

I looked up in the index listing in the Library Reference document, and
homed in on the "flock()" routine...it is in the fcntl module, and I
discovered that it is a UNIX-specific module --- there seems to be no
equivalent version for Win 95/Win NT.

I appreciate any helps and ideas!

Thanks!

John "The Blue Wizard" Rogers



Thu, 14 Dec 2000 03:00:00 GMT  
 File locking in Win 95/Win NT

Quote:
>I would like to be able to check to see whether some application has
>finished writing to a file, so my little Python program can safely read it
>and delete it.

One way to do this under Windows is to check the file size;
it's zero as long as the other application is writing to the file
(assuming it didn't exist from the beginning, that is).

Alternatively, you can check the modification time.  (there's
probably also something really windows-specific in the win32api
module that could be used for this purpose).

Quote:
>I looked up in the index listing in the Library Reference document, and
>homed in on the "flock()" routine...it is in the fcntl module, and I
>discovered that it is a UNIX-specific module --- there seems to be no
>equivalent version for Win 95/Win NT.

Even if it existed under Windows (1), flock wouldn't have helped -- it's
advisory, so if the application that writes the file doesn't care, there's
nothing you can do about it.

On the other hand, if you control both applications, you don't need
locking to do this: just make the application write the file under a
temporary name, and rename the file when it's finished.  The rename
operation is atomic on all reasonable operating systems, so if your
application can find the file, the application has finished.

Cheers /F

http://www.pythonware.com

1) Does anyone know if it's possible to add flock-emulation for
Windows? Does Perl support it?



Thu, 14 Dec 2000 03:00:00 GMT  
 File locking in Win 95/Win NT

In the undocumented, but built-in on Windows module called "msvcrt",
you will find a function called "locking" which allows locks to be
setup on files.

Ive included some doco on this function from the MSVC documentation.

There is no portable way to do this at this stage.

Mark.

_locking
Locks or unlocks bytes of a file.

int _locking( int handle, int mode, long nbytes );

Remarks

The _locking function locks or unlocks nbytes bytes of the file
specified by handle. Locking bytes in a file prevents access to
those bytes by other processes. All locking or unlocking begins at
the current position of the file pointer and proceeds for the next
nbytes bytes. It is possible to lock bytes past end of file.

mode must be one of the following manifest constants, which are
defined in LOCKING.H:

_LK_LOCK Locks the specified bytes. If the bytes cannot be locked,
the program immediately tries again after 1 second. If, after 10
attempts, the bytes cannot be locked, the constant returns an error.

_LK_NBLCK Locks the specified bytes. If the bytes cannot be locked,
the constant returns an error.

_LK_NBRLCK Same as _LK_NBLCK.

_LK_RLCK Same as _LK_LOCK.

_LK_UNLCK Unlocks the specified bytes, which must have been
previously locked.

Multiple regions of a file that do not overlap can be locked. A
region being unlocked must have been previously locked. _locking
does not merge adjacent regions; if two locked regions are adjacent,
each region must be unlocked separately. Regions should be locked
only briefly and should be unlocked before closing a file or exiting
the program
----

Quote:
>I would like to be able to check to see whether some application
has
>finished writing to a file, so my little Python program can safely
read it
>and delete it.

>I looked up in the index listing in the Library Reference document,
and
>homed in on the "flock()" routine...it is in the fcntl module, and
I
>discovered that it is a UNIX-specific module --- there seems to be
no
>equivalent version for Win 95/Win NT.

>I appreciate any helps and ideas!

>Thanks!

>John "The Blue Wizard" Rogers



Fri, 15 Dec 2000 03:00:00 GMT  
 File locking in Win 95/Win NT
: >I would like to be able to check to see whether some application has
: >finished writing to a file, so my little Python program can safely read it
: >and delete it.

: >I looked up in the index listing in the Library Reference document, and
: >homed in on the "flock()" routine...it is in the fcntl module, and I
: >discovered that it is a UNIX-specific module --- there seems to be no
: >equivalent version for Win 95/Win NT.

: Even if it existed under Windows (1), flock wouldn't have helped -- it's
: advisory, so if the application that writes the file doesn't care, there's
: nothing you can do about it.

flock() is advisory on UNIX as well.

: 1) Does anyone know if it's possible to add flock-emulation for
: Windows? Does Perl support it?

Here is perl's implementation for win32. Note that it only works
on NT.

Quote:
>From /usr/src/perl/win32/win32.c:

    /* simulate flock by locking a range on the file */

    #define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError()))
    #define LK_LEN              0xffff0000

    DllExport int
    win32_flock(int fd, int oper)
    {
        OVERLAPPED o;
        int i = -1;
        HANDLE fh;

        if (!IsWinNT()) {
            croak("flock() unimplemented on this platform");
            return -1;
        }
        fh = (HANDLE)_get_osfhandle(fd);
        memset(&o, 0, sizeof(o));

        switch(oper) {
        case LOCK_SH:           /* shared lock */
            LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
            break;
        case LOCK_EX:           /* exclusive lock */
            LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
            break;
        case LOCK_SH|LOCK_NB:   /* non-blocking shared lock */
            LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
            break;
        case LOCK_EX|LOCK_NB:   /* non-blocking exclusive lock */
            LK_ERR(LockFileEx(fh,
                           LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
                           0, LK_LEN, 0, &o),i);
            break;
        case LOCK_UN:           /* unlock lock */
            LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
            break;
        default:                        /* unknown */
            errno = EINVAL;
            break;
        }
        return i;
    }

--

+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+

|                                                                    |
| Hibbitt, Karlsson & Sorensen, Inc.    Tel:  (401) 727-4200 x 4442  |
| 1080 Main Street, Pawtucket RI 02860  Fax:  (401) 727-4208         |
|                                                                    |
| http://www.ugcs.caltech.edu/~werdna/sttng/trek6/trek6-2a2.html     |
|   Search for "2167"                                                |
+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+-=+



Sat, 23 Dec 2000 03:00:00 GMT  
 File locking in Win 95/Win NT

Quote:


>: >I would like to be able to check to see whether some application
has
>: >finished writing to a file, so my little Python program can
safely read it
>: >and delete it.

>: >I looked up in the index listing in the Library Reference
document, and
>: >homed in on the "flock()" routine...it is in the fcntl module,
and I
>: >discovered that it is a UNIX-specific module --- there seems to
be no
>: >equivalent version for Win 95/Win NT.

>: Even if it existed under Windows (1), flock wouldn't have
helped -- it's
>: advisory, so if the application that writes the file doesn't
care, there's
>: nothing you can do about it.

>flock() is advisory on UNIX as well.

>: 1) Does anyone know if it's possible to add flock-emulation for
>: Windows? Does Perl support it?

Python on Win32 has a built-in module called "msvcrt" with native
locking functions.  a .py wrapper around this should be trivial.
Check dejanews for my previous posting where I posted some of the
MSVC doco for the msvcrt function you need...

Mark.



Sun, 24 Dec 2000 03:00:00 GMT  
 File locking in Win 95/Win NT
: >I would like to be able to check to see whether some application has
: >finished writing to a file, so my little Python program can safely
read it
: >and delete it.

Windows NT will not allow two programs to open the same file
if one of them is writing.  So a trick is to open the
file using "a" or "r+" mode.  If it succeeds, you must be the
only user of the file.  These modes do not destroy the file as "w"
would.

Windows NT will not allow a file to be deleted if another app has
it open.  So delete is atomic and "safe".

Careful:  Windows being as it is, there may be a file mode you can
set (that I don't know about) to allow two apps to access the file.
And I don't know about 95.

Jim



Sun, 24 Dec 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. XDS for Win NT and Win 95 now available

2. SmallEiffel -0.82 for DOS, Win NT, Win 95

3. REXX for Win 95 and Win NT

4. Need VHDL tools for Win NT/ Win 95

5. One solution to File Sharing problems on Win 95/NT networks

6. Object Rexx (Win 95/NT ) RunTime Files

7. Win '95 DDE calls in ObjectREXX for Windows 95/NT

8. Files locking problem under Win NT

9. Locking problem with Win 95 (VShare)

10. Record locks and Win 95

11. WIDGET DEMO under MS win 3.1 and 95 LOCKS UP

12. Win 3.11 vs Win 95 (For Arnor)

 

 
Powered by phpBB® Forum Software