diff options
Diffstat (limited to 'win32/win32io.c')
-rw-r--r-- | win32/win32io.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/win32/win32io.c b/win32/win32io.c index e75754abb9..db156cf133 100644 --- a/win32/win32io.c +++ b/win32/win32io.c @@ -190,6 +190,52 @@ my_get_osfhandle( int filehandle ) return _get_osfhandle(filehandle); } + +/* simulate flock by locking a range on the file */ + + +#define LK_ERR(f,i) ((f) ? (i = 0) : (errno = GetLastError())) +#define LK_LEN 0xffff0000 + +int +my_flock(int fd, int oper) +{ + OVERLAPPED o; + int i = -1; + HANDLE fh; + + fh = (HANDLE)my_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; +} + +#undef LK_ERR +#undef LK_LEN + + #ifdef PERLDLL __declspec(dllexport) #endif @@ -247,6 +293,7 @@ WIN32_IOSUBSYSTEM win32stdio = { _mkdir, _rmdir, _chdir, + my_flock, /* (*pfunc_flock)(int fd, int oper) */ 87654321L, /* end of structure */ }; |