From ffd9b480a4b3d7252515260a781acbaeb4e063b4 Mon Sep 17 00:00:00 2001 From: Przemyslaw Czerpak Date: Thu, 2 Oct 2008 12:34:36 +0000 Subject: [PATCH] 2008-10-02 14:33 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/include/hbstack.h % enable assembler inline function to access HVM thread stack pointer without native compiler TLS support even if HB_STACK_PRELOAD is not defined in MinGW builds. It gives some additional small speed improvement in MT mode * harbour/source/rtl/idle.c * unlock HVM inside hb_releaseCPU() function * harbour/contrib/hbwin/win_ole.c ! fixed casting and C++ compilation in some compilers * harbour/include/Makefile + harbour/contrib/examples/rdddbt/hbrdddbt.h - harbour/include/hbrdddbt.h ! moved hbrdddbt.h to correct location * harbour/include/Makefile ! added missinf hbthread.h * harbour/tests/rddtest/rddmktst.prg * harbour/tests/rddtest/adscl52.prg * harbour/tests/rddtest/adscl53.prg * harbour/tests/rddtest/ntxcl52.prg * harbour/tests/rddtest/ntxcl53.prg * harbour/tests/rddtest/cdxcl52.prg * harbour/tests/rddtest/rddtst.prg * harbour/tests/rddtest/cdxcl53.prg ! fixed description in header I wrongly copied without updating from other files + harbour/tests/hsxtest.prg + added test code for HiPer-SEEK indexes * harbour/source/rtl/filesys.c + added new functions: hb_fsExtName(), hb_fsReadAt(), hb_fsWriteAt(), hb_fsTruncAt() to use with shared file handles. Please test it in other *nixes. I used pread[64]()/pwrite[64]() POSIX functions but I do not know if they are available by default in other supported platforms like MacOSX, BSD, HPUX, SunOS. If not they I will have to implement some workaround. * harbour/common.mak * harbour/source/rtl/Makefile * harbour/include/hbapifs.h + harbour/source/rtl/filebuf.c + added set of new file functions (hb_file*()) which are designed to use with shared file handles and locks (buffers in the future): hb_fileExtOpen(), hb_fileCreateTemp(), hb_fileClose(), hb_fileLock(), hb_fileReadAt(), hb_fileWriteAt(), hb_fileTruncAt(), hb_fileSize(), hb_fileCommit(), hb_fileHandle() These functions operate on PHB_FILE structure instead of HB_FHANDLE. Now in POSIX systems they share file handle between aliased or simultaneously open by other thread work areas. It resolves the problem with releasing all process FCNTL locks by any close() operation even on duplicated or open() separately handle. Now DOS deny flags emulation works in single process even if BSD locks are disabled (non Linux *nixes). They also keep internal file lock list what allows to synchronize threads and aliases with file locks in POSIX systems. In the future it will be used also for internal locking to synchronize threads without OS level locks - pseudo exclusive mode in cloned WA. Finally these structures will be used also for file buffers which will be shared between aliased WA and threads. * harbour/include/hbrdddbf.h * harbour/include/hbrddcdx.h * harbour/include/hbrddntx.h * harbour/include/hbrdddel.h * harbour/include/hbrddsdf.h * harbour/source/rdd/dbf1.c * harbour/source/rdd/delim1.c * harbour/source/rdd/sdf1.c * harbour/source/rdd/dbffpt/dbffpt1.c * harbour/source/rdd/dbfntx/dbfntx1.c * harbour/source/rdd/dbfcdx/dbfcdx1.c * harbour/source/rdd/hsx/hsx.c * harbour/contrib/hbbmcdx/bmdbfcdx.c * harbour/contrib/hbbmcdx/hbbmcdx.h * use PHB_FILE and hb_file*() functions instead of HB_FHANDLE (hb_fs*()) to access files. + harbour/tests/aliaslck.prg + added test code for file lock synchronization between aliased work areas --- harbour/ChangeLog | 87 +++ harbour/common.mak | 1 + .../examples/rdddbt}/hbrdddbt.h | 0 harbour/contrib/hbbmcdx/bmdbfcdx.c | 136 ++--- harbour/contrib/hbbmcdx/hbbmcdx.h | 8 +- harbour/contrib/hbwin/win_ole.c | 8 +- harbour/include/Makefile | 2 +- harbour/include/hbapifs.h | 31 +- harbour/include/hbrddcdx.h | 8 +- harbour/include/hbrdddbf.h | 8 +- harbour/include/hbrdddel.h | 2 +- harbour/include/hbrddntx.h | 8 +- harbour/include/hbrddsdf.h | 2 +- harbour/include/hbstack.h | 16 +- harbour/source/rdd/dbf1.c | 253 ++++---- harbour/source/rdd/dbfcdx/dbfcdx1.c | 136 ++--- harbour/source/rdd/dbffpt/dbffpt1.c | 527 ++++++++-------- harbour/source/rdd/dbfntx/dbfntx1.c | 74 ++- harbour/source/rdd/delim1.c | 49 +- harbour/source/rdd/hsx/hsx.c | 564 +++++++++--------- harbour/source/rdd/sdf1.c | 60 +- harbour/source/rtl/Makefile | 1 + harbour/source/rtl/filebuf.c | 500 ++++++++++++++++ harbour/source/rtl/filesys.c | 253 ++++++-- harbour/source/rtl/idle.c | 6 +- harbour/tests/aliaslck.prg | 113 ++++ harbour/tests/hsxtest.prg | 48 ++ harbour/tests/rddtest/adscl52.prg | 3 +- harbour/tests/rddtest/adscl53.prg | 3 +- harbour/tests/rddtest/cdxcl52.prg | 3 +- harbour/tests/rddtest/cdxcl53.prg | 3 +- harbour/tests/rddtest/ntxcl52.prg | 3 +- harbour/tests/rddtest/ntxcl53.prg | 3 +- harbour/tests/rddtest/rddmktst.prg | 3 +- harbour/tests/rddtest/rddtst.prg | 3 +- 35 files changed, 1942 insertions(+), 983 deletions(-) rename harbour/{include => contrib/examples/rdddbt}/hbrdddbt.h (100%) create mode 100644 harbour/source/rtl/filebuf.c create mode 100644 harbour/tests/aliaslck.prg create mode 100644 harbour/tests/hsxtest.prg diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 34e8395c5c..c94452250e 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,93 @@ 2008-12-31 13:59 UTC+0100 Foo Bar (foo.bar foobar.org) */ +2008-10-02 14:33 UTC+0200 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + * harbour/include/hbstack.h + % enable assembler inline function to access HVM thread stack pointer + without native compiler TLS support even if HB_STACK_PRELOAD is not + defined in MinGW builds. It gives some additional small speed + improvement in MT mode + + * harbour/source/rtl/idle.c + * unlock HVM inside hb_releaseCPU() function + + * harbour/contrib/hbwin/win_ole.c + ! fixed casting and C++ compilation in some compilers + + * harbour/include/Makefile + + harbour/contrib/examples/rdddbt/hbrdddbt.h + - harbour/include/hbrdddbt.h + ! moved hbrdddbt.h to correct location + + * harbour/include/Makefile + ! added missinf hbthread.h + + * harbour/tests/rddtest/rddmktst.prg + * harbour/tests/rddtest/adscl52.prg + * harbour/tests/rddtest/adscl53.prg + * harbour/tests/rddtest/ntxcl52.prg + * harbour/tests/rddtest/ntxcl53.prg + * harbour/tests/rddtest/cdxcl52.prg + * harbour/tests/rddtest/rddtst.prg + * harbour/tests/rddtest/cdxcl53.prg + ! fixed description in header I wrongly copied without updating + from other files + + + harbour/tests/hsxtest.prg + + added test code for HiPer-SEEK indexes + + * harbour/source/rtl/filesys.c + + added new functions: hb_fsExtName(), hb_fsReadAt(), hb_fsWriteAt(), + hb_fsTruncAt() to use with shared file handles. + Please test it in other *nixes. I used pread[64]()/pwrite[64]() + POSIX functions but I do not know if they are available by default + in other supported platforms like MacOSX, BSD, HPUX, SunOS. + If not they I will have to implement some workaround. + + * harbour/common.mak + * harbour/source/rtl/Makefile + * harbour/include/hbapifs.h + + harbour/source/rtl/filebuf.c + + added set of new file functions (hb_file*()) which are designed to + use with shared file handles and locks (buffers in the future): + hb_fileExtOpen(), hb_fileCreateTemp(), hb_fileClose(), hb_fileLock(), + hb_fileReadAt(), hb_fileWriteAt(), hb_fileTruncAt(), hb_fileSize(), + hb_fileCommit(), hb_fileHandle() + These functions operate on PHB_FILE structure instead of HB_FHANDLE. + Now in POSIX systems they share file handle between aliased or + simultaneously open by other thread work areas. It resolves the + problem with releasing all process FCNTL locks by any close() + operation even on duplicated or open() separately handle. + Now DOS deny flags emulation works in single process even if + BSD locks are disabled (non Linux *nixes). + They also keep internal file lock list what allows to synchronize + threads and aliases with file locks in POSIX systems. In the future + it will be used also for internal locking to synchronize threads + without OS level locks - pseudo exclusive mode in cloned WA. + Finally these structures will be used also for file buffers + which will be shared between aliased WA and threads. + + * harbour/include/hbrdddbf.h + * harbour/include/hbrddcdx.h + * harbour/include/hbrddntx.h + * harbour/include/hbrdddel.h + * harbour/include/hbrddsdf.h + * harbour/source/rdd/dbf1.c + * harbour/source/rdd/delim1.c + * harbour/source/rdd/sdf1.c + * harbour/source/rdd/dbffpt/dbffpt1.c + * harbour/source/rdd/dbfntx/dbfntx1.c + * harbour/source/rdd/dbfcdx/dbfcdx1.c + * harbour/source/rdd/hsx/hsx.c + * harbour/contrib/hbbmcdx/bmdbfcdx.c + * harbour/contrib/hbbmcdx/hbbmcdx.h + * use PHB_FILE and hb_file*() functions instead of HB_FHANDLE (hb_fs*()) + to access files. + + + harbour/tests/aliaslck.prg + + added test code for file lock synchronization between aliased + work areas + 2008-10-02 11:49 UTC+0200 Viktor Szakats (harbour.01 syenar hu) * source/vm/extrap.c * Added "error" text in exception message. diff --git a/harbour/common.mak b/harbour/common.mak index f9e79eeaed..9dd82254db 100644 --- a/harbour/common.mak +++ b/harbour/common.mak @@ -474,6 +474,7 @@ RTL_LIB_OBJS = \ $(OBJ_DIR)\errorapu$(OBJEXT) \ $(OBJ_DIR)\errorint$(OBJEXT) \ $(OBJ_DIR)\file$(OBJEXT) \ + $(OBJ_DIR)\filebuf$(OBJEXT) \ $(OBJ_DIR)\filehb$(OBJEXT) \ $(OBJ_DIR)\filesys$(OBJEXT) \ $(OBJ_DIR)\fkmax$(OBJEXT) \ diff --git a/harbour/include/hbrdddbt.h b/harbour/contrib/examples/rdddbt/hbrdddbt.h similarity index 100% rename from harbour/include/hbrdddbt.h rename to harbour/contrib/examples/rdddbt/hbrdddbt.h diff --git a/harbour/contrib/hbbmcdx/bmdbfcdx.c b/harbour/contrib/hbbmcdx/bmdbfcdx.c index 57b407ccd5..b1ff922ddd 100644 --- a/harbour/contrib/hbbmcdx/bmdbfcdx.c +++ b/harbour/contrib/hbbmcdx/bmdbfcdx.c @@ -1073,7 +1073,7 @@ static void hb_cdxIndexCheckBuffers( LPCDXINDEX pIndex ) */ static ULONG hb_cdxIndexGetAvailPage( LPCDXINDEX pIndex, BOOL bHeader ) { - HB_FHANDLE hFile = pIndex->hFile; + PHB_FILE pFile = pIndex->pFile; BYTE byBuf[4]; ULONG ulPos; @@ -1098,8 +1098,7 @@ static ULONG hb_cdxIndexGetAvailPage( LPCDXINDEX pIndex, BOOL bHeader ) } else { - if ( hb_fsSeekLarge( hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsRead( hFile, (BYTE *) byBuf, 4 ) != 4 ) + if ( hb_fileReadAt( pFile, (BYTE *) byBuf, 4, ulPos ) != 4 ) hb_errInternal( EDBF_READ, "hb_cdxIndexGetAvailPage: Read index page failed.", NULL, NULL ); pIndex->freePage = HB_GET_LE_UINT32( byBuf ); #ifdef HB_CDX_DBGUPDT @@ -1114,21 +1113,22 @@ static ULONG hb_cdxIndexGetAvailPage( LPCDXINDEX pIndex, BOOL bHeader ) if ( pIndex->nextAvail != CDX_DUMMYNODE ) ulPos = pIndex->nextAvail; else - ulPos = ( ULONG ) hb_fsSeekLarge( hFile, 0, FS_END ); + ulPos = ( ULONG ) hb_fileSize( pFile ); pIndex->nextAvail = ulPos + iCnt * CDX_PAGELEN; /* TODO: ### */ if ( bHeader ) { BYTE byBuf[CDX_PAGELEN]; + HB_FOFFSET fOffset = ulPos; memset( byBuf, 0, CDX_PAGELEN ); - if ( hb_fsSeekLarge( hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos ) - hb_errInternal( EDBF_WRITE, "Write in index page failed.(1)", NULL, NULL ); - while ( iCnt-- ) + do { - if ( hb_fsWrite( hFile, byBuf, CDX_PAGELEN ) != CDX_PAGELEN ) - hb_errInternal( EDBF_WRITE, "Write in index page failed.(2)", NULL, NULL ); + if( hb_fileWriteAt( pFile, byBuf, CDX_PAGELEN, fOffset ) != CDX_PAGELEN ) + hb_errInternal( EDBF_WRITE, "Write in index page failed.", NULL, NULL ); + fOffset += CDX_PAGELEN; } + while( --iCnt ); pIndex->fChanged = TRUE; } } @@ -1187,8 +1187,7 @@ static void hb_cdxIndexFlushAvailPage( LPCDXINDEX pIndex ) fClean = FALSE; } HB_PUT_LE_UINT32( byPageBuf, pLst->ulAddr ); - if ( hb_fsSeekLarge( pIndex->hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsWrite( pIndex->hFile, byPageBuf, CDX_PAGELEN ) != CDX_PAGELEN ) + if( hb_fileWriteAt( pIndex->pFile, byPageBuf, CDX_PAGELEN, ulPos ) != CDX_PAGELEN ) { hb_errInternal( EDBF_WRITE, "Write in index page failed.", NULL, NULL ); } @@ -1223,13 +1222,12 @@ static void hb_cdxIndexDropAvailPage( LPCDXINDEX pIndex ) static void hb_cdxIndexPageWrite( LPCDXINDEX pIndex, ULONG ulPos, BYTE * pBuffer, USHORT uiSize ) { - if ( pIndex->fReadonly ) + if( pIndex->fReadonly ) hb_errInternal( 9101, "hb_cdxIndexPageWrite on readonly database.", NULL, NULL ); - if ( pIndex->fShared && !pIndex->lockWrite ) + if( pIndex->fShared && !pIndex->lockWrite ) hb_errInternal( 9102, "hb_cdxIndexPageWrite on not locked index file.", NULL, NULL ); - if ( hb_fsSeekLarge( pIndex->hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsWrite( pIndex->hFile, pBuffer, uiSize ) != uiSize ) + if( hb_fileWriteAt( pIndex->pFile, pBuffer, uiSize, ulPos ) != ( ULONG ) uiSize ) hb_errInternal( EDBF_WRITE, "Write in index page failed.", NULL, NULL ); pIndex->fChanged = TRUE; #ifdef HB_CDX_DBGUPDT @@ -1243,11 +1241,10 @@ static void hb_cdxIndexPageWrite( LPCDXINDEX pIndex, ULONG ulPos, BYTE * pBuffer static void hb_cdxIndexPageRead( LPCDXINDEX pIndex, ULONG ulPos, BYTE * pBuffer, USHORT uiSize ) { - if ( pIndex->fShared && !( pIndex->lockRead || pIndex->lockWrite ) ) + if( pIndex->fShared && !( pIndex->lockRead || pIndex->lockWrite ) ) hb_errInternal( 9103, "hb_cdxIndexPageRead on not locked index file.", NULL, NULL ); - if ( hb_fsSeekLarge( pIndex->hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsRead( pIndex->hFile, pBuffer, uiSize ) != uiSize ) + if( hb_fileReadAt( pIndex->pFile, pBuffer, uiSize, ulPos ) != ( ULONG ) uiSize ) hb_errInternal( EDBF_READ, "hb_cdxIndexPageRead: Read index page failed.", NULL, NULL ); #ifdef HB_CDX_DBGUPDT cdxReadNO++; @@ -1262,10 +1259,9 @@ static void hb_cdxIndexCheckVersion( LPCDXINDEX pIndex ) BYTE byBuf[8]; ULONG ulVer, ulFree; - if ( hb_fsSeek( pIndex->hFile, 0x04, FS_SET ) != 0x04 || - hb_fsRead( pIndex->hFile, byBuf, 8 ) != 8 ) + if( hb_fileReadAt( pIndex->pFile, byBuf, 8, 0x04 ) != 8 ) { - if ( pIndex->lockWrite > 0 && hb_fsSeek( pIndex->hFile, 0, FS_END ) == 0 ) + if( pIndex->lockWrite > 0 && hb_fileSize( pIndex->pFile ) == 0 ) memset( byBuf, 0, 8 ); else hb_errInternal( 2155, "hb_cdxIndexCheckVersion: Read error on index heading page.", NULL, NULL ); @@ -1319,7 +1315,7 @@ static BOOL hb_cdxIndexLockRead( LPCDXINDEX pIndex ) } else { - ret = hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, + ret = hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_LOCK | FLX_SHARED | FLX_WAIT, &pIndex->ulLockPos ); if ( !ret ) hb_cdxErrorRT( pIndex->pArea, EG_LOCK, EDBF_LOCK, pIndex->szFileName, hb_fsError(), 0 ); @@ -1362,7 +1358,7 @@ static BOOL hb_cdxIndexLockWrite( LPCDXINDEX pIndex ) hb_errInternal( 9107, "hb_cdxIndexLockWrite: lock failure (*)", NULL, NULL ); pIndex->WrLck = TRUE; #endif - ret = hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, + ret = hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_LOCK | FLX_EXCLUSIVE | FLX_WAIT, &pIndex->ulLockPos ); } if ( !ret ) @@ -1415,7 +1411,7 @@ static BOOL hb_cdxIndexUnLockRead( LPCDXINDEX pIndex ) hb_errInternal( 9108, "hb_cdxIndexUnLockRead: unlock error (*)", NULL, NULL ); pIndex->RdLck = FALSE; #endif - if ( !hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) + if ( !hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) { hb_errInternal( 9108, "hb_cdxIndexUnLockRead: unlock error.", NULL, NULL ); } @@ -1456,8 +1452,7 @@ static BOOL hb_cdxIndexUnLockWrite( LPCDXINDEX pIndex ) (pIndex->ulVersion)++; HB_PUT_LE_UINT32( &byBuf[0], pIndex->freePage ); HB_PUT_BE_UINT32( &byBuf[4], pIndex->ulVersion ); - if ( hb_fsSeek( pIndex->hFile, 0x04, FS_SET ) != 0x04 || - hb_fsWrite( pIndex->hFile, byBuf, 8) != 8 ) + if( hb_fileWriteAt( pIndex->pFile, byBuf, 8, 0x04 ) != 8 ) { hb_errInternal( EDBF_WRITE, "Write in index page failed (ver)", NULL, NULL ); } @@ -1469,7 +1464,7 @@ static BOOL hb_cdxIndexUnLockWrite( LPCDXINDEX pIndex ) hb_errInternal( 9108, "hb_cdxIndexUnLockWrite: unlock error (*)", NULL, NULL ); pIndex->WrLck = FALSE; #endif - if ( !hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) + if ( !hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) { hb_errInternal( 9108, "hb_cdxIndexUnLockWrite: unlock error.", NULL, NULL ); } @@ -1480,8 +1475,7 @@ static BOOL hb_cdxIndexUnLockWrite( LPCDXINDEX pIndex ) { BYTE byBuf[4]; HB_PUT_LE_UINT32( &byBuf[0], pIndex->freePage ); - if ( hb_fsSeek( pIndex->hFile, 0x04, FS_SET ) != 0x04 || - hb_fsWrite( pIndex->hFile, byBuf, 4) != 4 ) + if( hb_fileWriteAt( pIndex->pFile, byBuf, 4, 0x04 ) != 4 ) { hb_errInternal( EDBF_WRITE, "Write in index page failed (ver.ex)", NULL, NULL ); } @@ -3537,7 +3531,7 @@ static void hb_cdxTagLoad( LPCDXTAG pTag ) * invalid key value length */ if( pTag->RootBlock == 0 || pTag->RootBlock % CDX_PAGELEN != 0 || - ( HB_FOFFSET ) pTag->RootBlock >= hb_fsSeekLarge( pTag->pIndex->hFile, 0, FS_END ) || + ( HB_FOFFSET ) pTag->RootBlock >= hb_fileSize( pTag->pIndex->pFile ) || HB_GET_LE_UINT16( tagHeader.keySize ) > CDX_MAXKEY || uiForPos + uiForLen > CDX_HEADEREXPLEN || uiKeyPos + uiKeyLen > CDX_HEADEREXPLEN || @@ -4730,8 +4724,7 @@ static void hb_cdxIndexReindex( LPCDXINDEX pIndex ) pIndex->ulVersion = 0; pIndex->nextAvail = 0; pIndex->freePage = 0; - hb_fsSeek( pIndex->hFile, 0, FS_SET ); - hb_fsWrite( pIndex->hFile, NULL, 0 ); + hb_fileTruncAt( pIndex->pFile, 0 ); pIndex->fChanged = TRUE; /* Rebuild the compound (master) tag */ @@ -4764,7 +4757,7 @@ static LPCDXINDEX hb_cdxIndexNew( CDXAREAP pArea ) pIndex = ( LPCDXINDEX ) hb_xgrab( sizeof( CDXINDEX ) ); memset( pIndex, 0, sizeof( CDXINDEX ) ); - pIndex->hFile = FS_ERROR; + pIndex->pFile = NULL; pIndex->pArea = pArea; pIndex->nextAvail = CDX_DUMMYNODE; return pIndex; @@ -4804,9 +4797,9 @@ static void hb_cdxIndexFree( LPCDXINDEX pIndex ) hb_cdxIndexFreeTags( pIndex ); /* Close file */ - if( pIndex->hFile != FS_ERROR ) + if( pIndex->pFile ) { - hb_fsClose( pIndex->hFile ); + hb_fileClose( pIndex->pFile ); if( pIndex->fDelete ) { hb_fsDelete( ( BYTE * ) ( pIndex->szRealName ? @@ -7309,9 +7302,9 @@ static ERRCODE hb_cdxFlush( CDXAREAP pArea ) pIndex = pArea->lpIndexes; while ( pIndex ) { - if ( pIndex->hFile != FS_ERROR && pIndex->fFlush ) + if ( pIndex->pFile && pIndex->fFlush ) { - hb_fsCommit( pIndex->hFile ); + hb_fileCommit( pIndex->pFile ); pIndex->fFlush = FALSE; } pIndex = pIndex->pNext; @@ -7747,7 +7740,7 @@ static ERRCODE hb_cdxZap ( CDXAREAP pArea ) static ERRCODE hb_cdxOrderListAdd( CDXAREAP pArea, LPDBORDERINFO pOrderInfo ) { USHORT uiFlags; - HB_FHANDLE hFile; + PHB_FILE pFile; char szBaseName[ CDX_MAXTAGNAMELEN + 1 ]; char szFileName[ _POSIX_PATH_MAX + 1 ]; LPCDXINDEX pIndex, * pIndexPtr; @@ -7785,33 +7778,33 @@ static ERRCODE hb_cdxOrderListAdd( CDXAREAP pArea, LPDBORDERINFO pOrderInfo ) ( pArea->fShared ? FO_DENYNONE : FO_EXCLUSIVE ); do { - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); - if( hFile == FS_ERROR ) + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); + if( !pFile ) bRetry = ( hb_cdxErrorRT( pArea, EG_OPEN, EDBF_OPEN_INDEX, szFileName, hb_fsError(), EF_CANRETRY | EF_CANDEFAULT ) == E_RETRY ); else { - if( hb_fsSeekLarge( hFile, 0, FS_END ) <= ( HB_FOFFSET ) sizeof( CDXTAGHEADER ) ) + if( hb_fileSize( pFile ) <= ( HB_FOFFSET ) sizeof( CDXTAGHEADER ) ) { - hb_fsClose( hFile ); - hFile = FS_ERROR; + hb_fileClose( pFile ); + pFile = NULL; hb_cdxErrorRT( pArea, EG_CORRUPTION, EDBF_CORRUPT, szFileName, hb_fsError(), EF_CANDEFAULT ); } bRetry = FALSE; } - } while ( bRetry ); + } while( bRetry ); - if ( hFile == FS_ERROR ) + if( !pFile ) { return FAILURE; } pIndex = hb_cdxIndexNew( pArea ); - pIndex->hFile = hFile; + pIndex->pFile = pFile; pIndex->fShared = pArea->fShared; pIndex->fReadonly = pArea->fReadonly; pIndex->szFileName = hb_strdup( szFileName ); @@ -8143,40 +8136,40 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo if ( !fOpenedIndex ) { - HB_FHANDLE hFile; + PHB_FILE pFile; BOOL bRetry, fShared = pArea->fShared && !fTemporary && !fExclusive; do { if( fTemporary ) { - hFile = hb_fsCreateTemp( NULL, NULL, FC_NORMAL, ( BYTE * ) szTempFile ); + pFile = hb_fileCreateTemp( NULL, NULL, FC_NORMAL, ( BYTE * ) szTempFile ); fNewFile = TRUE; } else { - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, NULL, FO_READWRITE | - ( fShared ? FO_DENYNONE : FO_EXCLUSIVE ) | - ( fNewFile ? FXO_TRUNCATE : FXO_APPEND ) | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, NULL, FO_READWRITE | + ( fShared ? FO_DENYNONE : FO_EXCLUSIVE ) | + ( fNewFile ? FXO_TRUNCATE : FXO_APPEND ) | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); } - if( hFile == FS_ERROR ) + if( !pFile ) bRetry = ( hb_cdxErrorRT( pArea, EG_CREATE, EDBF_CREATE, szFileName, hb_fsError(), EF_CANRETRY | EF_CANDEFAULT ) == E_RETRY ); else { bRetry = FALSE; - if ( !fNewFile ) - fNewFile = ( hb_fsSeekLarge( hFile, 0, FS_END ) == 0 ); + if( !fNewFile ) + fNewFile = ( hb_fileSize( pFile ) == 0 ); } } - while ( bRetry ); + while( bRetry ); - if ( hFile != FS_ERROR ) + if( pFile ) { pIndex = hb_cdxIndexNew( pArea ); - pIndex->hFile = hFile; + pIndex->pFile = pFile; pIndex->fShared = fShared; pIndex->fReadonly = FALSE; pIndex->szFileName = hb_strdup( szFileName ); @@ -8192,8 +8185,8 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo /* TODO: What should be default? */ /* hb_cdxIndexFree( pIndex ); - hb_fsClose( hFile ); - hFile = FS_ERROR; + hb_fileClose( pFile ); + pFile = NULL; hb_cdxErrorRT( pArea, EG_CORRUPTION, EDBF_CORRUPT, szFileName, hb_fsError(), EF_CANDEFAULT ); */ @@ -8203,7 +8196,7 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo } } - if ( hFile == FS_ERROR ) + if( !pFile ) { hb_vmDestroyBlockOrMacro( pKeyExp ); if ( pForExp != NULL ) @@ -8222,8 +8215,7 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo if ( fNewFile ) { - hb_fsSeek( pIndex->hFile, 0, FS_SET ); - hb_fsWrite( pIndex->hFile, NULL, 0 ); + hb_fileTruncAt( pIndex->pFile, 0 ); pIndex->fChanged = TRUE; hb_cdxIndexDropAvailPage( pIndex ); if ( pIndex->pCompound != NULL ) @@ -8734,7 +8726,8 @@ static ERRCODE hb_cdxOrderInfo( CDXAREAP pArea, USHORT uiIndex, LPDBORDERINFO pI break; case DBOI_FILEHANDLE: - pInfo->itmResult = hb_itemPutNInt( pInfo->itmResult, ( HB_NHANDLE ) ( pTag ? pTag->pIndex->hFile : FS_ERROR ) ); + pInfo->itmResult = hb_itemPutNInt( pInfo->itmResult, ( HB_NHANDLE ) + ( pTag ? hb_fileHandle( pTag->pIndex->pFile ) : FS_ERROR ) ); break; case DBOI_ISCOND: @@ -10065,11 +10058,10 @@ static void hb_cdxTagDoIndex( LPCDXTAG pTag, BOOL fReindex ) iRec = ulRecCount - ulRecNo + 1; if( ulNextCount > 0 && ulNextCount < ( ULONG ) iRec ) iRec = ( int ) ulNextCount; - hb_fsSeekLarge( pArea->hDataFile, - ( HB_FOFFSET ) pArea->uiHeaderLen + - ( HB_FOFFSET ) ( ulRecNo - 1 ) * - ( HB_FOFFSET ) pArea->uiRecordLen, FS_SET ); - hb_fsReadLarge( pArea->hDataFile, pSort->pRecBuff, pArea->uiRecordLen * iRec ); + hb_fileReadAt( pArea->pDataFile, pSort->pRecBuff, pArea->uiRecordLen * iRec, + ( HB_FOFFSET ) pArea->uiHeaderLen + + ( HB_FOFFSET ) ( ulRecNo - 1 ) * + ( HB_FOFFSET ) pArea->uiRecordLen ); iRecBuff = 0; } pArea->pRecord = pSort->pRecBuff + iRecBuff * pArea->uiRecordLen; diff --git a/harbour/contrib/hbbmcdx/hbbmcdx.h b/harbour/contrib/hbbmcdx/hbbmcdx.h index d35ce85c1d..74b2ca1316 100644 --- a/harbour/contrib/hbbmcdx/hbbmcdx.h +++ b/harbour/contrib/hbbmcdx/hbbmcdx.h @@ -373,7 +373,7 @@ typedef struct _CDXINDEX { char * szFileName; /* Name of index file */ char * szRealName; /* Real name of index file */ - HB_FHANDLE hFile; /* Index file handle */ + PHB_FILE pFile; /* Index file handle */ struct _CDXAREA * pArea; /* Parent WorkArea */ struct _CDXINDEX * pNext; /* The next index in the list */ LPCDXTAG pCompound; /* Compound tag */ @@ -481,9 +481,9 @@ typedef struct _CDXAREA * example. */ - HB_FHANDLE hDataFile; /* Data file handle */ - HB_FHANDLE hMemoFile; /* Memo file handle */ - HB_FHANDLE hMemoTmpFile; /* Memo temporary file handle */ + PHB_FILE pDataFile; /* Data file handle */ + PHB_FILE pMemoFile; /* Memo file handle */ + PHB_FILE pMemoTmpFile; /* Memo temporary file handle */ char * szDataFileName; /* Name of data file */ char * szMemoFileName; /* Name of memo file */ USHORT uiHeaderLen; /* Size of header */ diff --git a/harbour/contrib/hbwin/win_ole.c b/harbour/contrib/hbwin/win_ole.c index 7573b55376..ba6414dc7d 100644 --- a/harbour/contrib/hbwin/win_ole.c +++ b/harbour/contrib/hbwin/win_ole.c @@ -74,6 +74,7 @@ #include #include #include +#include #ifndef __MINGW32__ /* Missing in Mingw V 2. */ @@ -1419,15 +1420,16 @@ HB_FUNC( CREATEOLEOBJECT ) /* ( cOleName | cCLSID [, cIID ] [, cLicense ] ) */ { if( ISCHAR( 3 ) ) { - IClassFactory2 * pCF; + LPVOID * pCFPtr = NULL; - s_nOleError = CoGetClassObject( HB_ID_REF( REFCLSID, ClassID ), CLSCTX_SERVER, NULL, (LPIID) &IID_IClassFactory2, (LPVOID *) &pCF ); + s_nOleError = CoGetClassObject( HB_ID_REF( REFCLSID, ClassID ), CLSCTX_SERVER, NULL, HB_ID_REF( REFIID, IID_IClassFactory2 ), pCFPtr ); if( SUCCEEDED( s_nOleError ) ) { + IClassFactory2 * pCF = ( IClassFactory2 * ) pCFPtr; BSTR bstrLic = hb_oleAnsiToSysString( hb_parc( 3 ) ); - s_nOleError = pCF->lpVtbl->CreateInstanceLic( pCF, NULL, NULL, riid, bstrLic, &pDisp ); + s_nOleError = pCF->lpVtbl->CreateInstanceLic( pCF, NULL, NULL, (REFIID) riid, bstrLic, &pDisp ); SysFreeString( bstrLic ); pCF->lpVtbl->Release( pCF ); diff --git a/harbour/include/Makefile b/harbour/include/Makefile index 65fb578224..0b64be06ef 100644 --- a/harbour/include/Makefile +++ b/harbour/include/Makefile @@ -38,7 +38,6 @@ C_HEADERS=\ hbpp.h \ hbrddcdx.h \ hbrdddbf.h \ - hbrdddbt.h \ hbrddfpt.h \ hbrdddel.h \ hbrddntx.h \ @@ -48,6 +47,7 @@ C_HEADERS=\ hbsetup.h \ hbstack.h \ hbsxfunc.h \ + hbthread.h \ hbtrace.h \ hbtypes.h \ hbver.h \ diff --git a/harbour/include/hbapifs.h b/harbour/include/hbapifs.h index 3b1f459364..987a8f3d9c 100644 --- a/harbour/include/hbapifs.h +++ b/harbour/include/hbapifs.h @@ -173,6 +173,8 @@ extern HB_EXPORT BOOL hb_fsIsDirectory( BYTE * pFilename ); extern HB_EXPORT HB_FOFFSET hb_fsFSize ( BYTE * pszFileName, BOOL bUseDirEntry ); /* determine the size of a file */ extern HB_EXPORT HB_FHANDLE hb_fsExtOpen ( BYTE * pszFileName, BYTE * pDefExt, USHORT uiFlags, BYTE * pPaths, PHB_ITEM pError ); /* open a file using default extension and a list of paths */ +extern HB_EXPORT BYTE * hb_fsExtName ( BYTE * pFilename, BYTE * pDefExt, + USHORT uiExFlags, BYTE * pPaths ); /* convert file name for hb_fsExtOpen, caller must free the returned buffer */ extern HB_EXPORT USHORT hb_fsIsDrv ( BYTE nDrive ); /* determine if a drive number is a valid drive */ extern HB_EXPORT BOOL hb_fsIsDevice ( HB_FHANDLE hFileHandle ); /* determine if a file is attached to a device (console?) */ extern HB_EXPORT BOOL hb_fsLock ( HB_FHANDLE hFileHandle, ULONG ulStart, ULONG ulLength, USHORT uiMode ); /* request a lock on a portion of a file */ @@ -182,6 +184,7 @@ extern HB_EXPORT BOOL hb_fsMkDir ( BYTE * pszDirName ); /* create a d extern HB_EXPORT HB_FHANDLE hb_fsOpen ( BYTE * pszFileName, USHORT uiFlags ); /* open a file */ extern HB_EXPORT USHORT hb_fsRead ( HB_FHANDLE hFileHandle, BYTE * pBuff, USHORT ulCount ); /* read contents of a file into a buffer (<=64K) */ extern HB_EXPORT ULONG hb_fsReadLarge ( HB_FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCount ); /* read contents of a file into a buffer (>64K) */ +extern HB_EXPORT ULONG hb_fsReadAt ( HB_FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCount, HB_FOFFSET llOffset ); /* read from given offset contents of a file into a buffer (>64K) */ extern HB_EXPORT BOOL hb_fsRmDir ( BYTE * pszDirName ); /* remove a directory */ extern HB_EXPORT BOOL hb_fsRename ( BYTE * pszOldName, BYTE * pszNewName ); /* rename a file */ extern HB_EXPORT ULONG hb_fsSeek ( HB_FHANDLE hFileHandle, LONG lOffset, USHORT uiMode ); /* reposition an open file */ @@ -194,8 +197,10 @@ extern HB_EXPORT BOOL hb_fsGetAttr ( BYTE * pszFileName, ULONG * pulAtt extern HB_EXPORT BOOL hb_fsSetAttr ( BYTE * pszFileName, ULONG ulAttr ); extern HB_EXPORT void hb_fsSetError ( USHORT uiError ); /* set the file system DOS error number */ extern HB_EXPORT void hb_fsSetIOError ( BOOL fResult, USHORT uiOperation ); /* set the file system error number after IO operation */ +extern HB_EXPORT BOOL hb_fsTruncAt ( HB_FHANDLE hFileHandle, HB_FOFFSET llOffset ); /* truncate file to given size */ extern HB_EXPORT USHORT hb_fsWrite ( HB_FHANDLE hFileHandle, const BYTE * pBuff, USHORT ulCount ); /* write to an open file from a buffer (<=64K) */ extern HB_EXPORT ULONG hb_fsWriteLarge ( HB_FHANDLE hFileHandle, const BYTE * pBuff, ULONG ulCount ); /* write to an open file from a buffer (>64K) */ +extern HB_EXPORT ULONG hb_fsWriteAt ( HB_FHANDLE hFileHandle, const BYTE * pBuff, ULONG ulCount, HB_FOFFSET llOffset ); /* write to an open file at given offset from a buffer (>64K) */ extern HB_EXPORT HB_FHANDLE hb_fsPOpen ( BYTE * pFilename, BYTE * pMode ); extern HB_EXPORT HB_FHANDLE hb_fsGetOsHandle( HB_FHANDLE hFileHandle ); extern HB_EXPORT USHORT hb_fsGetFError ( void ); /* get FERROR() flag */ @@ -290,14 +295,34 @@ extern char * hb_fsAttrDecode( ULONG ulAttr, char * szAttr ); extern HB_EXPORT BYTE * hb_fsNameConv( BYTE * szFileName, BOOL * pfFree ); extern HB_EXPORT BOOL hb_fsMaxFilesError( void ); +/* Harbour file functions with shared file handles and locks + * (buffers in the future) + */ +#ifndef _HB_FILE_INTERNAL_ + typedef void * PHB_FILE; +#endif +HB_EXPORT PHB_FILE hb_fileExtOpen( BYTE * pFilename, BYTE * pDefExt, + USHORT uiExFlags, BYTE * pPaths, + PHB_ITEM pError ); +HB_EXPORT PHB_FILE hb_fileCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, + ULONG ulAttr, BYTE * pszName ); +HB_EXPORT void hb_fileClose( PHB_FILE pFile ); +HB_EXPORT BOOL hb_fileLock( PHB_FILE pFile, HB_FOFFSET ulStart, HB_FOFFSET ulLen, int iType ); +HB_EXPORT ULONG hb_fileReadAt( PHB_FILE pFile, BYTE * buffer, ULONG ulSize, HB_FOFFSET llOffset ); +HB_EXPORT ULONG hb_fileWriteAt( PHB_FILE pFile, const BYTE * buffer, ULONG ulSize, HB_FOFFSET llOffset ); +HB_EXPORT BOOL hb_fileTruncAt( PHB_FILE pFile, HB_FOFFSET llOffset ); +HB_EXPORT HB_FOFFSET hb_fileSize( PHB_FILE pFile ); +HB_EXPORT void hb_fileCommit( PHB_FILE pFile ); +HB_EXPORT HB_FHANDLE hb_fileHandle( PHB_FILE pFile ); + +/* wrapper to fopen() which calls hb_fsNameConv() */ +extern FILE * hb_fopen( const char *path, const char *mode ); + #ifdef HB_LEGACY_LEVEL /* Compatibility. Obsolete. */ extern HB_EXPORT BYTE * hb_fileNameConv( char * str ); #endif -/* wrapper to fopen() which calls hb_fsNameConv() */ -extern FILE * hb_fopen( const char *path, const char *mode ); - HB_EXTERN_END #endif /* HB_APIFS_H_ */ diff --git a/harbour/include/hbrddcdx.h b/harbour/include/hbrddcdx.h index fcd6f4e0f1..dce087bb4c 100644 --- a/harbour/include/hbrddcdx.h +++ b/harbour/include/hbrddcdx.h @@ -370,7 +370,7 @@ typedef struct _CDXINDEX { char * szFileName; /* Name of index file */ char * szRealName; /* Real name of index file */ - HB_FHANDLE hFile; /* Index file handle */ + PHB_FILE pFile; /* Index file handle */ struct _CDXAREA * pArea; /* Parent WorkArea */ struct _CDXINDEX * pNext; /* The next index in the list */ LPCDXTAG pCompound; /* Compound tag */ @@ -478,9 +478,9 @@ typedef struct _CDXAREA * example. */ - HB_FHANDLE hDataFile; /* Data file handle */ - HB_FHANDLE hMemoFile; /* Memo file handle */ - HB_FHANDLE hMemoTmpFile; /* Memo temporary file handle */ + PHB_FILE pDataFile; /* Data file handle */ + PHB_FILE pMemoFile; /* Memo file handle */ + PHB_FILE pMemoTmpFile; /* Memo temporary file handle */ char * szDataFileName; /* Name of data file */ char * szMemoFileName; /* Name of memo file */ USHORT uiHeaderLen; /* Size of header */ diff --git a/harbour/include/hbrdddbf.h b/harbour/include/hbrdddbf.h index 9b9eab1cf4..6ca0a9d86e 100644 --- a/harbour/include/hbrdddbf.h +++ b/harbour/include/hbrdddbf.h @@ -191,9 +191,9 @@ typedef struct _DBFAREA * example. */ - HB_FHANDLE hDataFile; /* Data file handle */ - HB_FHANDLE hMemoFile; /* Memo file handle */ - HB_FHANDLE hMemoTmpFile; /* Memo temporary file handle */ + PHB_FILE pDataFile; /* Data file handle */ + PHB_FILE pMemoFile; /* Memo file handle */ + PHB_FILE pMemoTmpFile; /* Memo temporary file handle */ char * szDataFileName; /* Name of data file */ char * szMemoFileName; /* Name of memo file */ USHORT uiHeaderLen; /* Size of header */ @@ -368,7 +368,7 @@ extern HB_EXPORT ERRCODE hb_dbfSetMemoData( DBFAREAP pArea, USHORT uiIndex, ULONG ulBlock, ULONG ulSize, ULONG ulType ); extern HB_EXPORT ERRCODE hb_dbfGetEGcode( ERRCODE errCode ); -extern HB_EXPORT BOOL hb_dbfLockIdxFile( HB_FHANDLE hFile, BYTE bScheme, USHORT usMode, HB_FOFFSET *pPoolPos ); +extern HB_EXPORT BOOL hb_dbfLockIdxFile( PHB_FILE pFile, BYTE bScheme, USHORT usMode, HB_FOFFSET *pPoolPos ); extern HB_EXPORT BOOL hb_dbfLockIdxGetData( BYTE bScheme, HB_FOFFSET *ulPos, HB_FOFFSET *ulPool ); #ifndef HB_CDP_SUPPORT_OFF diff --git a/harbour/include/hbrdddel.h b/harbour/include/hbrdddel.h index e86322c92d..5627682e9b 100644 --- a/harbour/include/hbrdddel.h +++ b/harbour/include/hbrdddel.h @@ -102,7 +102,7 @@ typedef struct _DELIMAREA * example. */ - HB_FHANDLE hFile; /* Data file handle */ + PHB_FILE pFile; /* Data file handle */ char * szFileName; /* Name of data file */ char * szEol; /* EOL marker */ USHORT uiEolLen; /* Size of EOL marker */ diff --git a/harbour/include/hbrddntx.h b/harbour/include/hbrddntx.h index d623f795fa..71bc36c13b 100644 --- a/harbour/include/hbrddntx.h +++ b/harbour/include/hbrddntx.h @@ -257,7 +257,7 @@ typedef struct _NTXINDEX ULONG NextAvail; ULONG TagBlock; /* Index attr, next free page */ struct _NTXAREA * Owner; - HB_FHANDLE DiskFile; + PHB_FILE DiskFile; BOOL fDelete; /* delete on close flag */ BOOL fReadonly; BOOL fShared; @@ -372,9 +372,9 @@ typedef struct _NTXAREA * example. */ - HB_FHANDLE hDataFile; /* Data file handle */ - HB_FHANDLE hMemoFile; /* Memo file handle */ - HB_FHANDLE hMemoTmpFile; /* Memo temporary file handle */ + PHB_FILE pDataFile; /* Data file handle */ + PHB_FILE pMemoFile; /* Memo file handle */ + PHB_FILE pMemoTmpFile; /* Memo temporary file handle */ char * szDataFileName; /* Name of data file */ char * szMemoFileName; /* Name of memo file */ USHORT uiHeaderLen; /* Size of header */ diff --git a/harbour/include/hbrddsdf.h b/harbour/include/hbrddsdf.h index 697e82d729..12c2d9de28 100644 --- a/harbour/include/hbrddsdf.h +++ b/harbour/include/hbrddsdf.h @@ -102,7 +102,7 @@ typedef struct _SDFAREA * example. */ - HB_FHANDLE hFile; /* Data file handle */ + PHB_FILE pFile; /* Data file handle */ char * szFileName; /* Name of data file */ char * szEol; /* EOL marker */ USHORT uiEolLen; /* Size of EOL marker */ diff --git a/harbour/include/hbstack.h b/harbour/include/hbstack.h index 001e003477..3f6012dc3c 100644 --- a/harbour/include/hbstack.h +++ b/harbour/include/hbstack.h @@ -197,9 +197,6 @@ typedef struct # endif # else extern HB_TLS_KEY hb_stack_key; -# define hb_stack_ptr ( ( PHB_STACK ) hb_tls_get( hb_stack_key ) ) -# endif -# if defined( HB_STACK_PRELOAD ) && !defined( HB_USE_TLS ) # if defined( __BORLANDC__ ) static __inline void* hb_stack_ptr_from_tls( void ) { @@ -212,9 +209,8 @@ typedef struct /* ret (if function is not inlined) */ return (void*) _EAX; } -# define HB_STACK_TLS_PRELOAD PHB_STACK _hb_stack_ptr_ = hb_stack_ptr_from_tls(); # elif defined( __MINGW32__ ) - static __inline__ void * hb_stack_ptr_from_tls( void ) + static __inline__ __attribute__ ((pure, malloc)) void * hb_stack_ptr_from_tls( void ) { void * p; __asm__ ( @@ -225,9 +221,15 @@ typedef struct ); return p; } -# define HB_STACK_TLS_PRELOAD PHB_STACK _hb_stack_ptr_ = hb_stack_ptr_from_tls(); +# define hb_stack_ptr ( ( PHB_STACK ) hb_stack_ptr_from_tls() ) # endif -# if defined( HB_STACK_TLS_PRELOAD ) +# if !defined( hb_stack_ptr ) +# define hb_stack_ptr ( ( PHB_STACK ) hb_tls_get( hb_stack_key ) ) +# endif +# endif +# if defined( HB_STACK_PRELOAD ) && !defined( HB_USE_TLS ) +# if defined( __BORLANDC__ ) || defined( __MINGW32__ ) +# define HB_STACK_TLS_PRELOAD PHB_STACK _hb_stack_ptr_ = ( PHB_STACK ) hb_stack_ptr_from_tls(); # undef hb_stack_ptr # else # define HB_STACK_TLS_PRELOAD PHB_STACK _hb_stack_ptr_ = hb_stack_ptr; diff --git a/harbour/source/rdd/dbf1.c b/harbour/source/rdd/dbf1.c index cc18222d84..67786a985a 100644 --- a/harbour/source/rdd/dbf1.c +++ b/harbour/source/rdd/dbf1.c @@ -200,16 +200,14 @@ static HB_LONG hb_dbfGetRowVer( DBFAREAP pArea, USHORT uiField, HB_LONG * pValue fLck = TRUE; } - hb_fsSeek( pArea->hDataFile, sizeof( DBFHEADER ) + - uiField * sizeof( DBFFIELD ), FS_SET ); - if( hb_fsRead( pArea->hDataFile, ( BYTE * ) &dbField, - sizeof( dbField ) ) == sizeof( dbField ) ) + if( hb_fileReadAt( pArea->pDataFile, ( BYTE * ) &dbField, sizeof( dbField ), + sizeof( DBFHEADER ) + uiField * sizeof( DBFFIELD ) ) == + sizeof( dbField ) ) { *pValue = HB_GET_LE_UINT64( &dbField.bReserved2 ) + 1; HB_PUT_LE_UINT64( dbField.bReserved2, *pValue ); - hb_fsSeek( pArea->hDataFile, sizeof( DBFHEADER ) + - uiField * sizeof( DBFFIELD ), FS_SET ); - hb_fsWrite( pArea->hDataFile, ( BYTE * ) &dbField, sizeof( dbField ) ); + hb_fileWriteAt( pArea->pDataFile, ( BYTE * ) &dbField, sizeof( dbField ), + sizeof( DBFHEADER ) + uiField * sizeof( DBFFIELD ) ); } if( fLck ) @@ -226,16 +224,14 @@ static HB_LONG hb_dbfGetNextValue( DBFAREAP pArea, USHORT uiField ) HB_LONG nValue = 0; DBFFIELD dbField; - hb_fsSeek( pArea->hDataFile, sizeof( DBFHEADER ) + - uiField * sizeof( DBFFIELD ), FS_SET ); - if( hb_fsRead( pArea->hDataFile, ( BYTE * ) &dbField, - sizeof( dbField ) ) == sizeof( dbField ) ) + if( hb_fileReadAt( pArea->pDataFile, ( BYTE * ) &dbField, sizeof( dbField ), + sizeof( DBFHEADER ) + uiField * sizeof( DBFFIELD ) ) == + sizeof( dbField ) ) { nValue = HB_GET_LE_UINT32( dbField.bCounter ); HB_PUT_LE_UINT32( dbField.bCounter, nValue + dbField.bStep ); - hb_fsSeek( pArea->hDataFile, sizeof( DBFHEADER ) + - uiField * sizeof( DBFFIELD ), FS_SET ); - hb_fsWrite( pArea->hDataFile, ( BYTE * ) &dbField, sizeof( dbField ) ); + hb_fileWriteAt( pArea->pDataFile, ( BYTE * ) &dbField, sizeof( dbField ), + sizeof( DBFHEADER ) + uiField * sizeof( DBFFIELD ) ); } return nValue; @@ -483,10 +479,10 @@ static ULONG hb_dbfCalcRecCount( DBFAREAP pArea ) { HB_TRACE(HB_TR_DEBUG, ("hb_dbfCalcRecCount(%p)", pArea)); - if( pArea->hDataFile == FS_ERROR ) + if( ! pArea->pDataFile ) return 0; else - return ( ULONG ) ( ( hb_fsSeekLarge( pArea->hDataFile, 0, FS_END ) - + return ( ULONG ) ( ( hb_fileSize( pArea->pDataFile ) - pArea->uiHeaderLen ) / pArea->uiRecordLen ); } @@ -517,11 +513,11 @@ static BOOL hb_dbfReadRecord( DBFAREAP pArea ) } /* Read data from file */ - hb_fsSeekLarge( pArea->hDataFile, ( HB_FOFFSET ) pArea->uiHeaderLen + - ( HB_FOFFSET ) ( pArea->ulRecNo - 1 ) * - ( HB_FOFFSET ) pArea->uiRecordLen, FS_SET ); - if( hb_fsRead( pArea->hDataFile, pArea->pRecord, pArea->uiRecordLen ) != - pArea->uiRecordLen ) + if( hb_fileReadAt( pArea->pDataFile, pArea->pRecord, pArea->uiRecordLen, + ( HB_FOFFSET ) pArea->uiHeaderLen + + ( HB_FOFFSET ) ( pArea->ulRecNo - 1 ) * + ( HB_FOFFSET ) pArea->uiRecordLen ) != + ( ULONG ) pArea->uiRecordLen ) { PHB_ITEM pError = hb_errNew(); @@ -1228,17 +1224,17 @@ HB_EXPORT BOOL hb_dbfLockIdxGetData( BYTE bScheme, HB_FOFFSET *ulPos, HB_FOFFSET * This function is common for different MEMO implementation * so I left it in DBF. */ -HB_EXPORT BOOL hb_dbfLockIdxFile( HB_FHANDLE hFile, BYTE bScheme, USHORT usMode, HB_FOFFSET *pPoolPos ) +HB_EXPORT BOOL hb_dbfLockIdxFile( PHB_FILE pFile, BYTE bScheme, USHORT usMode, HB_FOFFSET *pPoolPos ) { HB_FOFFSET ulPos, ulPool, ulSize = 1; - BOOL fRet = FALSE, fWait; + BOOL fRet = FALSE; if( !hb_dbfLockIdxGetData( bScheme, &ulPos, &ulPool ) ) { return fRet; } - do + for( ;; ) { switch( usMode & FL_MASK ) { @@ -1274,11 +1270,12 @@ HB_EXPORT BOOL hb_dbfLockIdxFile( HB_FHANDLE hFile, BYTE bScheme, USHORT usMode, default: return FALSE; } - fRet = hb_fsLockLarge( hFile, ulPos + *pPoolPos, ulSize, usMode ); - fWait = ( !fRet && ( usMode & FLX_WAIT ) != 0 && ( usMode & FL_MASK ) == FL_LOCK ); + fRet = hb_fileLock( pFile, ulPos + *pPoolPos, ulSize, usMode ); + if( fRet || ( usMode & FLX_WAIT ) == 0 || ( usMode & FL_MASK ) != FL_LOCK ) + break; /* TODO: call special error handler (LOCKHANDLER) here if fWait */ - - } while( fWait ); + hb_releaseCPU(); + } return fRet; } @@ -1785,12 +1782,12 @@ static ERRCODE hb_dbfFlush( DBFAREAP pArea ) { if( pArea->fDataFlush ) { - hb_fsCommit( pArea->hDataFile ); + hb_fileCommit( pArea->pDataFile ); pArea->fDataFlush = FALSE; } - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR && pArea->fMemoFlush ) + if( pArea->fHasMemo && pArea->pMemoFile && pArea->fMemoFlush ) { - hb_fsCommit( pArea->hMemoFile ); + hb_fileCommit( pArea->pMemoFile ); pArea->fMemoFlush = FALSE; } } @@ -2107,7 +2104,7 @@ static ERRCODE hb_dbfGoCold( DBFAREAP pArea ) if( pArea->fModStamp ) hb_dbfUpdateStampFields( pArea ); - + /* Write current record */ if( ! hb_dbfWriteRecord( pArea ) ) return FAILURE; @@ -2218,11 +2215,10 @@ static ERRCODE hb_dbfPutRec( DBFAREAP pArea, BYTE * pBuffer ) } /* Write data to file */ - hb_fsSeekLarge( pArea->hDataFile, ( HB_FOFFSET ) pArea->uiHeaderLen + - ( HB_FOFFSET ) ( pArea->ulRecNo - 1 ) * - ( HB_FOFFSET ) pArea->uiRecordLen, FS_SET ); - uiWritten = hb_fsWrite( pArea->hDataFile, pRecord, pArea->uiRecordLen ); - + uiWritten = hb_fileWriteAt( pArea->pDataFile, pRecord, pArea->uiRecordLen, + ( HB_FOFFSET ) pArea->uiHeaderLen + + ( HB_FOFFSET ) ( pArea->ulRecNo - 1 ) * + ( HB_FOFFSET ) pArea->uiRecordLen ); if( pRecord != pArea->pRecord ) hb_xfree( pRecord ); @@ -2639,7 +2635,7 @@ static ERRCODE hb_dbfClose( DBFAREAP pArea ) pArea->lpdbPendingRel = NULL; /* Update record and unlock records */ - if( pArea->hDataFile != FS_ERROR ) + if( pArea->pDataFile ) { /* update buffers */ SELF_GOCOLD( ( AREAP ) pArea ); @@ -2659,17 +2655,17 @@ static ERRCODE hb_dbfClose( DBFAREAP pArea ) SUPER_CLOSE( ( AREAP ) pArea ); - if( pArea->hDataFile != FS_ERROR ) + if( pArea->pDataFile ) { - hb_fsClose( pArea->hDataFile ); - pArea->hDataFile = FS_ERROR; + hb_fileClose( pArea->pDataFile ); + pArea->pDataFile = NULL; } /* Close the memo file */ - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR ) + if( pArea->fHasMemo && pArea->pMemoFile ) { - hb_fsClose( pArea->hMemoFile ); - pArea->hMemoFile = FS_ERROR; + hb_fileClose( pArea->pMemoFile ); + pArea->pMemoFile = NULL; } /* Free field offset array */ @@ -2721,9 +2717,11 @@ static ERRCODE hb_dbfClose( DBFAREAP pArea ) static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) { ERRCODE errCode = SUCCESS; - USHORT uiSize, uiCount; + ULONG ulSize; + USHORT uiCount; BOOL fRetry, fError, fRawBlob; - DBFFIELD * pBuffer, *pThisField; + DBFFIELD * pThisField; + BYTE * pBuffer; PHB_FNAME pFileName; PHB_ITEM pItem = NULL, pError; BYTE szFileName[ _POSIX_PATH_MAX + 1 ]; @@ -2827,11 +2825,11 @@ static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) /* Try create */ do { - pArea->hDataFile = hb_fsExtOpen( szFileName, NULL, - FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, pError ); - if( pArea->hDataFile == FS_ERROR ) + pArea->pDataFile = hb_fileExtOpen( szFileName, NULL, + FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, pError ); + if( ! pArea->pDataFile ) { if( !pError ) { @@ -2854,7 +2852,7 @@ static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) hb_itemRelease( pError ); } - if( pArea->hDataFile == FS_ERROR ) + if( ! pArea->pDataFile ) { pArea->lpdbOpenInfo = NULL; return FAILURE; @@ -2863,17 +2861,18 @@ static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) pArea->szDataFileName = hb_strdup( ( char * ) szFileName ); - uiSize = pArea->uiFieldCount * sizeof( DBFFIELD ); + ulSize = pArea->uiFieldCount * sizeof( DBFFIELD ) + + ( pArea->bTableType == DB_DBF_VFP ? 1 : 2 ); if( pArea->uiFieldCount ) { - pBuffer = ( DBFFIELD * ) hb_xgrab( uiSize ); - memset( pBuffer, 0, uiSize ); + pBuffer = ( BYTE * ) hb_xgrab( ulSize + 1 ); + memset( pBuffer, 0, ulSize ); } else { pBuffer = NULL; } - pThisField = pBuffer; + pThisField = ( DBFFIELD * ) pBuffer; pArea->fHasMemo = fError = FALSE; @@ -3078,8 +3077,7 @@ static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) pArea->fShared = FALSE; /* pCreateInfo->fShared; */ pArea->fReadonly = FALSE; /* pCreateInfo->fReadonly */ pArea->ulRecCount = 0; - pArea->uiHeaderLen = sizeof( DBFHEADER ) + uiSize + - ( pArea->bTableType == DB_DBF_VFP ? 1 : 2 ); + pArea->uiHeaderLen = ( USHORT ) ( sizeof( DBFHEADER ) + ulSize ); if( fRawBlob ) { pArea->fHasMemo = TRUE; @@ -3128,30 +3126,33 @@ static ERRCODE hb_dbfCreate( DBFAREAP pArea, LPDBOPENINFO pCreateInfo ) errCode = SELF_WRITEDBHEADER( ( AREAP ) pArea ); if( errCode != SUCCESS ) { - if( pBuffer ) - hb_xfree( pBuffer ); + hb_xfree( pBuffer ); SELF_CLOSE( ( AREAP ) pArea ); pArea->lpdbOpenInfo = NULL; return errCode; } /* Write fields and eof mark */ - if( hb_fsWrite( pArea->hDataFile, ( BYTE * ) pBuffer, uiSize ) != uiSize || - ( pArea->bTableType == DB_DBF_VFP ? - hb_fsWrite( pArea->hDataFile, ( BYTE * ) "\r\032", 2 ) != 2 : - hb_fsWrite( pArea->hDataFile, ( BYTE * ) "\r\0\032", 3 ) != 3 ) ) + if( pArea->bTableType == DB_DBF_VFP ) + pBuffer[ ulSize - 1 ] = '\r'; + else + { + pBuffer[ ulSize - 2 ] = '\r'; + pBuffer[ ulSize - 1 ] = '\0'; + } + pBuffer[ ulSize ] = ' '; + if( hb_fileWriteAt( pArea->pDataFile, pBuffer, ulSize + 1, + sizeof( DBFHEADER ) ) != ulSize + 1 ) { /* TODO: add RT error */ - if( pBuffer ) - hb_xfree( pBuffer ); + hb_xfree( pBuffer ); SELF_CLOSE( ( AREAP ) pArea ); pArea->lpdbOpenInfo = NULL; return FAILURE; } pArea->fDataFlush = TRUE; - if( pBuffer ) - hb_xfree( pBuffer ); + hb_xfree( pBuffer ); } /* Create memo file */ @@ -3236,7 +3237,7 @@ static ERRCODE hb_dbfInfo( DBFAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) break; case DBI_TABLETYPE: - if( pArea->hDataFile == FS_ERROR ) + if( !pArea->pDataFile ) { hb_itemClear( pItem ); return SELF_RDDINFO( SELF_RDDNODE( pArea ), RDDI_TABLETYPE, 0, pItem ); @@ -3245,11 +3246,11 @@ static ERRCODE hb_dbfInfo( DBFAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) break; case DBI_FILEHANDLE: - hb_itemPutNL( pItem, ( LONG ) pArea->hDataFile ); + hb_itemPutNInt( pItem, ( HB_NHANDLE ) hb_fileHandle( pArea->pDataFile ) ); break; case DBI_MEMOHANDLE: - hb_itemPutNL( pItem, ( LONG ) pArea->hMemoFile ); + hb_itemPutNInt( pItem, ( HB_NHANDLE ) hb_fileHandle( pArea->pMemoFile ) ); break; case DBI_SHARED: @@ -3552,7 +3553,7 @@ static ERRCODE hb_dbfNewArea( DBFAREAP pArea ) if( SUPER_NEW( ( AREAP ) pArea ) == FAILURE ) return FAILURE; - pArea->hDataFile = pArea->hMemoFile = pArea->hMemoTmpFile = FS_ERROR; + pArea->pDataFile = pArea->pMemoFile = pArea->pMemoTmpFile = NULL; pArea->fDataFlush = pArea->fMemoFlush = FALSE; /* Index dirty read flag initialized to global RDD setting */ pArea->uiDirtyRead = HB_IDXREAD_DEFAULT; @@ -3567,7 +3568,8 @@ static ERRCODE hb_dbfNewArea( DBFAREAP pArea ) static ERRCODE hb_dbfOpen( DBFAREAP pArea, LPDBOPENINFO pOpenInfo ) { ERRCODE errCode; - USHORT uiFlags, uiFields, uiSize, uiCount, uiSkip; + USHORT uiFlags, uiFields, uiCount, uiSkip; + ULONG ulSize; BOOL fRetry, fRawBlob; PHB_ITEM pError, pItem; PHB_FNAME pFileName; @@ -3691,10 +3693,10 @@ static ERRCODE hb_dbfOpen( DBFAREAP pArea, LPDBOPENINFO pOpenInfo ) /* Try open */ do { - pArea->hDataFile = hb_fsExtOpen( szFileName, NULL, uiFlags | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, pError ); - if( pArea->hDataFile == FS_ERROR ) + pArea->pDataFile = hb_fileExtOpen( szFileName, NULL, uiFlags | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, pError ); + if( !pArea->pDataFile ) { if( !pError ) { @@ -3719,7 +3721,7 @@ static ERRCODE hb_dbfOpen( DBFAREAP pArea, LPDBOPENINFO pOpenInfo ) } /* Exit if error */ - if( pArea->hDataFile == FS_ERROR ) + if( !pArea->pDataFile ) { SELF_CLOSE( ( AREAP ) pArea ); pArea->lpdbOpenInfo = NULL; @@ -3741,14 +3743,14 @@ static ERRCODE hb_dbfOpen( DBFAREAP pArea, LPDBOPENINFO pOpenInfo ) /* Add fields */ uiSkip = 0; uiFields = ( pArea->uiHeaderLen - sizeof( DBFHEADER ) ) / sizeof( DBFFIELD ); - uiSize = uiFields * sizeof( DBFFIELD ); - pBuffer = uiFields ? ( BYTE * ) hb_xgrab( uiSize ) : NULL; + ulSize = ( ULONG ) uiFields * sizeof( DBFFIELD ); + pBuffer = uiFields ? ( BYTE * ) hb_xgrab( ulSize ) : NULL; /* Read fields and exit if error */ do { - hb_fsSeek( pArea->hDataFile, sizeof( DBFHEADER ), FS_SET ); - if( hb_fsRead( pArea->hDataFile, pBuffer, uiSize ) != uiSize ) + if( hb_fileReadAt( pArea->pDataFile, pBuffer, ulSize, + sizeof( DBFHEADER ) ) != ulSize ) { errCode = FAILURE; if( !pError ) @@ -4591,9 +4593,9 @@ static ERRCODE hb_dbfRawLock( DBFAREAP pArea, USHORT uiAction, ULONG ulRecNo ) if( !pArea->fFLocked ) { if( iDir < 0 ) - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos - ulFlSize, ulFlSize, FL_LOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos - ulFlSize, ulFlSize, FL_LOCK ); else - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos + 1, ulFlSize, FL_LOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos + 1, ulFlSize, FL_LOCK ); if( !fLck ) uiErr = FAILURE; @@ -4606,9 +4608,9 @@ static ERRCODE hb_dbfRawLock( DBFAREAP pArea, USHORT uiAction, ULONG ulRecNo ) if( pArea->fFLocked ) { if( iDir < 0 ) - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos - ulFlSize, ulFlSize, FL_UNLOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos - ulFlSize, ulFlSize, FL_UNLOCK ); else - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos + 1, ulFlSize, FL_UNLOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos + 1, ulFlSize, FL_UNLOCK ); if( !fLck ) uiErr = FAILURE; @@ -4620,11 +4622,11 @@ static ERRCODE hb_dbfRawLock( DBFAREAP pArea, USHORT uiAction, ULONG ulRecNo ) if( !pArea->fFLocked ) { if( iDir < 0 ) - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos - ulRecNo, ulRlSize, FL_LOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos - ulRecNo, ulRlSize, FL_LOCK ); else if( iDir == 2 ) - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos + ( ulRecNo - 1 ) * pArea->uiRecordLen + pArea->uiHeaderLen, ulRlSize, FL_LOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos + ( ulRecNo - 1 ) * pArea->uiRecordLen + pArea->uiHeaderLen, ulRlSize, FL_LOCK ); else - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos + ulRecNo, ulRlSize, FL_LOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos + ulRecNo, ulRlSize, FL_LOCK ); if( !fLck ) uiErr = FAILURE; @@ -4635,11 +4637,11 @@ static ERRCODE hb_dbfRawLock( DBFAREAP pArea, USHORT uiAction, ULONG ulRecNo ) if( !pArea->fFLocked ) { if( iDir < 0 ) - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos - ulRecNo, ulRlSize, FL_UNLOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos - ulRecNo, ulRlSize, FL_UNLOCK ); else if( iDir == 2 ) - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos + ( ulRecNo - 1 ) * pArea->uiRecordLen + pArea->uiHeaderLen, ulRlSize, FL_UNLOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos + ( ulRecNo - 1 ) * pArea->uiRecordLen + pArea->uiHeaderLen, ulRlSize, FL_UNLOCK ); else - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos + ulRecNo, ulRlSize, FL_UNLOCK ); + fLck = hb_fileLock( pArea->pDataFile, ulPos + ulRecNo, ulRlSize, FL_UNLOCK ); if( !fLck ) uiErr = FAILURE; } @@ -4649,11 +4651,14 @@ static ERRCODE hb_dbfRawLock( DBFAREAP pArea, USHORT uiAction, ULONG ulRecNo ) case HEADER_LOCK: if( !pArea->fHeaderLocked ) { - do + for( ;; ) { - fLck = hb_fsLockLarge( pArea->hDataFile, ulPos, 1, FL_LOCK | FLX_WAIT ); + fLck = hb_fileLock( pArea->pDataFile, ulPos, 1, FL_LOCK | FLX_WAIT ); /* TODO: call special error handler (LOCKHANDLER) hiere if !fLck */ - } while( !fLck ); + if( fLck ) + break; + hb_releaseCPU(); + } if( !fLck ) uiErr = FAILURE; else @@ -4665,7 +4670,7 @@ static ERRCODE hb_dbfRawLock( DBFAREAP pArea, USHORT uiAction, ULONG ulRecNo ) case HEADER_UNLOCK: if( pArea->fHeaderLocked ) { - if( !hb_fsLockLarge( pArea->hDataFile, ulPos, 1, FL_UNLOCK ) ) + if( !hb_fileLock( pArea->pDataFile, ulPos, 1, FL_UNLOCK ) ) uiErr = FAILURE; pArea->fHeaderLocked = FALSE; } @@ -4786,25 +4791,25 @@ static ERRCODE hb_dbfGetValueFile( DBFAREAP pArea, USHORT uiIndex, BYTE * szFile pField = pArea->lpFields + uiIndex; if( pField->uiType == HB_FT_STRING ) { - HB_FHANDLE hFile; + PHB_FILE pFile; - hFile = hb_fsExtOpen( szFile, NULL, FO_WRITE | FO_EXCLUSIVE | - FXO_DEFAULTS | FXO_SHARELOCK | - ( uiMode == FILEGET_APPEND ? FXO_APPEND : FXO_TRUNCATE ), - NULL, NULL ); - if( hFile == FS_ERROR ) + pFile = hb_fileExtOpen( szFile, NULL, FO_WRITE | FO_EXCLUSIVE | + FXO_DEFAULTS | FXO_SHARELOCK | + ( uiMode == FILEGET_APPEND ? FXO_APPEND : FXO_TRUNCATE ), + NULL, NULL ); + if( !pFile ) { uiError = uiMode != FILEGET_APPEND ? EDBF_CREATE : EDBF_OPEN_DBF; } else { - hb_fsSeekLarge( hFile, 0, FS_END ); - if( hb_fsWrite( hFile, pArea->pRecord + pArea->pFieldOffset[ uiIndex ], - pField->uiLen ) != pField->uiLen ) + if( hb_fileWriteAt( pFile, pArea->pRecord + pArea->pFieldOffset[ uiIndex ], + pField->uiLen, hb_fileSize( pFile ) ) != + ( ULONG ) pField->uiLen ) { uiError = EDBF_WRITE; } - hb_fsClose( hFile ); + hb_fileClose( pFile ); } } else @@ -4886,22 +4891,22 @@ static ERRCODE hb_dbfPutValueFile( DBFAREAP pArea, USHORT uiIndex, BYTE * szFile pField = pArea->lpFields + uiIndex; if( pField->uiType == HB_FT_STRING ) { - HB_FHANDLE hFile; + PHB_FILE pFile; - hFile = hb_fsExtOpen( szFile, NULL, FO_READ | FO_DENYNONE | - FXO_DEFAULTS | FXO_SHARELOCK, NULL, NULL ); - if( hFile == FS_ERROR ) + pFile = hb_fileExtOpen( szFile, NULL, FO_READ | FO_DENYNONE | + FXO_DEFAULTS | FXO_SHARELOCK, NULL, NULL ); + if( !pFile ) { uiError = EDBF_OPEN_DBF; } else { - uiRead = hb_fsRead( hFile, pArea->pRecord + - pArea->pFieldOffset[ uiIndex ], pField->uiLen ); + uiRead = hb_fileReadAt( pFile, pArea->pRecord + pArea->pFieldOffset[ uiIndex ], + pField->uiLen, 0 ); if( uiRead != ( USHORT ) FS_ERROR && uiRead < pField->uiLen ) memset( pArea->pRecord + pArea->pFieldOffset[ uiIndex ] + uiRead, ' ', pField->uiLen - uiRead ); - hb_fsClose( hFile ); + hb_fileClose( pFile ); } } else @@ -4945,9 +4950,8 @@ static ERRCODE hb_dbfReadDBHeader( DBFAREAP pArea ) { fError = FALSE; - hb_fsSeek( pArea->hDataFile, 0, FS_SET ); - if( hb_fsRead( pArea->hDataFile, ( BYTE * ) &pArea->dbfHeader, - sizeof( DBFHEADER ) ) != sizeof( DBFHEADER ) ) + if( hb_fileReadAt( pArea->pDataFile, ( BYTE * ) &pArea->dbfHeader, + sizeof( DBFHEADER ), 0 ) != sizeof( DBFHEADER ) ) { fError = TRUE; } @@ -5118,20 +5122,19 @@ static ERRCODE hb_dbfWriteDBHeader( DBFAREAP pArea ) else { /* Exclusive mode */ - /* Seek to logical eof and write eof mark */ - hb_fsSeekLarge( pArea->hDataFile, ( HB_FOFFSET ) pArea->uiHeaderLen + - ( HB_FOFFSET ) pArea->uiRecordLen * - ( HB_FOFFSET ) pArea->ulRecCount, FS_SET ); - hb_fsWrite( pArea->hDataFile, ( BYTE * ) "\032", 1 ); - hb_fsWrite( pArea->hDataFile, NULL, 0 ); + /* write eof mark */ + HB_FOFFSET llOffset = ( HB_FOFFSET ) pArea->uiHeaderLen + + ( HB_FOFFSET ) pArea->uiRecordLen * + ( HB_FOFFSET ) pArea->ulRecCount; + hb_fileWriteAt( pArea->pDataFile, ( BYTE * ) "\032", 1, llOffset ); + hb_fileTruncAt( pArea->pDataFile, llOffset + 1 ); } HB_PUT_LE_UINT32( pArea->dbfHeader.ulRecCount, pArea->ulRecCount ); HB_PUT_LE_UINT16( pArea->dbfHeader.uiHeaderLen, pArea->uiHeaderLen ); HB_PUT_LE_UINT16( pArea->dbfHeader.uiRecordLen, pArea->uiRecordLen ); - hb_fsSeek( pArea->hDataFile, 0, FS_SET ); - if( hb_fsWrite( pArea->hDataFile, ( BYTE * ) &pArea->dbfHeader, - sizeof( DBFHEADER ) ) == sizeof( DBFHEADER ) ) + if( hb_fileWriteAt( pArea->pDataFile, ( BYTE * ) &pArea->dbfHeader, + sizeof( DBFHEADER ), 0 ) == sizeof( DBFHEADER ) ) { errCode = SUCCESS; } diff --git a/harbour/source/rdd/dbfcdx/dbfcdx1.c b/harbour/source/rdd/dbfcdx/dbfcdx1.c index f2b7c21dea..8630df63c5 100644 --- a/harbour/source/rdd/dbfcdx/dbfcdx1.c +++ b/harbour/source/rdd/dbfcdx/dbfcdx1.c @@ -1055,7 +1055,7 @@ static void hb_cdxIndexCheckBuffers( LPCDXINDEX pIndex ) */ static ULONG hb_cdxIndexGetAvailPage( LPCDXINDEX pIndex, BOOL bHeader ) { - HB_FHANDLE hFile = pIndex->hFile; + PHB_FILE pFile = pIndex->pFile; BYTE byBuf[4]; ULONG ulPos; @@ -1080,8 +1080,7 @@ static ULONG hb_cdxIndexGetAvailPage( LPCDXINDEX pIndex, BOOL bHeader ) } else { - if ( hb_fsSeekLarge( hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsRead( hFile, (BYTE *) byBuf, 4 ) != 4 ) + if ( hb_fileReadAt( pFile, (BYTE *) byBuf, 4, ulPos ) != 4 ) hb_errInternal( EDBF_READ, "hb_cdxIndexGetAvailPage: Read index page failed.", NULL, NULL ); pIndex->freePage = HB_GET_LE_UINT32( byBuf ); #ifdef HB_CDX_DBGUPDT @@ -1096,21 +1095,22 @@ static ULONG hb_cdxIndexGetAvailPage( LPCDXINDEX pIndex, BOOL bHeader ) if ( pIndex->nextAvail != CDX_DUMMYNODE ) ulPos = pIndex->nextAvail; else - ulPos = ( ULONG ) hb_fsSeekLarge( hFile, 0, FS_END ); + ulPos = ( ULONG ) hb_fileSize( pFile ); pIndex->nextAvail = ulPos + iCnt * CDX_PAGELEN; /* TODO: ### */ if ( bHeader ) { BYTE byBuf[CDX_PAGELEN]; + HB_FOFFSET fOffset = ulPos; memset( byBuf, 0, CDX_PAGELEN ); - if ( hb_fsSeekLarge( hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos ) - hb_errInternal( EDBF_WRITE, "Write in index page failed.(1)", NULL, NULL ); - while ( iCnt-- ) + do { - if ( hb_fsWrite( hFile, byBuf, CDX_PAGELEN ) != CDX_PAGELEN ) - hb_errInternal( EDBF_WRITE, "Write in index page failed.(2)", NULL, NULL ); + if( hb_fileWriteAt( pFile, byBuf, CDX_PAGELEN, fOffset ) != CDX_PAGELEN ) + hb_errInternal( EDBF_WRITE, "Write in index page failed.", NULL, NULL ); + fOffset += CDX_PAGELEN; } + while( --iCnt ); pIndex->fChanged = TRUE; } } @@ -1169,8 +1169,7 @@ static void hb_cdxIndexFlushAvailPage( LPCDXINDEX pIndex ) fClean = FALSE; } HB_PUT_LE_UINT32( byPageBuf, pLst->ulAddr ); - if ( hb_fsSeekLarge( pIndex->hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsWrite( pIndex->hFile, byPageBuf, CDX_PAGELEN ) != CDX_PAGELEN ) + if( hb_fileWriteAt( pIndex->pFile, byPageBuf, CDX_PAGELEN, ulPos ) != CDX_PAGELEN ) { hb_errInternal( EDBF_WRITE, "Write in index page failed.", NULL, NULL ); } @@ -1205,13 +1204,12 @@ static void hb_cdxIndexDropAvailPage( LPCDXINDEX pIndex ) static void hb_cdxIndexPageWrite( LPCDXINDEX pIndex, ULONG ulPos, BYTE * pBuffer, USHORT uiSize ) { - if ( pIndex->fReadonly ) + if( pIndex->fReadonly ) hb_errInternal( 9101, "hb_cdxIndexPageWrite on readonly database.", NULL, NULL ); - if ( pIndex->fShared && !pIndex->lockWrite ) + if( pIndex->fShared && !pIndex->lockWrite ) hb_errInternal( 9102, "hb_cdxIndexPageWrite on not locked index file.", NULL, NULL ); - if ( hb_fsSeekLarge( pIndex->hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsWrite( pIndex->hFile, pBuffer, uiSize ) != uiSize ) + if( hb_fileWriteAt( pIndex->pFile, pBuffer, uiSize, ulPos ) != ( ULONG ) uiSize ) hb_errInternal( EDBF_WRITE, "Write in index page failed.", NULL, NULL ); pIndex->fChanged = TRUE; #ifdef HB_CDX_DBGUPDT @@ -1225,11 +1223,10 @@ static void hb_cdxIndexPageWrite( LPCDXINDEX pIndex, ULONG ulPos, BYTE * pBuffer static void hb_cdxIndexPageRead( LPCDXINDEX pIndex, ULONG ulPos, BYTE * pBuffer, USHORT uiSize ) { - if ( pIndex->fShared && !( pIndex->lockRead || pIndex->lockWrite ) ) + if( pIndex->fShared && !( pIndex->lockRead || pIndex->lockWrite ) ) hb_errInternal( 9103, "hb_cdxIndexPageRead on not locked index file.", NULL, NULL ); - if ( hb_fsSeekLarge( pIndex->hFile, ulPos, FS_SET ) != ( HB_FOFFSET ) ulPos || - hb_fsRead( pIndex->hFile, pBuffer, uiSize ) != uiSize ) + if( hb_fileReadAt( pIndex->pFile, pBuffer, uiSize, ulPos ) != ( ULONG ) uiSize ) hb_errInternal( EDBF_READ, "hb_cdxIndexPageRead: Read index page failed.", NULL, NULL ); #ifdef HB_CDX_DBGUPDT cdxReadNO++; @@ -1244,10 +1241,9 @@ static void hb_cdxIndexCheckVersion( LPCDXINDEX pIndex ) BYTE byBuf[8]; ULONG ulVer, ulFree; - if ( hb_fsSeek( pIndex->hFile, 0x04, FS_SET ) != 0x04 || - hb_fsRead( pIndex->hFile, byBuf, 8 ) != 8 ) + if( hb_fileReadAt( pIndex->pFile, byBuf, 8, 0x04 ) != 8 ) { - if ( pIndex->lockWrite > 0 && hb_fsSeek( pIndex->hFile, 0, FS_END ) == 0 ) + if( pIndex->lockWrite > 0 && hb_fileSize( pIndex->pFile ) == 0 ) memset( byBuf, 0, 8 ); else hb_errInternal( 2155, "hb_cdxIndexCheckVersion: Read error on index heading page.", NULL, NULL ); @@ -1293,7 +1289,7 @@ static BOOL hb_cdxIndexLockRead( LPCDXINDEX pIndex ) pIndex->RdLck = TRUE; #endif - ret = hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, + ret = hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_LOCK | FLX_SHARED | FLX_WAIT, &pIndex->ulLockPos ); if ( !ret ) hb_cdxErrorRT( pIndex->pArea, EG_LOCK, EDBF_LOCK, pIndex->szFileName, hb_fsError(), 0 ); @@ -1334,7 +1330,7 @@ static BOOL hb_cdxIndexLockWrite( LPCDXINDEX pIndex ) hb_errInternal( 9107, "hb_cdxIndexLockWrite: lock failure (*)", NULL, NULL ); pIndex->WrLck = TRUE; #endif - ret = hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, + ret = hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_LOCK | FLX_EXCLUSIVE | FLX_WAIT, &pIndex->ulLockPos ); } if ( !ret ) @@ -1377,7 +1373,7 @@ static BOOL hb_cdxIndexUnLockRead( LPCDXINDEX pIndex ) hb_errInternal( 9108, "hb_cdxIndexUnLockRead: unlock error (*)", NULL, NULL ); pIndex->RdLck = FALSE; #endif - if ( !hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) + if ( !hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) { hb_errInternal( 9108, "hb_cdxIndexUnLockRead: unlock error.", NULL, NULL ); } @@ -1417,8 +1413,7 @@ static BOOL hb_cdxIndexUnLockWrite( LPCDXINDEX pIndex ) (pIndex->ulVersion)++; HB_PUT_LE_UINT32( &byBuf[0], pIndex->freePage ); HB_PUT_BE_UINT32( &byBuf[4], pIndex->ulVersion ); - if ( hb_fsSeek( pIndex->hFile, 0x04, FS_SET ) != 0x04 || - hb_fsWrite( pIndex->hFile, byBuf, 8) != 8 ) + if( hb_fileWriteAt( pIndex->pFile, byBuf, 8, 0x04 ) != 8 ) { hb_errInternal( EDBF_WRITE, "Write in index page failed (ver)", NULL, NULL ); } @@ -1430,7 +1425,7 @@ static BOOL hb_cdxIndexUnLockWrite( LPCDXINDEX pIndex ) hb_errInternal( 9108, "hb_cdxIndexUnLockWrite: unlock error (*)", NULL, NULL ); pIndex->WrLck = FALSE; #endif - if ( !hb_dbfLockIdxFile( pIndex->hFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) + if ( !hb_dbfLockIdxFile( pIndex->pFile, pIndex->pArea->bLockType, FL_UNLOCK, &pIndex->ulLockPos ) ) { hb_errInternal( 9108, "hb_cdxIndexUnLockWrite: unlock error.", NULL, NULL ); } @@ -1441,8 +1436,7 @@ static BOOL hb_cdxIndexUnLockWrite( LPCDXINDEX pIndex ) { BYTE byBuf[4]; HB_PUT_LE_UINT32( &byBuf[0], pIndex->freePage ); - if ( hb_fsSeek( pIndex->hFile, 0x04, FS_SET ) != 0x04 || - hb_fsWrite( pIndex->hFile, byBuf, 4) != 4 ) + if( hb_fileWriteAt( pIndex->pFile, byBuf, 4, 0x04 ) != 4 ) { hb_errInternal( EDBF_WRITE, "Write in index page failed (ver.ex)", NULL, NULL ); } @@ -3498,7 +3492,7 @@ static void hb_cdxTagLoad( LPCDXTAG pTag ) * invalid key value length */ if( pTag->RootBlock == 0 || pTag->RootBlock % CDX_PAGELEN != 0 || - ( HB_FOFFSET ) pTag->RootBlock >= hb_fsSeekLarge( pTag->pIndex->hFile, 0, FS_END ) || + ( HB_FOFFSET ) pTag->RootBlock >= hb_fileSize( pTag->pIndex->pFile ) || HB_GET_LE_UINT16( tagHeader.keySize ) > CDX_MAXKEY || uiForPos + uiForLen > CDX_HEADEREXPLEN || uiKeyPos + uiKeyLen > CDX_HEADEREXPLEN || @@ -4712,8 +4706,7 @@ static void hb_cdxIndexReindex( LPCDXINDEX pIndex ) pIndex->ulVersion = 0; pIndex->nextAvail = 0; pIndex->freePage = 0; - hb_fsSeek( pIndex->hFile, 0, FS_SET ); - hb_fsWrite( pIndex->hFile, NULL, 0 ); + hb_fileTruncAt( pIndex->pFile, 0 ); pIndex->fChanged = TRUE; /* Rebuild the compound (master) tag */ @@ -4746,7 +4739,7 @@ static LPCDXINDEX hb_cdxIndexNew( CDXAREAP pArea ) pIndex = ( LPCDXINDEX ) hb_xgrab( sizeof( CDXINDEX ) ); memset( pIndex, 0, sizeof( CDXINDEX ) ); - pIndex->hFile = FS_ERROR; + pIndex->pFile = NULL; pIndex->pArea = pArea; pIndex->nextAvail = CDX_DUMMYNODE; return pIndex; @@ -4786,9 +4779,9 @@ static void hb_cdxIndexFree( LPCDXINDEX pIndex ) hb_cdxIndexFreeTags( pIndex ); /* Close file */ - if( pIndex->hFile != FS_ERROR ) + if( pIndex->pFile ) { - hb_fsClose( pIndex->hFile ); + hb_fileClose( pIndex->pFile ); if( pIndex->fDelete ) { hb_fsDelete( ( BYTE * ) ( pIndex->szRealName ? @@ -6773,9 +6766,9 @@ static ERRCODE hb_cdxFlush( CDXAREAP pArea ) pIndex = pArea->lpIndexes; while ( pIndex ) { - if ( pIndex->hFile != FS_ERROR && pIndex->fFlush ) + if ( pIndex->pFile && pIndex->fFlush ) { - hb_fsCommit( pIndex->hFile ); + hb_fileCommit( pIndex->pFile ); pIndex->fFlush = FALSE; } pIndex = pIndex->pNext; @@ -7162,7 +7155,7 @@ static ERRCODE hb_cdxZap ( CDXAREAP pArea ) static ERRCODE hb_cdxOrderListAdd( CDXAREAP pArea, LPDBORDERINFO pOrderInfo ) { USHORT uiFlags; - HB_FHANDLE hFile; + PHB_FILE pFile; char szBaseName[ CDX_MAXTAGNAMELEN + 1 ]; char szFileName[ _POSIX_PATH_MAX + 1 ]; LPCDXINDEX pIndex, * pIndexPtr; @@ -7200,33 +7193,33 @@ static ERRCODE hb_cdxOrderListAdd( CDXAREAP pArea, LPDBORDERINFO pOrderInfo ) ( pArea->fShared ? FO_DENYNONE : FO_EXCLUSIVE ); do { - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); - if( hFile == FS_ERROR ) + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); + if( !pFile ) bRetry = ( hb_cdxErrorRT( pArea, EG_OPEN, EDBF_OPEN_INDEX, szFileName, hb_fsError(), EF_CANRETRY | EF_CANDEFAULT ) == E_RETRY ); else { - if( hb_fsSeekLarge( hFile, 0, FS_END ) <= ( HB_FOFFSET ) sizeof( CDXTAGHEADER ) ) + if( hb_fileSize( pFile ) <= ( HB_FOFFSET ) sizeof( CDXTAGHEADER ) ) { - hb_fsClose( hFile ); - hFile = FS_ERROR; + hb_fileClose( pFile ); + pFile = NULL; hb_cdxErrorRT( pArea, EG_CORRUPTION, EDBF_CORRUPT, szFileName, hb_fsError(), EF_CANDEFAULT ); } bRetry = FALSE; } - } while ( bRetry ); + } while( bRetry ); - if ( hFile == FS_ERROR ) + if( !pFile ) { return FAILURE; } pIndex = hb_cdxIndexNew( pArea ); - pIndex->hFile = hFile; + pIndex->pFile = pFile; pIndex->fShared = pArea->fShared; pIndex->fReadonly = pArea->fReadonly; pIndex->szFileName = hb_strdup( szFileName ); @@ -7602,40 +7595,40 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo if ( !fOpenedIndex ) { - HB_FHANDLE hFile; + PHB_FILE pFile; BOOL bRetry, fShared = pArea->fShared && !fTemporary && !fExclusive; do { if( fTemporary ) { - hFile = hb_fsCreateTemp( NULL, NULL, FC_NORMAL, ( BYTE * ) szTempFile ); + pFile = hb_fileCreateTemp( NULL, NULL, FC_NORMAL, ( BYTE * ) szTempFile ); fNewFile = TRUE; } else { - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, NULL, FO_READWRITE | - ( fShared ? FO_DENYNONE : FO_EXCLUSIVE ) | - ( fNewFile ? FXO_TRUNCATE : FXO_APPEND ) | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, NULL, FO_READWRITE | + ( fShared ? FO_DENYNONE : FO_EXCLUSIVE ) | + ( fNewFile ? FXO_TRUNCATE : FXO_APPEND ) | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); } - if( hFile == FS_ERROR ) + if( !pFile ) bRetry = ( hb_cdxErrorRT( pArea, EG_CREATE, EDBF_CREATE, szFileName, hb_fsError(), EF_CANRETRY | EF_CANDEFAULT ) == E_RETRY ); else { bRetry = FALSE; - if ( !fNewFile ) - fNewFile = ( hb_fsSeekLarge( hFile, 0, FS_END ) == 0 ); + if( !fNewFile ) + fNewFile = ( hb_fileSize( pFile ) == 0 ); } } - while ( bRetry ); + while( bRetry ); - if ( hFile != FS_ERROR ) + if( pFile ) { pIndex = hb_cdxIndexNew( pArea ); - pIndex->hFile = hFile; + pIndex->pFile = pFile; pIndex->fShared = fShared; pIndex->fReadonly = FALSE; pIndex->szFileName = hb_strdup( szFileName ); @@ -7651,8 +7644,8 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo /* TODO: What should be default? */ /* hb_cdxIndexFree( pIndex ); - hb_fsClose( hFile ); - hFile = FS_ERROR; + hb_fileClose( pFile ); + pFile = NULL; hb_cdxErrorRT( pArea, EG_CORRUPTION, EDBF_CORRUPT, szFileName, hb_fsError(), EF_CANDEFAULT ); */ @@ -7662,7 +7655,7 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo } } - if ( hFile == FS_ERROR ) + if( !pFile ) { hb_vmDestroyBlockOrMacro( pKeyExp ); if ( pForExp != NULL ) @@ -7681,8 +7674,7 @@ static ERRCODE hb_cdxOrderCreate( CDXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo if ( fNewFile ) { - hb_fsSeek( pIndex->hFile, 0, FS_SET ); - hb_fsWrite( pIndex->hFile, NULL, 0 ); + hb_fileTruncAt( pIndex->pFile, 0 ); pIndex->fChanged = TRUE; hb_cdxIndexDropAvailPage( pIndex ); if ( pIndex->pCompound != NULL ) @@ -8193,7 +8185,8 @@ static ERRCODE hb_cdxOrderInfo( CDXAREAP pArea, USHORT uiIndex, LPDBORDERINFO pI break; case DBOI_FILEHANDLE: - pInfo->itmResult = hb_itemPutNInt( pInfo->itmResult, ( HB_NHANDLE ) ( pTag ? pTag->pIndex->hFile : FS_ERROR ) ); + pInfo->itmResult = hb_itemPutNInt( pInfo->itmResult, ( HB_NHANDLE ) + ( pTag ? hb_fileHandle( pTag->pIndex->pFile ) : FS_ERROR ) ); break; case DBOI_ISCOND: @@ -9426,11 +9419,10 @@ static void hb_cdxTagDoIndex( LPCDXTAG pTag, BOOL fReindex ) iRec = ulRecCount - ulRecNo + 1; if( ulNextCount > 0 && ulNextCount < ( ULONG ) iRec ) iRec = ( int ) ulNextCount; - hb_fsSeekLarge( pArea->hDataFile, - ( HB_FOFFSET ) pArea->uiHeaderLen + - ( HB_FOFFSET ) ( ulRecNo - 1 ) * - ( HB_FOFFSET ) pArea->uiRecordLen, FS_SET ); - hb_fsReadLarge( pArea->hDataFile, pSort->pRecBuff, pArea->uiRecordLen * iRec ); + hb_fileReadAt( pArea->pDataFile, pSort->pRecBuff, pArea->uiRecordLen * iRec, + ( HB_FOFFSET ) pArea->uiHeaderLen + + ( HB_FOFFSET ) ( ulRecNo - 1 ) * + ( HB_FOFFSET ) pArea->uiRecordLen ); iRecBuff = 0; } pArea->pRecord = pSort->pRecBuff + iRecBuff * pArea->uiRecordLen; diff --git a/harbour/source/rdd/dbffpt/dbffpt1.c b/harbour/source/rdd/dbffpt/dbffpt1.c index 415176a66a..90746af93c 100644 --- a/harbour/source/rdd/dbffpt/dbffpt1.c +++ b/harbour/source/rdd/dbffpt/dbffpt1.c @@ -77,6 +77,9 @@ # include "hbapicdp.h" #endif +#define FPT_BLOCK_OFFSET( b ) ( ( HB_FOFFSET ) ( b ) * \ + ( HB_FOFFSET ) pArea->uiMemoBlockSize ) + static USHORT s_uiRddIdBLOB = ( USHORT ) -1; static USHORT s_uiRddIdFPT = ( USHORT ) -1; @@ -291,11 +294,14 @@ static BOOL hb_fptFileLockEx( FPTAREAP pArea, BOOL fWait ) } else { - do + for( ;; ) { - fRet = hb_fsLock( pArea->hMemoFile, FPT_LOCKPOS, FPT_LOCKSIZE, - FL_LOCK | FLX_EXCLUSIVE | ( fWait ? FLX_WAIT : 0 ) ); - } while( !fRet && fWait ); + fRet = hb_fileLock( pArea->pMemoFile, FPT_LOCKPOS, FPT_LOCKSIZE, + FL_LOCK | FLX_EXCLUSIVE | ( fWait ? FLX_WAIT : 0 ) ); + if( fRet || !fWait ) + break; + hb_releaseCPU(); + } } return fRet; } @@ -315,11 +321,14 @@ static BOOL hb_fptFileLockSh( FPTAREAP pArea, BOOL fWait ) } else { - do + for( ;; ) { - fRet = hb_fsLock( pArea->hMemoFile, FPT_LOCKPOS, FPT_LOCKSIZE, - FL_LOCK | FLX_SHARED | ( fWait ? FLX_WAIT : 0 ) ); - } while( !fRet && fWait ); + fRet = hb_fileLock( pArea->pMemoFile, FPT_LOCKPOS, FPT_LOCKSIZE, + FL_LOCK | FLX_SHARED | ( fWait ? FLX_WAIT : 0 ) ); + if( fRet || !fWait ) + break; + hb_releaseCPU(); + } } return fRet; } @@ -329,7 +338,7 @@ static BOOL hb_fptFileLockSh( FPTAREAP pArea, BOOL fWait ) */ static BOOL hb_fptFileUnLock( FPTAREAP pArea ) { - return !pArea->fShared || hb_fsLock( pArea->hMemoFile, FPT_LOCKPOS, FPT_LOCKSIZE, FL_UNLOCK ); + return !pArea->fShared || hb_fileLock( pArea->pMemoFile, FPT_LOCKPOS, FPT_LOCKSIZE, FL_UNLOCK ); } /* @@ -352,8 +361,8 @@ static BOOL hb_fptRootBlockLock( FPTAREAP pArea ) if( !pArea->fShared ) fRet = TRUE; else - fRet = hb_fsLock( pArea->hMemoFile, FPT_ROOTBLOCK_OFFSET, 4, - FL_LOCK | FLX_EXCLUSIVE ); + fRet = hb_fileLock( pArea->pMemoFile, FPT_ROOTBLOCK_OFFSET, 4, + FL_LOCK | FLX_EXCLUSIVE ); return fRet; } @@ -363,7 +372,7 @@ static BOOL hb_fptRootBlockLock( FPTAREAP pArea ) static BOOL hb_fptRootBlockUnLock( FPTAREAP pArea ) { return !pArea->fShared || - hb_fsLock( pArea->hMemoFile, FPT_ROOTBLOCK_OFFSET, 4, FL_UNLOCK ); + hb_fileLock( pArea->pMemoFile, FPT_ROOTBLOCK_OFFSET, 4, FL_UNLOCK ); } /* @@ -377,8 +386,7 @@ static ERRCODE hb_fptGetRootBlock( FPTAREAP pArea, ULONG * pulBlock ) { BYTE buffer[ 4 ]; - hb_fsSeek( pArea->hMemoFile, FPT_ROOTBLOCK_OFFSET, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, buffer, 4 ) == 4 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 4, FPT_ROOTBLOCK_OFFSET ) == 4 ) { *pulBlock = HB_GET_LE_UINT32( buffer ); return SUCCESS; @@ -399,8 +407,7 @@ static ERRCODE hb_fptPutRootBlock( FPTAREAP pArea, ULONG ulBlock ) BYTE buffer[ 4 ]; HB_PUT_LE_UINT32( buffer, ulBlock ); - hb_fsSeek( pArea->hMemoFile, FPT_ROOTBLOCK_OFFSET, FS_SET ); - if( hb_fsWrite( pArea->hMemoFile, buffer, 4 ) == 4 ) + if( hb_fileWriteAt( pArea->pMemoFile, buffer, 4, FPT_ROOTBLOCK_OFFSET ) == 4 ) return SUCCESS; else return EDBF_WRITE; @@ -604,11 +611,10 @@ static ERRCODE hb_fptWriteGCitems( FPTAREAP pArea, LPMEMOGCTABLE pGCtable, USHOR HB_PUT_BE_UINT32( fptBlock.type, FPTIT_FLEX_UNUSED ); HB_PUT_BE_UINT32( fptBlock.size, pArea->uiMemoBlockSize * pGCtable->pGCitems[i].ulSize - sizeof( FPTBLOCK ) ); - hb_fsSeekLarge( pArea->hMemoFile, - ( HB_FOFFSET ) pGCtable->pGCitems[i].ulOffset * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hb_fsWrite( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) != sizeof( FPTBLOCK ) ) + if( hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), + FPT_BLOCK_OFFSET( pGCtable->pGCitems[i].ulOffset ) ) != + sizeof( FPTBLOCK ) ) { errCode = EDBF_WRITE; } @@ -639,10 +645,9 @@ static ERRCODE hb_fptGCfreeBlock( FPTAREAP pArea, LPMEMOGCTABLE pGCtable, { FPTBLOCK fptBlock; - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulOffset * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) == sizeof( FPTBLOCK ) ) + if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), FPT_BLOCK_OFFSET( ulOffset ) ) == + sizeof( FPTBLOCK ) ) { ulByteSize = HB_GET_BE_UINT32( fptBlock.size ) + sizeof( FPTBLOCK ); } @@ -829,8 +834,8 @@ static ERRCODE hb_fptReadGCdata( FPTAREAP pArea, LPMEMOGCTABLE pGCtable ) hb_fptDestroyGCdata( pGCtable ); memset( &pGCtable->fptHeader, 0, sizeof( FPTHEADER ) ); - hb_fsSeek( pArea->hMemoFile, 0, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &pGCtable->fptHeader, sizeof( FPTHEADER ) ) >= 512 ) + if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &pGCtable->fptHeader, + sizeof( FPTHEADER ), 0 ) >= 512 ) { if( pArea->bMemoType == DB_MEMO_SMT || pArea->bMemoType == DB_MEMO_DBT ) pGCtable->ulNextBlock = HB_GET_LE_UINT32( pGCtable->fptHeader.nextBlock ); @@ -872,22 +877,24 @@ static ERRCODE hb_fptReadGCdata( FPTAREAP pArea, LPMEMOGCTABLE pGCtable ) pGCtable->ulCounter = HB_GET_LE_UINT32( pGCtable->fptHeader.counter ); if( pGCtable->ulDirPage ) { - hb_fsSeekLarge( pArea->hMemoFile, pGCtable->ulDirPage, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) != sizeof( FPTBLOCK ) || - HB_GET_BE_UINT32( fptBlock.type ) != FPTIT_FLEX_GC ) + if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), pGCtable->ulDirPage ) != + sizeof( FPTBLOCK ) || + HB_GET_BE_UINT32( fptBlock.type ) != FPTIT_FLEX_GC ) { return EDBF_CORRUPT; } pGCtable->ulSize = HB_GET_BE_UINT32( fptBlock.size ); bPageBuf = ( BYTE * ) hb_xgrab( pGCtable->ulSize ); - if( hb_fsRead( pArea->hMemoFile, bPageBuf, ( USHORT ) pGCtable->ulSize ) != - ( USHORT ) pGCtable->ulSize ) + if( hb_fileReadAt( pArea->pMemoFile, bPageBuf, + pGCtable->ulSize, + pGCtable->ulDirPage + sizeof( FPTBLOCK ) ) != + pGCtable->ulSize ) { hb_xfree( bPageBuf ); return EDBF_CORRUPT; } - pGCtable->usMaxItem = (USHORT ) ( ( pGCtable->ulSize - 2 ) >> 3 ); + pGCtable->usMaxItem = ( USHORT ) ( ( pGCtable->ulSize - 2 ) >> 3 ); pGCtable->usItems = ( HB_GET_LE_UINT16( bPageBuf ) - 3 ) >> 2; pGCtable->pGCitems = ( LPMEMOGCITEM ) hb_xgrab( sizeof( MEMOGCITEM ) * @@ -935,7 +942,7 @@ static ERRCODE hb_fptWriteGCdata( FPTAREAP pArea, LPMEMOGCTABLE pGCtable ) for( i = j; i < pGCtable->usItems; i++ ) { HB_PUT_LE_UINT16( &pGCtable->fptHeader.reserved2[ ( i - j ) * 6 ], - (( USHORT ) pGCtable->pGCitems[i].ulSize ) ); + ( ( USHORT ) pGCtable->pGCitems[i].ulSize ) ); HB_PUT_LE_UINT32( &pGCtable->fptHeader.reserved2[ ( i - j ) * 6 + 2 ], pGCtable->pGCitems[i].ulOffset ); } @@ -988,7 +995,7 @@ static ERRCODE hb_fptWriteGCdata( FPTAREAP pArea, LPMEMOGCTABLE pGCtable ) HB_PUT_BE_UINT32( fptBlock.size, pGCtable->ulSize ); bPageBuf = ( BYTE * ) hb_xgrab( pGCtable->ulSize ); memset( bPageBuf, 0xAD, pGCtable->ulSize ); - HB_PUT_LE_UINT16( bPageBuf, ( (USHORT) usItems << 2 ) + 3 ); + HB_PUT_LE_UINT16( bPageBuf, ( ( USHORT ) usItems << 2 ) + 3 ); j = pGCtable->usItems - usItems; for( i = j; i < pGCtable->usItems; i++ ) { @@ -997,11 +1004,12 @@ static ERRCODE hb_fptWriteGCdata( FPTAREAP pArea, LPMEMOGCTABLE pGCtable ) HB_PUT_LE_UINT32( &bPageBuf[ ( i - j ) * 8 + 6 ], pGCtable->pGCitems[i].ulSize * pArea->uiMemoBlockSize ); } - hb_fsSeekLarge( pArea->hMemoFile, pGCtable->ulDirPage, FS_SET ); - if( hb_fsWrite( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) != sizeof( FPTBLOCK ) || - hb_fsWrite( pArea->hMemoFile, bPageBuf, - ( USHORT ) pGCtable->ulSize ) != ( USHORT ) pGCtable->ulSize ) + if( hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), pGCtable->ulDirPage ) != + sizeof( FPTBLOCK ) || + hb_fileWriteAt( pArea->pMemoFile, bPageBuf, pGCtable->ulSize, + pGCtable->ulDirPage + sizeof( FPTBLOCK ) ) != + pGCtable->ulSize ) { errCode = EDBF_WRITE; } @@ -1014,11 +1022,12 @@ static ERRCODE hb_fptWriteGCdata( FPTAREAP pArea, LPMEMOGCTABLE pGCtable ) HB_PUT_LE_UINT32( &bPageBuf[ ( i - j ) * 8 + 6 ], pGCtable->pGCitems[i].ulOffset * pArea->uiMemoBlockSize ); } - hb_fsSeekLarge( pArea->hMemoFile, pGCtable->ulRevPage, FS_SET ); - if( hb_fsWrite( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) != sizeof( FPTBLOCK ) || - hb_fsWrite( pArea->hMemoFile, bPageBuf, - ( USHORT )pGCtable->ulSize ) != ( USHORT ) pGCtable->ulSize ) + if( hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), pGCtable->ulRevPage ) != + sizeof( FPTBLOCK ) || + hb_fileWriteAt( pArea->pMemoFile, bPageBuf, pGCtable->ulSize, + pGCtable->ulRevPage + sizeof( FPTBLOCK ) ) != + pGCtable->ulSize ) { errCode = EDBF_WRITE; } @@ -1040,19 +1049,15 @@ static ERRCODE hb_fptWriteGCdata( FPTAREAP pArea, LPMEMOGCTABLE pGCtable ) HB_PUT_LE_UINT32( pGCtable->fptHeader.nextBlock, pGCtable->ulNextBlock ); else HB_PUT_BE_UINT32( pGCtable->fptHeader.nextBlock, pGCtable->ulNextBlock ); - hb_fsSeek( pArea->hMemoFile, 0, FS_SET ); - if( hb_fsWrite( pArea->hMemoFile, ( BYTE * ) &pGCtable->fptHeader, - ( USHORT ) ulHdrSize ) != ( USHORT ) ulHdrSize ) + if( hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) &pGCtable->fptHeader, + ulHdrSize, 0 ) != ulHdrSize ) { errCode = EDBF_WRITE; } else if( pGCtable->ulNextBlock < pGCtable->ulPrevBlock ) { /* trunc file */ - hb_fsSeekLarge( pArea->hMemoFile, - ( HB_FOFFSET ) pGCtable->ulNextBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - hb_fsWrite( pArea->hMemoFile, NULL, 0 ); + hb_fileTruncAt( pArea->pMemoFile, FPT_BLOCK_OFFSET( pGCtable->ulNextBlock ) ); } } pArea->fMemoFlush = TRUE; @@ -1079,26 +1084,27 @@ static ULONG hb_fptGetMemoLen( FPTAREAP pArea, USHORT uiIndex ) if( ulSize == 0 && ( pArea->bMemoType == DB_MEMO_DBT || pArea->bMemoType == DB_MEMO_FPT ) ) { - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); + HB_FOFFSET fOffset = FPT_BLOCK_OFFSET( ulBlock ); if( pArea->bMemoType == DB_MEMO_DBT ) { BYTE pBlock[ DBT_DEFBLOCKSIZE ]; - int iLen, i; + ULONG ulLen, u; do { - iLen = hb_fsRead( pArea->hMemoFile, pBlock, DBT_DEFBLOCKSIZE ); - if( iLen <= 0 ) + ulLen = hb_fileReadAt( pArea->pMemoFile, pBlock, DBT_DEFBLOCKSIZE, fOffset ); + fOffset += ulLen; + if( ulLen <= 0 ) break; - i = 0; - while( i < iLen && pBlock[ i ] != 0x1A ) - i++; - ulSize += i; - } while( i == DBT_DEFBLOCKSIZE ); + u = 0; + while( u < ulLen && pBlock[ u ] != 0x1A ) + u++; + ulSize += u; + } while( u == DBT_DEFBLOCKSIZE ); } - else if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) == sizeof( FPTBLOCK ) ) + else if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), fOffset ) == + sizeof( FPTBLOCK ) ) ulSize = HB_GET_BE_UINT32( fptBlock.size ); } return ulSize; @@ -1124,10 +1130,9 @@ static const char * hb_fptGetMemoType( FPTAREAP pArea, USHORT uiIndex ) { if( ulType == 0 && pArea->bMemoType == DB_MEMO_FPT ) { - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) != sizeof( FPTBLOCK ) ) + if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), FPT_BLOCK_OFFSET( ulBlock ) ) != + sizeof( FPTBLOCK ) ) return "U"; ulType = HB_GET_BE_UINT32( fptBlock.type ); } @@ -1270,52 +1275,48 @@ static ULONG hb_fptCountSMTItemLength( FPTAREAP pArea, PHB_ITEM pItem, /* * Calculate the size of SMT memo data */ -static ERRCODE hb_fptCountSMTDataLength( FPTAREAP pArea, ULONG * ulLen ) +static ERRCODE hb_fptCountSMTDataLength( FPTAREAP pArea, HB_FOFFSET * pfOffset ) { USHORT i, uiSize; BYTE buffer[ 2 ]; - if( hb_fsRead( pArea->hMemoFile, buffer, 1 ) != 1 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 1, *pfOffset ) != 1 ) return EDBF_READ; - *ulLen += 1; + *pfOffset += 1; switch( buffer[ 0 ] ) { case SMT_IT_ARRAY: - if( hb_fsRead( pArea->hMemoFile, buffer, 2 ) != 2 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 2, *pfOffset ) != 2 ) return EDBF_READ; - *ulLen += 2; + *pfOffset += 2; uiSize = HB_GET_LE_UINT16( buffer ); for( i = 0; i < uiSize; i++ ) { - ERRCODE errCode = hb_fptCountSMTDataLength( pArea, ulLen ); + ERRCODE errCode = hb_fptCountSMTDataLength( pArea, pfOffset ); if( errCode != SUCCESS ) return errCode; } break; case SMT_IT_CHAR: - if( hb_fsRead( pArea->hMemoFile, buffer, 2 ) != 2 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 2, *pfOffset ) != 2 ) return EDBF_READ; uiSize = HB_GET_LE_UINT16( buffer ); - *ulLen += uiSize + 2; - hb_fsSeek( pArea->hMemoFile, uiSize, FS_RELATIVE ); + *pfOffset += uiSize + 2; break; case SMT_IT_INT: case SMT_IT_DATE: - *ulLen += 4; - hb_fsSeek( pArea->hMemoFile, 4, FS_RELATIVE ); + *pfOffset += 4; break; case SMT_IT_DOUBLE: - *ulLen += 10; - hb_fsSeek( pArea->hMemoFile, 10, FS_RELATIVE ); + *pfOffset += 10; break; case SMT_IT_LOGICAL: - *ulLen += 1; - hb_fsSeek( pArea->hMemoFile, 1, FS_RELATIVE ); + *pfOffset += 1; break; case SMT_IT_NIL: @@ -1418,41 +1419,46 @@ static ULONG hb_fptStoreSMTItem( FPTAREAP pArea, PHB_ITEM pItem, BYTE ** bBufPtr /* * Read SMT item from file */ -static ERRCODE hb_fptReadRawSMTItem( FPTAREAP pArea, PHB_ITEM pItem ) +static ERRCODE hb_fptReadRawSMTItem( FPTAREAP pArea, PHB_ITEM pItem, HB_FOFFSET *pfOffset ) { ULONG ulLen, i; BYTE buffer[ 10 ], *pBuffer; int iWidth, iDec; - if( hb_fsRead( pArea->hMemoFile, buffer, 1 ) != 1 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 1, *pfOffset ) != 1 ) return EDBF_READ; + *pfOffset += 1; switch( buffer[ 0 ] ) { case SMT_IT_ARRAY: - if( hb_fsRead( pArea->hMemoFile, buffer, 2 ) != 2 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 2, *pfOffset ) != 2 ) return EDBF_READ; + *pfOffset += 2; ulLen = HB_GET_LE_UINT16( buffer ); hb_arrayNew( pItem, ulLen ); for( i = 1; i <= ulLen; i++ ) { - ERRCODE errCode = hb_fptReadRawSMTItem( pArea, hb_arrayGetItemPtr( pItem, i ) ); + ERRCODE errCode = hb_fptReadRawSMTItem( pArea, + hb_arrayGetItemPtr( pItem, i ), pfOffset ); if( errCode != SUCCESS ) return errCode; } break; case SMT_IT_CHAR: - if( hb_fsRead( pArea->hMemoFile, buffer, 2 ) != 2 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 2, *pfOffset ) != 2 ) return EDBF_READ; + *pfOffset += 2; ulLen = HB_GET_LE_UINT16( buffer ); pBuffer = ( BYTE * ) hb_xgrab( ulLen + 1 ); - if( hb_fsRead( pArea->hMemoFile, pBuffer, ( USHORT ) ulLen ) != ( USHORT ) ulLen ) + if( hb_fileReadAt( pArea->pMemoFile, pBuffer, ulLen, *pfOffset ) != ulLen ) { hb_xfree( pBuffer ); return EDBF_READ; } + *pfOffset += ulLen; #ifndef HB_CDP_SUPPORT_OFF hb_cdpnTranslate( ( char *) pBuffer, pArea->cdPage, hb_vmCDP(), ulLen ); #endif @@ -1460,14 +1466,16 @@ static ERRCODE hb_fptReadRawSMTItem( FPTAREAP pArea, PHB_ITEM pItem ) break; case SMT_IT_INT: - if( hb_fsRead( pArea->hMemoFile, buffer, 4 ) != 4 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 4, *pfOffset ) != 4 ) return EDBF_READ; + *pfOffset += 4; hb_itemPutNInt( pItem, ( LONG ) HB_GET_LE_UINT32( buffer ) ); break; case SMT_IT_DOUBLE: - if( hb_fsRead( pArea->hMemoFile, buffer, 10 ) != 10 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 10, *pfOffset ) != 10 ) return EDBF_READ; + *pfOffset += 10; iWidth = buffer[ 0 ]; iDec = buffer[ 1 ]; if( iDec ) @@ -1476,14 +1484,16 @@ static ERRCODE hb_fptReadRawSMTItem( FPTAREAP pArea, PHB_ITEM pItem ) break; case SMT_IT_DATE: - if( hb_fsRead( pArea->hMemoFile, buffer, 4 ) != 4 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 4, *pfOffset ) != 4 ) return EDBF_READ; + *pfOffset += 4; hb_itemPutDL( pItem, ( LONG ) HB_GET_LE_UINT32( buffer ) ); break; case SMT_IT_LOGICAL: - if( hb_fsRead( pArea->hMemoFile, buffer, 1 ) != 1 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 1, *pfOffset ) != 1 ) return EDBF_READ; + *pfOffset += 1; hb_itemPutL( pItem, buffer[ 0 ] != 0 ); break; @@ -2401,7 +2411,9 @@ static ERRCODE hb_fptReadFlexItem( FPTAREAP pArea, BYTE ** pbMemoBuf, BYTE * bBu return errCode; } -static ERRCODE hb_fptCopyToFile( HB_FHANDLE hSrc, HB_FHANDLE hDst, HB_FOFFSET size ) +static ERRCODE hb_fptCopyToRawFile( PHB_FILE pSrc, HB_FOFFSET from, + HB_FHANDLE hDst, + HB_FOFFSET size ) { ERRCODE errCode = SUCCESS; if( size ) @@ -2415,8 +2427,9 @@ static ERRCODE hb_fptCopyToFile( HB_FHANDLE hSrc, HB_FHANDLE hDst, HB_FOFFSET si do { - ulRead = hb_fsReadLarge( hSrc, pBuffer, ( ULONG ) - HB_MIN( ( HB_FOFFSET ) ulBufSize, size - written ) ); + ulRead = hb_fileReadAt( pSrc, pBuffer, ( ULONG ) + HB_MIN( ( HB_FOFFSET ) ulBufSize, size - written ), + from + written ); if( ulRead <= 0 ) errCode = EDBF_READ; else if( hb_fsWriteLarge( hDst, pBuffer, ulRead ) != ulRead ) @@ -2432,6 +2445,41 @@ static ERRCODE hb_fptCopyToFile( HB_FHANDLE hSrc, HB_FHANDLE hDst, HB_FOFFSET si return errCode; } +static ERRCODE hb_fptCopyToFile( PHB_FILE pSrc, HB_FOFFSET from, + PHB_FILE pDst, HB_FOFFSET to, + HB_FOFFSET size ) +{ + ERRCODE errCode = SUCCESS; + if( size ) + { + HB_FOFFSET written = 0; + ULONG ulBufSize, ulRead; + BYTE * pBuffer; + + ulBufSize = ( ULONG ) HB_MIN( 0x10000, size ); + pBuffer = ( BYTE * ) hb_xgrab( ulBufSize ); + + do + { + ulRead = hb_fileReadAt( pSrc, pBuffer, ( ULONG ) + HB_MIN( ( HB_FOFFSET ) ulBufSize, size - written ), + from + written ); + if( ulRead <= 0 ) + errCode = EDBF_READ; + else if( hb_fileWriteAt( pDst, pBuffer, ulRead, + to + written ) != ulRead ) + errCode = EDBF_WRITE; + else + written += ulRead; + } + while( errCode == SUCCESS && written < size ); + + hb_xfree( pBuffer ); + } + + return errCode; +} + static ERRCODE hb_fptReadRawBlock( FPTAREAP pArea, BYTE * bBuffer, HB_FHANDLE hFile, ULONG ulBlock, ULONG ulSize ) { @@ -2440,16 +2488,15 @@ static ERRCODE hb_fptReadRawBlock( FPTAREAP pArea, BYTE * bBuffer, HB_FHANDLE hF if( ulBlock == 0 ) return EDBF_CORRUPT; - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hFile != FS_ERROR ) { - errCode = hb_fptCopyToFile( pArea->hMemoFile, hFile, ulSize ); + errCode = hb_fptCopyToRawFile( pArea->pMemoFile, FPT_BLOCK_OFFSET( ulBlock ), + hFile, ulSize ); } else { - if( hb_fsReadLarge( pArea->hMemoFile, bBuffer, ulSize ) != ulSize ) + if( hb_fileReadAt( pArea->pMemoFile, bBuffer, ulSize, + FPT_BLOCK_OFFSET( ulBlock ) ) != ulSize ) errCode = EDBF_READ; } @@ -2469,15 +2516,13 @@ static ERRCODE hb_fptReadBlobBlock( FPTAREAP pArea, PHB_ITEM pItem, /* TODO: uiMode => BLOB_IMPORT_COMPRESS, BLOB_IMPORT_ENCRYPT */ HB_SYMBOL_UNUSED( uiMode ); - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - - if( hb_fsReadLarge( pArea->hMemoFile, buffer, 4 ) != 4 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 4, FPT_BLOCK_OFFSET( ulBlock ) ) != 4 ) return EDBF_READ; ulSize = HB_GET_LE_UINT32( buffer ); if( hFile != FS_ERROR ) - return hb_fptCopyToFile( pArea->hMemoFile, hFile, ulSize ); + return hb_fptCopyToRawFile( pArea->pMemoFile, FPT_BLOCK_OFFSET( ulBlock ) + 4, + hFile, ulSize ); if( ulSize == 0 ) hb_itemPutC( pItem, NULL ); @@ -2490,7 +2535,8 @@ static ERRCODE hb_fptReadBlobBlock( FPTAREAP pArea, PHB_ITEM pItem, /* in most cases this means that file is corrupted */ return EDBF_CORRUPT; } - if( hb_fsReadLarge( pArea->hMemoFile, bBuffer, ulSize ) != ulSize ) + if( hb_fileReadAt( pArea->pMemoFile, bBuffer, ulSize, + FPT_BLOCK_OFFSET( ulBlock ) + 4 ) != ulSize ) { hb_xfree( bBuffer ); return EDBF_READ; @@ -2506,12 +2552,10 @@ static ERRCODE hb_fptReadSMTBlock( FPTAREAP pArea, PHB_ITEM pItem, if( ulBlock == 0 ) return EDBF_CORRUPT; - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( ulSize == 0 ) { - return hb_fptReadRawSMTItem( pArea, pItem ); + HB_FOFFSET fOffset = FPT_BLOCK_OFFSET( ulBlock ); + return hb_fptReadRawSMTItem( pArea, pItem, &fOffset ); } else { @@ -2524,7 +2568,8 @@ static ERRCODE hb_fptReadSMTBlock( FPTAREAP pArea, PHB_ITEM pItem, return EDBF_CORRUPT; } - if( hb_fsReadLarge( pArea->hMemoFile, bBuffer, ulSize ) != ulSize ) + if( hb_fileReadAt( pArea->pMemoFile, bBuffer, ulSize, + FPT_BLOCK_OFFSET( ulBlock ) ) != ulSize ) { errCode = EDBF_READ; } @@ -2571,15 +2616,15 @@ static ERRCODE hb_fptGetMemo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem, if( ulBlock > 0 ) { + HB_FOFFSET fOffset = FPT_BLOCK_OFFSET( ulBlock ); if( pArea->bMemoType == DB_MEMO_FPT ) { - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) != sizeof( FPTBLOCK ) ) + if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), fOffset ) != sizeof( FPTBLOCK ) ) { return EDBF_READ; } + fOffset += sizeof( FPTBLOCK ); ulType = HB_GET_BE_UINT32( fptBlock.type ); ulSize = HB_GET_BE_UINT32( fptBlock.size ); } @@ -2590,8 +2635,6 @@ static ERRCODE hb_fptGetMemo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem, ulSize = hb_fptGetMemoLen( pArea, uiIndex ); ulType = FPTIT_BINARY; } - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); } if( ulStart || ulCount ) @@ -2615,11 +2658,11 @@ static ERRCODE hb_fptGetMemo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem, if( ulCount && ulCount < ulSize ) ulSize = ulCount; if( ulStart && ulSize ) - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulStart, FS_RELATIVE ); + fOffset += ulStart; if( hFile != FS_ERROR ) { - return hb_fptCopyToFile( pArea->hMemoFile, hFile, ulSize ); + return hb_fptCopyToRawFile( pArea->pMemoFile, fOffset, hFile, ulSize ); } if( pArea->bMemoType == DB_MEMO_FPT ) @@ -2632,14 +2675,15 @@ static ERRCODE hb_fptGetMemo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem, { pBuffer = ( BYTE * ) hb_xalloc( ulSize + 1 ); } - + if( !pBuffer ) { /* in most cases this means that file is corrupted */ return EDBF_CORRUPT; } - if( ulSize != 0 && hb_fsReadLarge( pArea->hMemoFile, pBuffer, ulSize ) != ulSize ) + if( ulSize != 0 && hb_fileReadAt( pArea->pMemoFile, pBuffer, ulSize, + fOffset ) != ulSize ) { errCode = EDBF_READ; } @@ -2811,6 +2855,8 @@ static ERRCODE hb_fptWriteMemo( FPTAREAP pArea, ULONG ulBlock, ULONG ulSize, /* Write memo header and data */ if( bWrite ) { + HB_FOFFSET fOffset; + errCode = hb_fptGCgetFreeBlock( pArea, &fptGCtable, ulStoredBlock, ulLen, ulType == FPTIT_DUMMY ); if( errCode != SUCCESS ) @@ -2819,17 +2865,17 @@ static ERRCODE hb_fptWriteMemo( FPTAREAP pArea, ULONG ulBlock, ULONG ulSize, return errCode; } - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) *ulStoredBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - + fOffset = FPT_BLOCK_OFFSET( *ulStoredBlock ); if( pArea->bMemoType == DB_MEMO_FPT && ulType != FPTIT_DUMMY ) { FPTBLOCK fptBlock; HB_PUT_BE_UINT32( fptBlock.type, ulType ); HB_PUT_BE_UINT32( fptBlock.size, ulLen ); - if( hb_fsWrite( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) != sizeof( FPTBLOCK ) ) + if( hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), fOffset ) != sizeof( FPTBLOCK ) ) errCode = EDBF_WRITE; + else + fOffset +=sizeof( FPTBLOCK ); } if( errCode == SUCCESS && ulLen > 0 ) @@ -2846,19 +2892,26 @@ static ERRCODE hb_fptWriteMemo( FPTAREAP pArea, ULONG ulBlock, ULONG ulSize, HB_MIN( ulBufSize, ulLen - ulWritten ) ); if( ulRead <= 0 ) errCode = EDBF_READ; - else if( hb_fsWriteLarge( pArea->hMemoFile, bBuffer, - ulRead ) != ulRead ) + else if( hb_fileWriteAt( pArea->pMemoFile, bBuffer, + ulRead, fOffset ) != ulRead ) errCode = EDBF_WRITE; else + { ulWritten += ulRead; + fOffset += ulRead; + } } while( errCode == SUCCESS && ulWritten < ulLen ); hb_xfree(bBuffer); } - else if( hb_fsWriteLarge( pArea->hMemoFile, bBufPtr, ulLen ) != ulLen ) + else { - errCode = EDBF_WRITE; + if( hb_fileWriteAt( pArea->pMemoFile, bBufPtr, + ulLen, fOffset ) != ulLen ) + errCode = EDBF_WRITE; + else + fOffset += ulLen; } } /* if written block is smaller then block size we should write at last @@ -2867,17 +2920,15 @@ static ERRCODE hb_fptWriteMemo( FPTAREAP pArea, ULONG ulBlock, ULONG ulSize, { if( pArea->bMemoType == DB_MEMO_DBT ) { - hb_fsWrite( pArea->hMemoFile, ( BYTE * ) "\x1A\x1A", 2 ); + hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) "\x1A\x1A", 2, fOffset ); } else if( pArea->uiMemoVersion == DB_MEMOVER_FLEX && ( ulLen + sizeof( FPTBLOCK ) ) % pArea->uiMemoBlockSize != 0 ) { ULONG ulBlocks = ( ulLen + sizeof( FPTBLOCK ) + pArea->uiMemoBlockSize - 1 ) / pArea->uiMemoBlockSize; - hb_fsSeekLarge( pArea->hMemoFile, - ( HB_FOFFSET ) ( *ulStoredBlock + ulBlocks ) * - ( HB_FOFFSET ) pArea->uiMemoBlockSize - 1, FS_SET ); - hb_fsWrite( pArea->hMemoFile, ( BYTE * ) "\xAF", 1 ); + hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) "\xAF", 1, + FPT_BLOCK_OFFSET( *ulStoredBlock + ulBlocks ) - 1 ); } } pArea->fMemoFlush = TRUE; @@ -3536,10 +3587,11 @@ static ERRCODE hb_fptPutVarField( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ulOldSize = 11; else if( uiType == HB_VF_ARRAY ) { - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulOldBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hb_fptCountSMTDataLength( pArea, &ulOldSize ) != SUCCESS ) + HB_FOFFSET fOffset = FPT_BLOCK_OFFSET( ulOldBlock ); + if( hb_fptCountSMTDataLength( pArea, &fOffset ) != SUCCESS ) ulOldSize = 0; + else + ulOldSize = fOffset - FPT_BLOCK_OFFSET( ulOldBlock ); } } } @@ -3697,7 +3749,7 @@ static ERRCODE hb_fptGetVarLen( FPTAREAP pArea, USHORT uiIndex, ULONG * pLength HB_TRACE(HB_TR_DEBUG, ("hb_fptGetVarLen(%p, %hu, %p)", pArea, uiIndex, pLength)); - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR && + if( pArea->fHasMemo && pArea->pMemoFile && ( pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_MEMO || pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_IMAGE || pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_BLOB || @@ -3866,11 +3918,11 @@ static ERRCODE hb_fptCreateMemFile( FPTAREAP pArea, LPDBOPENINFO pCreateInfo ) /* Try create */ do { - pArea->hMemoFile = hb_fsExtOpen( szFileName, NULL, - FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | - FXO_DEFAULTS | FXO_SHARELOCK, - NULL, pError ); - if( pArea->hMemoFile == FS_ERROR ) + pArea->pMemoFile = hb_fileExtOpen( szFileName, NULL, + FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | + FXO_DEFAULTS | FXO_SHARELOCK, + NULL, pError ); + if( !pArea->pMemoFile ) { if( !pError ) { @@ -3890,13 +3942,12 @@ static ERRCODE hb_fptCreateMemFile( FPTAREAP pArea, LPDBOPENINFO pCreateInfo ) if( pError ) hb_itemRelease( pError ); - if( pArea->hMemoFile == FS_ERROR ) + if( !pArea->pMemoFile ) return FAILURE; pArea->szMemoFileName = hb_strdup( ( char * ) szFileName ); } - else /* For zap file */ - hb_fsSeek( pArea->hMemoFile, 0, FS_SET ); + /* else -> For zap file */ memset( &fptHeader, 0, sizeof( FPTHEADER ) ); ulSize = 512; @@ -3929,20 +3980,25 @@ static ERRCODE hb_fptCreateMemFile( FPTAREAP pArea, LPDBOPENINFO pCreateInfo ) HB_PUT_BE_UINT32( fptHeader.nextBlock, ulNextBlock ); HB_PUT_BE_UINT32( fptHeader.blockSize, ( UINT32 ) pArea->uiMemoBlockSize ); } - if( hb_fsWrite( pArea->hMemoFile, ( BYTE * ) &fptHeader, ( USHORT) ulSize ) != ( USHORT ) ulSize ) + if( hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) &fptHeader, ulSize, 0 ) != ulSize ) return FAILURE; ulLen = ulNextBlock * pArea->uiMemoBlockSize - ulSize; - memset( &fptHeader, 0, sizeof( FPTHEADER ) ); - while( ulLen > 0 ) + if( ulLen > ulSize ) { - ulSize = HB_MIN( ulLen, sizeof( FPTHEADER ) ); - if( hb_fsWrite( pArea->hMemoFile, ( BYTE * ) &fptHeader, ( USHORT ) ulSize ) != ( USHORT ) ulSize ) - return FAILURE; - ulLen -= ulSize; + memset( &fptHeader, 0, sizeof( FPTHEADER ) ); + do + { + ULONG ulWrite = HB_MIN( ulLen - ulSize, sizeof( FPTHEADER ) ); + if( hb_fileWriteAt( pArea->pMemoFile, ( BYTE * ) &fptHeader, + ulWrite, ulSize ) != ulWrite ) + return FAILURE; + ulSize += ulWrite; + } + while( ulLen > ulSize ); } /* trunc file */ - hb_fsWrite( pArea->hMemoFile, NULL, 0 ); + hb_fileTruncAt( pArea->pMemoFile, ulSize ); pArea->fMemoFlush = TRUE; return SUCCESS; } @@ -3959,7 +4015,7 @@ static ERRCODE hb_fptGetValueFile( FPTAREAP pArea, USHORT uiIndex, BYTE * szFile if( !uiIndex || uiIndex > pArea->uiFieldCount ) return FAILURE; - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR && + if( pArea->fHasMemo && pArea->pMemoFile && ( pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_MEMO || pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_IMAGE || pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_BLOB || @@ -4054,10 +4110,10 @@ static ERRCODE hb_fptOpenMemFile( FPTAREAP pArea, LPDBOPENINFO pOpenInfo ) /* Try open */ do { - pArea->hMemoFile = hb_fsExtOpen( szFileName, NULL, uiFlags | - FXO_DEFAULTS | FXO_SHARELOCK, - NULL, pError ); - if( pArea->hMemoFile == FS_ERROR ) + pArea->pMemoFile = hb_fileExtOpen( szFileName, NULL, uiFlags | + FXO_DEFAULTS | FXO_SHARELOCK, + NULL, pError ); + if( !pArea->pMemoFile ) { if( !pError ) { @@ -4078,7 +4134,7 @@ static ERRCODE hb_fptOpenMemFile( FPTAREAP pArea, LPDBOPENINFO pOpenInfo ) if( pError ) hb_itemRelease( pError ); - if( pArea->hMemoFile == FS_ERROR ) + if( !pArea->pMemoFile ) return FAILURE; pArea->szMemoFileName = hb_strdup( ( char * ) szFileName ); @@ -4093,8 +4149,8 @@ static ERRCODE hb_fptOpenMemFile( FPTAREAP pArea, LPDBOPENINFO pOpenInfo ) memset( &fptHeader, 0, sizeof( FPTHEADER ) ); if( hb_fptFileLockSh( pArea, TRUE ) ) { - hb_fsSeek( pArea->hMemoFile, 0, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &fptHeader, sizeof( FPTHEADER ) ) >= 512 ) + if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &fptHeader, + sizeof( FPTHEADER ), 0 ) >= 512 ) { pArea->uiMemoVersion = DB_MEMOVER_STD; if( pArea->bMemoType == DB_MEMO_SMT ) @@ -4153,7 +4209,7 @@ static ERRCODE hb_fptPutValueFile( FPTAREAP pArea, USHORT uiIndex, BYTE * szFile pField = pArea->lpFields + uiIndex - 1; - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR && + if( pArea->fHasMemo && pArea->pMemoFile && ( pField->uiType == HB_FT_MEMO || pField->uiType == HB_FT_IMAGE || pField->uiType == HB_FT_BLOB || @@ -4184,14 +4240,13 @@ static ERRCODE hb_fptPutValueFile( FPTAREAP pArea, USHORT uiIndex, BYTE * szFile } else if( pField->uiType == HB_FT_ANY ) { - PHB_ITEM pItem; BYTE * pAlloc; ULONG ulSize; HB_FOFFSET size = hb_fsSeekLarge( hFile, 0, FS_END ); - hb_fsSeek( hFile, 0, FS_SET ); ulSize = ( ULONG ) HB_MIN( size, HB_VF_CHAR ); pAlloc = ( BYTE * ) hb_xgrab( ulSize + 1 ); + hb_fsSeek( hFile, 0, FS_SET ); if( hb_fsRead( hFile, pAlloc, ( USHORT ) ulSize ) != ( USHORT ) ulSize ) { uiError = EDBF_READ; @@ -4200,7 +4255,11 @@ static ERRCODE hb_fptPutValueFile( FPTAREAP pArea, USHORT uiIndex, BYTE * szFile else { pAlloc[ ulSize ] = '\0'; - pItem = hb_itemPutCLPtr( NULL, ( char * ) pAlloc, ulSize ); + } + hb_fsClose( hFile ); + if( uiError == SUCCESS ) + { + PHB_ITEM pItem = hb_itemPutCLPtr( NULL, ( char * ) pAlloc, ulSize ); uiError = hb_fptPutVarField( pArea, uiIndex, pItem ); hb_itemRelease( pItem ); } @@ -4265,9 +4324,9 @@ static ERRCODE hb_fptPutValueFile( FPTAREAP pArea, USHORT uiIndex, BYTE * szFile static ERRCODE hb_fptDoPackRec( FPTAREAP pArea ) { ERRCODE errCode = SUCCESS; - ULONG ulBlock, ulSize, ulSkip, ulType; + ULONG ulBlock, ulSize, ulType; USHORT uiField; - HB_FOFFSET pos; + HB_FOFFSET pos, from; HB_TRACE(HB_TR_DEBUG, ("hb_fptDoPackRec(%p)", pArea)); @@ -4299,28 +4358,22 @@ static ERRCODE hb_fptDoPackRec( FPTAREAP pArea ) else if( pArea->bMemoType == DB_MEMO_FPT ) { FPTBLOCK fptBlock; - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hb_fsRead( pArea->hMemoFile, ( BYTE * ) &fptBlock, - sizeof( FPTBLOCK ) ) == sizeof( FPTBLOCK ) ) - ulSize = HB_GET_BE_UINT32( fptBlock.size ) + - sizeof( FPTBLOCK ); + if( hb_fileReadAt( pArea->pMemoFile, ( BYTE * ) &fptBlock, + sizeof( FPTBLOCK ), FPT_BLOCK_OFFSET( ulBlock ) ) == + sizeof( FPTBLOCK ) ) + ulSize = HB_GET_BE_UINT32( fptBlock.size ) + sizeof( FPTBLOCK ); } } if( ulSize && errCode == SUCCESS ) { - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - pos = hb_fsSeekLarge( pArea->hMemoTmpFile, 0, FS_RELATIVE ); + from = FPT_BLOCK_OFFSET( ulBlock ); + pos = hb_fileSize( pArea->pMemoTmpFile ); ulBlock = ( ULONG ) ( ( pos + pArea->uiNewBlockSize - 1 ) / pArea->uiNewBlockSize ); - ulSkip = ( ULONG ) ( ( ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiNewBlockSize ) - - pos ); - if( ulSkip ) - hb_fsSeek( pArea->hMemoTmpFile, ulSkip, FS_RELATIVE ); - errCode = hb_fptCopyToFile( pArea->hMemoFile, - pArea->hMemoTmpFile, ulSize ); + pos = ( HB_FOFFSET ) ulBlock * + ( HB_FOFFSET ) pArea->uiNewBlockSize; + errCode = hb_fptCopyToFile( pArea->pMemoFile, from, + pArea->pMemoTmpFile, pos, ulSize ); } else ulBlock = ulType = 0; @@ -4344,17 +4397,16 @@ static ERRCODE hb_fptDoPackRec( FPTAREAP pArea ) case HB_VF_BLOB: case HB_VF_BLOBCOMPRESS: case HB_VF_BLOBENCRYPT: - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - if( hb_fsReadLarge( pArea->hMemoFile, buffer, 4 ) != 4 ) + if( hb_fileReadAt( pArea->pMemoFile, buffer, 4, + FPT_BLOCK_OFFSET( ulBlock ) ) != 4 ) errCode = EDBF_READ; else ulSize = HB_GET_LE_UINT32( buffer ) + 4; break; case HB_VF_ARRAY: - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - errCode = hb_fptCountSMTDataLength( pArea, &ulSize ); + from = FPT_BLOCK_OFFSET( ulBlock ); + errCode = hb_fptCountSMTDataLength( pArea, &from ); + ulSize = from - FPT_BLOCK_OFFSET( ulBlock ); break; case HB_VF_DNUM: if( pField->uiLen <= 12 ) @@ -4372,18 +4424,14 @@ static ERRCODE hb_fptDoPackRec( FPTAREAP pArea ) errCode = SELF_GOHOT( ( AREAP ) pArea ); if( errCode == SUCCESS ) { - hb_fsSeekLarge( pArea->hMemoFile, ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiMemoBlockSize, FS_SET ); - pos = hb_fsSeekLarge( pArea->hMemoTmpFile, 0, FS_RELATIVE ); + from = FPT_BLOCK_OFFSET( ulBlock ); + pos = hb_fileSize( pArea->pMemoTmpFile ); ulBlock = ( ULONG ) ( ( pos + pArea->uiNewBlockSize - 1 ) / pArea->uiNewBlockSize ); - ulSkip = ( ULONG ) ( ( ( HB_FOFFSET ) ulBlock * - ( HB_FOFFSET ) pArea->uiNewBlockSize ) - - pos ); - if( ulSkip ) - hb_fsSeek( pArea->hMemoTmpFile, ulSkip, FS_RELATIVE ); - errCode = hb_fptCopyToFile( pArea->hMemoFile, - pArea->hMemoTmpFile, ulSize ); + pos = ( HB_FOFFSET ) ulBlock * + ( HB_FOFFSET ) pArea->uiNewBlockSize; + errCode = hb_fptCopyToFile( pArea->pMemoFile, from, + pArea->pMemoTmpFile, pos, ulSize ); if( errCode == SUCCESS ) HB_PUT_LE_UINT32( pFieldBuf + pField->uiLen - 6, ulBlock ); } @@ -4407,8 +4455,7 @@ static ERRCODE hb_fptDoPack( FPTAREAP pArea, USHORT uiBlockSize, errCode = EDBF_SHARED; else if( SELF_GOCOLD( ( AREAP ) pArea ) != SUCCESS ) return FAILURE; - else if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR && - pArea->hDataFile != FS_ERROR ) + else if( pArea->fHasMemo && pArea->pMemoFile && pArea->pDataFile ) { BYTE szFile[ _POSIX_PATH_MAX + 1 ]; ULONG ulRecNo, ulRecords; @@ -4422,16 +4469,16 @@ static ERRCODE hb_fptDoPack( FPTAREAP pArea, USHORT uiBlockSize, { pArea->uiNewBlockSize = uiBlockSize && pArea->bMemoType != DB_MEMO_DBT ? uiBlockSize : pArea->uiMemoBlockSize; - pArea->hMemoTmpFile = hb_fsCreateTemp( NULL, NULL, FC_NORMAL, szFile ); - if( pArea->hMemoTmpFile != FS_ERROR ) + pArea->pMemoTmpFile = hb_fileCreateTemp( NULL, NULL, FC_NORMAL, szFile ); + if( pArea->pMemoTmpFile ) { USHORT uiBlockSize = pArea->uiMemoBlockSize; - HB_FHANDLE hFile = pArea->hMemoFile; + PHB_FILE pFile = pArea->pMemoFile; pArea->uiMemoBlockSize = pArea->uiNewBlockSize; - pArea->hMemoFile = pArea->hMemoTmpFile; + pArea->pMemoFile = pArea->pMemoTmpFile; errCode = SELF_CREATEMEMFILE( ( AREAP ) pArea, NULL ); - pArea->hMemoFile = hFile; + pArea->pMemoFile = pFile; pArea->uiMemoBlockSize = uiBlockSize; if( errCode == SUCCESS ) { @@ -4479,7 +4526,7 @@ static ERRCODE hb_fptDoPack( FPTAREAP pArea, USHORT uiBlockSize, } if( errCode == SUCCESS ) { - HB_FOFFSET size = hb_fsSeekLarge( pArea->hMemoTmpFile, 0, FS_END ); + HB_FOFFSET size = hb_fileSize( pArea->pMemoTmpFile ); ULONG ulNextBlock; BYTE buffer[4]; @@ -4490,15 +4537,10 @@ static ERRCODE hb_fptDoPack( FPTAREAP pArea, USHORT uiBlockSize, HB_PUT_LE_UINT32( buffer, ulNextBlock ); else HB_PUT_BE_UINT32( buffer, ulNextBlock ); - hb_fsSeek( pArea->hMemoTmpFile, 0, FS_SET ); - hb_fsWrite( pArea->hMemoTmpFile, buffer, sizeof( buffer ) ); - - hb_fsSeek( pArea->hMemoTmpFile, 0, FS_SET ); - hb_fsSeek( pArea->hMemoFile, 0, FS_SET ); - errCode = hb_fptCopyToFile( pArea->hMemoTmpFile, - pArea->hMemoFile, size ); - /* trunc file */ - hb_fsWrite( pArea->hMemoFile, NULL, 0 ); + hb_fileWriteAt( pArea->pMemoTmpFile, buffer, sizeof( buffer ), 0 ); + errCode = hb_fptCopyToFile( pArea->pMemoTmpFile, 0, + pArea->pMemoFile, 0, size ); + hb_fileTruncAt( pArea->pMemoFile, size ); pArea->uiMemoBlockSize = pArea->uiNewBlockSize; if( errCode != SUCCESS ) { @@ -4507,9 +4549,9 @@ static ERRCODE hb_fptDoPack( FPTAREAP pArea, USHORT uiBlockSize, errCode = FAILURE; } } - hb_fsClose( pArea->hMemoTmpFile ); + hb_fileClose( pArea->pMemoTmpFile ); hb_fsDelete( ( BYTE * ) szFile ); - pArea->hMemoTmpFile = FS_ERROR; + pArea->pMemoTmpFile = NULL; } } } @@ -4557,25 +4599,24 @@ static ERRCODE hb_fptPack( FPTAREAP pArea ) HB_TRACE(HB_TR_DEBUG, ("hb_fptPack(%p)", pArea)); if( !pArea->fReadonly && !pArea->fShared && - pArea->fHasMemo && pArea->hMemoFile != FS_ERROR && - pArea->hDataFile != FS_ERROR ) + pArea->fHasMemo && pArea->pMemoFile && pArea->pDataFile ) { BYTE szFile[ _POSIX_PATH_MAX + 1 ]; if( SELF_GOCOLD( ( AREAP ) pArea ) != SUCCESS ) return FAILURE; - pArea->hMemoTmpFile = hb_fsCreateTemp( NULL, NULL, FC_NORMAL, szFile ); - if( pArea->hMemoTmpFile != FS_ERROR ) + pArea->pMemoTmpFile = hb_fileCreateTemp( NULL, NULL, FC_NORMAL, szFile ); + if( pArea->pMemoTmpFile ) { ERRCODE errCode; - HB_FHANDLE hFile = pArea->hMemoFile; + PHB_FILE pFile = pArea->pMemoFile; pArea->uiNewBlockSize = pArea->uiMemoBlockSize; - pArea->hMemoFile = pArea->hMemoTmpFile; + pArea->pMemoFile = pArea->pMemoTmpFile; errCode = SELF_CREATEMEMFILE( ( AREAP ) pArea, NULL ); - pArea->hMemoFile = hFile; + pArea->pMemoFile = pFile; if( errCode == SUCCESS ) { @@ -4584,7 +4625,7 @@ static ERRCODE hb_fptPack( FPTAREAP pArea ) pArea->fPackMemo = FALSE; if( errCode == SUCCESS ) { - HB_FOFFSET size = hb_fsSeekLarge( pArea->hMemoTmpFile, 0, FS_END ); + HB_FOFFSET size = hb_fileSize( pArea->pMemoTmpFile ); ULONG ulNextBlock; BYTE buffer[4]; @@ -4595,15 +4636,11 @@ static ERRCODE hb_fptPack( FPTAREAP pArea ) HB_PUT_LE_UINT32( buffer, ulNextBlock ); else HB_PUT_BE_UINT32( buffer, ulNextBlock ); - hb_fsSeek( pArea->hMemoTmpFile, 0, FS_SET ); - hb_fsWrite( pArea->hMemoTmpFile, buffer, sizeof( buffer ) ); + hb_fileWriteAt( pArea->pMemoTmpFile, buffer, sizeof( buffer ), 0 ); - hb_fsSeek( pArea->hMemoTmpFile, 0, FS_SET ); - hb_fsSeek( pArea->hMemoFile, 0, FS_SET ); - errCode = hb_fptCopyToFile( pArea->hMemoTmpFile, - pArea->hMemoFile, size ); - /* trunc file */ - hb_fsWrite( pArea->hMemoFile, NULL, 0 ); + errCode = hb_fptCopyToFile( pArea->pMemoTmpFile, 0, + pArea->pMemoFile, 0, size ); + hb_fileTruncAt( pArea->pMemoFile, size ); pArea->uiMemoBlockSize = pArea->uiNewBlockSize; if( errCode != SUCCESS ) { @@ -4613,9 +4650,9 @@ static ERRCODE hb_fptPack( FPTAREAP pArea ) } } } - hb_fsClose( pArea->hMemoTmpFile ); + hb_fileClose( pArea->pMemoTmpFile ); hb_fsDelete( ( BYTE * ) szFile ); - pArea->hMemoTmpFile = FS_ERROR; + pArea->pMemoTmpFile = NULL; return errCode; } } @@ -4634,7 +4671,7 @@ static ERRCODE hb_fptInfo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) switch( uiIndex ) { case DBI_MEMOEXT: - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR ) + if( pArea->fHasMemo && pArea->pMemoFile ) { PHB_FNAME pFileName; @@ -4663,7 +4700,7 @@ static ERRCODE hb_fptInfo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) break; case DBI_MEMOBLOCKSIZE: - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR ) + if( pArea->fHasMemo && pArea->pMemoFile ) hb_itemPutNI( pItem, pArea->uiMemoBlockSize ); else if( pArea->bMemoType && pArea->uiMemoBlockSize ) hb_itemPutNI( pItem, pArea->uiMemoBlockSize ); @@ -4677,7 +4714,7 @@ static ERRCODE hb_fptInfo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) break; case DBI_MEMOTYPE: - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR ) + if( pArea->fHasMemo && pArea->pMemoFile ) hb_itemPutNI( pItem, pArea->bMemoType ); else if( pArea->bMemoType ) hb_itemPutNI( pItem, pArea->bMemoType ); @@ -4689,7 +4726,7 @@ static ERRCODE hb_fptInfo( FPTAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) break; case DBI_MEMOVERSION: - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR ) + if( pArea->fHasMemo && pArea->pMemoFile ) hb_itemPutNI( pItem, pArea->uiMemoVersion ); else if( pArea->bMemoType != DB_MEMO_NONE && pArea->uiMemoVersion != 0 ) @@ -4871,7 +4908,7 @@ static ERRCODE hb_fptFieldInfo( FPTAREAP pArea, USHORT uiIndex, USHORT uiType, P if( !uiIndex || uiIndex > pArea->uiFieldCount ) return FAILURE; - if( pArea->fHasMemo && pArea->hMemoFile != FS_ERROR && + if( pArea->fHasMemo && pArea->pMemoFile && ( pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_MEMO || pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_IMAGE || pArea->lpFields[ uiIndex - 1 ].uiType == HB_FT_BLOB || diff --git a/harbour/source/rdd/dbfntx/dbfntx1.c b/harbour/source/rdd/dbfntx/dbfntx1.c index 82d15b244e..a6ebb8f31c 100644 --- a/harbour/source/rdd/dbfntx/dbfntx1.c +++ b/harbour/source/rdd/dbfntx/dbfntx1.c @@ -903,9 +903,8 @@ static BOOL hb_ntxBlockRead( LPNTXINDEX pIndex, ULONG ulBlock, BYTE *buffer, int #ifdef HB_NTX_DEBUG_DISP s_rdNO++; #endif - hb_fsSeekLarge( pIndex->DiskFile, - hb_ntxFileOffset( pIndex, ulBlock ), FS_SET ); - if( hb_fsRead( pIndex->DiskFile, buffer, iSize ) != iSize ) + if( hb_fileReadAt( pIndex->DiskFile, buffer, iSize, + hb_ntxFileOffset( pIndex, ulBlock ) ) != ( ULONG ) iSize ) { hb_ntxErrorRT( pIndex->Owner, EG_READ, EDBF_READ, pIndex->IndexName, hb_fsError(), 0 ); @@ -925,9 +924,8 @@ static BOOL hb_ntxBlockWrite( LPNTXINDEX pIndex, ULONG ulBlock, BYTE *buffer, in #ifdef HB_NTX_DEBUG_DISP s_wrNO++; #endif - hb_fsSeekLarge( pIndex->DiskFile, - hb_ntxFileOffset( pIndex, ulBlock ), FS_SET ); - if( hb_fsWrite( pIndex->DiskFile, buffer, iSize ) != iSize ) + if( hb_fileWriteAt( pIndex->DiskFile, buffer, iSize, + hb_ntxFileOffset( pIndex, ulBlock ) ) != ( ULONG ) iSize ) { hb_ntxErrorRT( pIndex->Owner, EG_WRITE, EDBF_WRITE, pIndex->IndexName, hb_fsError(), 0 ); @@ -1056,8 +1054,7 @@ static void hb_ntxIndexTrunc( LPNTXINDEX pIndex ) pIndex->Update = pIndex->Changed = pIndex->fFlush = TRUE; pIndex->TagBlock = pIndex->NextAvail = 0; pIndex->Version = 0; - hb_fsSeek( pIndex->DiskFile, NTXBLOCKSIZE, FS_SET ); - hb_fsWrite( pIndex->DiskFile, NULL, 0 ); + hb_fileTruncAt( pIndex->DiskFile, NTXBLOCKSIZE ); } /* @@ -1283,7 +1280,7 @@ static ULONG hb_ntxPageAlloc( LPNTXINDEX pIndex ) if( !pIndex->TagBlock ) { HB_FOFFSET fOffset; - fOffset = hb_fsSeekLarge( pIndex->DiskFile, 0, FS_END ); + fOffset = hb_fileSize( pIndex->DiskFile ); pIndex->TagBlock = ( ULONG ) ( fOffset >> ( pIndex->LargeFile ? NTXBLOCKBITS : 0 ) ); } @@ -1750,7 +1747,7 @@ static LPNTXINDEX hb_ntxIndexNew( NTXAREAP pArea ) pIndex = ( LPNTXINDEX ) hb_xgrab( sizeof( NTXINDEX ) ); memset( pIndex, 0, sizeof( NTXINDEX ) ); - pIndex->DiskFile = FS_ERROR; + pIndex->DiskFile = NULL; pIndex->Owner = pArea; return pIndex; } @@ -1770,9 +1767,9 @@ static void hb_ntxIndexFree( LPNTXINDEX pIndex ) } if( pIndex->HeaderBuff ) hb_xfree( pIndex->HeaderBuff ); - if( pIndex->DiskFile != FS_ERROR ) + if( pIndex->DiskFile ) { - hb_fsClose( pIndex->DiskFile ); + hb_fileClose( pIndex->DiskFile ); if( pIndex->fDelete ) { hb_fsDelete( ( BYTE * ) ( pIndex->RealName ? @@ -4665,10 +4662,9 @@ static void hb_ntxSortBufferFlush( LPNTXSORTINFO pSort ) if( pSort->ulPagesIO ) { LPNTXINDEX pIndex = pSort->pTag->Owner; - hb_fsSeekLarge( pIndex->DiskFile, - hb_ntxFileOffset( pIndex, pSort->ulFirstIO ), FS_SET ); ulSize = pSort->ulPagesIO * NTXBLOCKSIZE; - if( hb_fsWriteLarge( pIndex->DiskFile, pSort->pBuffIO, ulSize ) != ulSize ) + if( hb_fileWriteAt( pIndex->DiskFile, pSort->pBuffIO, ulSize, + hb_ntxFileOffset( pIndex, pSort->ulFirstIO ) ) != ulSize ) { hb_ntxErrorRT( pIndex->Owner, EG_WRITE, EDBF_WRITE, pIndex->IndexName, hb_fsError(), 0 ); @@ -5366,11 +5362,10 @@ static ERRCODE hb_ntxTagCreate( LPTAGINFO pTag, BOOL fReindex ) iRec = ulRecCount - ulRecNo + 1; if( ulNextCount > 0 && ulNextCount < ( ULONG ) iRec ) iRec = ( int ) ulNextCount; - hb_fsSeekLarge( pArea->hDataFile, - ( HB_FOFFSET ) pArea->uiHeaderLen + - ( HB_FOFFSET ) ( ulRecNo - 1 ) * - ( HB_FOFFSET ) pArea->uiRecordLen, FS_SET ); - hb_fsReadLarge( pArea->hDataFile, pSort->pBuffIO, pArea->uiRecordLen * iRec ); + hb_fileReadAt( pArea->pDataFile, pSort->pBuffIO, pArea->uiRecordLen * iRec, + ( HB_FOFFSET ) pArea->uiHeaderLen + + ( HB_FOFFSET ) ( ulRecNo - 1 ) * + ( HB_FOFFSET ) pArea->uiRecordLen ); iRecBuff = 0; } pArea->pRecord = pSort->pBuffIO + iRecBuff * pArea->uiRecordLen; @@ -5777,7 +5772,7 @@ static ERRCODE ntxFlush( NTXAREAP pArea ) { if( pIndex->fFlush /* && !pIndex->Temporary */ ) { - hb_fsCommit( pIndex->DiskFile ); + hb_fileCommit( pIndex->DiskFile ); pIndex->fFlush = FALSE; } pIndex = pIndex->pNext; @@ -6290,7 +6285,7 @@ static ERRCODE ntxOrderCreate( NTXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo ) } else { - HB_FHANDLE hFile; + PHB_FILE pFile; BOOL bRetry, fOld, fShared = pArea->fShared && !fTemporary && !fExclusive; USHORT uiFlags = FO_READWRITE | ( fShared ? FO_DENYNONE : FO_EXCLUSIVE ); @@ -6299,29 +6294,29 @@ static ERRCODE ntxOrderCreate( NTXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo ) { if( fTemporary ) { - hFile = hb_fsCreateTemp( NULL, NULL, FC_NORMAL, ( BYTE * ) szSpFile ); + pFile = hb_fileCreateTemp( NULL, NULL, FC_NORMAL, ( BYTE * ) szSpFile ); fOld = FALSE; } else { - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | - ( fOld ? FXO_APPEND : FXO_TRUNCATE ) | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | + ( fOld ? FXO_APPEND : FXO_TRUNCATE ) | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); } - if( hFile == FS_ERROR ) + if( !pFile ) bRetry = ( hb_ntxErrorRT( pArea, EG_CREATE, EDBF_CREATE, szFileName, hb_fsError(), EF_CANRETRY | EF_CANDEFAULT ) == E_RETRY ); else { bRetry = FALSE; if( fOld ) - fOld = ( hb_fsSeekLarge( hFile, 0, FS_END ) != 0 ); + fOld = ( hb_fileSize( pFile ) != 0 ); } } while( bRetry ); - if( hFile == FS_ERROR ) + if( !pFile ) { hb_vmDestroyBlockOrMacro( pKeyExp ); if( pForExp != NULL ) @@ -6334,7 +6329,7 @@ static ERRCODE ntxOrderCreate( NTXAREAP pArea, LPDBORDERCREATEINFO pOrderInfo ) pIndex->IndexName = hb_strdup( szFileName ); pIndex->fReadonly = FALSE; pIndex->fShared = fShared; - pIndex->DiskFile = hFile; + pIndex->DiskFile = pFile; pIndex->fDelete = fTemporary; if( fTemporary ) pIndex->RealName = hb_strdup( szSpFile ); @@ -6781,7 +6776,8 @@ static ERRCODE ntxOrderInfo( NTXAREAP pArea, USHORT uiIndex, LPDBORDERINFO pInfo hb_itemPutNI( pInfo->itmResult, hb_ntxFindTagNum( pArea, pTag ) ); break; case DBOI_FILEHANDLE: - hb_itemPutNInt( pInfo->itmResult, ( HB_NHANDLE ) pTag->Owner->DiskFile ); + hb_itemPutNInt( pInfo->itmResult, + ( HB_NHANDLE ) hb_fileHandle( pTag->Owner->DiskFile ) ); break; case DBOI_FULLPATH: hb_itemPutC( pInfo->itmResult, pTag->Owner->IndexName ); @@ -7249,7 +7245,7 @@ static ERRCODE ntxCountScope( NTXAREAP pArea, void * pPtr, LONG * plRecNo ) static ERRCODE ntxOrderListAdd( NTXAREAP pArea, LPDBORDERINFO pOrderInfo ) { USHORT uiFlags; - HB_FHANDLE hFile; + PHB_FILE pFile; char szFileName[ _POSIX_PATH_MAX + 1 ], szTagName[ NTX_MAX_TAGNAME + 1 ]; LPNTXINDEX pIndex, *pIndexPtr; ERRCODE errCode; @@ -7283,10 +7279,10 @@ static ERRCODE ntxOrderListAdd( NTXAREAP pArea, LPDBORDERINFO pOrderInfo ) do { fRetry = FALSE; - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); - if( hFile == FS_ERROR ) + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, NULL, uiFlags | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); + if( !pFile ) { fRetry = ( hb_ntxErrorRT( pArea, EG_OPEN, EDBF_OPEN_INDEX, szFileName, hb_fsError(), EF_CANRETRY | EF_CANDEFAULT ) == E_RETRY ); @@ -7294,14 +7290,14 @@ static ERRCODE ntxOrderListAdd( NTXAREAP pArea, LPDBORDERINFO pOrderInfo ) } while( fRetry ); - if( hFile == FS_ERROR ) + if( !pFile ) return FAILURE; pIndex = hb_ntxIndexNew( pArea ); pIndex->IndexName = hb_strdup( szFileName ); pIndex->fReadonly = fReadonly; pIndex->fShared = fShared; - pIndex->DiskFile = hFile; + pIndex->DiskFile = pFile; pIndex->Production = fProd; pIndexPtr = &pArea->lpIndexes; diff --git a/harbour/source/rdd/delim1.c b/harbour/source/rdd/delim1.c index bd10a93284..821bcd3bec 100644 --- a/harbour/source/rdd/delim1.c +++ b/harbour/source/rdd/delim1.c @@ -206,10 +206,10 @@ static int hb_delimNextChar( DELIMAREAP pArea ) pArea->pBuffer + pArea->ulBufferIndex, ulLeft ); pArea->ulBufferStart += pArea->ulBufferIndex; pArea->ulBufferIndex = 0; - hb_fsSeekLarge( pArea->hFile, pArea->ulBufferStart + ulLeft, FS_SET ); - pArea->ulBufferRead = hb_fsReadLarge( pArea->hFile, - pArea->pBuffer + ulLeft, - pArea->ulBufferSize - ulLeft ); + pArea->ulBufferRead = hb_fileReadAt( pArea->pFile, + pArea->pBuffer + ulLeft, + pArea->ulBufferSize - ulLeft, + pArea->ulBufferStart + ulLeft ); if( pArea->ulBufferRead > 0 && pArea->pBuffer[ pArea->ulBufferRead + ulLeft - 1 ] == '\032' ) pArea->ulBufferRead--; @@ -814,8 +814,8 @@ static ERRCODE hb_delimGoCold( DELIMAREAP pArea ) { ULONG ulSize = hb_delimEncodeBuffer( pArea ); - hb_fsSeekLarge( pArea->hFile, pArea->ulRecordOffset, FS_SET ); - if( hb_fsWriteLarge( pArea->hFile, pArea->pBuffer, ulSize ) != ulSize ) + if( hb_fileWriteAt( pArea->pFile, pArea->pBuffer, ulSize, + pArea->ulRecordOffset ) != ulSize ) { PHB_ITEM pError = hb_errNew(); @@ -872,11 +872,10 @@ static ERRCODE hb_delimFlush( DELIMAREAP pArea ) if( pArea->fFlush ) { - hb_fsSeekLarge( pArea->hFile, pArea->ulFileSize, FS_SET ); - hb_fsWrite( pArea->hFile, ( BYTE * ) "\032", 1 ); + hb_fileWriteAt( pArea->pFile, ( BYTE * ) "\032", 1, pArea->ulFileSize ); if( hb_setGetHardCommit() ) { - hb_fsCommit( pArea->hFile ); + hb_fileCommit( pArea->pFile ); pArea->fFlush = FALSE; } } @@ -975,7 +974,7 @@ static ERRCODE hb_delimInfo( DELIMAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) break; case DBI_FILEHANDLE: - hb_itemPutNInt( pItem, ( HB_NHANDLE ) pArea->hFile ); + hb_itemPutNInt( pItem, ( HB_NHANDLE ) hb_fileHandle( pArea->pFile ) ); break; case DBI_SHARED: @@ -1155,7 +1154,7 @@ static ERRCODE hb_delimNewArea( DELIMAREAP pArea ) if( SUPER_NEW( ( AREAP ) pArea ) == FAILURE ) return FAILURE; - pArea->hFile = FS_ERROR; + pArea->pFile = NULL; pArea->fTransRec = TRUE; pArea->uiRecordLen = 0; pArea->ulBufferSize = 0; @@ -1191,11 +1190,11 @@ static ERRCODE hb_delimClose( DELIMAREAP pArea ) SUPER_CLOSE( ( AREAP ) pArea ); /* Update record and unlock records */ - if( pArea->hFile != FS_ERROR ) + if( pArea->pFile ) { SELF_FLUSH( ( AREAP ) pArea ); - hb_fsClose( pArea->hFile ); - pArea->hFile = FS_ERROR; + hb_fileClose( pArea->pFile ); + pArea->pFile = NULL; } if( pArea->pFieldOffset ) @@ -1271,11 +1270,11 @@ static ERRCODE hb_delimCreate( DELIMAREAP pArea, LPDBOPENINFO pCreateInfo ) /* Try create */ do { - pArea->hFile = hb_fsExtOpen( szFileName, NULL, - FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, pError ); - if( pArea->hFile == FS_ERROR ) + pArea->pFile = hb_fileExtOpen( szFileName, NULL, + FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, pError ); + if( !pArea->pFile ) { if( !pError ) { @@ -1297,7 +1296,7 @@ static ERRCODE hb_delimCreate( DELIMAREAP pArea, LPDBOPENINFO pCreateInfo ) if( pError ) hb_itemRelease( pError ); - if( pArea->hFile == FS_ERROR ) + if( !pArea->pFile ) return FAILURE; errCode = SUPER_CREATE( ( AREAP ) pArea, pCreateInfo ); @@ -1370,10 +1369,10 @@ static ERRCODE hb_delimOpen( DELIMAREAP pArea, LPDBOPENINFO pOpenInfo ) /* Try open */ do { - pArea->hFile = hb_fsExtOpen( szFileName, NULL, uiFlags | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, pError ); - if( pArea->hFile == FS_ERROR ) + pArea->pFile = hb_fileExtOpen( szFileName, NULL, uiFlags | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, pError ); + if( !pArea->pFile ) { if( !pError ) { @@ -1395,7 +1394,7 @@ static ERRCODE hb_delimOpen( DELIMAREAP pArea, LPDBOPENINFO pOpenInfo ) if( pError ) hb_itemRelease( pError ); - if( pArea->hFile == FS_ERROR ) + if( !pArea->pFile ) return FAILURE; errCode = SUPER_OPEN( ( AREAP ) pArea, pOpenInfo ); diff --git a/harbour/source/rdd/hsx/hsx.c b/harbour/source/rdd/hsx/hsx.c index 18348e0618..2134652615 100644 --- a/harbour/source/rdd/hsx/hsx.c +++ b/harbour/source/rdd/hsx/hsx.c @@ -315,7 +315,7 @@ typedef struct _HSXINFO int iFilterType; /* character filter */ BOOL fUseHash; /* use Hash functions for alphas */ - HB_FHANDLE hFile; /* file handle */ + PHB_FILE pFile; /* file handle */ char * szFileName; /* file name */ BOOL fShared; /* Shared file */ BOOL fReadonly; /* Read only file */ @@ -405,11 +405,11 @@ static int hb_hsxHashVal( int c1, int c2, int iKeyBits, { int iBitNum; - if ( fNoCase ) + if( fNoCase ) { #ifndef HB_CDP_SUPPORT_OFF PHB_CODEPAGE cdp; - if ( iFilter == 3 && ( cdp = hb_vmCDP() )->nChars ) + if( iFilter == 3 && ( cdp = hb_vmCDP() )->nChars ) { c1 = ( BYTE ) cdp->s_upper[ c1 ]; c2 = ( BYTE ) cdp->s_upper[ c2 ]; @@ -417,30 +417,30 @@ static int hb_hsxHashVal( int c1, int c2, int iKeyBits, else #endif { - if ( c1 >= 'a' && c1 <= 'z' ) + if( c1 >= 'a' && c1 <= 'z' ) c1 -= 'a' - 'A'; - if ( c2 >= 'a' && c2 <= 'z' ) + if( c2 >= 'a' && c2 <= 'z' ) c2 -= 'a' - 'A'; } } - if ( iFilter == 1 ) + if( iFilter == 1 ) { c1 &= 0x7F; - if ( c1 < 0x20 || c1 == 0x7f ) c1 = ' '; + if( c1 < 0x20 || c1 == 0x7f ) c1 = ' '; c2 &= 0x7F; - if ( c2 < 0x20 || c2 == 0x7f ) c2 = ' '; + if( c2 < 0x20 || c2 == 0x7f ) c2 = ' '; } - if ( c1 == ' ' || c2 == ' ' || c1 == 0 || c2 == 0 ) + if( c1 == ' ' || c2 == ' ' || c1 == 0 || c2 == 0 ) iBitNum = 0; - else if ( fUseHash && c1 >= 'A' && c1 <= 'Z' && c2 >= 'A' && c2 <= 'Z' ) + else if( fUseHash && c1 >= 'A' && c1 <= 'Z' && c2 >= 'A' && c2 <= 'Z' ) { iBitNum = hb_hsxHashArray[ ( c1 - 'A' ) * 26 + ( c2 - 'A' ) ] + 1; } else { iBitNum = ( c1 + c2 * 78 ) % ( iKeyBits - 1 ) + 1; - if ( iBitNum == 1 ) + if( iBitNum == 1 ) iBitNum++; } return iBitNum; @@ -455,21 +455,21 @@ static void hb_hsxHashStr( BYTE * pStr, ULONG ulLen, BYTE * pKey, int iKeySize, #if 0 /* This code keeps the strict CFTS behavior which stops string manipulating at first chr(0) character */ - if ( pStr && ulLen-- && ( c1 = *pStr++ ) != 0 ) + if( pStr && ulLen-- && ( c1 = *pStr++ ) != 0 ) { - while ( ulLen-- && ( c2 = *pStr++ ) != 0 ) + while( ulLen-- && ( c2 = *pStr++ ) != 0 ) { #else /* This version can work well with embedded 0 characters */ - if ( pStr && ulLen-- ) + if( pStr && ulLen-- ) { c1 = *pStr++; - while ( ulLen-- ) + while( ulLen-- ) { c2 = *pStr++; #endif iBitNum = hb_hsxHashVal( c1, c2, iKeyBits, fNoCase, iFilter, fUseHash ); - if ( iBitNum-- ) + if( iBitNum-- ) { pKey[ iBitNum >> 3 ] |= 0x80 >> ( iBitNum & 7 ); } @@ -485,21 +485,21 @@ static int hb_hsxStrCmp( BYTE * pSub, ULONG ulSub, BYTE * pStr, ULONG ulLen, BYTE c1, c2; ULONG ul; - if ( ulSub == 0 ) + if( ulSub == 0 ) return HSX_SUCCESSFALSE; - while ( !fResult && ulLen >= ulSub ) + while( !fResult && ulLen >= ulSub ) { fResult = TRUE; - for ( ul = 0; fResult && ul < ulSub; ul++ ) + for( ul = 0; fResult && ul < ulSub; ul++ ) { c1 = pSub[ ul ]; c2 = pStr[ ul ]; - if ( fNoCase ) + if( fNoCase ) { #ifndef HB_CDP_SUPPORT_OFF PHB_CODEPAGE cdp; - if ( iFilter == 3 && ( cdp = hb_vmCDP() )->nChars ) + if( iFilter == 3 && ( cdp = hb_vmCDP() )->nChars ) { c1 = ( BYTE ) cdp->s_upper[ c1 ]; c2 = ( BYTE ) cdp->s_upper[ c2 ]; @@ -507,21 +507,21 @@ static int hb_hsxStrCmp( BYTE * pSub, ULONG ulSub, BYTE * pStr, ULONG ulLen, else #endif { - if ( c1 >= 'a' && c1 <= 'z' ) + if( c1 >= 'a' && c1 <= 'z' ) c1 -= 'a' - 'A'; - if ( c2 >= 'a' && c2 <= 'z' ) + if( c2 >= 'a' && c2 <= 'z' ) c2 -= 'a' - 'A'; } } #if 0 /* This code is for strict CftsVeri() behavior - uncomment if necessary but it's IMHO bug */ - if ( iFilter == 1 ) + if( iFilter == 1 ) { c1 &= 0x7F; - if ( c1 < 0x20 || c1 == 0x7f ) c1 = ' '; + if( c1 < 0x20 || c1 == 0x7f ) c1 = ' '; c2 &= 0x7F; - if ( c2 < 0x20 || c2 == 0x7f ) c2 = ' '; + if( c2 < 0x20 || c2 == 0x7f ) c2 = ' '; } #elif defined( HB_CDP_SUPPORT_OFF ) HB_SYMBOL_UNUSED( iFilter ); @@ -548,9 +548,9 @@ static int hb_hsxCompile( char * szExpr, PHB_ITEM * pExpr ) AREAP pArea = ( AREAP ) hb_rddGetCurrentWorkAreaPointer(); *pExpr = NULL; - if ( pArea ) + if( pArea ) { - if ( SELF_COMPILE( pArea, ( BYTE * ) szExpr ) == FAILURE ) + if( SELF_COMPILE( pArea, ( BYTE * ) szExpr ) == FAILURE ) return HSX_BADPARMS; *pExpr = pArea->valResult; pArea->valResult = NULL; @@ -572,20 +572,20 @@ static int hb_hsxEval( int iHandle, PHB_ITEM pExpr, BYTE *pKey, BOOL *fDeleted ) BYTE * pStr; ULONG ulLen; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( !pExpr ) + if( !pExpr ) pExpr = pHSX->pKeyItem; - if ( !pExpr ) + if( !pExpr ) return HSX_BADPARMS; - if ( hb_itemType( pExpr ) & HB_IT_STRING ) + if( hb_itemType( pExpr ) & HB_IT_STRING ) { pStr = ( BYTE * ) hb_itemGetCPtr( pExpr ); ulLen = hb_itemGetCLen( pExpr ); - if ( fDeleted ) + if( fDeleted ) *fDeleted = FALSE; } else @@ -593,10 +593,10 @@ static int hb_hsxEval( int iHandle, PHB_ITEM pExpr, BYTE *pKey, BOOL *fDeleted ) int iArea = 0; PHB_ITEM pItem; - if ( pHSX->iArea != 0 ) + if( pHSX->iArea != 0 ) { iArea = hb_rddGetCurrentWorkAreaNumber(); - if ( iArea != pHSX->iArea ) + if( iArea != pHSX->iArea ) hb_rddSelectWorkAreaNumber( pHSX->iArea ); else iArea = 0; @@ -604,21 +604,21 @@ static int hb_hsxEval( int iHandle, PHB_ITEM pExpr, BYTE *pKey, BOOL *fDeleted ) pItem = hb_vmEvalBlockOrMacro( pExpr ); pStr = ( BYTE * ) hb_itemGetCPtr( pItem ); ulLen = hb_itemGetCLen( pItem ); - if ( fDeleted ) + if( fDeleted ) { AREAP pArea = ( AREAP ) hb_rddGetCurrentWorkAreaPointer(); - if ( !pArea ) + if( !pArea ) *fDeleted = FALSE; - else if ( SELF_DELETED( pArea, fDeleted ) == FAILURE ) + else if( SELF_DELETED( pArea, fDeleted ) == FAILURE ) iResult = HSX_RDDFAILURE; } - if ( iArea ) + if( iArea ) hb_rddSelectWorkAreaNumber( iArea ); - if ( hb_vmRequestQuery() ) + if( hb_vmRequestQuery() ) iResult = HSX_BADPARMS; } - if ( iResult == HSX_SUCCESS ) + if( iResult == HSX_SUCCESS ) hb_hsxHashStr( pStr, ulLen, pKey, pHSX->uiRecordSize, pHSX->fIgnoreCase, pHSX->iFilterType, pHSX->fUseHash ); @@ -627,7 +627,7 @@ static int hb_hsxEval( int iHandle, PHB_ITEM pExpr, BYTE *pKey, BOOL *fDeleted ) static void hb_hsxGetRecCount( LPHSXINFO pHSX ) { - pHSX->ulRecCount = ( ULONG ) ( ( hb_fsSeekLarge( pHSX->hFile, 0, FS_END ) - + pHSX->ulRecCount = ( ULONG ) ( ( hb_fileSize( pHSX->pFile ) - HSXHEADER_LEN ) / pHSX->uiRecordSize ); } @@ -635,16 +635,16 @@ static int hb_hsxHdrFlush( int iHandle ) { LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( pHSX->fHdrChanged ) + if( pHSX->fHdrChanged ) { BYTE headrBuf[ HSXHEADER_LEN ]; LPHSXHEADER pHeader = ( LPHSXHEADER ) headrBuf; USHORT uiBits = 0, uiSize = pHSX->uiRecordSize; - while ( uiSize >>= 1 ) + while( uiSize >>= 1 ) uiBits++; HB_PUT_LE_UINT32( pHeader->recCount, pHSX->ulRecCount ); @@ -655,12 +655,10 @@ static int hb_hsxHdrFlush( int iHandle ) HB_PUT_LE_UINT32( pHeader->hashLetters, pHSX->fUseHash ? 1 : 0 ); memset( pHeader->keyExpression, 0, HSXKEYEXP_LEN + 1 ); - if ( pHSX->szKeyExpr ) + if( pHSX->szKeyExpr ) hb_strncpy( ( char * ) pHeader->keyExpression, pHSX->szKeyExpr, HSXKEYEXP_LEN ); - if ( hb_fsSeek( pHSX->hFile, 0, FS_SET ) != 0 ) - return HSX_BADHDRWRITE; - if ( hb_fsWrite( pHSX->hFile, headrBuf, HSXHEADER_LEN ) != HSXHEADER_LEN ) + if( hb_fileWriteAt( pHSX->pFile, headrBuf, HSXHEADER_LEN, 0 ) != HSXHEADER_LEN ) return HSX_BADHDRWRITE; pHSX->fHdrChanged = FALSE; @@ -673,10 +671,10 @@ static int hb_hsxFlush( int iHandle ) { LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( pHSX->fChanged ) + if( pHSX->fChanged ) { HB_FOFFSET fOffset; ULONG ulSize; @@ -684,13 +682,9 @@ static int hb_hsxFlush( int iHandle ) fOffset = ( HB_FOFFSET ) HSXHEADER_LEN + ( HB_FOFFSET ) ( pHSX->ulFirstRec - 1 ) * ( HB_FOFFSET ) pHSX->uiRecordSize; - - if ( hb_fsSeekLarge( pHSX->hFile, fOffset, FS_SET ) != fOffset ) - return HSX_BADSEEK; - ulSize = pHSX->ulBufRec * pHSX->uiRecordSize; - if ( hb_fsWriteLarge( pHSX->hFile, pHSX->pBuffer, ulSize ) != ulSize ) + if( hb_fileWriteAt( pHSX->pFile, pHSX->pBuffer, ulSize, fOffset ) != ulSize ) return HSX_BADWRITE; pHSX->fChanged = FALSE; @@ -704,7 +698,7 @@ static int hb_hsxFlushAll( int iHandle ) int iRetVal; iRetVal = hb_hsxFlush( iHandle ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = hb_hsxHdrFlush( iHandle ); return iRetVal; @@ -717,12 +711,10 @@ static int hb_hsxHdrRead( int iHandle ) LPHSXHEADER pHeader = ( LPHSXHEADER ) headrBuf; int iResult = HSX_SUCCESS; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( hb_fsSeek( pHSX->hFile, 0, FS_SET ) != 0 ) - return HSX_BADREAD; - if ( hb_fsRead( pHSX->hFile, headrBuf, HSXHEADER_LEN ) != HSXHEADER_LEN ) + if( hb_fileReadAt( pHSX->pFile, headrBuf, HSXHEADER_LEN, 0 ) != HSXHEADER_LEN ) return HSX_BADREAD; pHSX->ulRecCount = HB_GET_LE_UINT32( pHeader->recCount ); @@ -731,7 +723,7 @@ static int hb_hsxHdrRead( int iHandle ) pHSX->iFilterType = HB_GET_LE_UINT16( pHeader->filterType ); pHSX->fUseHash = HB_GET_LE_UINT32( pHeader->hashLetters ) != 0; - if ( pHeader->keyExpression[0] >= ' ' ) + if( pHeader->keyExpression[0] >= ' ' ) { headrBuf[ HSXHEADER_LEN - 1 ] = '\0'; pHSX->szKeyExpr = hb_strdup( ( char * ) pHeader->keyExpression ); @@ -749,36 +741,36 @@ static int hb_hsxRead( int iHandle, ULONG ulRecord, BYTE ** pRecPtr ) LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); BOOL fCount = pHSX->fShared; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( ulRecord > pHSX->ulRecCount && fCount ) + if( ulRecord > pHSX->ulRecCount && fCount ) { hb_hsxGetRecCount( pHSX ); fCount = FALSE; } - if ( ulRecord == 0 || ulRecord > pHSX->ulRecCount ) + if( ulRecord == 0 || ulRecord > pHSX->ulRecCount ) return HSX_RECBOUND; - if ( pHSX->ulFirstRec == 0 || ulRecord < pHSX->ulFirstRec || + if( pHSX->ulFirstRec == 0 || ulRecord < pHSX->ulFirstRec || ulRecord >= pHSX->ulFirstRec + pHSX->ulBufRec ) { HB_FOFFSET fOffset; ULONG ulSize, ulFirst; int iRetVal; - if ( ( iRetVal = hb_hsxFlush( iHandle ) ) != HSX_SUCCESS ) + if( ( iRetVal = hb_hsxFlush( iHandle ) ) != HSX_SUCCESS ) return iRetVal; ulFirst = ulRecord; - if ( pHSX->fWrLocked && pHSX->fShared ) + if( pHSX->fWrLocked && pHSX->fShared ) pHSX->ulBufRec = 1; - else if ( ulFirst + pHSX->ulBufSize - 1 <= pHSX->ulRecCount ) + else if( ulFirst + pHSX->ulBufSize - 1 <= pHSX->ulRecCount ) pHSX->ulBufRec = pHSX->ulBufSize; else { - if ( fCount ) + if( fCount ) hb_hsxGetRecCount( pHSX ); pHSX->ulBufRec = HB_MIN( pHSX->ulBufSize, pHSX->ulRecCount - ulFirst + 1 ); } @@ -788,12 +780,7 @@ static int hb_hsxRead( int iHandle, ULONG ulRecord, BYTE ** pRecPtr ) ( HB_FOFFSET ) pHSX->uiRecordSize; ulSize = pHSX->ulBufRec * pHSX->uiRecordSize; - if ( hb_fsSeekLarge( pHSX->hFile, fOffset, FS_SET ) != fOffset ) - { - pHSX->ulFirstRec = pHSX->ulBufRec = 0; - return HSX_BADREAD; - } - if ( hb_fsReadLarge( pHSX->hFile, pHSX->pBuffer, ulSize ) != ulSize ) + if( hb_fileReadAt( pHSX->pFile, pHSX->pBuffer, ulSize, fOffset ) != ulSize ) { pHSX->ulFirstRec = pHSX->ulBufRec = 0; return HSX_BADREAD; @@ -810,15 +797,15 @@ static int hb_hsxAppend( int iHandle, ULONG * pulRecNo, BYTE **pRecPtr ) { LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( pHSX->ulFirstRec == 0 || pHSX->ulBufRec == pHSX->ulBufSize || + if( pHSX->ulFirstRec == 0 || pHSX->ulBufRec == pHSX->ulBufSize || pHSX->ulFirstRec + pHSX->ulBufRec != pHSX->ulRecCount + 1 ) { int iRetVal; - if ( ( iRetVal = hb_hsxFlush( iHandle ) ) != HSX_SUCCESS ) + if( ( iRetVal = hb_hsxFlush( iHandle ) ) != HSX_SUCCESS ) return iRetVal; *pulRecNo = pHSX->ulFirstRec = ++pHSX->ulRecCount; @@ -839,30 +826,30 @@ static int hb_hsxUpdate( int iHandle, ULONG ulRecord, BYTE **pRecPtr ) { LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( ulRecord > pHSX->ulRecCount ) + if( ulRecord > pHSX->ulRecCount ) { /* this is intentional - when HSX index is bound with workarea * then all updates should be synced by WA locks and it should * be save to use REPLACE called from GOCOLD() method instead of * ADD for newly appended records */ - if ( pHSX->iArea != 0 ) + if( pHSX->iArea != 0 ) pHSX->ulRecCount = ulRecord; - else if ( pHSX->fShared ) + else if( pHSX->fShared ) hb_hsxGetRecCount( pHSX ); } - if ( ulRecord == 0 || ulRecord > pHSX->ulRecCount ) + if( ulRecord == 0 || ulRecord > pHSX->ulRecCount ) return HSX_RECBOUND; - if ( pHSX->ulFirstRec == 0 || ulRecord < pHSX->ulFirstRec || + if( pHSX->ulFirstRec == 0 || ulRecord < pHSX->ulFirstRec || ulRecord >= pHSX->ulFirstRec + pHSX->ulBufRec ) { int iRetVal; - if ( ( iRetVal = hb_hsxFlush( iHandle ) ) != HSX_SUCCESS ) + if( ( iRetVal = hb_hsxFlush( iHandle ) ) != HSX_SUCCESS ) return iRetVal; pHSX->ulFirstRec = ulRecord; @@ -881,12 +868,12 @@ static int hb_hsxLock( int iHandle, int iAction, ULONG ulRecord ) HB_SYMBOL_UNUSED( ulRecord ); - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( pHSX->fReadonly ) + if( pHSX->fReadonly ) { - switch ( iAction ) + switch( iAction ) { case HSX_WRITELOCK: case HSX_UPDATELOCK: @@ -902,9 +889,9 @@ static int hb_hsxLock( int iHandle, int iAction, ULONG ulRecord ) * operations. hb_hsxAdd() should be called when WA APPEND_LOCK * is set and hb_hsxReplace() inside GOCOLD() method */ - if ( pHSX->fShared && pHSX->iArea == 0 ) + if( pHSX->fShared && pHSX->iArea == 0 ) { - switch ( iAction ) + switch( iAction ) { case HSX_READLOCK: break; @@ -912,36 +899,45 @@ static int hb_hsxLock( int iHandle, int iAction, ULONG ulRecord ) case HSX_WRITELOCK: case HSX_UPDATELOCK: case HSX_APPENDLOCK: - do + for( ;; ) { - fResult = hb_fsLockLarge( pHSX->hFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, - FL_LOCK | FLX_EXCLUSIVE | FLX_WAIT ); - } while ( !fResult ); - if ( iRetVal == HSX_SUCCESS ) + fResult = hb_fileLock( pHSX->pFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, + FL_LOCK | FLX_EXCLUSIVE | FLX_WAIT ); + if( fResult ) + break; + hb_releaseCPU(); + } + if( iRetVal == HSX_SUCCESS ) { /* discrad buffers in shared mode */ pHSX->ulFirstRec = pHSX->ulBufRec = 0; - if ( iAction == HSX_APPENDLOCK ) + if( iAction == HSX_APPENDLOCK ) hb_hsxGetRecCount( pHSX ); - else if ( iAction == HSX_WRITELOCK ) + else if( iAction == HSX_WRITELOCK ) pHSX->fWrLocked = TRUE; } break; case HSX_HDRREADLOCK: - do + for( ;; ) { - fResult = hb_fsLockLarge( pHSX->hFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, - FL_LOCK | FLX_SHARED | FLX_WAIT ); - } while ( !fResult ); + fResult = hb_fileLock( pHSX->pFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, + FL_LOCK | FLX_SHARED | FLX_WAIT ); + if( fResult ) + break; + hb_releaseCPU(); + } break; case HSX_HDRWRITELOCK: - do + for( ;; ) { - fResult = hb_fsLockLarge( pHSX->hFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, - FL_LOCK | FLX_EXCLUSIVE | FLX_WAIT ); - } while ( !fResult ); + fResult = hb_fileLock( pHSX->pFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, + FL_LOCK | FLX_EXCLUSIVE | FLX_WAIT ); + if( fResult ) + break; + hb_releaseCPU(); + } break; case HSX_READUNLOCK: @@ -951,17 +947,17 @@ static int hb_hsxLock( int iHandle, int iAction, ULONG ulRecord ) case HSX_UPDATEUNLOCK: case HSX_APPENDUNLOCK: iRetVal = hb_hsxFlush( iHandle ); - if ( iAction == HSX_APPENDLOCK ) + if( iAction == HSX_APPENDLOCK ) pHSX->fWrLocked = FALSE; case HSX_HDRWRITEUNLOCK: iRet = hb_hsxHdrFlush( iHandle ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = iRet; case HSX_HDRREADUNLOCK: - if ( ! hb_fsLockLarge( pHSX->hFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, - FL_UNLOCK ) ) + if( ! hb_fileLock( pHSX->pFile, HSX_HDRLOCKPOS, HSX_HDRLOCKSIZE, + FL_UNLOCK ) ) { - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = HSX_CANNOTUNLOCK; } break; @@ -978,14 +974,14 @@ static int hb_hsxIfDel( int iHandle, ULONG ulRecord ) iRetVal = hb_hsxLock( iHandle, HSX_READLOCK, ulRecord ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { iRetVal = hb_hsxRead( iHandle, ulRecord, &pRecPtr ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = *pRecPtr & 0x80 ? HSX_SUCCESS : HSX_SUCCESSFALSE; } iRet = hb_hsxLock( iHandle, HSX_READUNLOCK, ulRecord ); - if ( iRet != HSX_SUCCESS ) + if( iRet != HSX_SUCCESS ) iRetVal = iRet; return iRetVal; } @@ -995,18 +991,18 @@ static int hb_hsxDelete( int iHandle, ULONG ulRecord ) LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); int iRetVal, iRet; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; iRetVal = hb_hsxLock( iHandle, HSX_UPDATELOCK, ulRecord ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { BYTE *pRecPtr; iRetVal = hb_hsxRead( iHandle, ulRecord, &pRecPtr ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { - if ( *pRecPtr & 0x80 ) + if( *pRecPtr & 0x80 ) iRetVal = HSX_ISDELETED; else { @@ -1016,7 +1012,7 @@ static int hb_hsxDelete( int iHandle, ULONG ulRecord ) } } iRet = hb_hsxLock( iHandle, HSX_UPDATEUNLOCK, ulRecord ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = iRet; } return iRetVal; @@ -1027,18 +1023,18 @@ static int hb_hsxUnDelete( int iHandle, ULONG ulRecord ) LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); int iRetVal, iRet; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; iRetVal = hb_hsxLock( iHandle, HSX_UPDATELOCK, ulRecord ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { BYTE *pRecPtr; iRetVal = hb_hsxRead( iHandle, ulRecord, &pRecPtr ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { - if ( ( *pRecPtr & 0x80 ) == 0 ) + if( ( *pRecPtr & 0x80 ) == 0 ) iRetVal = HSX_NOTDELETED; else { @@ -1048,7 +1044,7 @@ static int hb_hsxUnDelete( int iHandle, ULONG ulRecord ) } } iRet = hb_hsxLock( iHandle, HSX_UPDATEUNLOCK, ulRecord ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = iRet; } return iRetVal; @@ -1059,27 +1055,27 @@ static int hb_hsxReplace( int iHandle, ULONG ulRecord, PHB_ITEM pExpr, BOOL fDel LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); int iRetVal, iRet; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; iRetVal = hb_hsxLock( iHandle, HSX_WRITELOCK, ulRecord ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { BYTE * pRecPtr; iRetVal = hb_hsxUpdate( iHandle, ulRecord, &pRecPtr ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { iRetVal = hb_hsxEval( iHandle, pExpr, pRecPtr, pExpr ? NULL : &fDeleted ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { - if ( fDeleted ) + if( fDeleted ) *pRecPtr |= 0x80; pHSX->fChanged = TRUE; } } iRet = hb_hsxLock( iHandle, HSX_WRITEUNLOCK, ulRecord ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = iRet; } return iRetVal; @@ -1090,33 +1086,33 @@ static int hb_hsxAdd( int iHandle, ULONG *pulRecNo, PHB_ITEM pExpr, BOOL fDelete LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); int iRetVal, iRet; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; - if ( !pExpr && !pHSX->pKeyItem ) + if( !pExpr && !pHSX->pKeyItem ) return HSX_BADPARMS; iRetVal = hb_hsxLock( iHandle, HSX_APPENDLOCK, 0 ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { BYTE * pRecPtr; ULONG ulRecNo; iRetVal = hb_hsxAppend( iHandle, &ulRecNo, &pRecPtr ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { iRetVal = hb_hsxEval( iHandle, pExpr, pRecPtr, pExpr ? NULL : &fDeleted ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { - if ( fDeleted ) + if( fDeleted ) *pRecPtr |= 0x80; pHSX->fChanged = TRUE; - if ( pulRecNo ) + if( pulRecNo ) *pulRecNo = ulRecNo; } } iRet = hb_hsxLock( iHandle, HSX_APPENDUNLOCK, 0 ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = iRet; } @@ -1128,23 +1124,23 @@ static int hb_hsxSeekSet( int iHandle, BYTE * pStr, ULONG ulLen ) LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); int iRetVal; - if ( !pHSX ) + if( !pHSX ) return HSX_BADHANDLE; iRetVal = hb_hsxFlushAll( iHandle ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { - if ( pHSX->ulRecCount == 0 ) + if( pHSX->ulRecCount == 0 ) iRetVal = HSX_NORECS; else { - if ( pHSX->pSearchVal ) + if( pHSX->pSearchVal ) hb_xfree( pHSX->pSearchVal ); pHSX->pSearchVal = ( BYTE * ) hb_xgrab( ulLen + 1 ); memcpy( pHSX->pSearchVal, pStr, ulLen ); pHSX->pSearchVal[ ulLen ] = '\0'; pHSX->ulSearch = ulLen; - if ( ! pHSX->pSearchKey ) + if( ! pHSX->pSearchKey ) pHSX->pSearchKey = ( BYTE * ) hb_xgrab( pHSX->uiRecordSize ); hb_hsxHashStr( pStr, ulLen, pHSX->pSearchKey, pHSX->uiRecordSize, pHSX->fIgnoreCase, @@ -1162,28 +1158,28 @@ static int hb_hsxNext( int iHandle, ULONG * pulRecNo ) *pulRecNo = 0; - if ( ! pHSX ) + if( ! pHSX ) return HSX_BADHANDLE; iRetVal = hb_hsxLock( iHandle, HSX_READLOCK, 0 ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { BYTE * pRecPtr; int i; - while ( pHSX->ulCurrRec < pHSX->ulRecCount ) + while( pHSX->ulCurrRec < pHSX->ulRecCount ) { iRetVal = hb_hsxRead( iHandle, ++pHSX->ulCurrRec, &pRecPtr ); - if ( iRetVal != HSX_SUCCESS ) + if( iRetVal != HSX_SUCCESS ) break; - if ( ! hb_setGetDeleted() || ( *pRecPtr & 0x80 ) == 0 ) /* Not deleted */ + if( ! hb_setGetDeleted() || ( *pRecPtr & 0x80 ) == 0 ) /* Not deleted */ { - for ( i = 0; i < pHSX->uiRecordSize; i++ ) + for( i = 0; i < pHSX->uiRecordSize; i++ ) { - if ( ( pRecPtr[ i ] & pHSX->pSearchKey[ i ] ) != pHSX->pSearchKey[ i ] ) + if( ( pRecPtr[ i ] & pHSX->pSearchKey[ i ] ) != pHSX->pSearchKey[ i ] ) break; } - if ( i == pHSX->uiRecordSize ) + if( i == pHSX->uiRecordSize ) { *pulRecNo = pHSX->ulCurrRec; break; @@ -1192,7 +1188,7 @@ static int hb_hsxNext( int iHandle, ULONG * pulRecNo ) } iRet = hb_hsxLock( iHandle, HSX_READUNLOCK, 0 ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = iRet; } return iRetVal; @@ -1204,7 +1200,7 @@ static LPHSXINFO hb_hsxNew( void ) int iHandle = 0; LPHSXTABLE pTable = hb_hsxTable(); - if ( pTable->iHandleSize == 0 ) + if( pTable->iHandleSize == 0 ) { pTable->iHandleSize = HSX_HALLOC; pTable->handleArray = ( LPHSXINFO * ) hb_xgrab( sizeof( LPHSXINFO ) * HSX_HALLOC ); @@ -1212,13 +1208,13 @@ static LPHSXINFO hb_hsxNew( void ) } else { - while ( iHandle < pTable->iHandleSize ) + while( iHandle < pTable->iHandleSize ) { - if ( pTable->handleArray[ iHandle ] == NULL ) + if( pTable->handleArray[ iHandle ] == NULL ) break; iHandle++; } - if ( iHandle == pTable->iHandleSize ) + if( iHandle == pTable->iHandleSize ) { pTable->iHandleSize += HSX_HALLOC; pTable->handleArray = ( LPHSXINFO * ) hb_xrealloc( pTable->handleArray, @@ -1230,14 +1226,14 @@ static LPHSXINFO hb_hsxNew( void ) pTable->iHandleCount++; memset( pHSX, 0, sizeof( HSXINFO ) ); pHSX->iHandle = iHandle; - pHSX->hFile = FS_ERROR; + pHSX->pFile = NULL; return pHSX; } static void hb_hsxExpDestroy( PHB_ITEM pItem ) { - if ( hb_itemType( pItem ) == HB_IT_POINTER ) + if( hb_itemType( pItem ) == HB_IT_POINTER ) hb_macroDelete( ( HB_MACRO_PTR ) hb_itemGetPtr( pItem ) ); hb_itemRelease( pItem ); } @@ -1248,23 +1244,23 @@ static int hb_hsxVerify( int iHandle, BYTE * szText, ULONG ulLen, LPHSXINFO pHSX = hb_hsxGetPointer( iHandle ); int iResult; - if ( !szSub && pHSX ) + if( !szSub && pHSX ) { szSub = pHSX->pSearchVal; ulSub = pHSX->ulSearch; } - if ( !pHSX ) + if( !pHSX ) iResult = HSX_BADHANDLE; - else if ( !szText || !szSub ) + else if( !szText || !szSub ) iResult = HSX_BADPARMS; - else if ( ulSub > ulLen || ulSub == 0 ) + else if( ulSub > ulLen || ulSub == 0 ) /* !ulSub -> do not accept empty substrings as $ operator at runtime */ iResult = HSX_SUCCESSFALSE; else { ULONG ul, ull; - switch ( iType ) + switch( iType ) { case HSX_VERIFY_BEGIN: iResult = hb_hsxStrCmp( szSub, ulSub, szText, ulSub, @@ -1276,12 +1272,12 @@ static int hb_hsxVerify( int iHandle, BYTE * szText, ULONG ulLen, break; case HSX_VERIFY_AND: iResult = HSX_SUCCESS; - for ( ul = 0; ul < ulSub && iResult == HSX_SUCCESS; ul++ ) + for( ul = 0; ul < ulSub && iResult == HSX_SUCCESS; ul++ ) { - while ( szSub[ ul ] == ' ' && ul < ulSub ) + while( szSub[ ul ] == ' ' && ul < ulSub ) ++ul; ull = ul; - while ( szSub[ ull ] != ' ' && ull < ulSub ) + while( szSub[ ull ] != ' ' && ull < ulSub ) ++ull; iResult = hb_hsxStrCmp( &szSub[ ul ], ull - ul, szText, ulLen, pHSX->fIgnoreCase, pHSX->iFilterType ); @@ -1291,12 +1287,12 @@ static int hb_hsxVerify( int iHandle, BYTE * szText, ULONG ulLen, /* case HSX_VERIFY_OR: iResult = HSX_SUCCESSFALSE; - for ( ul = 0; ul < ulSub && iResult == HSX_SUCCESSFALSE; ul++ ) + for( ul = 0; ul < ulSub && iResult == HSX_SUCCESSFALSE; ul++ ) { - while ( szSub[ ul ] == ' ' && ul < ulSub ) + while( szSub[ ul ] == ' ' && ul < ulSub ) ++ul; ull = ul; - while ( szSub[ ull ] != ' ' && ull < ulSub ) + while( szSub[ ull ] != ' ' && ull < ulSub ) ++ull; iResult = hb_hsxStrCmp( &szSub[ ul ], ull - ul, szText, ulLen, pHSX->fIgnoreCase, pHSX->iFilterType ); @@ -1317,33 +1313,33 @@ static int hb_hsxDestroy( int iHandle ) { LPHSXTABLE pTable = hb_hsxTable(); - if ( iHandle >=0 && iHandle < pTable->iHandleSize && pTable->handleArray[ iHandle ] != NULL ) + if( iHandle >=0 && iHandle < pTable->iHandleSize && pTable->handleArray[ iHandle ] != NULL ) { LPHSXINFO pHSX = pTable->handleArray[ iHandle ]; int iRetVal = HSX_SUCCESS; - if ( pHSX->hFile != FS_ERROR ) + if( pHSX->pFile ) { iRetVal = hb_hsxFlushAll( iHandle ); - hb_fsClose( pHSX->hFile ); + hb_fileClose( pHSX->pFile ); } - if ( pHSX->szFileName ) + if( pHSX->szFileName ) hb_xfree( pHSX->szFileName ); - if ( pHSX->pSearchVal ) + if( pHSX->pSearchVal ) hb_xfree( pHSX->pSearchVal ); - if ( pHSX->pSearchKey ) + if( pHSX->pSearchKey ) hb_xfree( pHSX->pSearchKey ); - if ( pHSX->pBuffer ) + if( pHSX->pBuffer ) hb_xfree( pHSX->pBuffer ); - if ( pHSX->szKeyExpr ) + if( pHSX->szKeyExpr ) hb_xfree( pHSX->szKeyExpr ); - if ( pHSX->pKeyItem ) + if( pHSX->pKeyItem ) hb_hsxExpDestroy( pHSX->pKeyItem ); hb_xfree( pHSX ); pTable->handleArray[ iHandle ] = NULL; - if ( --pTable->iHandleCount == 0 ) + if( --pTable->iHandleCount == 0 ) { hb_xfree( pTable->handleArray ); pTable->iHandleSize = 0; @@ -1362,52 +1358,52 @@ static int hb_hsxCreate( char * szFile, int iBufSize, int iKeySize, ULONG ulBufSize; USHORT uiRecordSize; LPHSXINFO pHSX; - HB_FHANDLE hFile; + PHB_FILE pFile; int iRetVal; - if ( !szFile || ! *szFile ) + if( !szFile || ! *szFile ) return HSX_BADPARMS; hb_strncpy( szFileName, szFile, _POSIX_PATH_MAX ); - if ( iKeySize < 1 || iKeySize > HSXMAXKEY_SIZE ) + if( iKeySize < 1 || iKeySize > HSXMAXKEY_SIZE ) iKeySize = HSXDEFKEY_SIZE; - if ( iFilter < 1 || iFilter > 3 ) + if( iFilter < 1 || iFilter > 3 ) iFilter = HSXDEFFILTER; ulBufSize = iBufSize * 1024; - if ( ulBufSize == 0 ) + if( ulBufSize == 0 ) ulBufSize = HSXDEFBUF_LEN; - else if ( ulBufSize < HSXMINBUF_LEN ) + else if( ulBufSize < HSXMINBUF_LEN ) ulBufSize = HSXMINBUF_LEN; - else if ( ulBufSize > HSXMAXBUF_LEN ) + else if( ulBufSize > HSXMAXBUF_LEN ) ulBufSize = HSXMAXBUF_LEN; uiRecordSize = ( USHORT ) 0x08 << iKeySize; ulBufSize /= uiRecordSize; - if ( ulBufSize == 0 ) + if( ulBufSize == 0 ) ulBufSize = 1; - if ( pExpr ) + if( pExpr ) { - if ( hb_itemGetCLen( pExpr ) > 0 ) + if( hb_itemGetCLen( pExpr ) > 0 ) { szExpr = hb_itemGetCPtr( pExpr ); iRetVal = hb_hsxCompile( szExpr, &pKeyExpr ); - if ( iRetVal != HSX_SUCCESS ) + if( iRetVal != HSX_SUCCESS ) return iRetVal; } - else if ( hb_itemType( pExpr ) == HB_IT_BLOCK ) + else if( hb_itemType( pExpr ) == HB_IT_BLOCK ) pKeyExpr = hb_itemNew( pExpr ); } - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, ( BYTE * ) HSX_FILEEXT, - FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, ( BYTE * ) HSX_FILEEXT, + FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); - if( hFile == FS_ERROR ) + if( !pFile ) { - if ( pKeyExpr ) + if( pKeyExpr ) hb_hsxExpDestroy( pKeyExpr ); return HSX_CREATEFAIL; } @@ -1417,15 +1413,15 @@ static int hb_hsxCreate( char * szFile, int iBufSize, int iKeySize, pHSX->fIgnoreCase = fIgnoreCase; pHSX->iFilterType = iFilter; pHSX->fUseHash = fIgnoreCase && iKeySize == 2 && iFilter != 3; - pHSX->hFile = hFile; + pHSX->pFile = pFile; pHSX->szFileName = hb_strdup( szFileName ); pHSX->fShared = FALSE; pHSX->fReadonly = FALSE; - if ( szExpr ) + if( szExpr ) pHSX->szKeyExpr = hb_strdup( szExpr ); pHSX->pKeyItem = pKeyExpr; pHSX->pBuffer = ( BYTE * ) hb_xalloc( ulBufSize * uiRecordSize ); - if ( pHSX->pBuffer == NULL ) + if( pHSX->pBuffer == NULL ) { hb_hsxDestroy( pHSX->iHandle ); return HSX_MEMERR; @@ -1434,7 +1430,7 @@ static int hb_hsxCreate( char * szFile, int iBufSize, int iKeySize, pHSX->fHdrChanged = TRUE; iRetVal = hb_hsxHdrFlush( pHSX->iHandle ); - if ( iRetVal != HSX_SUCCESS ) + if( iRetVal != HSX_SUCCESS ) { hb_hsxDestroy( pHSX->iHandle ); return iRetVal; @@ -1447,26 +1443,26 @@ static int hb_hsxOpen( char * szFile, int iBufSize, int iMode ) { char szFileName[ _POSIX_PATH_MAX + 1 ]; BOOL fShared, fReadonly; - HB_FHANDLE hFile; + PHB_FILE pFile; ULONG ulBufSize; USHORT uiFlags; LPHSXINFO pHSX; int iRetVal, iRet; - if ( !szFile || ! *szFile ) + if( !szFile || ! *szFile ) return HSX_BADPARMS; hb_strncpy( szFileName, szFile, _POSIX_PATH_MAX ); ulBufSize = iBufSize * 1024; - if ( ulBufSize == 0 ) + if( ulBufSize == 0 ) ulBufSize = HSXDEFBUF_LEN; - else if ( ulBufSize < HSXMINBUF_LEN ) + else if( ulBufSize < HSXMINBUF_LEN ) ulBufSize = HSXMINBUF_LEN; - else if ( ulBufSize > HSXMAXBUF_LEN ) + else if( ulBufSize > HSXMAXBUF_LEN ) ulBufSize = HSXMAXBUF_LEN; - if ( iMode < 0 || iMode > 3 ) + if( iMode < 0 || iMode > 3 ) iMode = HSXDEFOPENMODE; fReadonly = ( iMode & 0x02 ) != 0; @@ -1476,38 +1472,38 @@ static int hb_hsxOpen( char * szFile, int iBufSize, int iMode ) uiFlags = ( fReadonly ? FO_READ : FO_READWRITE ) | ( fShared ? FO_DENYNONE : FO_EXCLUSIVE ); - hFile = hb_fsExtOpen( ( BYTE * ) szFileName, ( BYTE * ) HSX_FILEEXT, - uiFlags | FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, NULL ); + pFile = hb_fileExtOpen( ( BYTE * ) szFileName, ( BYTE * ) HSX_FILEEXT, + uiFlags | FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, NULL ); - if ( hFile == FS_ERROR ) + if( !pFile ) return HSX_OPENERR; pHSX = hb_hsxNew(); - pHSX->hFile = hFile; + pHSX->pFile = pFile; pHSX->szFileName = hb_strdup( szFileName ); pHSX->fShared = fShared; pHSX->fReadonly = fReadonly; iRetVal = hb_hsxLock( pHSX->iHandle, HSX_HDRREADLOCK, 0 ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) { iRetVal = hb_hsxHdrRead( pHSX->iHandle ); iRet = hb_hsxLock( pHSX->iHandle, HSX_HDRREADUNLOCK, 0 ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) iRetVal = iRet; } - if ( iRetVal != HSX_SUCCESS ) + if( iRetVal != HSX_SUCCESS ) { hb_hsxDestroy( pHSX->iHandle ); return iRetVal; } ulBufSize /= pHSX->uiRecordSize; - if ( ulBufSize == 0 ) + if( ulBufSize == 0 ) ulBufSize = 1; pHSX->pBuffer = ( BYTE * ) hb_xalloc( ulBufSize * pHSX->uiRecordSize ); - if ( pHSX->pBuffer == NULL ) + if( pHSX->pBuffer == NULL ) { hb_hsxDestroy( pHSX->iHandle ); return HSX_MEMERR; @@ -1525,49 +1521,49 @@ static int hb_hsxIndex( char * szFile, PHB_ITEM pExpr, int iKeySize, int iMode, ERRCODE errCode; AREAP pArea = ( AREAP ) hb_rddGetCurrentWorkAreaPointer(); - if ( !pArea ) + if( !pArea ) { hb_errRT_DBCMD( EG_NOTABLE, EDBCMD_NOTABLE, NULL, "HS_INDEX" ); return HSX_NOTABLE; } iHandle = hb_hsxCreate( szFile, iBufSize, iKeySize, fIgnoreCase, iFilter, pExpr ); - if ( iHandle < 0 ) + if( iHandle < 0 ) return iHandle; errCode = SELF_RECCOUNT( pArea, &ulRecCount ); - if ( errCode != FAILURE && ulRecCount ) + if( errCode != FAILURE && ulRecCount ) { errCode = SELF_RECNO( pArea, &ulRecNo ); - if ( errCode != FAILURE ) + if( errCode != FAILURE ) { - for ( ulRec = 1; ulRec <= ulRecCount; ulRec++ ) + for( ulRec = 1; ulRec <= ulRecCount; ulRec++ ) { errCode = SELF_GOTO( pArea, ulRec ); - if ( errCode == FAILURE ) + if( errCode == FAILURE ) break; iRetVal = hb_hsxAdd( iHandle, &ulNewRec, NULL, FALSE ); - if ( iRetVal != HSX_SUCCESS ) + if( iRetVal != HSX_SUCCESS ) break; - if ( ulNewRec != ulRec ) + if( ulNewRec != ulRec ) { iRetVal = HSX_RECBOUND; break; } } - if ( pArea->valResult ) + if( pArea->valResult ) { hb_itemRelease( pArea->valResult ); pArea->valResult = NULL; } - if ( ulRecNo ) + if( ulRecNo ) SELF_GOTO( pArea, ulRecNo ); } } hb_hsxDestroy( iHandle ); - if ( iRetVal != HSX_SUCCESS ) + if( iRetVal != HSX_SUCCESS ) return iRetVal; - if ( errCode == FAILURE ) + if( errCode == FAILURE ) return HSX_RDDFAILURE; return hb_hsxOpen( szFile, iBufSize, iMode ); @@ -1584,74 +1580,74 @@ static int hb_hsxFilter( int iHandle, BYTE * pSeek, ULONG ulSeek, ULONG ulRecNo = 0, ulRec; PHB_ITEM pItem; - if ( !pHSX ) + if( !pHSX ) return HSX_BADHANDLE; - if ( !pArea ) + if( !pArea ) { hb_errRT_DBCMD( EG_NOTABLE, EDBCMD_NOTABLE, NULL, "HS_FILTER" ); return HSX_NOTABLE; } - if ( ! pVerify || hb_itemType( pVerify ) == HB_IT_NIL ) + if( ! pVerify || hb_itemType( pVerify ) == HB_IT_NIL ) pVerify = pHSX->pKeyItem; else { - if ( hb_itemGetCLen( pVerify ) > 0 ) + if( hb_itemGetCLen( pVerify ) > 0 ) { iResult = hb_hsxCompile( hb_itemGetCPtr( pVerify ), &pVerify ); - if ( iResult != HSX_SUCCESS ) + if( iResult != HSX_SUCCESS ) return HSX_BADPARMS; fDestroyExpr = TRUE; } - else if ( hb_itemType( pVerify ) != HB_IT_BLOCK ) + else if( hb_itemType( pVerify ) != HB_IT_BLOCK ) { pVerify = NULL; } } errCode = SELF_RECNO( pArea, &ulRecNo ); - if ( errCode != FAILURE ) + if( errCode != FAILURE ) iResult = hb_hsxSeekSet( iHandle, pSeek, ulSeek ); fValid = TRUE; pItem = hb_itemNew( NULL ); - while ( iResult == HSX_SUCCESS && errCode != FAILURE ) + while( iResult == HSX_SUCCESS && errCode != FAILURE ) { iResult = hb_hsxNext( iHandle, &ulRec ); - if ( iResult != HSX_SUCCESS || ulRec == 0 ) + if( iResult != HSX_SUCCESS || ulRec == 0 ) break; - if ( pVerify ) + if( pVerify ) { errCode = SELF_GOTO( pArea, ulRec ); - if ( errCode == FAILURE ) + if( errCode == FAILURE ) break; errCode = SELF_EVALBLOCK( pArea, pVerify ); - if ( errCode == FAILURE ) + if( errCode == FAILURE ) break; fValid = hb_hsxVerify( iHandle, ( BYTE * ) hb_itemGetCPtr( pArea->valResult ), hb_itemGetCLen( pArea->valResult ), pSeek, ulSeek, iVerifyType ) == HSX_SUCCESS; } - if ( fValid ) + if( fValid ) { /* set record in WA RM filter */ hb_itemPutNInt( pItem, ulRec ); errCode = SELF_INFO( pArea, DBI_RM_ADD, pItem ); } } - if ( pArea->valResult ) + if( pArea->valResult ) { hb_itemRelease( pArea->valResult ); pArea->valResult = NULL; } hb_itemRelease( pItem ); - if ( ulRecNo ) + if( ulRecNo ) SELF_GOTO( pArea, ulRecNo ); - if ( fDestroyExpr ) + if( fDestroyExpr ) hb_hsxExpDestroy( pVerify ); return errCode == FAILURE ? HSX_RDDFAILURE : iResult; @@ -1685,7 +1681,7 @@ HB_FUNC( HS_OPEN ) Closes a previously opened HiPer-SEEK index file */ HB_FUNC( HS_CLOSE ) { - if ( hb_param( 1, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) ) hb_retni( hb_hsxDestroy( hb_parni( 1 ) ) ); else hb_retni( HSX_BADPARMS ); @@ -1707,7 +1703,7 @@ HB_FUNC( HS_INDEX ) Adds a text string entry to a HiPer-SEEK index file */ HB_FUNC( HS_ADD ) { - if ( hb_param( 1, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) ) { ULONG ulRecNo; int iRetVal; @@ -1716,7 +1712,7 @@ HB_FUNC( HS_ADD ) hb_param( 2, HB_IT_BLOCK | HB_IT_STRING ), hb_parl( 3 ) ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) hb_retnint( ulRecNo ); else hb_retni( iRetVal ); @@ -1729,7 +1725,7 @@ HB_FUNC( HS_ADD ) Replaces current HiPer-SEEK index entry with a new value */ HB_FUNC( HS_REPLACE ) { - if ( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 3, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 3, HB_IT_NUMERIC ) ) hb_retni( hb_hsxReplace( hb_parni( 1 ), hb_parnl( 3 ), hb_param( 2, HB_IT_BLOCK | HB_IT_STRING ), hb_parl( 4 ) ) ); @@ -1741,7 +1737,7 @@ HB_FUNC( HS_REPLACE ) Determines if a HiPer-SEEK record is marked as deleted */ HB_FUNC( HS_IFDEL ) { - if ( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 2, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 2, HB_IT_NUMERIC ) ) hb_retni( hb_hsxIfDel( hb_parni( 1 ), hb_parnl( 2 ) ) ); else hb_retni( HSX_BADPARMS ); @@ -1751,7 +1747,7 @@ HB_FUNC( HS_IFDEL ) Deletes specifed index record from HiPer-SEEK index file */ HB_FUNC( HS_DELETE ) { - if ( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 2, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 2, HB_IT_NUMERIC ) ) hb_retni( hb_hsxDelete( hb_parni( 1 ), hb_parnl( 2 ) ) ); else hb_retni( HSX_BADPARMS ); @@ -1761,7 +1757,7 @@ HB_FUNC( HS_DELETE ) Unmarks the specified HiPer-SEEK record as being deleted */ HB_FUNC( HS_UNDELETE ) { - if ( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 2, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) && hb_param( 2, HB_IT_NUMERIC ) ) hb_retni( hb_hsxUnDelete( hb_parni( 1 ), hb_parnl( 2 ) ) ); else hb_retni( HSX_BADPARMS ); @@ -1771,13 +1767,13 @@ HB_FUNC( HS_UNDELETE ) Returns the number of entries in a HiPer-SEEK index */ HB_FUNC( HS_KEYCOUNT ) { - if ( hb_param( 1, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) ) { LPHSXINFO pHSX = hb_hsxGetPointer( hb_parni( 1 ) ); - if ( pHSX ) + if( pHSX ) { - if ( pHSX->fShared ) + if( pHSX->fShared ) hb_hsxGetRecCount( pHSX ); hb_retnint( pHSX->ulRecCount ); @@ -1796,7 +1792,7 @@ HB_FUNC( HS_SET ) BYTE * pStr = ( BYTE * ) hb_parc( 2 ); int iRetVal = HSX_BADPARMS; - if ( pStr && hb_param( 1, HB_IT_NUMERIC ) ) + if( pStr && hb_param( 1, HB_IT_NUMERIC ) ) iRetVal = hb_hsxSeekSet( hb_parni( 1 ), pStr, hb_parclen( 2 ) ); hb_retni( iRetVal ); } @@ -1810,31 +1806,31 @@ HB_FUNC( HS_FILTER ) int iHandle = -1, iResult = HSX_BADPARMS; BOOL fNew = FALSE, fToken = TRUE; - if ( hb_parclen( 1 ) > 0 ) + if( hb_parclen( 1 ) > 0 ) { - if ( ulLen > 0 ) + if( ulLen > 0 ) { iHandle = hb_hsxOpen( hb_parc( 1 ), hb_parni( 4 ), hb_param( 5, HB_IT_NUMERIC ) ? hb_parni( 5 ) : HSXDEFOPENMODE ); - if ( iHandle >= 0 ) + if( iHandle >= 0 ) fNew = TRUE; else iResult = iHandle; } } - else if ( hb_param( 1, HB_IT_NUMERIC ) ) + else if( hb_param( 1, HB_IT_NUMERIC ) ) { LPHSXINFO pHSX = hb_hsxGetPointer( hb_parni( 1 ) ); - if ( ! pHSX ) + if( ! pHSX ) iResult = HSX_BADHANDLE; else { iHandle = pHSX->iHandle; - if ( !szText ) + if( !szText ) { ulLen = pHSX->ulSearch; - if ( ulLen && pHSX->pSearchVal ) + if( ulLen && pHSX->pSearchVal ) { pBuff = ( BYTE * ) hb_xgrab( ulLen + 1 ); memcpy( pBuff, pHSX->pSearchVal, ulLen ); @@ -1845,31 +1841,31 @@ HB_FUNC( HS_FILTER ) } } } - if ( iHandle >= 0 && ulLen > 0 ) + if( iHandle >= 0 && ulLen > 0 ) { PHB_ITEM pItem = hb_itemNew( NULL ); AREAP pArea = ( AREAP ) hb_rddGetCurrentWorkAreaPointer(); - if ( !pArea ) + if( !pArea ) { hb_errRT_DBCMD( EG_NOTABLE, EDBCMD_NOTABLE, NULL, "HS_FILTER" ); iResult = HSX_NOTABLE; } /* create empty workarea RM filter */ - else if ( SELF_INFO( pArea, DBI_RM_CREATE, pItem ) == FAILURE ) + else if( SELF_INFO( pArea, DBI_RM_CREATE, pItem ) == FAILURE ) iResult = HSX_RDDFAILURE; else { /* to be SIX compatible divide given text on space delimited tokens */ - if ( fToken ) + if( fToken ) { iResult = HSX_SUCCESS; - for ( ul = 0; ul < ulLen && iResult == HSX_SUCCESS; ul++ ) + for( ul = 0; ul < ulLen && iResult == HSX_SUCCESS; ul++ ) { - while ( szText[ ul ] == ' ' && ul < ulLen ) + while( szText[ ul ] == ' ' && ul < ulLen ) ++ul; ull = ul; - while ( szText[ ull ] != ' ' && ull < ulLen ) + while( szText[ ull ] != ' ' && ull < ulLen ) ++ull; iResult = hb_hsxFilter( iHandle, &szText[ ul ], ull - ul, hb_param( 3, HB_IT_ANY ), HSX_VERIFY_PHRASE ); @@ -1883,23 +1879,23 @@ HB_FUNC( HS_FILTER ) HSX_VERIFY_PHRASE ); } } - if ( iResult == HSX_SUCCESS ) + if( iResult == HSX_SUCCESS ) { hb_itemPutNI( pItem, 0 ); - if ( SELF_INFO( pArea, DBI_RM_COUNT, pItem ) == FAILURE ) + if( SELF_INFO( pArea, DBI_RM_COUNT, pItem ) == FAILURE ) iResult = HSX_RDDFAILURE; else ulRecords = hb_itemGetNL( pItem ); } hb_itemRelease( pItem ); - if ( fNew ) + if( fNew ) hb_hsxDestroy( iHandle ); } - if ( pBuff ) + if( pBuff ) hb_xfree( pBuff ); - if ( iResult != HSX_SUCCESS ) + if( iResult != HSX_SUCCESS ) hb_retni( iResult ); else hb_retnint( ulRecords ); @@ -1912,10 +1908,10 @@ HB_FUNC( HS_NEXT ) ULONG ulRecNo = 0; int iRetVal = HSX_BADPARMS; - if ( hb_param( 1, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) ) iRetVal = hb_hsxNext( hb_parni( 1 ), &ulRecNo ); - if ( iRetVal == HSX_SUCCESS ) + if( iRetVal == HSX_SUCCESS ) hb_retnint( ulRecNo ); else hb_retni( iRetVal ); @@ -1927,7 +1923,7 @@ HB_FUNC( HS_NEXT ) Verifies hs_Next() hit against code block expression */ HB_FUNC( HS_VERIFY ) { - if ( hb_param( 1, HB_IT_NUMERIC ) ) + if( hb_param( 1, HB_IT_NUMERIC ) ) { int iHandle = hb_parni( 1 ); PHB_ITEM pExpr = hb_param( 2, HB_IT_BLOCK ); @@ -1936,20 +1932,20 @@ HB_FUNC( HS_VERIFY ) LPHSXINFO pHSX; pHSX = hb_hsxGetPointer( iHandle ); - if ( !pHSX ) + if( !pHSX ) { hb_retni( HSX_BADHANDLE ); return; } - if ( pExpr ) + if( pExpr ) pExpr = hb_vmEvalBlockOrMacro( pExpr ); else { pExpr = hb_param( 2, HB_IT_STRING ); - if ( !pExpr && pHSX->pKeyItem ) + if( !pExpr && pHSX->pKeyItem ) pExpr = hb_vmEvalBlockOrMacro( pHSX->pKeyItem ); } - if ( pExpr ) + if( pExpr ) { szText = ( BYTE * ) hb_itemGetCPtr( pExpr ); ulLen = hb_itemGetCLen( pExpr ); @@ -1966,11 +1962,11 @@ HB_FUNC( HS_VERIFY ) ULONG ulSub = hb_parclen( 2 ), ulLen = 0; BOOL fIgnoreCase = hb_parl( 3 ); - if ( ulSub ) + if( ulSub ) { pExpr = pExpr ? hb_vmEvalBlockOrMacro( pExpr ) : hb_param( 2, HB_IT_STRING ); - if ( pExpr ) + if( pExpr ) { szText = ( BYTE * ) hb_itemGetCPtr( pExpr ); ulLen = hb_itemGetCLen( pExpr ); diff --git a/harbour/source/rdd/sdf1.c b/harbour/source/rdd/sdf1.c index f33fa2a6a6..955c83705a 100644 --- a/harbour/source/rdd/sdf1.c +++ b/harbour/source/rdd/sdf1.c @@ -104,9 +104,8 @@ static ERRCODE hb_sdfReadRecord( SDFAREAP pArea ) HB_TRACE(HB_TR_DEBUG, ("hb_sdfReadRecord(%p)", pArea)); uiToRead = pArea->uiRecordLen + pArea->uiEolLen + 2; - hb_fsSeekLarge( pArea->hFile, pArea->ulRecordOffset, FS_SET ); - uiRead = hb_fsRead( pArea->hFile, pArea->pRecord, uiToRead ); - + uiRead = hb_fileReadAt( pArea->pFile, pArea->pRecord, uiToRead, + pArea->ulRecordOffset ); if( uiRead > 0 && uiRead < uiToRead && pArea->pRecord[ uiRead - 1 ] == '\032' ) --uiRead; @@ -163,15 +162,14 @@ static ERRCODE hb_sdfNextRecord( SDFAREAP pArea ) if( pArea->ulNextOffset == 0 ) { USHORT uiRead, uiToRead, uiEolPos, uiRest = 0; - HB_FOFFSET ulOffset = 0; + HB_FOFFSET ulOffset = pArea->ulRecordOffset; uiToRead = pArea->uiRecordLen + pArea->uiEolLen + 2; - hb_fsSeekLarge( pArea->hFile, pArea->ulRecordOffset, FS_SET ); do { - uiRead = hb_fsRead( pArea->hFile, pArea->pRecord + uiRest, - uiToRead - uiRest ) + uiRest; + uiRead = hb_fileReadAt( pArea->pFile, pArea->pRecord + uiRest, + uiToRead - uiRest, ulOffset + uiRest ) + uiRest; if( uiRead > 0 && uiRead < uiToRead && pArea->pRecord[ uiRead - 1 ] == '\032' ) --uiRead; @@ -184,8 +182,7 @@ static ERRCODE hb_sdfNextRecord( SDFAREAP pArea ) if( uiRead == pArea->uiRecordLen + pArea->uiEolLen ) pArea->ulNextOffset = ( HB_FOFFSET ) -1; else - pArea->ulNextOffset = pArea->ulRecordOffset + ulOffset + - uiEolPos + pArea->uiEolLen; + pArea->ulNextOffset = ulOffset + uiEolPos + pArea->uiEolLen; } else if( uiRead < uiToRead ) { @@ -637,10 +634,10 @@ static ERRCODE hb_sdfGoCold( SDFAREAP pArea ) if( pArea->fRecordChanged ) { - USHORT uiWrite = pArea->uiRecordLen + pArea->uiEolLen; + ULONG ulWrite = pArea->uiRecordLen + pArea->uiEolLen; - hb_fsSeekLarge( pArea->hFile, pArea->ulRecordOffset, FS_SET ); - if( hb_fsWrite( pArea->hFile, pArea->pRecord, uiWrite ) != uiWrite ) + if( hb_fileWriteAt( pArea->pFile, pArea->pRecord, ulWrite, + pArea->ulRecordOffset ) != ulWrite ) { PHB_ITEM pError = hb_errNew(); @@ -653,7 +650,7 @@ static ERRCODE hb_sdfGoCold( SDFAREAP pArea ) hb_itemRelease( pError ); return FAILURE; } - pArea->ulFileSize += uiWrite; + pArea->ulFileSize += ulWrite; pArea->ulNextOffset = pArea->ulFileSize; pArea->fRecordChanged = FALSE; pArea->fFlush = TRUE; @@ -697,11 +694,10 @@ static ERRCODE hb_sdfFlush( SDFAREAP pArea ) if( pArea->fFlush ) { - hb_fsSeekLarge( pArea->hFile, pArea->ulFileSize, FS_SET ); - hb_fsWrite( pArea->hFile, ( BYTE * ) "\032", 1 ); + hb_fileWriteAt( pArea->pFile, ( BYTE * ) "\032", 1, pArea->ulFileSize ); if( hb_setGetHardCommit() ) { - hb_fsCommit( pArea->hFile ); + hb_fileCommit( pArea->pFile ); pArea->fFlush = FALSE; } } @@ -731,7 +727,7 @@ static ERRCODE hb_sdfInfo( SDFAREAP pArea, USHORT uiIndex, PHB_ITEM pItem ) break; case DBI_FILEHANDLE: - hb_itemPutNInt( pItem, ( HB_NHANDLE ) pArea->hFile ); + hb_itemPutNInt( pItem, ( HB_NHANDLE ) hb_fileHandle( pArea->pFile ) ); break; case DBI_SHARED: @@ -904,7 +900,7 @@ static ERRCODE hb_sdfNewArea( SDFAREAP pArea ) if( SUPER_NEW( ( AREAP ) pArea ) == FAILURE ) return FAILURE; - pArea->hFile = FS_ERROR; + pArea->pFile = NULL; pArea->fTransRec = TRUE; pArea->uiRecordLen = 0; @@ -933,11 +929,11 @@ static ERRCODE hb_sdfClose( SDFAREAP pArea ) SUPER_CLOSE( ( AREAP ) pArea ); /* Update record and unlock records */ - if( pArea->hFile != FS_ERROR ) + if( pArea->pFile ) { SELF_FLUSH( ( AREAP ) pArea ); - hb_fsClose( pArea->hFile ); - pArea->hFile = FS_ERROR; + hb_fileClose( pArea->pFile ); + pArea->pFile = NULL; } if( pArea->pFieldOffset ) @@ -1008,11 +1004,11 @@ static ERRCODE hb_sdfCreate( SDFAREAP pArea, LPDBOPENINFO pCreateInfo ) /* Try create */ do { - pArea->hFile = hb_fsExtOpen( szFileName, NULL, - FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, pError ); - if( pArea->hFile == FS_ERROR ) + pArea->pFile = hb_fileExtOpen( szFileName, NULL, + FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, pError ); + if( !pArea->pFile ) { if( !pError ) { @@ -1034,7 +1030,7 @@ static ERRCODE hb_sdfCreate( SDFAREAP pArea, LPDBOPENINFO pCreateInfo ) if( pError ) hb_itemRelease( pError ); - if( pArea->hFile == FS_ERROR ) + if( !pArea->pFile ) return FAILURE; errCode = SUPER_CREATE( ( AREAP ) pArea, pCreateInfo ); @@ -1107,10 +1103,10 @@ static ERRCODE hb_sdfOpen( SDFAREAP pArea, LPDBOPENINFO pOpenInfo ) /* Try open */ do { - pArea->hFile = hb_fsExtOpen( szFileName, NULL, uiFlags | - FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, - NULL, pError ); - if( pArea->hFile == FS_ERROR ) + pArea->pFile = hb_fileExtOpen( szFileName, NULL, uiFlags | + FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, + NULL, pError ); + if( !pArea->pFile ) { if( !pError ) { @@ -1132,7 +1128,7 @@ static ERRCODE hb_sdfOpen( SDFAREAP pArea, LPDBOPENINFO pOpenInfo ) if( pError ) hb_itemRelease( pError ); - if( pArea->hFile == FS_ERROR ) + if( pArea->pFile ) return FAILURE; errCode = SUPER_OPEN( ( AREAP ) pArea, pOpenInfo ); diff --git a/harbour/source/rtl/Makefile b/harbour/source/rtl/Makefile index f43df0526f..307d17fb56 100644 --- a/harbour/source/rtl/Makefile +++ b/harbour/source/rtl/Makefile @@ -34,6 +34,7 @@ C_SOURCES=\ errorapu.c \ errorint.c \ file.c \ + filebuf.c \ filehb.c \ filesys.c \ fkmax.c \ diff --git a/harbour/source/rtl/filebuf.c b/harbour/source/rtl/filebuf.c new file mode 100644 index 0000000000..b8822baa37 --- /dev/null +++ b/harbour/source/rtl/filebuf.c @@ -0,0 +1,500 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * functions to access files with shared handles and locks + * (buffers in the future) + * + * Copyright 2008 Przemyslaw Czerpak + * www - http://www.harbour-project.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/). + * + * As a special exception, the Harbour Project gives permission for + * additional uses of the text contained in its release of Harbour. + * + * The exception is that, if you link the Harbour libraries with other + * files to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the Harbour library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the Harbour + * Project under the name Harbour. If you copy code from other + * Harbour Project or Free Software Foundation releases into a copy of + * Harbour, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for Harbour, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. + * + */ + +/* this has to be declared before hbapifs.h is included */ +#define _HB_FILE_INTERNAL_ +struct _HB_FILE; +typedef struct _HB_FILE * PHB_FILE; + + +#include "hbapi.h" +#include "hbapifs.h" +#include "hbapierr.h" +#include "hbthread.h" +#include "hbvm.h" + +#if !defined(HB_WINCE) +# include +# include +#endif + +#define HB_FLOCK_RESIZE 16 + +typedef struct +{ + HB_FOFFSET start; + HB_FOFFSET len; +} +HB_FLOCK, * PHB_FLOCK; + +typedef struct _HB_FILE +{ + ULONG device; + ULONG inode; + int used; + BOOL shared; + HB_FHANDLE hFile; + PHB_FLOCK pLocks; + UINT uiLocks; + UINT uiSize; + struct _HB_FILE * pNext; + struct _HB_FILE * pPrev; +} +HB_FILE; + +static HB_CRITICAL_NEW( s_fileMtx ); + +static PHB_FILE s_openFiles = NULL; + +void hb_fileDsp( PHB_FILE pFile, const char * szMsg ) +{ + UINT uiPos = 0; + printf( "\r\n[%s][", szMsg ); + while( uiPos < pFile->uiLocks ) + { + PHB_FLOCK pLock = &pFile->pLocks[ uiPos ]; + printf( "%lld:%lld ", pLock->start, pLock->len ); + ++uiPos; + } + printf( "]" ); + fflush(stdout); +} + +static PHB_FILE hb_fileFind( ULONG device, ULONG inode ) +{ + if( s_openFiles && ( device || inode ) ) + { + PHB_FILE pFile = s_openFiles; + do + { + if( pFile->device == device && pFile->inode == inode ) + return pFile; + pFile = pFile->pNext; + } + while( s_openFiles != pFile ); + } + return NULL; +} + +static PHB_FILE hb_fileNew( HB_FHANDLE hFile, BOOL fShared, + ULONG device, ULONG inode, BOOL fBind ) +{ + PHB_FILE pFile = hb_fileFind( device, inode ); + + if( !pFile ) + { + pFile = ( PHB_FILE ) hb_xgrab( sizeof( HB_FILE ) ); + memset( pFile, 0, sizeof( HB_FILE ) ); + pFile->device = device; + pFile->inode = inode; + pFile->hFile = hFile; + pFile->shared = fShared; + + if( fBind ) + { + if( s_openFiles ) + { + pFile->pNext = s_openFiles; + pFile->pPrev = s_openFiles->pPrev; + pFile->pPrev->pNext = pFile; + s_openFiles->pPrev = pFile; + } + else + s_openFiles = pFile->pNext = pFile->pPrev = pFile; + } + } + pFile->used++; + + return pFile; +} + +static UINT hb_fileFindOffset( PHB_FILE pFile, HB_FOFFSET ulOffset ) +{ + UINT uiFirst, uiLast, uiMiddle; + + uiFirst = 0; + uiLast = pFile->uiLocks; + uiMiddle = uiLast >> 1; + + while( uiFirst < uiLast ) + { + HB_FOFFSET ulEnd = pFile->pLocks[ uiMiddle ].start + + pFile->pLocks[ uiMiddle ].len; + if( ulEnd <= ulOffset ) + uiFirst = uiMiddle + 1; + else + uiLast = uiMiddle; + uiMiddle = ( uiFirst + uiLast ) >> 1; + } + + return uiMiddle; +} + +static void hb_fileInsertLock( PHB_FILE pFile, UINT uiPos, + HB_FOFFSET ulStart, HB_FOFFSET ulLen ) +{ + if( pFile->uiLocks == pFile->uiSize ) + { + pFile->uiSize += HB_FLOCK_RESIZE; + pFile->pLocks = ( PHB_FLOCK ) hb_xrealloc( pFile->pLocks, + sizeof( HB_FLOCK ) * pFile->uiSize ); + memset( &pFile->pLocks[ pFile->uiLocks ], 0, + sizeof( HB_FLOCK ) * HB_FLOCK_RESIZE ); + } + pFile->uiLocks++; + memmove( &pFile->pLocks[ uiPos + 1 ], &pFile->pLocks[ uiPos ], + ( pFile->uiLocks - uiPos ) * sizeof( HB_FLOCK ) ); + pFile->pLocks[ uiPos ].start = ulStart; + pFile->pLocks[ uiPos ].len = ulLen; +} + +static void hb_fileDeleteLock( PHB_FILE pFile, UINT uiPos ) +{ + pFile->uiLocks--; + memmove( &pFile->pLocks[ uiPos ], &pFile->pLocks[ uiPos + 1 ], + ( pFile->uiLocks - uiPos ) * sizeof( HB_FLOCK ) ); + if( pFile->uiSize - pFile->uiLocks >= ( HB_FLOCK_RESIZE << 1 ) ) + { + pFile->uiSize -= HB_FLOCK_RESIZE; + pFile->pLocks = ( PHB_FLOCK ) hb_xrealloc( pFile->pLocks, + sizeof( HB_FLOCK ) * pFile->uiSize ); + } +} + +static BOOL hb_fileSetLock( PHB_FILE pFile, BOOL * pfLockFS, + HB_FOFFSET ulStart, HB_FOFFSET ulLen ) +{ + BOOL fLJoin, fRJoin; + UINT uiPos; + + uiPos = hb_fileFindOffset( pFile, ulStart ); + fLJoin = fRJoin = FALSE; + if( uiPos < pFile->uiLocks ) + { + PHB_FLOCK pLock = &pFile->pLocks[ uiPos ]; + if( ulStart + ulLen > pLock->start ) + return FALSE; + if( ulStart + ulLen == pLock->start ) + fRJoin = TRUE; + } + if( uiPos > 0 ) + { + PHB_FLOCK pLock = &pFile->pLocks[ uiPos - 1 ]; + if( pLock->start + pLock->len == ulStart ) + fLJoin = TRUE; + } + if( fLJoin ) + { + if( fRJoin ) + { + pFile->pLocks[ uiPos - 1 ].len += ulLen + pFile->pLocks[ uiPos ].len; + hb_fileDeleteLock( pFile, uiPos ); + } + else + pFile->pLocks[ uiPos - 1 ].len += ulLen; + } + else if( fRJoin ) + { + pFile->pLocks[ uiPos ].start -= ulLen; + pFile->pLocks[ uiPos ].len += ulLen; + } + else + hb_fileInsertLock( pFile, uiPos, ulStart, ulLen ); + + if( pFile->shared ) + * pfLockFS = TRUE; + return TRUE; +} + +static BOOL hb_fileUnlock( PHB_FILE pFile, BOOL * pfLockFS, + HB_FOFFSET ulStart, HB_FOFFSET ulLen ) +{ + BOOL fResult = FALSE; + UINT uiPos; + + uiPos = hb_fileFindOffset( pFile, ulStart ); + if( uiPos < pFile->uiLocks ) + { + PHB_FLOCK pLock = &pFile->pLocks[ uiPos ]; + if( ulStart >= pLock->start && + ulStart + ulLen <= pLock->start + pLock->len ) + { + if( ulStart == pLock->start ) + { + if( ulLen == pLock->len ) + hb_fileDeleteLock( pFile, uiPos ); + else + { + pLock->start += ulLen; + pLock->len -= ulLen; + } + } + else if( ulStart + ulLen == pLock->start + pLock->len ) + pLock->len -= ulLen; + else + { + hb_fileInsertLock( pFile, uiPos + 1, ulStart + ulLen, + pLock->start + pLock->len - ulStart - ulLen ); + pLock->len = ulStart - pLock->start; + } + if( pFile->shared ) + * pfLockFS = TRUE; + fResult = TRUE; + } + } + return fResult; +} + +/* + * public API functions + */ + +PHB_FILE hb_fileExtOpen( BYTE * pFilename, BYTE * pDefExt, + USHORT uiExFlags, BYTE * pPaths, + PHB_ITEM pError ) +{ + PHB_FILE pFile = NULL; +#if defined( HB_OS_UNIX ) + struct stat statbuf; + BOOL fResult; +#endif + BOOL fShared; + HB_FHANDLE hFile; + BYTE * pszFile; + + fShared = ( uiExFlags & ( FO_DENYREAD | FO_DENYWRITE | FO_EXCLUSIVE ) ) == 0; + pszFile = hb_fsExtName( pFilename, pDefExt, uiExFlags, pPaths ); + +#if defined( HB_OS_UNIX ) + hb_vmUnlock(); + fResult = stat( ( char * ) pszFile, &statbuf ) == 0; + hb_fsSetIOError( fResult, 0 ); + hb_vmLock(); + + if( fResult ) + { + + hb_threadEnterCriticalSection( &s_fileMtx ); + pFile = hb_fileFind( statbuf.st_dev, statbuf.st_ino ); + if( pFile ) + { + if( !fShared || ! pFile->shared || ( uiExFlags & FXO_TRUNCATE ) != 0 ) + fResult = FALSE; + else + pFile->used++; + } + hb_threadLeaveCriticalSection( &s_fileMtx ); + } + + if( pFile ) + { + if( !fResult ) + { + hb_fsSetError( ( uiExFlags & FXO_TRUNCATE ) ? 5 : 32 ); + pFile = NULL; + } + else if( uiExFlags & FXO_COPYNAME ) + hb_strncpy( ( char * ) pFilename, ( char * ) pszFile, _POSIX_PATH_MAX ); + + if( pError ) + { + hb_errPutFileName( pError, ( char * ) pszFile ); + if( !fResult ) + { + hb_errPutOsCode( pError, hb_fsError() ); + hb_errPutGenCode( pError, ( USHORT ) ( ( uiExFlags & FXO_TRUNCATE ) ? EG_CREATE : EG_OPEN ) ); + } + } + } + else +#endif + { + hFile = hb_fsExtOpen( pFilename, pDefExt, uiExFlags, pPaths, pError ); + if( hFile != FS_ERROR ) + { + ULONG device = 0, inode = 0; +#if defined( HB_OS_UNIX ) + hb_vmUnlock(); + if( fstat( hFile, &statbuf ) == 0 ) + { + device = ( ULONG ) statbuf.st_dev; + inode = ( ULONG ) statbuf.st_ino; + } + hb_fsSetIOError( fResult, 0 ); + hb_vmLock(); +#endif + + hb_threadEnterCriticalSection( &s_fileMtx ); + pFile = hb_fileNew( hFile, fShared, device, inode, TRUE ); + hb_threadLeaveCriticalSection( &s_fileMtx ); + + if( pFile->hFile != hFile ) + hb_fsClose( hFile ); + } + } + hb_xfree( pszFile ); + + return pFile; +} + +void hb_fileClose( PHB_FILE pFile ) +{ + HB_FHANDLE hFile = FS_ERROR; + + hb_threadEnterCriticalSection( &s_fileMtx ); + + if( --pFile->used == 0 ) + { + if( pFile->pNext ) + { + pFile->pPrev->pNext = pFile->pNext; + pFile->pNext->pPrev = pFile->pPrev; + if( pFile == s_openFiles ) + { + s_openFiles = pFile->pNext; + if( pFile == s_openFiles ) + s_openFiles = NULL; + } + } + + hFile = pFile->hFile; + + if( pFile->pLocks ) + hb_xfree( pFile->pLocks ); + + hb_xfree( pFile ); + } + + hb_threadLeaveCriticalSection( &s_fileMtx ); + + if( hFile != FS_ERROR ) + hb_fsClose( hFile ); +} + +BOOL hb_fileLock( PHB_FILE pFile, HB_FOFFSET ulStart, HB_FOFFSET ulLen, + int iType ) +{ + BOOL fResult, fLockFS = FALSE; + + if( ( iType & FL_MASK ) == FL_UNLOCK ) + { + hb_threadEnterCriticalSection( &s_fileMtx ); + fResult = hb_fileUnlock( pFile, &fLockFS, ulStart, ulLen ); + hb_threadLeaveCriticalSection( &s_fileMtx ); + if( fLockFS ) + hb_fsLockLarge( pFile->hFile, ulStart, ulLen, iType ); + } + else + { + hb_threadEnterCriticalSection( &s_fileMtx ); + fResult = hb_fileSetLock( pFile, &fLockFS, ulStart, ulLen ); + hb_threadLeaveCriticalSection( &s_fileMtx ); + if( fLockFS ) + { + fResult = hb_fsLockLarge( pFile->hFile, ulStart, ulLen, iType ); + if( !fResult ) + { + hb_threadEnterCriticalSection( &s_fileMtx ); + hb_fileUnlock( pFile, &fLockFS, ulStart, ulLen ); + hb_threadLeaveCriticalSection( &s_fileMtx ); + } + } + } + + return fResult; +} + +ULONG hb_fileReadAt( PHB_FILE pFile, BYTE * buffer, ULONG ulSize, + HB_FOFFSET llOffset ) +{ + return hb_fsReadAt( pFile->hFile, buffer, ulSize, llOffset ); +} + +ULONG hb_fileWriteAt( PHB_FILE pFile, const BYTE * buffer, ULONG ulSize, + HB_FOFFSET llOffset ) +{ + return hb_fsWriteAt( pFile->hFile, buffer, ulSize, llOffset ); +} + +BOOL hb_fileTruncAt( PHB_FILE pFile, HB_FOFFSET llOffset ) +{ + return hb_fsTruncAt( pFile->hFile, llOffset ); +} + +HB_FOFFSET hb_fileSize( PHB_FILE pFile ) +{ + return hb_fsSeekLarge( pFile->hFile, 0, FS_END ); +} + +void hb_fileCommit( PHB_FILE pFile ) +{ + hb_fsCommit( pFile->hFile ); +} + +HB_FHANDLE hb_fileHandle( PHB_FILE pFile ) +{ + return pFile ? pFile->hFile : FS_ERROR; +} + +PHB_FILE hb_fileCreateTemp( const BYTE * pszDir, const BYTE * pszPrefix, + ULONG ulAttr, BYTE * pszName ) +{ + PHB_FILE pFile = NULL; + HB_FHANDLE hFile; + + hFile = hb_fsCreateTemp( pszDir, pszPrefix, ulAttr, pszName ); + if( hFile != FS_ERROR ) + pFile = hb_fileNew( hFile, FALSE, 0, 0, FALSE ); + + return pFile; +} diff --git a/harbour/source/rtl/filesys.c b/harbour/source/rtl/filesys.c index 9814553f98..a42828c808 100644 --- a/harbour/source/rtl/filesys.c +++ b/harbour/source/rtl/filesys.c @@ -94,6 +94,9 @@ #if !defined( _LARGEFILE64_SOURCE ) # define _LARGEFILE64_SOURCE #endif +#if !defined( _XOPEN_SOURCE ) +# define _XOPEN_SOURCE 500 +#endif /* OS2 */ #define INCL_DOSFILEMGR /* File Manager values */ @@ -1414,9 +1417,9 @@ HB_EXPORT USHORT hb_fsWrite( HB_FHANDLE hFileHandle, const BYTE * pBuff, USHORT hb_vmUnlock(); uiWritten = write( hFileHandle, pBuff, uiCount ); hb_fsSetIOError( uiWritten != ( USHORT ) -1, 0 ); - hb_vmLock(); if( uiWritten == ( USHORT ) -1 ) uiWritten = 0; + hb_vmLock(); } else { @@ -1426,8 +1429,8 @@ HB_EXPORT USHORT hb_fsWrite( HB_FHANDLE hFileHandle, const BYTE * pBuff, USHORT #else hb_fsSetIOError( ftruncate( hFileHandle, lseek( hFileHandle, 0L, SEEK_CUR ) ) != -1, 0 ); #endif - hb_vmLock(); uiWritten = 0; + hb_vmLock(); } #endif #else @@ -1460,6 +1463,8 @@ HB_EXPORT ULONG hb_fsReadLarge( HB_FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCo hb_vmUnlock(); ulRead = read( hFileHandle, pBuff, ulCount ); hb_fsSetIOError( ulRead != (ULONG) -1, 0 ); + if( ulRead == ( ULONG ) -1 ) + ulRead = 0; hb_vmLock(); } #else @@ -1547,9 +1552,9 @@ HB_EXPORT ULONG hb_fsWriteLarge( HB_FHANDLE hFileHandle, const BYTE * pBuff, ULO hb_vmUnlock(); ulWritten = write( hFileHandle, pBuff, ulCount ); hb_fsSetIOError( ulWritten != ( ULONG ) -1, 0 ); - hb_vmLock(); if( ulWritten == ( ULONG ) -1 ) ulWritten = 0; + hb_vmLock(); } #else { @@ -1605,8 +1610,8 @@ HB_EXPORT ULONG hb_fsWriteLarge( HB_FHANDLE hFileHandle, const BYTE * pBuff, ULO #else hb_fsSetIOError( ftruncate( hFileHandle, lseek( hFileHandle, 0L, SEEK_CUR ) ) != -1, 0 ); #endif - hb_vmLock(); ulWritten = 0; + hb_vmLock(); } #endif @@ -1620,6 +1625,163 @@ HB_EXPORT ULONG hb_fsWriteLarge( HB_FHANDLE hFileHandle, const BYTE * pBuff, ULO return ulWritten; } +HB_EXPORT ULONG hb_fsReadAt( HB_FHANDLE hFileHandle, BYTE * pBuff, ULONG ulCount, HB_FOFFSET llOffset ) +{ + ULONG ulRead; + + HB_TRACE(HB_TR_DEBUG, ("hb_fsReadAt(%p, %p, %lu, " PFHL ")", hFileHandle, pBuff, ulCount, llOffset)); + +#if defined(HB_FS_FILE_IO) + + #if defined(HB_WIN32_IO) + { + OVERLAPPED Overlapped; + hb_vmUnlock(); + memset( &Overlapped, 0, sizeof( Overlapped ) ); + Overlapped.Offset = ( DWORD ) ( llOffset & 0xFFFFFFFF ), + Overlapped.OffsetHigh = ( DWORD ) ( llOffset >> 32 ), + hb_fsSetIOError( ReadFile( DosToWinHandle( hFileHandle ), + pBuff, ulCount, &ulRead, &Overlapped ), 0 ); + hb_vmLock(); + } + #elif defined(HB_OS_UNIX) + { + hb_vmUnlock(); + #if defined(HB_USE_LARGEFILE64) + ulRead = pread64( hFileHandle, pBuff, ulCount, llOffset ); + #else + ulRead = pread( hFileHandle, pBuff, ulCount, llOffset ); + #endif + hb_fsSetIOError( ulRead != (ULONG) -1, 0 ); + if( ulRead == ( ULONG ) -1 ) + ulRead = 0; + hb_vmLock(); + } + #else + { + hb_vmUnlock(); + /* TOFIX: this is not atom operation. It has to be fixed for RDD + * file access with shared file handles in aliased work areas + */ + if( hb_fsSeekLarge( hFileHandle, llOffset, FS_SET ) == llOffset ) + ulRead = hb_fsReadLarge( hFileHandle, pBuff, ulCount ); + else + ulRead = 0; + hb_vmLock(); + } + #endif + +#else + + ulRead = 0; + hb_fsSetError( ( USHORT ) FS_ERROR ); + +#endif + + return ulRead; +} + +HB_EXPORT ULONG hb_fsWriteAt( HB_FHANDLE hFileHandle, const BYTE * pBuff, ULONG ulCount, HB_FOFFSET llOffset ) +{ + ULONG ulWritten; + + HB_TRACE(HB_TR_DEBUG, ("hb_fsWriteAt(%p, %p, %lu, " PFHL ")", hFileHandle, pBuff, ulCount, llOffset)); + +#if defined(HB_FS_FILE_IO) + + #if defined(HB_WIN32_IO) + { + OVERLAPPED Overlapped; + hb_vmUnlock(); + memset( &Overlapped, 0, sizeof( Overlapped ) ); + Overlapped.Offset = ( DWORD ) ( llOffset & 0xFFFFFFFF ), + Overlapped.OffsetHigh = ( DWORD ) ( llOffset >> 32 ), + hb_fsSetIOError( WriteFile( DosToWinHandle( hFileHandle ), + pBuff, ulCount, &ulWritten, &Overlapped ), 0 ); + hb_vmLock(); + } + #elif defined(HB_OS_UNIX) + { + hb_vmUnlock(); + #if defined(HB_USE_LARGEFILE64) + ulWritten = pwrite64( hFileHandle, pBuff, ulCount, llOffset ); + #else + ulWritten = pwrite( hFileHandle, pBuff, ulCount, llOffset ); + #endif + hb_fsSetIOError( ulWritten != (ULONG) -1, 0 ); + if( ulWritten == ( ULONG ) -1 ) + ulWritten = 0; + hb_vmLock(); + } + #else + { + hb_vmUnlock(); + /* TOFIX: this is not atom operation. It has to be fixed for RDD + * file access with shared file handles in aliased work areas + */ + if( hb_fsSeekLarge( hFileHandle, llOffset, FS_SET ) == llOffset ) + ulWritten = hb_fsWriteLarge( hFileHandle, pBuff, ulCount ); + else + ulWritten = 0; + hb_vmLock(); + } + #endif + +#else + + ulWritten = 0; + hb_fsSetError( ( USHORT ) FS_ERROR ); + +#endif + + return ulWritten; +} + +HB_EXPORT BOOL hb_fsTruncAt( HB_FHANDLE hFileHandle, HB_FOFFSET llOffset ) +{ + BOOL fResult; + + HB_TRACE(HB_TR_DEBUG, ("hb_fsReadAt(%p," PFHL ")", hFileHandle, llOffset)); + +#if defined(HB_FS_FILE_IO) + + hb_vmUnlock(); + #if defined(HB_WIN32_IO) + { + ULONG ulOffsetLow = ( ULONG ) ( llOffset & ULONG_MAX ), + ulOffsetHigh = ( ULONG ) ( llOffset >> 32 ); + + /* This is not atom operation anyhow if someone want to truncate + * file then he has to made necessary synchronizations in upper level + * code. We have such situation in our RDD drivers and for us such + * version is enough. [druzus] + */ + ulOffsetLow = SetFilePointer( DosToWinHandle( hFileHandle ), + ulOffsetLow, ( PLONG ) &ulOffsetHigh, + ( DWORD ) SEEK_SET ); + if( ( ( ( HB_FOFFSET ) ulOffsetHigh << 32 ) | ulOffsetLow ) == llOffset ) + fResult = SetEndOfFile( DosToWinHandle( hFileHandle ) ); + else + fResult = FALSE; + } + #elif defined(HB_USE_LARGEFILE64) + fResult = ftruncate64( hFileHandle, llOffset ) != -1; + #else + fResult = ftruncate( hFileHandle, llOffset ) != -1; + #endif + hb_fsSetIOError( fResult, 0 ); + hb_vmLock(); + +#else + + fResult = FALSE; + hb_fsSetError( ( USHORT ) FS_ERROR ); + +#endif + + return fResult; +} + HB_EXPORT void hb_fsCommit( HB_FHANDLE hFileHandle ) { HB_TRACE(HB_TR_DEBUG, ("hb_fsCommit(%p)", hFileHandle)); @@ -2658,48 +2820,19 @@ HB_EXPORT BYTE hb_fsCurDrv( void ) return ( BYTE ) uiResult; /* Return the drive number, base 0. */ } -/* copied from xHarbour */ -HB_EXPORT HB_FHANDLE hb_fsExtOpen( BYTE * pFilename, BYTE * pDefExt, - USHORT uiExFlags, BYTE * pPaths, - PHB_ITEM pError ) +/* convert file name for hb_fsExtOpen + * caller must free the returned buffer + */ +HB_EXPORT BYTE * hb_fsExtName( BYTE * pFilename, BYTE * pDefExt, + USHORT uiExFlags, BYTE * pPaths ) { - HB_PATHNAMES * pSearchPath = NULL, * pNextPath; + HB_PATHNAMES * pNextPath; PHB_FNAME pFilepath; - HB_FHANDLE hFile; BOOL fIsFile = FALSE; BYTE * szPath; - USHORT uiFlags; - - HB_TRACE(HB_TR_DEBUG, ("hb_fsExtOpen(%s, %s, %hu, %p, %p)", pFilename, pDefExt, uiExFlags, pPaths, pError)); - -#if 0 - #define FXO_TRUNCATE 0x0100 /* Create (truncate if exists) */ - #define FXO_APPEND 0x0200 /* Create (append if exists) */ - #define FXO_UNIQUE 0x0400 /* Create unique file FO_EXCL ??? */ - #define FXO_FORCEEXT 0x0800 /* Force default extension */ - #define FXO_DEFAULTS 0x1000 /* Use SET command defaults */ - #define FXO_DEVICERAW 0x2000 /* Open devices in raw mode */ - /* xHarbour extension */ - #define FXO_SHARELOCK 0x4000 /* emulate DOS SH_DENY* mode in POSIX OS */ - #define FXO_COPYNAME 0x8000 /* copy final szPath into pFilename */ - - hb_errGetFileName( pError ); -#endif szPath = ( BYTE * ) hb_xgrab( _POSIX_PATH_MAX + 1 ); - uiFlags = uiExFlags & 0xff; - if( uiExFlags & ( FXO_TRUNCATE | FXO_APPEND | FXO_UNIQUE ) ) - { - uiFlags |= FO_CREAT; - if( uiExFlags & FXO_UNIQUE ) - uiFlags |= FO_EXCL; -#if !defined( HB_USE_SHARELOCKS ) - else if( uiExFlags & FXO_TRUNCATE ) - uiFlags |= FO_TRUNC; -#endif - } - pFilepath = hb_fsFNameSplit( ( char * ) pFilename ); if( pDefExt && ( ( uiExFlags & FXO_FORCEEXT ) || !pFilepath->szExtension ) ) @@ -2739,6 +2872,7 @@ HB_EXPORT HB_FHANDLE hb_fsExtOpen( BYTE * pFilename, BYTE * pDefExt, } else if( pPaths && *pPaths ) { + HB_PATHNAMES * pSearchPath = NULL; hb_fsAddSearchPath( ( char * ) pPaths, &pSearchPath ); pNextPath = pSearchPath; while( !fIsFile && pNextPath ) @@ -2761,6 +2895,47 @@ HB_EXPORT HB_FHANDLE hb_fsExtOpen( BYTE * pFilename, BYTE * pDefExt, } hb_xfree( pFilepath ); + return szPath; +} + +HB_EXPORT HB_FHANDLE hb_fsExtOpen( BYTE * pFilename, BYTE * pDefExt, + USHORT uiExFlags, BYTE * pPaths, + PHB_ITEM pError ) +{ + HB_FHANDLE hFile; + USHORT uiFlags; + BYTE * szPath; + + HB_TRACE(HB_TR_DEBUG, ("hb_fsExtOpen(%s, %s, %hu, %p, %p)", pFilename, pDefExt, uiExFlags, pPaths, pError)); + +#if 0 + #define FXO_TRUNCATE 0x0100 /* Create (truncate if exists) */ + #define FXO_APPEND 0x0200 /* Create (append if exists) */ + #define FXO_UNIQUE 0x0400 /* Create unique file FO_EXCL ??? */ + #define FXO_FORCEEXT 0x0800 /* Force default extension */ + #define FXO_DEFAULTS 0x1000 /* Use SET command defaults */ + #define FXO_DEVICERAW 0x2000 /* Open devices in raw mode */ + /* Harbour extension */ + #define FXO_SHARELOCK 0x4000 /* emulate DOS SH_DENY* mode in POSIX OS */ + #define FXO_COPYNAME 0x8000 /* copy final szPath into pFilename */ + + hb_errGetFileName( pError ); +#endif + + szPath = hb_fsExtName( pFilename, pDefExt, uiExFlags, pPaths ); + + uiFlags = uiExFlags & 0xff; + if( uiExFlags & ( FXO_TRUNCATE | FXO_APPEND | FXO_UNIQUE ) ) + { + uiFlags |= FO_CREAT; + if( uiExFlags & FXO_UNIQUE ) + uiFlags |= FO_EXCL; +#if !defined( HB_USE_SHARELOCKS ) + else if( uiExFlags & FXO_TRUNCATE ) + uiFlags |= FO_TRUNC; +#endif + } + hFile = hb_fsOpen( szPath, uiFlags ); #if defined( HB_USE_SHARELOCKS ) diff --git a/harbour/source/rtl/idle.c b/harbour/source/rtl/idle.c index 2f6a4b8f7d..adbfa1e99c 100644 --- a/harbour/source/rtl/idle.c +++ b/harbour/source/rtl/idle.c @@ -114,6 +114,8 @@ void hb_releaseCPU( void ) { HB_TRACE(HB_TR_DEBUG, ("hb_releaseCPU()")); + hb_vmUnlock(); + /* TODO: Add code to release time slices on all platforms */ #if defined(HB_OS_WIN_32) || defined(__CYGWIN__) @@ -174,6 +176,8 @@ void hb_releaseCPU( void ) /* Do nothing */ #endif + + hb_vmLock(); } /* performs all tasks defined for idle state */ @@ -185,9 +189,7 @@ void hb_idleState( void ) { pIdleData->fIamIdle = TRUE; - hb_vmUnlock(); hb_releaseCPU(); - hb_vmLock(); if( hb_vmRequestQuery() == 0 ) { if( pIdleData->fCollectGarbage ) diff --git a/harbour/tests/aliaslck.prg b/harbour/tests/aliaslck.prg new file mode 100644 index 0000000000..deb66de2ad --- /dev/null +++ b/harbour/tests/aliaslck.prg @@ -0,0 +1,113 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * demonstration/test code for file lock synchronization between + * aliased work areas + * + * Copyright 2008 Przemyslaw Czerpak + * www - http://www.harbour-project.org + * + */ + +proc main() + local cFile := "_tst" + + if !dbExists( cFile ) + dbCreate( cFile, {{"F","C",10,0}} ) + use _tst exclusive + while lastRec() < 100 + dbAppend() + enddo + close + endif + ? "Test exclusive access" + select 1 + use _tst exclusive alias tst1 + ? "neterr->", netErr(), "used->", used(), "alias->", alias() + select 2 + use _tst exclusive alias tst2 + ? "neterr->", netErr(), "used->", used(), "alias->", alias() + if used() + ? "Error, exclusive mode in aliased work areas does not work" + else + ? "OK" + endif + dbCloseAll() + wait + ? + ? "Test shared access" + select 1 + use _tst shared alias tst1 + ? "neterr->", netErr(), "used->", used(), "alias->", alias() + setLock(2) + setLock(4) + setLock(6) + setLock(7) + dspLock() + select 2 + use _tst shared alias tst2 + ? "neterr->", netErr(), "used->", used(), "alias->", alias() + setLock(1) + setLock(2) + setLock(3) + setLock(4) + setLock(5) + setLock(6) + setLock(7) + setLock(8) + setLock(9) + dspLock() + wait + ? + select 1 + ? "unlock all in", alias() + dbUnLock() + dspLock() + select 2 + ? "lock 10 records in", alias() + setLock(1) + setLock(2) + setLock(3) + setLock(4) + setLock(5) + setLock(6) + setLock(7) + setLock(8) + setLock(9) + dspLock() + wait + ? + ? "unlock 3, 5, 9 in", alias() + dbRUnLock(3) + dbRUnLock(5) + dbRUnLock(9) + dspLock() + select 1 + ? "lock 10 records in", alias() + setLock(1) + setLock(2) + setLock(3) + setLock(4) + setLock(5) + setLock(6) + setLock(7) + setLock(8) + setLock(9) + dspLock() + wait +return + +proc setLock( n ) + ? "locking record: " + ltrim( str( n ) ) + " ->", dbRLock( n ) +return + +proc dspLock() + local n + ? alias(), "active locks:" + for each n in dbRLockList() + ?? "", ltrim( str( n ) ) + next +return diff --git a/harbour/tests/hsxtest.prg b/harbour/tests/hsxtest.prg new file mode 100644 index 0000000000..6c5d1ec480 --- /dev/null +++ b/harbour/tests/hsxtest.prg @@ -0,0 +1,48 @@ +/* + * $Id$ + */ + +/* + * Harbour Project source code: + * HiPer-SEEK indexing/search test + * + * Copyright 2008 Przemyslaw Czerpak + * www - http://www.harbour-project.org + * + */ + +#include "dbinfo.ch" + +//REQUEST RMDBFCDX + +PROCEDURE Main() + FIELD FIRST, LAST, STREET, CITY + LOCAL n, hs + + if ascan( rddList(1), "RMDBFCDX" ) != 0 + rddSetDefault( "RMDBFCDX" ) + endif + + use test shared + hs := HS_INDEX( "test", "FIRST+LAST+STREET+CITY", 2, 0, , .T., 3 ) + + /* Look for all records which have 'SHERMAN' string inside */ + HS_SET( hs, "SHERMAN" ) + while ( n := HS_NEXT( hs ) ) > 0 + dbgoto( n ) + if HS_VERIFY( hs ) > 0 + ? rtrim( FIRST+LAST+STREET+CITY ) + endif + enddo + wait + + /* Does RDD support Record Map Filters? */ + if dbinfo( DBI_RM_SUPPORTED ) + /* if yest then let set filter for all records with 'SHERMAN' + word and look at them in browser */ + HS_FILTER( hs, "SHERMAN" ) + dbgotop() + browse() + endif + HS_CLOSE( hs ) +RETURN diff --git a/harbour/tests/rddtest/adscl52.prg b/harbour/tests/rddtest/adscl52.prg index 5bb7ca7a83..92338e383b 100644 --- a/harbour/tests/rddtest/adscl52.prg +++ b/harbour/tests/rddtest/adscl52.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org diff --git a/harbour/tests/rddtest/adscl53.prg b/harbour/tests/rddtest/adscl53.prg index 0e59efb2df..73563addd0 100644 --- a/harbour/tests/rddtest/adscl53.prg +++ b/harbour/tests/rddtest/adscl53.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org diff --git a/harbour/tests/rddtest/cdxcl52.prg b/harbour/tests/rddtest/cdxcl52.prg index 65b58b5c82..808755aba8 100644 --- a/harbour/tests/rddtest/cdxcl52.prg +++ b/harbour/tests/rddtest/cdxcl52.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org diff --git a/harbour/tests/rddtest/cdxcl53.prg b/harbour/tests/rddtest/cdxcl53.prg index 2589758cb9..87dc34e7e2 100644 --- a/harbour/tests/rddtest/cdxcl53.prg +++ b/harbour/tests/rddtest/cdxcl53.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org diff --git a/harbour/tests/rddtest/ntxcl52.prg b/harbour/tests/rddtest/ntxcl52.prg index c9accdda4a..95587d8874 100644 --- a/harbour/tests/rddtest/ntxcl52.prg +++ b/harbour/tests/rddtest/ntxcl52.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org diff --git a/harbour/tests/rddtest/ntxcl53.prg b/harbour/tests/rddtest/ntxcl53.prg index b3cdf8605a..b5e5f4c3fc 100644 --- a/harbour/tests/rddtest/ntxcl53.prg +++ b/harbour/tests/rddtest/ntxcl53.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org diff --git a/harbour/tests/rddtest/rddmktst.prg b/harbour/tests/rddtest/rddmktst.prg index e990f3e782..ae9680797b 100644 --- a/harbour/tests/rddtest/rddmktst.prg +++ b/harbour/tests/rddtest/rddmktst.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org diff --git a/harbour/tests/rddtest/rddtst.prg b/harbour/tests/rddtest/rddtst.prg index 51c60b5976..65b5161b90 100644 --- a/harbour/tests/rddtest/rddtst.prg +++ b/harbour/tests/rddtest/rddtst.prg @@ -4,8 +4,7 @@ /* * Harbour Project source code: - * demonstration/test code for terminal character output and codepage - * translations + * RDD tests * * Copyright 2008 Przemyslaw Czerpak * www - http://www.harbour-project.org