campo-sirio/cb/source/df4unlok.c
alex af15e0698b Codebase
git-svn-id: svn://10.65.10.50/trunk@4679 c028cbd2-c16b-5b4b-a496-9718f37d4682
1997-06-16 13:01:08 +00:00

420 lines
13 KiB
C
Executable File

/* df4unlok.c (c)Copyright Sequiter Software Inc., 1988-1996. All rights reserved. */
#include "d4all.h"
#ifndef S4UNIX
#ifdef __TURBOC__
#pragma hdrstop
#endif
#endif
#ifndef S4OFF_MULTI
#ifndef S4CLIENT
#ifndef N4OTHER
#ifndef S4MEMO_OFF
int dfile4memoUnlock( DATA4FILE *data )
{
#ifdef E4PARM_LOW
if ( data == 0 )
return error4( 0, e4parm_null, E91102 ) ;
#endif
if ( code4unlockAuto( data->c4 ) == LOCK4OFF )
return 0 ;
if ( data->memoFile.file.hand != -1 )
return memo4fileUnlock( &data->memoFile ) ;
else
return 0 ;
}
#endif
#endif
int dfile4unlockAppend( DATA4FILE *data, const long clientId, const long serverId )
{
#ifndef S4OFF_MULTI
#ifndef S4CLIENT
int rc ;
#endif
#ifdef E4PARM_LOW
if ( data == 0 || serverId == 0 )
return error4( 0, e4parm_null, E91102 ) ;
#endif
if ( code4unlockAuto( data->c4 ) == LOCK4OFF )
return 0 ;
if ( data->appendServerLock == serverId && ( clientId == 0 || clientId == data->appendClientLock ) )
{
#ifndef S4OFF_WRITE
#ifdef S4OFF_MULTI
dfile4updateHeader( data, 1, 1 ) ;
#else
if ( data->file.lowAccessMode == OPEN4DENY_RW )
dfile4updateHeader( data, 1, 1 ) ;
#endif
if ( data->doDate == 1 )
{
u4yymmdd( &data->yy ) ;
data->doDate = 0 ;
}
#endif
#ifndef S4CLIENT
rc = file4unlock( &data->file, L4LOCK_POS, 1L ) ;
if ( rc < 0 )
return error4stack( data->c4, (short)rc, E91102 ) ;
#endif
data->appendServerLock = 0 ;
data->appendClientLock = 0 ;
data->numRecs = -1 ;
}
if ( error4code( data->c4 ) < 0 )
return error4code( data->c4 ) ;
#endif
return 0 ;
}
#ifdef S4USE_ADDITIVE_LOCK
#ifndef S4CLIENT
int dfile4unlockRange( DATA4FILE *data, const long recNum, long numRecs )
{
int rc=0 ;
#ifdef E4PARM_LOW
if ( recNum == 0L && numRecs != 1L )
return error4( data->c4, e4parm, E91102 ) ;
#endif
#ifdef N4OTHER
if (numRecs == 0)
return(0);
if (numRecs == -1L)
numRecs = L4LOCK_POS-recNum;
rc = file4unlock( &data->file, L4LOCK_POS + recNum, numRecs ) ;
#endif
#ifdef S4MDX
if (numRecs == 0)
return(0);
if ( recNum == 0L ) /* append byte */
rc = file4unlock( &data->file, L4LOCK_POS, numRecs ) ;
else
{
if (numRecs == -1L)
rc = file4unlock( &data->file, L4LOCK_POS_OLD, L4LOCK_POS - L4LOCK_POS_OLD - recNum ) ;
else
rc = file4unlock( &data->file, L4LOCK_POS - recNum - numRecs, numRecs ) ;
if (rc == 0 && numRecs == -1L)
rc = file4unlock( &data->file, L4LOCK_POS - 1L, 1L ) ; /* unlock flag */
}
#endif
#ifdef S4FOX
if ( recNum == 0L ) /* append byte */
rc = file4unlock( &data->file, L4LOCK_POS, numRecs ) ;
else
{
if ( data->hasMdxMemo || data->version == 0x30 )
{
if (numRecs == 0)
return(0);
if (numRecs == -1L)
numRecs = L4LOCK_POS - L4LOCK_POS_OLD - recNum + 1L;
rc = file4unlock( &data->file, L4LOCK_POS - recNum - numRecs + 1L, numRecs ) ;
}
else
if (numRecs == -1)
if (recNum == 1) /* also unlock header bytes*/
rc = file4unlock( &data->file, L4LOCK_POS_OLD, L4LOCK_POS - L4LOCK_POS_OLD ) ;
else
rc = file4unlock( &data->file, L4LOCK_POS_OLD + dfile4recordPosition( data, recNum-1L )+1L, L4LOCK_POS - L4LOCK_POS_OLD - dfile4recordPosition( data, recNum-1L )-1L) ;
else
if (recNum == 1) /* also unlock header bytes*/
rc = file4unlock( &data->file, L4LOCK_POS_OLD, data->headerLen + numRecs*dfile4recWidth(data) ) ;
else
rc = file4unlock( &data->file, L4LOCK_POS_OLD + dfile4recordPosition( data, recNum-1L )+1L, (numRecs+1L)*dfile4recWidth(data)-1L ) ;
}
#endif
return rc ;
}
#endif /* S4CLIENT */
#endif /* S4USE_ADDITIVE_LOCK */
int dfile4unlockFile( DATA4FILE *data, const long clientId, const long serverId )
{
#ifndef S4OFF_MULTI
#ifndef S4CLIENT
#ifdef S4USE_ADDITIVE_LOCK
LOCK4 *lock ;
long prevLock;
int rc;
#endif
#endif
#ifdef E4PARM_LOW
if ( data == 0 || serverId == 0 )
return error4( 0, e4parm, E91102 ) ;
#endif
if ( code4unlockAuto( data->c4 ) == LOCK4OFF )
return 0 ;
#ifdef S4CLIENT
data->fileLock = 0 ;
data->numRecs = -1 ;
#else
#ifndef S4USE_ADDITIVE_LOCK
if ( data->fileServerLock == serverId && ( clientId == 0 || clientId == data->fileClientLock ) )
{
#ifndef S4OFF_WRITE
#ifdef S4OFF_MULTI
dfile4updateHeader( data, 1, 1 ) ;
#else
if ( data->file.lowAccessMode == OPEN4DENY_RW )
dfile4updateHeader( data, 1, 1 ) ;
#endif
if ( data->doDate == 1 )
{
u4yymmdd( &data->yy ) ;
data->doDate = 0 ;
}
#endif
#ifdef N4OTHER
if ( file4unlock( &data->file, L4LOCK_POS, L4LOCK_POS ) < 0 )
return -1 ;
#endif
#ifdef S4MDX
if ( file4unlock( &data->file, L4LOCK_POS_OLD, L4LOCK_POS - L4LOCK_POS_OLD + 1 ) < 0 )
return -1 ;
#endif
#ifdef S4FOX
/* codebase locks the append byte as well... */
if ( file4unlock( &data->file, L4LOCK_POS_OLD, L4LOCK_POS_OLD - 1L ) < 0 )
return -1 ;
#endif
data->fileServerLock = 0 ;
data->fileClientLock = 0 ;
data->numRecs = -1 ;
}
#else
if ( data->fileServerLock == serverId && ( clientId == 0 || clientId == data->fileClientLock ) )
{
#ifndef S4OFF_WRITE
#ifdef S4OFF_MULTI
dfile4updateHeader( data, 1, 1 ) ;
#else
if ( data->file.lowAccessMode == OPEN4DENY_RW )
dfile4updateHeader( data, 1, 1 ) ;
#endif
if ( data->doDate == 1 )
{
u4yymmdd( &data->yy ) ;
data->doDate = 0 ;
}
#endif
rc = file4unlock( &data->file, L4LOCK_POS, 1L ) ; /* unlock append byte*/
if ( rc < 0 )
return error4stack( data->c4, (short)rc, E91102 ) ;
prevLock = 0L ;
for ( lock = 0 ; rc == 0 ; )
{
lock = (LOCK4 *)l4next( &data->lockedRecords, lock ) ;
if ( lock == 0 )
{
rc = dfile4unlockRange( data, prevLock + 1L, -1L ) ;
break;
}
else
{
rc = dfile4unlockRange( data, prevLock + 1L, lock->id.recNum - prevLock - 1L ) ;
if ( rc == 0 )
prevLock = lock->id.recNum ;
}
}
if (rc != 0)
return(rc);
data->fileServerLock = 0 ;
data->fileClientLock = 0 ;
data->numRecs = -1 ;
}
#endif
#endif
if ( error4code( data->c4 ) < 0 )
return error4code( data->c4 ) ;
#endif
return 0 ;
}
#ifndef S4INDEX_OFF
int dfile4unlockIndex( DATA4FILE *data, const long serverId )
{
#ifdef S4OFF_MULTI
return 0 ;
#else
int rc ;
#ifdef N4OTHER
TAG4FILE *tagOn ;
#else
INDEX4FILE *indexOn ;
#endif
#ifdef E4PARM_LOW
if ( data == 0 || serverId == 0 )
return error4( 0, e4parm, E91102 ) ;
#endif
#ifdef N4OTHER
if ( data->indexLocked == 0 )
return 0 ;
#endif
/* if the data file is locked, then delay unlocking index if lock off */
if ( code4unlockAuto( data->c4 ) == LOCK4OFF )
if ( data->fileServerLock == serverId )
return 0 ;
#ifdef N4OTHER
data->indexLocked = 0 ;
for ( tagOn = 0 ;; )
{
tagOn = dfile4tagNext( data, tagOn ) ;
if ( tagOn == 0 )
{
rc = error4code( data->c4 ) ;
if ( rc < 0 )
return rc ;
return 0 ;
}
rc = tfile4unlock( tagOn, serverId ) ;
if ( rc < 0 )
return rc ;
}
#else
for ( indexOn = 0 ;; )
{
indexOn = (INDEX4FILE *)l4next(&data->indexes,indexOn) ;
if ( indexOn == 0 )
{
rc = error4code( data->c4 ) ;
if ( rc < 0 )
return rc ;
return 0 ;
}
index4unlock( indexOn, serverId ) ;
}
#endif
#endif
}
#endif
int S4FUNCTION dfile4unlockRecord( DATA4FILE *data, const long clientId, const long serverId, const long rec )
{
#ifndef S4CLIENT
LOCK4 *lock, *lockNext ;
for ( lock = (LOCK4 *)l4first( &data->lockedRecords ) ; lock != 0 ; )
{
lockNext = (LOCK4 *)l4next( &data->lockedRecords, lock ) ;
if ( lock->id.serverId == serverId && ( clientId == 0 || lock->id.clientId == clientId ) &&
lock->id.recNum == rec )
{
#ifdef N4OTHER
if ( file4unlock( &data->file, L4LOCK_POS + rec, 1L ) < 0 )
return -1 ;
#endif
#ifdef S4MDX
if ( file4unlock( &data->file, L4LOCK_POS - rec -1L, 1L ) < 0 )
return -1 ;
#endif
#ifdef S4FOX
if ( ( data->hasMdxMemo ) || data->version == 0x30 )
{
if ( file4unlock( &data->file, L4LOCK_POS - rec, 1L ) < 0 )
return -1 ;
}
else
if ( file4unlock( &data->file, L4LOCK_POS_OLD + dfile4recordPosition( data, rec ), 1L ) < 0 )
return -1 ;
#endif
l4remove( &data->lockedRecords, lock ) ;
mem4free( data->c4->lockMemory, lock ) ;
break ;
}
lock = lockNext ;
}
#endif
return 0 ;
}
int dfile4unlockRecords( DATA4FILE *data, const long clientId, const long serverId )
{
#ifndef S4OFF_MULTI
LOCK4 *lock, *lockNext ;
int rc ;
#ifdef E4PARM_LOW
if ( data == 0 || serverId == 0 )
return error4( 0, e4parm, E91102 ) ;
#endif
if ( code4unlockAuto( data->c4 ) == LOCK4OFF )
return 0 ;
for ( lock = (LOCK4 *)l4first( &data->lockedRecords ) ; lock != 0 ; )
{
lockNext = (LOCK4 *)l4next( &data->lockedRecords, lock ) ;
if ( lock->id.serverId == serverId && ( clientId == 0 || lock->id.clientId == clientId ) )
{
#ifndef S4CLIENT
#ifdef N4OTHER
rc = file4unlock( &data->file, L4LOCK_POS + lock->id.recNum, 1L ) ;
#endif
#ifdef S4MDX
rc = file4unlock( &data->file, L4LOCK_POS - lock->id.recNum -1L, 1L ) ;
#endif
#ifdef S4FOX
if ( data->hasMdxMemo || data->version == 0x30 )
rc = file4unlock( &data->file, L4LOCK_POS - lock->id.recNum, 1L ) ;
else
rc = file4unlock( &data->file, L4LOCK_POS_OLD + dfile4recordPosition( data, lock->id.recNum ), 1L ) ;
#endif
if ( rc < 0 )
return error4stack( data->c4, (short)rc, E91102 ) ;
#endif
l4remove( &data->lockedRecords, lock ) ;
mem4free( data->c4->lockMemory, lock ) ;
}
lock = lockNext ;
}
#endif
return 0 ;
}
int dfile4unlockData( DATA4FILE *data, const long clientId, const long serverId )
{
#ifndef S4OFF_MULTI
#ifdef E4PARM_LOW
if ( data == 0 )
return error4( 0, e4parm_null, E91102 ) ;
#endif
if ( code4unlockAuto( data->c4 ) == LOCK4OFF )
return 0 ;
dfile4unlockFile( data, clientId, serverId ) ;
dfile4unlockAppend( data, clientId, serverId ) ;
dfile4unlockRecords( data, clientId, serverId ) ;
if ( error4code( data->c4 ) < 0 )
return error4code( data->c4 ) ;
#endif
return 0 ;
}
#endif /* S4CLIENT */
#endif /* S4OFF_MULTI */