home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BTREE.ZIP
/
BTLOCK.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
4KB
|
187 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "btlock.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
#include "btree_.h"
/*man---------------------------------------------------------------------------
NAME
btlock - btree lock
SYNOPSIS
#include <btree.h>
int btlock(btp, l_type)
btree_t *btp;
int l_type;
DESCRIPTION
The btlock function controls the lock status of a btree. The btp
argument is an open btree. l_type indicates the target status of
the lock on the btree.
The lock types available are:
BT_RDLCK - lock btree for reading
BT_WRLCK - lock btree for reading and writing
BT_RDLKW - lock btree for reading (wait)
BT_WRLKW - lock btree for reading and writing (wait)
BT_UNLCK - unlock btree
For the lock types which wait, btlock will not return until the
lock is available. For the lock types which do not wait, if the
lock is unavailable because of a lock held by another process a
value of -1 is returned and errno set to EAGAIN.
btlock will fail if one or more of the following is true:
[EAGAIN] l_type is BT_RDLCK and btp is already
write locked by another process, or l_type
is BT_WRLCK and btp is already read
or write locked by another process.
[EINVAL] btp is is not a valid btree pointer.
[EINVAL] l_type is not one of the valid lock types.
[BTECORRUPT] btp is corrupt.
[BTENOPEN] btp is not open.
[BTENOPEN] l_type is BT_RDLCK or BT_RDLKW and btp
is not opened for reading or l_type is BT_WRLCK
or BT_WRLKW and btp is not open for writing.
SEE ALSO
btgetlck.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int btlock(btp, l_type)
btree_t * btp;
int l_type;
{
int rs = 0;
int bl_type = 0;
errno = 0;
/* validate arguments */
if (!bt_valid(btp)) {
errno = EINVAL;
return -1;
}
/* check if btree not open */
if (!(btp->flags & BTOPEN)) {
errno = BTENOPEN;
return -1;
}
/* check if file is open */
switch (l_type) {
case BT_RDLCK:
if (!(btp->flags & BTREAD)) {
errno = BTENOPEN;
return -1;
}
bl_type = B_RDLCK;
break;
case BT_RDLKW:
if (!(btp->flags & BTREAD)) {
errno = BTENOPEN;
return -1;
}
bl_type = B_RDLKW;
break;
case BT_WRLCK:
if (!(btp->flags & BTWRITE)) {
errno = BTENOPEN;
return -1;
}
bl_type = B_WRLCK;
break;
case BT_WRLKW:
if (!(btp->flags & BTWRITE)) {
errno = BTENOPEN;
return -1;
}
bl_type = B_WRLKW;
break;
case BT_UNLCK:
/* flush buffers */
rs = bflush(btp->bp);
if (rs == -1) {
BTEPRINT;
return -1;
}
bl_type = B_UNLCK;
break;
default:
errno = EINVAL;
return -1;
break;
}
/* lock btree file */
rs = lockb(btp->bp, bl_type, (bpos_t)0, (size_t)0);
if (rs == -1) {
if (errno != EAGAIN) BTEPRINT;
return -1;
}
/* set status bits in btree control structure */
switch (l_type) {
case BT_UNLCK:
if (btgetlck(btp) == BT_WRLCK) { /* update header */
rs = bputh(btp->bp, (void *)&btp->bthdr);
if (rs == -1) {
BTEPRINT;
return -1;
}
}
btp->flags &= ~BTLOCKS;
break; /* case BT_UNLCK: */
case BT_RDLCK:
case BT_RDLKW:
if (btgetlck(btp) == BT_UNLCK) {
rs = bgeth(btp->bp, (void *)&btp->bthdr);
if (rs == -1) {
BTEPRINT;
return -1;
}
if (btp->bthdr.flags & BTHMOD) {
errno = BTECORRUPT;
return -1;
}
}
btp->flags |= BTRDLCK;
btp->flags &= ~BTWRLCK;
break; /* case BT_RDLCK: */
case BT_WRLCK:
case BT_WRLKW:
if (btgetlck(btp) == BT_UNLCK) {
rs = bgeth(btp->bp, (void *)&btp->bthdr);
if (rs == -1) {
BTEPRINT;
return -1;
}
if (btp->bthdr.flags & BTHMOD) {
errno = BTECORRUPT;
return -1;
}
}
btp->flags |= (BTRDLCK | BTWRLCK);
break; /* case BT_WRLCK: */
default:
BTEPRINT;
errno = BTEPANIC;
return -1;
break; /* default: */
}
errno = 0;
return 0;
}