File: xblock.cpp

package info (click to toggle)
xbase64 3.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,844 kB
  • ctags: 2,180
  • sloc: cpp: 14,547; sh: 8,678; makefile: 112
file content (582 lines) | stat: -rwxr-xr-x 14,986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*  xblock.cpp

    Xbase64 project source code
    written at 35000 feet on SWA
   
    This file contains the implementation of the xbLock.

    This file conatains a header file for the xbLock virtual objects which
    is used for controlling file and record locking.  Record and file
    locking has been rewritten in version 3.

    Copyright (C) 1997,2003  Gary A Kunkel
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


    Contact:
    
     Email:
    
      xdb-devel@lists.sourceforge.net
      xdb-users@lists.sourceforge.net
      
      
     Regular Mail:
     
       XBase Support
       149C South Main St
       Keller Texas, 76248     
       USA
*/

#ifdef __GNU_LesserG__
  #pragma implementation "xblock.h"
#endif

#ifdef __WIN32__
#include <xbase64/xbwincfg.h>
#else
#include <xbase64/xbconfig.h>
#endif

#include <xbase64/xbase64.h>

#ifdef HAVE_IO_H     // windows locking
#include <io.h>
#endif

#ifdef HAVE_DOS_H    // _sleep
#include <dos.h>
#endif

//#include <stdio.h>
//#include <stdlib.h>

#include <cerrno>

/*! \file xblock.cpp
*/
#ifdef XB_LOCKING_ON

//! Constructor
/*!
  \param pdbf
*/

/*************************************************************************/
xbLock::xbLock(xbDbf * pdbf)
{
  dbf          = pdbf;
  HdrLockCnt   = 0;
  TableLockCnt = 0;
  MemoLockCnt  = 0;
  IndexLockCnt = 0;
  std::cout << "xbLock constructor" << std::cout;
}
/*************************************************************************/
xbLock::~xbLock()
{
  std::cout << "xbLock destructor" << std::endl;
}
/*************************************************************************/

//! File lock routine
/*!
  Lowest level lock routine
  Locks/unlocks a database,memo or index file.
  This function assumes the file position has been correctly set
  
  \param fn        file to lock/unlock
  \param LockType  lock type, one of: XB_LOCK or XB_UNLOCK  
  \param lockLen   byte count to lock
*/

#ifdef __WIN32__
xbShort xbLock::LockFile( int fn, xbShort LockType, xbOffT lockLen)
{
  int mode;
  int rc;
  int tries = 0;

  /* convert the xbase locking command into a windows locking command */
  if( LockType == XB_UNLOCK )
    mode = LK_UNLCK;
  else if( LockType == XB_LOCK || LockType == XB_LOCK_HOLD )
    mode = LK_NBLCK;
  else
    return XB_INVALID_LOCK_OPTION;

  do{
    rc = locking( fn, mode, lockLen );
    if( rc )
      _sleep( 1 );
  } while( rc == -1 && tries < dbf->xbase->GetLockRetryCount());
  
  if( rc )
    return XB_LOCK_FAILED;
  
  return 0;
}

#elif HAVE_FCNTL_H

xbShort xbLock::LockFile( int fn, xbShort LockType, xbOffT lockLen )
{
  xbShort cmd, rc;
  xbShort tries = 0;
  
/* convert cross platform xbase lock type to unix lock type */  
  if( LockType == XB_UNLOCK ) 
    cmd = F_ULOCK;
else if( LockType == XB_LOCK || LockType == XB_LOCK_HOLD )
    cmd = F_TLOCK;
  else
    return XB_INVALID_LOCK_OPTION;

/* do the actual lock */
  do{     
    #ifdef _LARGEFILE64_SOURCE
      rc = lockf64( fn, cmd, lockLen );
    #else
      rc = lockf( fn, cmd, lockLen );
    #endif
    if( rc == -1 && errno != EINTR ){    
      tries++; 
      sleep(1);
    }
  } while( rc == -1 && tries < dbf->xbase->GetLockRetryCount());
    
  if( rc )
    return XB_LOCK_FAILED;
    
  return XB_NO_ERROR;
}
#endif     // HAVE_FCNTL
/*************************************************************************/
/*************************************************************************/
xbaseLock::xbaseLock( xbDbf * pdbf ) : xbLock( pdbf )
{
  std::cout << "xbaseLock constructor" << std::cout;
}
/*************************************************************************/
xbShort xbaseLock::LockTableHeader( xbShort LockType )
{
  if( LockType == XB_UNLOCK )
    return XB_NO_ERROR;
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort xbaseLock::LockTable( xbShort LockType )
{
  if(( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ) && TableLockCnt ){
    TableLockCnt++;
    return XB_NO_ERROR;
  }
  if( LockType == XB_UNLOCK && TableLockCnt > 1 ){
    TableLockCnt--;
    return XB_NO_ERROR;
  }

#ifdef _LARGEFILE64_SOURCE
  if( lseek64( fileno( lfh ), 1, SEEK_SET ) != 1 )
    return XB_LOCK_FAILED;

  if( LockFile( fileno( lfh ), LockType, 4294967295LL ) != XB_NO_ERROR )
    return XB_LOCK_FAILED;
#else
  if( lseek( fileno( lfh ), 1, SEEK_SET ) != 1 )
    return XB_LOCK_FAILED;

  if( LockFile( fileno( lfh ), LockType, 4294967295L ) != XB_NO_ERROR )
    return XB_LOCK_FAILED;
#endif
  
  if( LockType == XB_UNLOCK )
    TableLockCnt--;
  else
    TableLockCnt++;

  return XB_NO_ERROR;
}
/*************************************************************************/
xbShort xbaseLock::LockRecord(xbShort LockType,xbULong RecNo,xbOffT RecCnt)
{

#ifdef _LARGEFILE64_SOURCE
  if( lseek64( fileno( lfh ), 100L + RecNo, SEEK_SET ) == -1 ){
    return XB_LOCK_FAILED;
  }
#else
  if( lseek( fileno( lfh ), 100L + RecNo, SEEK_SET ) == -1 ){
    return XB_LOCK_FAILED;
  }
#endif

  return LockFile( fileno( lfh ), LockType, (xbOffT) RecCnt );
}
/*************************************************************************/
xbShort xbaseLock::LockMemo( xbShort LockType )
{
  xbShort rc;

  if(( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ) && MemoLockCnt ){
    MemoLockCnt++;
    return XB_NO_ERROR;
  }
  else if ( LockType == XB_UNLOCK && MemoLockCnt > 1 ){
    MemoLockCnt--;
    return XB_NO_ERROR;
  }
  
#ifdef _LARGEFILE64_SOURCE
  if( lseek64( fileno( lfh ), 2, SEEK_SET ) != 2 )
    return XB_LOCK_FAILED;
#else
  if( lseek( fileno( lfh ), 2, SEEK_SET ) != 2 )
    return XB_LOCK_FAILED;
#endif
  rc = LockFile( fileno( lfh ), LockType, 1 );

  if( rc == XB_NO_ERROR ){
    if( LockType == XB_UNLOCK )
      MemoLockCnt--;
    else
      MemoLockCnt++;
  }
  return rc;
}
/*************************************************************************/
//! Lock Index
/*!
  Locks all indices for a table when using lock mode XB_XBASE_LOCK_MODE
  
  \param LockType  is one of XB_LOCK, XB_LOCK_HOLD or XB_UNLOCK
*/

xbShort xbaseLock::LockIndex( xbShort LockType )
{
  xbShort rc;

//  if( !NdxList )
//    printf( "no index\n" );

  if(( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ) && IndexLockCnt ){
    IndexLockCnt++;
    return XB_NO_ERROR;
  }
  if( LockType == XB_UNLOCK && IndexLockCnt > 1 ){
    IndexLockCnt--;
    return XB_NO_ERROR;
  }

#ifdef _LARGEFILE64_SOURCE
  if( lseek64( fileno( lfh ), 3, SEEK_SET ) == -1 ){
    printf( "here cp1\n");
    return XB_LOCK_FAILED;
  }
#else
  if( lseek( fileno( lfh ), 3, SEEK_SET ) == -1 ){
    printf( "here cp2\n" );
    return XB_LOCK_FAILED;
  }
#endif

  rc = LockFile( fileno( lfh ), LockType, 1 );
  
  if( rc == XB_NO_ERROR )
    if( LockType == XB_UNLOCK )
      IndexLockCnt--;
    else
      IndexLockCnt++;

  return rc; 
}
/*************************************************************************/
xbShort xbaseLock::UnlockAll()
{
  return XB_INVALID_LOCK_OPTION;
}
/************************************************************************/
xbShort xbaseLock::LockInit()
{
  xbShort  len;
  xbString lfn;

  lfn = dbf->GetDbfName();
  lfn.resize( lfn.len() - 3 );

  lfn += ".lck";

#ifdef _LARGEFILE_SOURCE
  if(( lfh = fopen64( lfn.getData(), "w+b" )) == NULL )
    return XB_OPEN_ERROR;
#else
  if(( lfh = fopen( lfn.getData(), "w+b" )) == NULL )
    return XB_OPEN_ERROR;
#endif
        
  else
    return XB_NO_ERROR;
}

/*************************************************************************/
/*************************************************************************/
dbaseLock::dbaseLock( xbDbf * pdbf ) : xbLock( pdbf )
{
  std::cout << "dbaseLock constructor" << std::cout;
}
/*************************************************************************/
xbShort dbaseLock::LockTableHeader( xbShort LockType )
{
  if( LockType == XB_UNLOCK )
    return XB_NO_ERROR;
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort dbaseLock::LockTable( xbShort LockType )
{

  if(( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ) && TableLockCnt ){
    TableLockCnt++;
    return XB_NO_ERROR;
  }
  if( LockType == XB_UNLOCK && TableLockCnt > 1 ){
    TableLockCnt--;
    return XB_NO_ERROR;
  }

#ifdef _LARGEFILE64_SOURCE
  if( lseek64( dbf->GetDbfFileNo(), 4026531838LL, SEEK_SET ))
    return XB_LOCK_FAILED;

  if( LockFile( dbf->GetDbfFileNo(), LockType, 1 ) != XB_NO_ERROR )
    return XB_LOCK_FAILED;

  if( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ){
    if( LockRecord( XB_LOCK, 1, 4294967295LL ) != XB_NO_ERROR )
      return LockTable( XB_UNLOCK );
    LockRecord( XB_UNLOCK, 1, 4294967295LL );
  }

  if( LockType == XB_UNLOCK )
    TableLockCnt--;
  else
    TableLockCnt++;

  return XB_NO_ERROR;
#else
  /* I couldn't figure out how Dbase locks a file at offset 4026531838 
     for a 32 bit platform - if you know how, please let me know
     
     Gary - gkunkel@zhsac.com
     
  */

  return XB_INVALID_LOCK_OPTION;
#endif

}

/*************************************************************************/
xbShort dbaseLock::LockRecord( xbShort LockType, xbULong RecNo, xbOffT RecCnt )
{
#ifdef _LARGEFILE64_SOURCE

  if( lseek64( dbf->GetDbfFileNo(), 4026531838LL - (RecNo+RecCnt-1), SEEK_SET ) == -1 )
    return XB_LOCK_FAILED;

  return LockFile( dbf->GetDbfFileNo(), LockType, RecCnt );

#else

  /* I couldn't figure out how dbase locks a file at offset 4026531838 
     for a 32 bit platform - if you know how, please let me know
     
     Gary - gkunkel@zhsac.com
     
  */

  return XB_INVALID_LOCK_OPTION;
#endif
}

/*************************************************************************/
xbShort dbaseLock::LockMemo( xbShort LockType )
{

  xbShort rc;

  if(( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ) && MemoLockCnt ){
    MemoLockCnt++;
    return XB_NO_ERROR;
  }
  else if ( LockType == XB_UNLOCK && MemoLockCnt > 1 ){
    MemoLockCnt--;
    return XB_NO_ERROR;
  }
  
#ifdef _LARGEFILE_SOURCE
  if( lseek64( dbf->GetMemoFileNo(), 4026531838LL, SEEK_SET ) == -1 )
    return XB_LOCK_FAILED;
  rc = LockFile( dbf->GetMemoFileNo(), LockType, 1 );
#else
  rc = XB_INVALID_OPTION;
#endif

  if( rc == XB_NO_ERROR ){
    if( LockType == XB_UNLOCK )
      MemoLockCnt--;
    else
      MemoLockCnt++;
  }
  return rc;
}
/*************************************************************************/
xbShort dbaseLock::LockIndex( xbShort LockType )
{
  if( LockType == XB_NO_ERROR )
    return XB_NO_ERROR;
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort dbaseLock::UnlockAll()
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
/*************************************************************************/
clipperLock::clipperLock( xbDbf * pdbf ) : xbLock( pdbf )
{
  std::cout << "clipperLock constructor" << std::cout;
}
/*************************************************************************/
xbShort clipperLock::LockTableHeader( xbShort LockType )
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort clipperLock::LockTable( xbShort LockType )
{
  if(( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ) && TableLockCnt ){
    TableLockCnt++;
    return XB_NO_ERROR;
  }
  if( LockType == XB_UNLOCK && TableLockCnt > 1 ){
    TableLockCnt--;
    return XB_NO_ERROR;
  }

  if( LockRecord( LockType, 1L, 1000000000L ) != XB_NO_ERROR )
    return XB_LOCK_FAILED;
  
  if( LockType == XB_UNLOCK )
    TableLockCnt--;
  else
    TableLockCnt++;

  return XB_NO_ERROR;
}
/*************************************************************************/
xbShort clipperLock::LockRecord(
     xbShort LockType, xbULong RecNo, xbOffT RecCnt )
{

#ifdef _LARGEFILE64_SOURCE
  if( lseek64( dbf->GetDbfFileNo(), 1000000000L + RecNo, SEEK_SET ))
    return XB_LOCK_FAILED;
#else
  if( lseek( dbf->GetDbfFileNo(), 1000000000L + RecNo, SEEK_SET ))
    return XB_LOCK_FAILED; 
#endif

  return LockFile( dbf->GetDbfFileNo(), LockType, RecCnt );
}
/*************************************************************************/
xbShort clipperLock::LockMemo( xbShort LockType )
{
  return XB_NO_ERROR;
}
/*************************************************************************/
xbShort clipperLock::LockIndex( xbShort LockType )
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort clipperLock::UnlockAll()
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
/*************************************************************************/
foxproLock::foxproLock( xbDbf * pdbf ) : xbLock( pdbf )
{
  std::cout << "foxproLock constructor" << std::cout;
}
/*************************************************************************/
xbShort foxproLock::LockTableHeader( xbShort LockType )
{

  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort foxproLock::LockTable( xbShort LockType )
{

  if(( LockType == XB_LOCK || LockType == XB_LOCK_HOLD ) && TableLockCnt ){
    TableLockCnt++;
    return XB_NO_ERROR;
  }
  if( LockType == XB_UNLOCK && TableLockCnt > 1 ){
    TableLockCnt--;
    return XB_NO_ERROR;
  }


// something goes in here


  if( LockType == XB_UNLOCK )
    TableLockCnt--;
  else
    TableLockCnt++;

  return XB_NO_ERROR;

}
/*************************************************************************/
xbShort foxproLock::LockRecord( xbShort LockType, xbULong RecNo, xbOffT len )
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort foxproLock::LockMemo( xbShort LockType )
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort foxproLock::LockIndex( xbShort LockType )
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/
xbShort foxproLock::UnlockAll()
{
  return XB_INVALID_LOCK_OPTION;
}
/*************************************************************************/

#endif  // XB_LOCKING_ON