Fixed error handling. Added credit.
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@9069 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02master
parent
35205da90c
commit
2ea9e887af
|
@ -51,7 +51,6 @@ static void set_file_pointer(HANDLE h, LARGE_INTEGER gohere,
|
|||
CAMLprim value unix_lockf(value fd, value cmd, value span)
|
||||
{
|
||||
CAMLparam3(fd, cmd, span);
|
||||
int lock_ret;
|
||||
OVERLAPPED overlap;
|
||||
intnat l_len;
|
||||
HANDLE h;
|
||||
|
@ -60,6 +59,7 @@ CAMLprim value unix_lockf(value fd, value cmd, value span)
|
|||
LARGE_INTEGER beg_position;
|
||||
LARGE_INTEGER lock_len;
|
||||
LARGE_INTEGER zero;
|
||||
DWORD err = NO_ERROR;
|
||||
|
||||
version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
||||
if(GetVersionEx(&version) == 0) {
|
||||
|
@ -106,18 +106,21 @@ CAMLprim value unix_lockf(value fd, value cmd, value span)
|
|||
|
||||
switch(Int_val(cmd)) {
|
||||
case 0: /* F_ULOCK - unlock */
|
||||
lock_ret = UnlockFileEx(h, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap);
|
||||
if (! UnlockFileEx(h, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap))
|
||||
err = GetLastError();
|
||||
break;
|
||||
case 1: /* F_LOCK - blocking write lock */
|
||||
enter_blocking_section();
|
||||
lock_ret = LockFileEx(h, LOCKFILE_EXCLUSIVE_LOCK, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap);
|
||||
if (! LockFileEx(h, LOCKFILE_EXCLUSIVE_LOCK, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap))
|
||||
err = GetLastError();
|
||||
leave_blocking_section();
|
||||
break;
|
||||
case 2: /* F_TLOCK - non-blocking write lock */
|
||||
lock_ret = LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap);
|
||||
if (! LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap))
|
||||
err = GetLastError();
|
||||
break;
|
||||
case 3: /* F_TEST - check whether a write lock can be obtained */
|
||||
/* I'm doing this by aquiring an immediate write
|
||||
|
@ -125,28 +128,31 @@ CAMLprim value unix_lockf(value fd, value cmd, value span)
|
|||
* this behavior matches anything in particular, but
|
||||
* it is not clear the nature of the lock test performed
|
||||
* by ocaml (unix) currently. */
|
||||
lock_ret = LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap);
|
||||
if (lock_ret != 0) {
|
||||
if (LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap)) {
|
||||
UnlockFileEx(h, 0, lock_len.LowPart, lock_len.HighPart, &overlap);
|
||||
} else {
|
||||
err = GetLastError();
|
||||
}
|
||||
break;
|
||||
case 4: /* F_RLOCK - blocking read lock */
|
||||
enter_blocking_section();
|
||||
lock_ret = LockFileEx(h, 0, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap);
|
||||
if (! LockFileEx(h, 0, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap))
|
||||
err = GetLastError();
|
||||
leave_blocking_section();
|
||||
break;
|
||||
case 5: /* F_TRLOCK - non-blocking read lock */
|
||||
lock_ret = LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap);
|
||||
if (! LockFileEx(h, LOCKFILE_FAIL_IMMEDIATELY, 0,
|
||||
lock_len.LowPart, lock_len.HighPart, &overlap))
|
||||
err = GetLastError();
|
||||
break;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
uerror("lockf", Nothing);
|
||||
}
|
||||
if (lock_ret == 0) {
|
||||
win32_maperr(GetLastError());
|
||||
if (err != NO_ERROR) {
|
||||
win32_maperr(err);
|
||||
uerror("lockf", Nothing);
|
||||
}
|
||||
CAMLreturn(Val_unit);
|
||||
|
|
Loading…
Reference in New Issue