123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- #include <stdio.h>
- #include <errno.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include "jam.h"
- #include "structrw.h"
- #if defined( __OS2__ )
- #include <os2.h> /* ANSI C does not include file locking :-( */
- #endif
- #if defined( __WIN32__ )
- #include <sys/locking.h>
- #include <io.h>
- #include <windows.h>
- #if !defined( _LK_UNLCK ) && defined ( _LK_UNLOCK )
- #define _LK_UNLCK _LK_UNLOCK
- #endif
- #endif
- #if defined( __LINUX__ )
- #include <sys/file.h>
- #include <unistd.h>
- #if defined(__sun) || defined(__MUSL__) || defined(__HAIKU__)
- #include <fcntl.h>
- #endif
- #endif
- #define OS_ERROR_OFFSET 10000
- #if defined( __OS2__ )
- #define JAM_Sleep( _x_ ) DosSleep( _x_*1000 )
- #endif
- #if defined( __WIN32__ )
- #define JAM_Sleep(x) Sleep(x*1000)
- #endif
- #if defined( __LINUX__ )
- #define JAM_Sleep(x) sleep(x)
- #endif
- int jam_Open( s_JamBase* Base_PS, char* Filename_PC, char* Mode_PC );
- int jam_Lock( s_JamBase* Base_PS, int DoLock_I );
- int JAM_OpenMB( char* Basename_PC, s_JamBase** NewArea_PPS )
- {
- s_JamBase* Base_PS;
- int Status_I;
- if ( !NewArea_PPS )
- return JAM_BAD_PARAM;
- *NewArea_PPS = NULL;
- Base_PS = (s_JamBase*) calloc( 1, sizeof( s_JamBase ) );
- if (!Base_PS)
- return JAM_NO_MEMORY;
- *NewArea_PPS = Base_PS;
- Status_I = jam_Open( Base_PS, Basename_PC, "r+b" );
- if ( Status_I ) {
- return Status_I;
- }
- return 0;
- }
- int JAM_CreateMB( char* Basename_PC,
- uint32_t BaseMsg_I,
- s_JamBase** NewArea_PPS )
- {
- s_JamBaseHeader Base_S;
- int Status_I;
- s_JamBase* Base_PS;
- if ( !NewArea_PPS || !BaseMsg_I )
- return JAM_BAD_PARAM;
- *NewArea_PPS = NULL;
- Base_PS = (s_JamBase*) calloc( 1, sizeof( s_JamBase ) );
- if (!Base_PS)
- return JAM_NO_MEMORY;
- *NewArea_PPS = Base_PS;
- Status_I = jam_Open( Base_PS, Basename_PC, "w+b" );
- if ( Status_I )
- return Status_I;
- Base_S.DateCreated = time(NULL);
- Base_S.ModCounter = 0;
- Base_S.ActiveMsgs = 0;
- Base_S.PasswordCRC = 0xffffffff;
- Base_S.BaseMsgNum = BaseMsg_I;
- memset( &Base_S.RSRVD, 0, sizeof( Base_S.RSRVD ) );
- Status_I = JAM_LockMB( Base_PS, 0 );
- if ( Status_I ) {
- JAM_CloseMB(Base_PS);
- return Status_I;
- }
- Status_I = JAM_WriteMBHeader( Base_PS, &Base_S );
- if ( Status_I ) {
- JAM_UnlockMB( Base_PS );
- JAM_CloseMB(Base_PS);
- return Status_I;
- }
- JAM_UnlockMB( Base_PS );
- return 0;
- }
- int JAM_CloseMB( s_JamBase* Base_PS )
- {
- if ( Base_PS->Locked_I ) {
- int Status_I = JAM_UnlockMB( Base_PS );
- if ( Status_I )
- return Status_I;
- }
- if ( Base_PS->HdrFile_PS ) {
- fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
- fclose( Base_PS->TxtFile_PS ); Base_PS->TxtFile_PS = NULL;
- fclose( Base_PS->IdxFile_PS ); Base_PS->IdxFile_PS = NULL;
- fclose( Base_PS->LrdFile_PS ); Base_PS->LrdFile_PS = NULL;
- }
- Base_PS->Locked_I = 0;
- return 0;
- }
- int JAM_RemoveMB( s_JamBase* Base_PS, char* Basename_PC )
- {
- char Filename_AC[250];
- int Status_AI[4];
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_HDRFILE );
- Status_AI[0] = remove( Filename_AC );
- if ( Status_AI[0] )
- Base_PS->Errno_I = errno;
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_TXTFILE );
- Status_AI[1] = remove( Filename_AC );
- if ( Status_AI[1] )
- Base_PS->Errno_I = errno;
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_IDXFILE );
- Status_AI[2] = remove( Filename_AC );
- if ( Status_AI[2] )
- Base_PS->Errno_I = errno;
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_LRDFILE );
- Status_AI[3] = remove( Filename_AC );
- if ( Status_AI[3] )
- Base_PS->Errno_I = errno;
- if (Status_AI[0] || Status_AI[1] || Status_AI[2] || Status_AI[3])
- return JAM_IO_ERROR;
- return 0;
- }
- int JAM_GetMBSize( s_JamBase* Base_PS, uint32_t* Messages_PI )
- {
- long Offset_I;
-
- if ( fseek( Base_PS->IdxFile_PS, 0, SEEK_END ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- Offset_I = ftell( Base_PS->IdxFile_PS );
- if ( Offset_I == -1 ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- *Messages_PI = Offset_I / sizeof( s_JamIndex );
- return 0;
- }
- int JAM_LockMB( s_JamBase* Base_PS, int Timeout_I )
- {
- if ( Base_PS->Locked_I )
- return 0;
- switch ( Timeout_I )
- {
-
- case -1:
- while ( jam_Lock( Base_PS, 1 ) == JAM_LOCK_FAILED )
- JAM_Sleep( 1 );
- return 0;
-
- case 0:
- return jam_Lock( Base_PS, 1 );
-
- default:
- {
- time_t Time_I = time(NULL) + Timeout_I;
-
- while ( time(NULL) < Time_I )
- {
- int Result_I;
- Result_I = jam_Lock( Base_PS, 1 );
- if ( Result_I == JAM_LOCK_FAILED )
- JAM_Sleep( 1 );
- else
- return Result_I;
- }
- return JAM_LOCK_FAILED;
- }
- }
- }
- int JAM_UnlockMB( s_JamBase* Base_PS )
- {
- fflush( Base_PS->HdrFile_PS );
- fflush( Base_PS->TxtFile_PS );
- fflush( Base_PS->IdxFile_PS );
- fflush( Base_PS->LrdFile_PS );
- return jam_Lock( Base_PS, 0 );
- }
- int JAM_ReadMBHeader( s_JamBase* Base_PS, s_JamBaseHeader* Header_PS )
- {
- if ( !Header_PS || !Base_PS )
- return JAM_BAD_PARAM;
- if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if ( 1 > freadjambaseheader(Base_PS->HdrFile_PS,Header_PS) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- return 0;
- }
- int JAM_WriteMBHeader( s_JamBase* Base_PS, s_JamBaseHeader* Header_PS )
- {
- if ( !Header_PS || !Base_PS )
- return JAM_BAD_PARAM;
- if ( !Base_PS->Locked_I )
- return JAM_NOT_LOCKED;
- if ( fseek( Base_PS->HdrFile_PS, 0, SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- memcpy( Header_PS->Signature, HEADERSIGNATURE, 4 );
- Header_PS->ModCounter++;
- if ( 1 > fwritejambaseheader(Base_PS->HdrFile_PS,Header_PS) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- fflush( Base_PS->HdrFile_PS );
- return 0;
- }
- int JAM_FindUser( s_JamBase* Base_PS,
- uint32_t UserCrc_I,
- uint32_t StartMsg_I,
- uint32_t* MsgNo_PI )
- {
- uint32_t MsgNo_I;
-
- if ( fseek( Base_PS->IdxFile_PS, StartMsg_I * sizeof( s_JamIndex ),
- SEEK_SET ) ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- for ( MsgNo_I = StartMsg_I; ; MsgNo_I++ ) {
- s_JamIndex Index_S;
- if ( 1 > freadjamindex(Base_PS->IdxFile_PS,&Index_S) ) {
- if ( feof(Base_PS->IdxFile_PS) )
- return JAM_NO_USER;
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if ( Index_S.UserCRC == UserCrc_I )
- break;
- }
- *MsgNo_PI = MsgNo_I;
- return 0;
- }
- int jam_Lock( s_JamBase* Base_PS, int DoLock_I )
- {
- #if defined(__OS2__)
- FILELOCK Area_S;
- APIRET Status_I;
- ULONG Timeout_I = 0;
- int Handle_I;
- Handle_I = fileno( Base_PS->HdrFile_PS );
- if ( Handle_I == -1 ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- Area_S.lOffset = 0;
- Area_S.lRange = 1;
- if ( DoLock_I )
- Status_I = DosSetFileLocks( Handle_I, NULL, &Area_S, Timeout_I, 0 );
- else
- Status_I = DosSetFileLocks( Handle_I, &Area_S, NULL, Timeout_I, 0 );
- if ( Status_I ) {
- if ( 232 == Status_I )
- return JAM_LOCK_FAILED;
- Base_PS->Errno_I = Status_I + OS_ERROR_OFFSET;
- return JAM_IO_ERROR;
- }
- if ( DoLock_I )
- Base_PS->Locked_I = 1;
- else
- Base_PS->Locked_I = 0;
- return 0;
- #elif defined(__WIN32__)
- int Handle_I,Status_I;
- fseek(Base_PS->HdrFile_PS,0,SEEK_SET);
- Handle_I = fileno( Base_PS->HdrFile_PS );
- if ( Handle_I == -1 ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if ( DoLock_I )
- Status_I = _locking(Handle_I,_LK_NBLCK,1);
- else
- Status_I = _locking(Handle_I,_LK_UNLCK,1);
- if ( Status_I )
- return JAM_LOCK_FAILED;
- if ( DoLock_I )
- Base_PS->Locked_I = 1;
- else
- Base_PS->Locked_I = 0;
- return 0;
- #elif defined(__LINUX__)
- int Handle_I,Status_I;
- struct flock fl;
- fseek(Base_PS->HdrFile_PS,0,SEEK_SET);
- Handle_I = fileno( Base_PS->HdrFile_PS );
- if ( Handle_I == -1 ) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
- if(DoLock_I) fl.l_type=F_WRLCK;
- else fl.l_type=F_UNLCK;
- fl.l_whence=SEEK_SET;
- fl.l_start=0;
- fl.l_len=1;
- fl.l_pid=getpid();
- Status_I=fcntl(Handle_I,F_SETLK,&fl);
- if ( Status_I ) {
- Base_PS->Errno_I = errno;
- return JAM_LOCK_FAILED;
- }
- if ( DoLock_I )
- Base_PS->Locked_I = 1;
- else
- Base_PS->Locked_I = 0;
- return 0;
- #else
- #error Unsupported platform
- #endif
- }
- int jam_Open( s_JamBase* Base_PS, char* Basename_PC, char* Mode_PC )
- {
- char Filename_AC[250];
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_HDRFILE );
- Base_PS->HdrFile_PS = fopen( Filename_AC, Mode_PC );
- if (!Base_PS->HdrFile_PS) {
- Base_PS->Errno_I = errno;
- return JAM_IO_ERROR;
- }
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_TXTFILE );
- Base_PS->TxtFile_PS = fopen( Filename_AC, Mode_PC );
- if (!Base_PS->TxtFile_PS) {
- Base_PS->Errno_I = errno;
- fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
- return JAM_IO_ERROR;
- }
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_IDXFILE );
- Base_PS->IdxFile_PS = fopen( Filename_AC, Mode_PC );
- if (!Base_PS->IdxFile_PS) {
- Base_PS->Errno_I = errno;
- fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
- fclose( Base_PS->TxtFile_PS ); Base_PS->TxtFile_PS = NULL;
- return JAM_IO_ERROR;
- }
-
- sprintf( Filename_AC, "%s%s", Basename_PC, EXT_LRDFILE );
- Base_PS->LrdFile_PS = fopen( Filename_AC, Mode_PC );
- if (!Base_PS->LrdFile_PS) {
- if (strcmp(Mode_PC, "r+b") == 0) {
- Base_PS->LrdFile_PS = fopen(Filename_AC, "w+b");
- if (!Base_PS->LrdFile_PS) {
- Base_PS->Errno_I = errno;
- fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
- fclose( Base_PS->TxtFile_PS ); Base_PS->TxtFile_PS = NULL;
- fclose( Base_PS->IdxFile_PS ); Base_PS->IdxFile_PS = NULL;
- return JAM_IO_ERROR;
- }
- } else {
- Base_PS->Errno_I = errno;
- fclose( Base_PS->HdrFile_PS ); Base_PS->HdrFile_PS = NULL;
- fclose( Base_PS->TxtFile_PS ); Base_PS->TxtFile_PS = NULL;
- fclose( Base_PS->IdxFile_PS ); Base_PS->IdxFile_PS = NULL;
- return JAM_IO_ERROR;
- }
- }
- return 0;
- }
|