Skip to content
75 changes: 75 additions & 0 deletions contrib/oads_hb/FILE_LOCKS_MTFIX85.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
OpenADS FS byte locks - mtfix85
Pritpal Bedi
2026-11-15 11:01

Remote ownership choice for review: each client connection is a separate
lock owner, like a separate Harbour process. Within that connection, file
opens share the merged registry. Different clients can share read locks,
and WAIT can wait for a different client. They do not all become one
owner merely because one server process handles them.

OAds_FLock( hFile, nStart, nLen, nType ) -> lOk
OAds_FUnlock( hFile, nStart, nLen ) -> lOk
OAds_FLockTest( hFile, nStart, nLen, nType ) -> nResult

hFile comes from OAds_FOpen or OAds_FCreate. These are file-system
byte-range locks, not RDD record locks. No RDD/CDX lock code is changed.
They use the host OS byte locks, visible across processes. Windows I/O
and locks use the same native handle. EnableFileFunc and jail rules apply.

nType flags, combined with hb_bitOr():
0x0000 = exclusive, no wait (default)
0x0100 = shared
0x0200 = wait until success
0x0300 = shared, wait until success

NO_WAIT makes one attempt. WAIT repeats after OS contention until success
or error. Each server request makes one non-blocking OS attempt, never
sleeps, and returns. The client retries every 10 ms without holding the
file/connection/registry mutexes between requests. The Harbour wrapper
releases the VM. Connection failure or a closed handle ends the call.
A caller can wait indefinitely if an external owner never releases.

Harbour filebuf.c behavior preserved:
- Opens of the same physical file within one owner share a merged range
registry and native file. Each public handle keeps its own position.
- An overlapping lock already in that registry fails immediately,
including with WAIT. An identical re-lock is not recursive/idempotent.
- Adjacent registrations merge. Unlock removes/splits a contained range
after the native OS unlock succeeds.
- LockTest reports an overlapping own registration as locked. Otherwise
Unix queries return the external owner PID where the OS supplies one;
OFD locks have no PID, so they return 1. Windows returns 1 for
conflict. Both return 0 for free, -1 for error. It never waits.
- Closing one reference does not release shared native-file locks while
another reference remains. Last close releases the OS locks.
- Unix read-only opens request shared OS locks, as Harbour does.
- POSIX uses OFD locks so separate client connections retain independent
ownership and closing one cannot drop another's locks. This differs
from Harbour's process-owned fcntl locks. OS-reported classic-lock PIDs
are returned when available; an OFD owner has no PID and returns 1.

OS behavior is preserved, including:
- Unix partial unlock and zero-length-to-EOF ranges work through fcntl.
- Windows UnlockFileEx requires the exact native acquired range. A
partial unlock or one spanning distinct acquisitions can fail, just as
Harbour's Windows backend does. Zero length has Windows native meaning,
not the Unix to-EOF meaning.
- Windows locks affect I/O. Unix locks are advisory. Other programs must
cooperate.

Nonnegative file offsets and ranges must fit the signed 64-bit offset
space used by the cross-platform API. No virtual-file/socket drivers are
implemented by these OAds handles. Error reporting is through OAds return
values, not Harbour FError().

DirSpace correction in mtfix85 (supersedes mtfix84 custom values):
HB_DISK_AVAIL = 0, HB_DISK_FREE = 1, HB_DISK_USED = 2,
HB_DISK_TOTAL = 3, matching Harbour include/fileio.ch.
Do not carry mtfix84's 1=capacity or 2=free meanings into this kit.

Reference source:
https://github.com/harbour/core/blob/master/src/rtl/vfile.c
https://github.com/harbour/core/blob/master/src/rtl/filebuf.c
https://github.com/harbour/core/blob/master/src/rtl/filesys.c
https://github.com/harbour/core/blob/master/include/fileio.ch
39 changes: 38 additions & 1 deletion contrib/oads_hb/oads_hb.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,43 @@ HB_FUNC( OADS_SETFILETIME )
/* ------------------------------------------------------------------ */
/* OADS_FTrunc( hFile, nSize ) -> lOk */
/* ------------------------------------------------------------------ */
/* FS byte locks only, independent of RDD record locks.
* nType: 0 exclusive/no-wait, 0x100 shared, 0x200 wait until success.
* LockTest: 0 free, POSIX PID (Windows 1) locked, -1 error.
* WAIT releases the Harbour VM so another Harbour thread can unlock.
*/
HB_FUNC( OADS_FLOCK )
{
ADSHANDLE h = ( ADSHANDLE ) hb_parnint( 1 );
HB_MAXINT start = hb_parnint( 2 ), len = hb_parnint( 3 );
UNSIGNED16 type = ( UNSIGNED16 ) hb_parni( 4 );
UNSIGNED32 rc = 5000;
if( h && start >= 0 && len >= 0 )
{
hb_vmUnlock();
rc = oads_FLock( h, ( UNSIGNED64 ) start, ( UNSIGNED64 ) len, type );
hb_vmLock();
}
hb_retl( rc == 0 );
}
HB_FUNC( OADS_FUNLOCK )
{
ADSHANDLE h = ( ADSHANDLE ) hb_parnint( 1 );
HB_MAXINT start = hb_parnint( 2 ), len = hb_parnint( 3 );
hb_retl( h && start >= 0 && len >= 0 &&
oads_FUnlock( h, ( UNSIGNED64 ) start, ( UNSIGNED64 ) len ) == 0 );
}
HB_FUNC( OADS_FLOCKTEST )
{
ADSHANDLE h = ( ADSHANDLE ) hb_parnint( 1 );
HB_MAXINT start = hb_parnint( 2 ), len = hb_parnint( 3 );
SIGNED32 result = -1;
if( h && start >= 0 && len >= 0 )
oads_FLockTest( h, ( UNSIGNED64 ) start, ( UNSIGNED64 ) len,
( UNSIGNED16 ) hb_parni( 4 ), &result );
hb_retni( result );
}

HB_FUNC( OADS_FTRUNC )
{
ADSHANDLE hFile = ( ADSHANDLE ) hb_parnint( 1 );
Expand Down Expand Up @@ -757,7 +794,7 @@ HB_FUNC( OADS_TEMPFILE )
/* ------------------------------------------------------------------ */
/* OADS_DirSpace( hConn, cDir, nType ) -> nBytes */
/* OADS_DirSpace( cDir, nType ) -> nBytes (default conn) */
/* nType: 0 = free available, 1 = total capacity, 2 = free. */
/* nType: 0 = available, 1 = free, 2 = used, 3 = total. */
/* ------------------------------------------------------------------ */
HB_FUNC( OADS_DIRSPACE )
{
Expand Down
8 changes: 7 additions & 1 deletion include/openads/ace.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,17 @@ UNSIGNED32 ENTRYPOINT AdsSetFileTime (ADSHANDLE hConnect, UNSIGNED8* pucName,
UNSIGNED16 usYear, UNSIGNED8 ucMon,
UNSIGNED8 ucDay, UNSIGNED8 ucHour,
UNSIGNED8 ucMin, UNSIGNED8 ucSec);
UNSIGNED32 ENTRYPOINT AdsFLock(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen, UNSIGNED16 usType);
UNSIGNED32 ENTRYPOINT AdsFUnlock(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen);
UNSIGNED32 ENTRYPOINT AdsFLockTest(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen, UNSIGNED16 usType, SIGNED32* plResult);
UNSIGNED32 ENTRYPOINT AdsFTrunc (ADSHANDLE hFile,
UNSIGNED64 ullSize);
// Creates an empty uniquely named file under pucDir; the name lands in
// pucName (capacity *pusNameLen in, length out, not NUL-padded).
UNSIGNED32 ENTRYPOINT AdsTempFile (ADSHANDLE hConnect, UNSIGNED8* pucDir,
UNSIGNED8* pucPrefix, UNSIGNED8* pucName,
UNSIGNED16* pusNameLen);
// ucType: 0 = free available, 1 = total capacity, 2 = free.
// ucType: 0 = available, 1 = free, 2 = used, 3 = total.
UNSIGNED32 ENTRYPOINT AdsDirSpace (ADSHANDLE hConnect, UNSIGNED8* pucDir,
UNSIGNED8 ucType, UNSIGNED64* pullBytes);
UNSIGNED32 ENTRYPOINT AdsGetFileSize (ADSHANDLE hConnect, UNSIGNED8* pucName,
Expand Down Expand Up @@ -237,6 +240,9 @@ UNSIGNED32 OADSAPI oads_SetFileTime (ADSHANDLE hConnect, UNSIGNED8* pucName,
UNSIGNED16 usYear, UNSIGNED8 ucMon,
UNSIGNED8 ucDay, UNSIGNED8 ucHour,
UNSIGNED8 ucMin, UNSIGNED8 ucSec);
UNSIGNED32 OADSAPI oads_FLock(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen, UNSIGNED16 usType);
UNSIGNED32 OADSAPI oads_FUnlock(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen);
UNSIGNED32 OADSAPI oads_FLockTest(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen, UNSIGNED16 usType, SIGNED32* plResult);
UNSIGNED32 OADSAPI oads_FTrunc (ADSHANDLE hFile,
UNSIGNED64 ullSize);
UNSIGNED32 OADSAPI oads_TempFile (ADSHANDLE hConnect, UNSIGNED8* pucDir,
Expand Down
50 changes: 50 additions & 0 deletions src/abi/ace_exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11560,6 +11560,56 @@ UNSIGNED32 ENTRYPOINT AdsSetFileTime(ADSHANDLE hConn, UNSIGNED8* pucName,
return ok();
}

// Each invocation makes one non-blocking attempt. Never sleep while holding
// the ABI registry mutex or the per-file/connection mutex.
extern "C++" {
static openads::util::Result<std::int32_t> oads_fs_lock_once(ADSHANDLE hFile,
UNSIGNED64 start, UNSIGNED64 len, UNSIGNED16 type, std::uint8_t op) {
auto& s = state();
std::unique_lock<std::recursive_mutex> lk(s.mu);
if (auto it = remote_files_map().find(hFile); it != remote_files_map().end()) {
auto rf = it->second;
lk.unlock();
std::lock_guard<std::mutex> file_lk(rf->mu);
if (rf->closed) return openads::util::Error{5000, 0, "closed file", ""};
return rf->conn->file_lock_step(rf->file_id, start, len, type, op);
}
if (auto* lf = s.registry.lookup<openads::engine::FsFile>(
hFile, openads::session::HandleKind::LocalFile))
return openads::engine::fs_lock_step(*lf, start, len, type, op);
return openads::util::Error{5000, 0, "bad file handle", ""};
}
}

UNSIGNED32 ENTRYPOINT AdsFLock(ADSHANDLE hFile, UNSIGNED64 ullStart,
UNSIGNED64 ullLen, UNSIGNED16 usType) {
arc2_trace("AdsFLock");
for (;;) {
auto v = oads_fs_lock_once(hFile, ullStart, ullLen, usType, 0);
if (!v) return fail(v.error());
if (v.value()) return ok();
if ((usType & 0x0200u) == 0) return fail(5012, "file lock busy");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
UNSIGNED32 ENTRYPOINT AdsFUnlock(ADSHANDLE hFile, UNSIGNED64 ullStart,
UNSIGNED64 ullLen) {
arc2_trace("AdsFUnlock");
auto v = oads_fs_lock_once(hFile, ullStart, ullLen, 0, 1);
if (!v) return fail(v.error());
return ok();
}
UNSIGNED32 ENTRYPOINT AdsFLockTest(ADSHANDLE hFile, UNSIGNED64 ullStart,
UNSIGNED64 ullLen, UNSIGNED16 usType, SIGNED32* plResult) {
arc2_trace("AdsFLockTest");
if (!plResult) return fail(5000, "null lock-test result");
*plResult = -1;
auto v = oads_fs_lock_once(hFile, ullStart, ullLen, usType, 2);
if (!v) return fail(v.error());
*plResult = v.value();
return ok();
}

UNSIGNED32 ENTRYPOINT AdsFTrunc(ADSHANDLE hFile, UNSIGNED64 ullSize) {
arc2_trace("AdsFTrunc");
auto& s = state();
Expand Down
9 changes: 9 additions & 0 deletions src/abi/adsfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,12 @@ UNSIGNED32 OADSAPI oads_Directory( ADSHANDLE hConnect,
{
return AdsDirectory( hConnect, pucMask, usAttr, pucBuffer, pulBufLen );
}

UNSIGNED32 OADSAPI oads_FLock(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen, UNSIGNED16 usType)
{ return AdsFLock(hFile, ullStart, ullLen, usType); }

UNSIGNED32 OADSAPI oads_FUnlock(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen)
{ return AdsFUnlock(hFile, ullStart, ullLen); }

UNSIGNED32 OADSAPI oads_FLockTest(ADSHANDLE hFile, UNSIGNED64 ullStart, UNSIGNED64 ullLen, UNSIGNED16 usType, SIGNED32* plResult)
{ return AdsFLockTest(hFile, ullStart, ullLen, usType, plResult); }
Loading
Loading