Record and File Locking

Chapter Updated 11/29/22


Locking Overview

Xbase64 supports multi-user processing through file and record locks. Record locking restricts multiple cooperating programs from simultaneously accessing the same data and corrupting it. Without record and file locking in a multi-user environment, simultaneous access to the data and index files can cause the files to become inaccurate and unusable.

Automatic record locking is on by default in the Xbase64 library. To disable it, use method xbXBase::DisableDefaultAutoLock() and to enable it, use method xbXBase::EnableDefaultAutoLock(). Locking can also be enabled / disabled at the table level with with xbDbf::SetAutoLock().

The current Xbase64 record locking logic is modeled after DBase (tm) V7 locking.

The locking methods return either XB_LOCK_FAILED or XB_NO_ERROR. If they return XB_LOCK_FAILED the actual reason can be found in the global variable errno or function perror() can be executed to view the results.

The errno field may contain one of the following values if the lock was not successful.

Error CodeDescription
EBADFInvalid file descriptor
EINVALInvalid lock information or file does not support locks
EACCESS
EAGAIN
Lock can not be set because it is blocked by an existing lock on the file.
ENOLCKThe system is out of lock resources, too many file locks in place.
EDEADLKDeadlock condition
EINTRProcess was interrupted by a signal while it was waiting


Linux/Windows File Locking Compatibility Issue

There is a compatibility locking issue to be aware of. Windows environments allow for the exclusive opening of file handles and Linux/Unix platforms do not. If you are writing an application that will be using a tool like Dbase on a Windows machine, accessing a file on a Linux/Samba configure machine, be aware that the file could be opened in exclusive mode by DBase on the Windows system, and the same file could be simultaneously opened with a program on the Unix box. That could cause some issues.

In Unix, a program can not lock a file so another process can not access it.
In Windows, a program can lock a file so another process can not access it.
DBase(tm) supports routines to open files exclusively, preventing other users from opening a file.

Samba settings

If you will be using Samba on Linux/Unix and sharing files between Linux and Windows machines, you will need to disable oplocks. In the smb.conf file, set:

oplocks = no