diff --git a/contrib/oads_hb/oads_hb.c b/contrib/oads_hb/oads_hb.c index 374f2eac..47068a60 100644 --- a/contrib/oads_hb/oads_hb.c +++ b/contrib/oads_hb/oads_hb.c @@ -47,6 +47,7 @@ #include "hbvm.h" #include "ace.h" #include +#include /* The no-handle OAds_* overloads belong to this Harbour glue, not to * rddads. ACE's default is thread-local; the app can set it on its login @@ -440,6 +441,349 @@ HB_FUNC( OADS_RENAMEFILE ) hb_retl( ulRc == 0 ); } +/* ------------------------------------------------------------------ */ +/* OADS_CopyFile( hConn, cSrcFile, cTgtFile ) -> lOk */ +/* OADS_CopyFile( cSrcFile, cTgtFile ) -> lOk (default conn) */ +/* Server-side copy; overwrites the target if it exists. */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_COPYFILE ) +{ + ADSHANDLE hConn; + const char *szSrc; + const char *szDst; + UNSIGNED32 ulRc = 1; + + if( hb_pcount() >= 3 ) + { + hConn = ( ADSHANDLE ) hb_parnint( 1 ); + szSrc = hb_parc( 2 ); + szDst = hb_parc( 3 ); + } + else + { + hConn = oads_default_connection(); + szSrc = hb_parc( 1 ); + szDst = hb_parc( 2 ); + } + + if( szSrc && szDst ) + ulRc = oads_CopyFile( hConn, ( UNSIGNED8 * ) szSrc, + ( UNSIGNED8 * ) szDst ); + hb_retl( ulRc == 0 ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_FLoad( hConn, cName ) -> cData | NIL */ +/* OADS_FLoad( cName ) -> cData | NIL (default conn) */ +/* Whole file in one call (hb_vfLoad). NIL on error. */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_FLOAD ) +{ + ADSHANDLE hConn; + const char *szName; + + if( hb_pcount() >= 2 ) + { + hConn = ( ADSHANDLE ) hb_parnint( 1 ); + szName = hb_parc( 2 ); + } + else + { + hConn = oads_default_connection(); + szName = hb_parc( 1 ); + } + + if( szName ) + { + UNSIGNED32 ulSize = 0; + if( oads_GetFileSize( hConn, ( UNSIGNED8 * ) szName, + &ulSize ) == 0 ) + { + char *pBuf = ( char * ) hb_xgrab( ( HB_SIZE ) ulSize + 1 ); + UNSIGNED32 ulLen = ulSize; + if( oads_LoadFile( hConn, ( UNSIGNED8 * ) szName, + pBuf, &ulLen ) == 0 ) + { + memset( pBuf + ulLen, 0, 1 ); + hb_retclen( pBuf, ( HB_SIZE ) ulLen ); + hb_xfree( pBuf ); + return; + } + hb_xfree( pBuf ); + } + } + hb_ret(); +} + +/* ------------------------------------------------------------------ */ +/* OADS_FSave( hConn, cName, cData ) -> lOk */ +/* OADS_FSave( cName, cData ) -> lOk (default conn) */ +/* Whole file in one call (hb_vfSave); creates or truncates. */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_FSAVE ) +{ + ADSHANDLE hConn; + const char *szName; + const char *szData; + UNSIGNED32 ulLen; + UNSIGNED32 ulRc = 1; + + if( hb_pcount() >= 3 ) + { + hConn = ( ADSHANDLE ) hb_parnint( 1 ); + szName = hb_parc( 2 ); + szData = hb_parc( 3 ); + ulLen = ( UNSIGNED32 ) hb_parclen( 3 ); + } + else + { + hConn = oads_default_connection(); + szName = hb_parc( 1 ); + szData = hb_parc( 2 ); + ulLen = ( UNSIGNED32 ) hb_parclen( 2 ); + } + + if( szName && ( szData || ulLen == 0 ) ) + ulRc = oads_SaveFile( hConn, ( UNSIGNED8 * ) szName, + szData, ulLen ); + hb_retl( ulRc == 0 ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_FReadAt( hFile, nOffset, nLen ) -> cData | NIL */ +/* Positional read in one call; NIL on error. */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_FREADAT ) +{ + ADSHANDLE hFile = ( ADSHANDLE ) hb_parnint( 1 ); + UNSIGNED64 ullOff = ( UNSIGNED64 ) hb_parnint( 2 ); + UNSIGNED32 ulLen = ( UNSIGNED32 ) hb_parnl( 3 ); + UNSIGNED32 ulRead = 0; + char *pBuf; + + if( !hFile || hb_pcount() < 3 ) + { + hb_ret(); + return; + } + pBuf = ( char * ) hb_xgrab( ( HB_SIZE ) ulLen + 1 ); + if( oads_FReadAt( hFile, ullOff, pBuf, ulLen, &ulRead ) == 0 ) + { + memset( pBuf + ulRead, 0, 1 ); + hb_retclen( pBuf, ( HB_SIZE ) ulRead ); + } + else + { + hb_ret(); + } + hb_xfree( pBuf ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_FWriteAt( hFile, nOffset, cData ) -> nWritten (-1 on error) */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_FWRITEAT ) +{ + ADSHANDLE hFile = ( ADSHANDLE ) hb_parnint( 1 ); + UNSIGNED64 ullOff = ( UNSIGNED64 ) hb_parnint( 2 ); + const char *szData = hb_parc( 3 ); + UNSIGNED32 ulLen = ( UNSIGNED32 ) hb_parclen( 3 ); + UNSIGNED32 ulWritten = 0; + + if( !hFile || !szData ) + { + hb_retni( -1 ); + return; + } + if( oads_FWriteAt( hFile, ullOff, szData, ulLen, &ulWritten ) == 0 ) + hb_retnl( ( long ) ulWritten ); + else + hb_retni( -1 ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_FFlush( hFile ) -> lOk */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_FFLUSH ) +{ + ADSHANDLE hFile = ( ADSHANDLE ) hb_parnint( 1 ); + + hb_retl( hFile && oads_FFlush( hFile ) == 0 ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_FEof( hFile ) -> lEof | NIL */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_FEOF ) +{ + ADSHANDLE hFile = ( ADSHANDLE ) hb_parnint( 1 ); + UNSIGNED16 bEof = 0; + + if( hFile && oads_FEof( hFile, &bEof ) == 0 ) + hb_retl( bEof != 0 ); + else + hb_ret(); +} + +/* ------------------------------------------------------------------ */ +/* OADS_SetFileAttr( hConn, cName, nAttr ) -> lOk */ +/* OADS_SetFileAttr( cName, nAttr ) -> lOk (default conn) */ +/* nAttr is a platform bitset (read-only = 1). */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_SETFILEATTR ) +{ + ADSHANDLE hConn; + const char *szName; + UNSIGNED32 ulAttr; + + if( hb_pcount() >= 3 ) + { + hConn = ( ADSHANDLE ) hb_parnint( 1 ); + szName = hb_parc( 2 ); + ulAttr = ( UNSIGNED32 ) hb_parnint( 3 ); + } + else + { + hConn = oads_default_connection(); + szName = hb_parc( 1 ); + ulAttr = ( UNSIGNED32 ) hb_parnint( 2 ); + } + + hb_retl( szName != NULL && + oads_SetFileAttr( hConn, ( UNSIGNED8 * ) szName, + ulAttr ) == 0 ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_SetFileTime( hConn, cName, dDate, cTime ) -> lOk */ +/* OADS_SetFileTime( cName, dDate, cTime ) -> lOk (def conn) */ +/* cTime is "hh:mm:ss" (24-hour). */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_SETFILETIME ) +{ + ADSHANDLE hConn; + const char *szName; + const char *szTime; + HB_LONG lDate; + int iYear = 0, iMonth = 0, iDay = 0; + int iHour = 0, iMin = 0, iSec = 0; + + if( hb_pcount() >= 4 ) + { + hConn = ( ADSHANDLE ) hb_parnint( 1 ); + szName = hb_parc( 2 ); + lDate = hb_pardl( 3 ); + szTime = hb_parc( 4 ); + } + else + { + hConn = oads_default_connection(); + szName = hb_parc( 1 ); + lDate = hb_pardl( 2 ); + szTime = hb_parc( 3 ); + } + + if( szName && lDate > 0 ) + { + hb_dateDecode( lDate, &iYear, &iMonth, &iDay ); + if( szTime && ( sscanf( szTime, "%d:%d:%d", &iHour, &iMin, &iSec ) != 3 || + iHour < 0 || iHour > 23 || iMin < 0 || iMin > 59 || + iSec < 0 || iSec > 59 ) ) + { + hb_retl( HB_FALSE ); + return; + } + hb_retl( oads_SetFileTime( hConn, ( UNSIGNED8 * ) szName, + ( UNSIGNED16 ) iYear, + ( UNSIGNED8 ) iMonth, + ( UNSIGNED8 ) iDay, + ( UNSIGNED8 ) iHour, + ( UNSIGNED8 ) iMin, + ( UNSIGNED8 ) iSec ) == 0 ); + } + else + hb_retl( HB_FALSE ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_FTrunc( hFile, nSize ) -> lOk */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_FTRUNC ) +{ + ADSHANDLE hFile = ( ADSHANDLE ) hb_parnint( 1 ); + UNSIGNED64 ullSize = ( UNSIGNED64 ) hb_parnint( 2 ); + + hb_retl( hFile && hb_parnint( 2 ) >= 0 && + oads_FTrunc( hFile, ullSize ) == 0 ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_TempFile( hConn, cDir, cPrefix ) -> cName */ +/* OADS_TempFile( cDir, cPrefix ) -> cName (default conn) */ +/* Creates an empty file with a unique name under cDir and returns */ +/* its jail-relative path, or "" on failure. */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_TEMPFILE ) +{ + ADSHANDLE hConn; + const char *szDir; + const char *szPrefix; + char szName[ 4096 ]; + UNSIGNED16 usLen = sizeof( szName ); + + if( hb_pcount() >= 3 ) + { + hConn = ( ADSHANDLE ) hb_parnint( 1 ); + szDir = hb_parc( 2 ); + szPrefix = hb_parc( 3 ); + } + else + { + hConn = oads_default_connection(); + szDir = hb_parc( 1 ); + szPrefix = hb_parc( 2 ); + } + + szName[ 0 ] = '\0'; + if( szDir && + oads_TempFile( hConn, ( UNSIGNED8 * ) szDir, + ( UNSIGNED8 * ) szPrefix, + ( UNSIGNED8 * ) szName, &usLen ) == 0 ) + hb_retclen( szName, usLen ); + else + hb_retc( "" ); +} + +/* ------------------------------------------------------------------ */ +/* OADS_DirSpace( hConn, cDir, nType ) -> nBytes */ +/* OADS_DirSpace( cDir, nType ) -> nBytes (default conn) */ +/* nType: 0 = free available, 1 = total capacity, 2 = free. */ +/* ------------------------------------------------------------------ */ +HB_FUNC( OADS_DIRSPACE ) +{ + ADSHANDLE hConn; + const char *szDir; + UNSIGNED8 ucType; + UNSIGNED64 ullBytes = 0; + + if( hb_pcount() >= 3 ) + { + hConn = ( ADSHANDLE ) hb_parnint( 1 ); + szDir = hb_parc( 2 ); + ucType = ( UNSIGNED8 ) hb_parnint( 3 ); + } + else + { + hConn = oads_default_connection(); + szDir = hb_parc( 1 ); + ucType = ( UNSIGNED8 ) hb_parnint( 2 ); + } + + if( szDir ) + oads_DirSpace( hConn, ( UNSIGNED8 * ) szDir, ucType, &ullBytes ); + hb_retnint( ( HB_MAXINT ) ullBytes ); +} + /* ------------------------------------------------------------------ */ /* OADS_GetFileSize( hConn, cName ) -> nSize */ /* OADS_GetFileSize( cName ) -> nSize (default conn) */ diff --git a/include/openads/ace.h b/include/openads/ace.h index 206f9aff..f9062e6e 100644 --- a/include/openads/ace.h +++ b/include/openads/ace.h @@ -103,6 +103,40 @@ UNSIGNED32 ENTRYPOINT AdsCheckExistence(ADSHANDLE hConnect, UNSIGNED8* pucName, UNSIGNED32 ENTRYPOINT AdsDeleteFile (ADSHANDLE hConnect, UNSIGNED8* pucName); UNSIGNED32 ENTRYPOINT AdsRenameFile (ADSHANDLE hConnect, UNSIGNED8* pucOld, UNSIGNED8* pucNew); +UNSIGNED32 ENTRYPOINT AdsCopyFile (ADSHANDLE hConnect, UNSIGNED8* pucSrc, + UNSIGNED8* pucDst); +// Whole-file load (hb_vfLoad). *pulBufLen carries the buffer capacity in +// and the file size out; AE_INSUFFICIENT_BUFFER when capacity is short. +UNSIGNED32 ENTRYPOINT AdsLoadFile (ADSHANDLE hConnect, UNSIGNED8* pucName, + void* pBuf, UNSIGNED32* pulBufLen); +// Whole-file save (hb_vfSave): creates or truncates. +UNSIGNED32 ENTRYPOINT AdsSaveFile (ADSHANDLE hConnect, UNSIGNED8* pucName, + const void* pBuf, UNSIGNED32 ulLen); +UNSIGNED32 ENTRYPOINT AdsFReadAt (ADSHANDLE hFile, UNSIGNED64 ullOffset, + void* pBuf, UNSIGNED32 ulLen, + UNSIGNED32* pulRead); +UNSIGNED32 ENTRYPOINT AdsFWriteAt (ADSHANDLE hFile, UNSIGNED64 ullOffset, + const void* pBuf, UNSIGNED32 ulLen, + UNSIGNED32* pulWritten); +UNSIGNED32 ENTRYPOINT AdsFFlush (ADSHANDLE hFile); +UNSIGNED32 ENTRYPOINT AdsFEof (ADSHANDLE hFile, UNSIGNED16* pbEof); +// hb_vf group 3 - metadata batch. +UNSIGNED32 ENTRYPOINT AdsSetFileAttr (ADSHANDLE hConnect, UNSIGNED8* pucName, + UNSIGNED32 ulAttr); +UNSIGNED32 ENTRYPOINT AdsSetFileTime (ADSHANDLE hConnect, UNSIGNED8* pucName, + UNSIGNED16 usYear, UNSIGNED8 ucMon, + UNSIGNED8 ucDay, UNSIGNED8 ucHour, + UNSIGNED8 ucMin, UNSIGNED8 ucSec); +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. +UNSIGNED32 ENTRYPOINT AdsDirSpace (ADSHANDLE hConnect, UNSIGNED8* pucDir, + UNSIGNED8 ucType, UNSIGNED64* pullBytes); UNSIGNED32 ENTRYPOINT AdsGetFileSize (ADSHANDLE hConnect, UNSIGNED8* pucName, UNSIGNED32* pulSize); UNSIGNED32 ENTRYPOINT AdsGetFileTime (ADSHANDLE hConnect, UNSIGNED8* pucName, @@ -183,6 +217,33 @@ UNSIGNED32 OADSAPI oads_CheckExistence (ADSHANDLE hConnect, UNSIGNED8* pucName, UNSIGNED32 OADSAPI oads_DeleteFile (ADSHANDLE hConnect, UNSIGNED8* pucName); UNSIGNED32 OADSAPI oads_RenameFile (ADSHANDLE hConnect, UNSIGNED8* pucOld, UNSIGNED8* pucNew); +UNSIGNED32 OADSAPI oads_CopyFile (ADSHANDLE hConnect, UNSIGNED8* pucSrc, + UNSIGNED8* pucDst); +UNSIGNED32 OADSAPI oads_LoadFile (ADSHANDLE hConnect, UNSIGNED8* pucName, + void* pBuf, UNSIGNED32* pulBufLen); +UNSIGNED32 OADSAPI oads_SaveFile (ADSHANDLE hConnect, UNSIGNED8* pucName, + const void* pBuf, UNSIGNED32 ulLen); +UNSIGNED32 OADSAPI oads_FReadAt (ADSHANDLE hFile, UNSIGNED64 ullOffset, + void* pBuf, UNSIGNED32 ulLen, + UNSIGNED32* pulRead); +UNSIGNED32 OADSAPI oads_FWriteAt (ADSHANDLE hFile, UNSIGNED64 ullOffset, + const void* pBuf, UNSIGNED32 ulLen, + UNSIGNED32* pulWritten); +UNSIGNED32 OADSAPI oads_FFlush (ADSHANDLE hFile); +UNSIGNED32 OADSAPI oads_FEof (ADSHANDLE hFile, UNSIGNED16* pbEof); +UNSIGNED32 OADSAPI oads_SetFileAttr (ADSHANDLE hConnect, UNSIGNED8* pucName, + UNSIGNED32 ulAttr); +UNSIGNED32 OADSAPI oads_SetFileTime (ADSHANDLE hConnect, UNSIGNED8* pucName, + UNSIGNED16 usYear, UNSIGNED8 ucMon, + UNSIGNED8 ucDay, UNSIGNED8 ucHour, + UNSIGNED8 ucMin, UNSIGNED8 ucSec); +UNSIGNED32 OADSAPI oads_FTrunc (ADSHANDLE hFile, + UNSIGNED64 ullSize); +UNSIGNED32 OADSAPI oads_TempFile (ADSHANDLE hConnect, UNSIGNED8* pucDir, + UNSIGNED8* pucPrefix, UNSIGNED8* pucName, + UNSIGNED16* pusNameLen); +UNSIGNED32 OADSAPI oads_DirSpace (ADSHANDLE hConnect, UNSIGNED8* pucDir, + UNSIGNED8 ucType, UNSIGNED64* pullBytes); UNSIGNED32 OADSAPI oads_GetFileSize (ADSHANDLE hConnect, UNSIGNED8* pucName, UNSIGNED32* pulSize); UNSIGNED32 OADSAPI oads_GetFileTime (ADSHANDLE hConnect, UNSIGNED8* pucName, diff --git a/src/abi/ace_exports.cpp b/src/abi/ace_exports.cpp index 0abe3b9f..7ad9dae7 100644 --- a/src/abi/ace_exports.cpp +++ b/src/abi/ace_exports.cpp @@ -11305,6 +11305,345 @@ UNSIGNED32 ENTRYPOINT AdsRenameFile(ADSHANDLE hConn, UNSIGNED8* pucOld, return ok(); } +UNSIGNED32 ENTRYPOINT AdsCopyFile(ADSHANDLE hConn, UNSIGNED8* pucSrc, + UNSIGNED8* pucDst) { + arc2_trace("AdsCopyFile"); + if (!pucSrc || !pucDst) + return fail(openads::AE_INTERNAL_ERROR, "null name"); + auto s = openads::abi::to_internal(pucSrc, 0); + auto d = openads::abi::to_internal(pucDst, 0); + auto ctx = resolve_fs_conn(hConn); + if (ctx.remote) { + remote_flush_pools(ctx.remote); + remote_invalidate_index_parks(ctx.remote); + auto r = ctx.remote->file_copy(s, d); + if (!r) return fail(r.error()); + return ok(); + } + if (!ctx.local) return fail(openads::AE_INVALID_CONNECTION_HANDLE, ""); + auto a = jail_local(ctx, s); + auto b = jail_local(ctx, d); + if (!a || !b) return fail(openads::AE_ACCESS_DENIED, "path"); + auto r = openads::engine::fs_copy(*a, *b); + if (!r) return fail(r.error()); + return ok(); +} + +UNSIGNED32 ENTRYPOINT AdsLoadFile(ADSHANDLE hConn, UNSIGNED8* pucName, + void* pBuf, UNSIGNED32* pulBufLen) { + arc2_trace("AdsLoadFile"); + if (!pucName || !pulBufLen) + return fail(openads::AE_INTERNAL_ERROR, "null arg"); + auto name = openads::abi::to_internal(pucName, 0); + auto ctx = resolve_fs_conn(hConn); + std::vector bytes; + if (ctx.remote) { + auto r = ctx.remote->file_load(name); + if (!r) return fail(r.error()); + bytes = std::move(r.value()); + } else { + if (!ctx.local) return fail(openads::AE_INVALID_CONNECTION_HANDLE, ""); + auto abs = jail_local(ctx, name); + if (!abs) return fail(openads::AE_ACCESS_DENIED, "path"); + auto r = openads::engine::fs_load(*abs); + if (!r) return fail(r.error()); + bytes = std::move(r.value()); + } + if (bytes.size() > *pulBufLen) { + *pulBufLen = static_cast(bytes.size()); + return fail(openads::AE_INSUFFICIENT_BUFFER, "buffer too small"); + } + if (pBuf && !bytes.empty()) + std::memcpy(pBuf, bytes.data(), bytes.size()); + *pulBufLen = static_cast(bytes.size()); + return ok(); +} + +UNSIGNED32 ENTRYPOINT AdsSaveFile(ADSHANDLE hConn, UNSIGNED8* pucName, + const void* pBuf, UNSIGNED32 ulLen) { + arc2_trace("AdsSaveFile"); + if (!pucName || (!pBuf && ulLen)) + return fail(openads::AE_INTERNAL_ERROR, "null arg"); + auto name = openads::abi::to_internal(pucName, 0); + auto ctx = resolve_fs_conn(hConn); + if (ctx.remote) { + remote_flush_pools(ctx.remote); + remote_invalidate_index_parks(ctx.remote); + auto r = ctx.remote->file_save( + name, static_cast(pBuf), ulLen); + if (!r) return fail(r.error()); + return ok(); + } + if (!ctx.local) return fail(openads::AE_INVALID_CONNECTION_HANDLE, ""); + auto abs = jail_local(ctx, name); + if (!abs) return fail(openads::AE_ACCESS_DENIED, "path"); + auto r = openads::engine::fs_save( + *abs, static_cast(pBuf), ulLen); + if (!r) return fail(r.error()); + return ok(); +} + +UNSIGNED32 ENTRYPOINT AdsFReadAt(ADSHANDLE hFile, UNSIGNED64 ullOffset, + void* pBuf, UNSIGNED32 ulLen, + UNSIGNED32* pulRead) { + arc2_trace("AdsFReadAt"); + if (!pulRead) return fail(openads::AE_INTERNAL_ERROR, "null out"); + *pulRead = 0; + auto& s = state(); + std::unique_lock lk(s.mu); + if (auto it = remote_files_map().find(hFile); it != remote_files_map().end()) { + // Retain lifetime under the registry lock, then release it BEFORE + // waiting on this file or its connection. Never invert that order. + auto rf = it->second; + lk.unlock(); + std::lock_guard file_lk(rf->mu); + if (rf->closed) return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); + auto r = rf->conn->fread_at(rf->file_id, ullOffset, ulLen); + if (!r) return fail(r.error()); + const auto& bytes = r.value(); + if (pBuf && !bytes.empty()) + std::memcpy(pBuf, bytes.data(), bytes.size()); + *pulRead = static_cast(bytes.size()); + return ok(); + } + if (auto* lf = s.registry.lookup( + hFile, openads::session::HandleKind::LocalFile)) { + if (!pBuf && ulLen) return fail(openads::AE_INTERNAL_ERROR, "null buf"); + lf->stream.clear(); + lf->stream.seekg(static_cast(ullOffset), std::ios::beg); + lf->stream.read(static_cast(pBuf), + static_cast(ulLen)); + *pulRead = static_cast(lf->stream.gcount()); + return ok(); + } + return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); +} + +UNSIGNED32 ENTRYPOINT AdsFWriteAt(ADSHANDLE hFile, UNSIGNED64 ullOffset, + const void* pBuf, UNSIGNED32 ulLen, + UNSIGNED32* pulWritten) { + arc2_trace("AdsFWriteAt"); + if (!pulWritten) return fail(openads::AE_INTERNAL_ERROR, "null out"); + *pulWritten = 0; + auto& s = state(); + std::unique_lock lk(s.mu); + if (auto it = remote_files_map().find(hFile); it != remote_files_map().end()) { + // Retain lifetime under the registry lock, then release it BEFORE + // waiting on this file or its connection. Never invert that order. + auto rf = it->second; + lk.unlock(); + std::lock_guard file_lk(rf->mu); + if (rf->closed) return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); + auto r = rf->conn->fwrite_at( + rf->file_id, ullOffset, + static_cast(pBuf), ulLen); + if (!r) return fail(r.error()); + *pulWritten = r.value(); + return ok(); + } + if (auto* lf = s.registry.lookup( + hFile, openads::session::HandleKind::LocalFile)) { + if (!pBuf && ulLen) return fail(openads::AE_INTERNAL_ERROR, "null buf"); + lf->stream.clear(); + lf->stream.seekg(static_cast(ullOffset), std::ios::beg); + lf->stream.write(static_cast(pBuf), + static_cast(ulLen)); + lf->stream.flush(); + if (!lf->stream) + return fail(openads::AE_INTERNAL_ERROR, "write failed"); + *pulWritten = ulLen; + return ok(); + } + return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); +} + +UNSIGNED32 ENTRYPOINT AdsFFlush(ADSHANDLE hFile) { + arc2_trace("AdsFFlush"); + auto& s = state(); + std::unique_lock lk(s.mu); + if (auto it = remote_files_map().find(hFile); it != remote_files_map().end()) { + // Retain lifetime under the registry lock, then release it BEFORE + // waiting on this file or its connection. Never invert that order. + auto rf = it->second; + lk.unlock(); + std::lock_guard file_lk(rf->mu); + if (rf->closed) return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); + auto r = rf->conn->fflush(rf->file_id); + if (!r) return fail(r.error()); + return ok(); + } + if (auto* lf = s.registry.lookup( + hFile, openads::session::HandleKind::LocalFile)) { + lf->stream.flush(); + if (!lf->stream) + return fail(openads::AE_INTERNAL_ERROR, "flush failed"); + return ok(); + } + return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); +} + +UNSIGNED32 ENTRYPOINT AdsFEof(ADSHANDLE hFile, UNSIGNED16* pbEof) { + arc2_trace("AdsFEof"); + if (!pbEof) return fail(openads::AE_INTERNAL_ERROR, "null out"); + *pbEof = 0; + auto& s = state(); + std::unique_lock lk(s.mu); + if (auto it = remote_files_map().find(hFile); it != remote_files_map().end()) { + // Retain lifetime under the registry lock, then release it BEFORE + // waiting on this file or its connection. Never invert that order. + auto rf = it->second; + lk.unlock(); + std::lock_guard file_lk(rf->mu); + if (rf->closed) return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); + auto r = rf->conn->feof(rf->file_id); + if (!r) return fail(r.error()); + *pbEof = r.value() ? 1 : 0; + return ok(); + } + if (auto* lf = s.registry.lookup( + hFile, openads::session::HandleKind::LocalFile)) { + lf->stream.clear(); + auto cur = lf->stream.tellg(); + lf->stream.seekg(0, std::ios::end); + auto end = lf->stream.tellg(); + lf->stream.clear(); + if (cur >= 0) lf->stream.seekg(cur); + *pbEof = (cur < 0 || (end >= 0 && cur >= end)) ? 1 : 0; + return ok(); + } + return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); +} + +UNSIGNED32 ENTRYPOINT AdsSetFileAttr(ADSHANDLE hConn, UNSIGNED8* pucName, + UNSIGNED32 ulAttr) { + arc2_trace("AdsSetFileAttr"); + if (!pucName) return fail(openads::AE_INTERNAL_ERROR, "null name"); + auto name = openads::abi::to_internal(pucName, 0); + auto ctx = resolve_fs_conn(hConn); + if (ctx.remote) { + remote_flush_pools(ctx.remote); + remote_invalidate_index_parks(ctx.remote); + auto r = ctx.remote->file_set_attr(name, ulAttr); + if (!r) return fail(r.error()); + return ok(); + } + if (!ctx.local) return fail(openads::AE_INVALID_CONNECTION_HANDLE, ""); + auto abs = jail_local(ctx, name); + if (!abs) return fail(openads::AE_ACCESS_DENIED, "path"); + auto r = openads::engine::fs_set_attr(*abs, ulAttr); + if (!r) return fail(r.error()); + return ok(); +} + +UNSIGNED32 ENTRYPOINT AdsSetFileTime(ADSHANDLE hConn, UNSIGNED8* pucName, + UNSIGNED16 usYear, UNSIGNED8 ucMon, + UNSIGNED8 ucDay, UNSIGNED8 ucHour, + UNSIGNED8 ucMin, UNSIGNED8 ucSec) { + arc2_trace("AdsSetFileTime"); + if (!pucName) return fail(openads::AE_INTERNAL_ERROR, "null name"); + auto name = openads::abi::to_internal(pucName, 0); + auto ctx = resolve_fs_conn(hConn); + if (ctx.remote) { + remote_flush_pools(ctx.remote); + remote_invalidate_index_parks(ctx.remote); + auto r = ctx.remote->file_set_time(name, usYear, ucMon, ucDay, + ucHour, ucMin, ucSec); + if (!r) return fail(r.error()); + return ok(); + } + if (!ctx.local) return fail(openads::AE_INVALID_CONNECTION_HANDLE, ""); + auto abs = jail_local(ctx, name); + if (!abs) return fail(openads::AE_ACCESS_DENIED, "path"); + auto r = openads::engine::fs_set_time(*abs, usYear, ucMon, ucDay, + ucHour, ucMin, ucSec); + if (!r) return fail(r.error()); + return ok(); +} + +UNSIGNED32 ENTRYPOINT AdsFTrunc(ADSHANDLE hFile, UNSIGNED64 ullSize) { + arc2_trace("AdsFTrunc"); + auto& s = state(); + std::unique_lock lk(s.mu); + if (auto it = remote_files_map().find(hFile); it != remote_files_map().end()) { + // Retain lifetime under the registry lock, then release it BEFORE + // waiting on this file or its connection. Never invert that order. + auto rf = it->second; + lk.unlock(); + std::lock_guard file_lk(rf->mu); + if (rf->closed) return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); + auto r = rf->conn->ftruncate(rf->file_id, ullSize); + if (!r) return fail(r.error()); + return ok(); + } + if (auto* lf = s.registry.lookup( + hFile, openads::session::HandleKind::LocalFile)) { + auto r = openads::engine::fs_truncate(*lf, ullSize); + if (!r) return fail(r.error()); + lf->stream.clear(); + return ok(); + } + return fail(openads::AE_INTERNAL_ERROR, "bad file handle"); +} + +UNSIGNED32 ENTRYPOINT AdsTempFile(ADSHANDLE hConn, UNSIGNED8* pucDir, + UNSIGNED8* pucPrefix, UNSIGNED8* pucName, + UNSIGNED16* pusNameLen) { + arc2_trace("AdsTempFile"); + if (!pucDir || !pucName || !pusNameLen) + return fail(openads::AE_INTERNAL_ERROR, "null arg"); + auto dir = openads::abi::to_internal(pucDir, 0); + if (dir.empty()) dir = "."; + std::string prefix; + if (pucPrefix) prefix = openads::abi::to_internal(pucPrefix, 0); + auto ctx = resolve_fs_conn(hConn); + std::string name; + if (ctx.remote) { + auto r = ctx.remote->file_temp(dir, prefix); + if (!r) return fail(r.error()); + name = std::move(r.value()); + } else { + if (!ctx.local) return fail(openads::AE_INVALID_CONNECTION_HANDLE, ""); + auto abs = jail_local(ctx, dir); + if (!abs) return fail(openads::AE_ACCESS_DENIED, "path"); + auto r = openads::engine::fs_tempfile(*abs, prefix); + if (!r) return fail(r.error()); + name = (std::filesystem::path( + openads::platform::fold_absolute_to_relative(dir)) / + std::filesystem::path(r.value()).filename()).generic_string(); + } + if (name.size() > *pusNameLen) { + *pusNameLen = static_cast(name.size()); + return fail(openads::AE_INSUFFICIENT_BUFFER, "buffer too small"); + } + std::memcpy(pucName, name.data(), name.size()); + *pusNameLen = static_cast(name.size()); + return ok(); +} + +UNSIGNED32 ENTRYPOINT AdsDirSpace(ADSHANDLE hConn, UNSIGNED8* pucDir, + UNSIGNED8 ucType, UNSIGNED64* pullBytes) { + arc2_trace("AdsDirSpace"); + if (!pucDir || !pullBytes) + return fail(openads::AE_INTERNAL_ERROR, "null arg"); + *pullBytes = 0; + auto dir = openads::abi::to_internal(pucDir, 0); + if (dir.empty()) dir = "."; + auto ctx = resolve_fs_conn(hConn); + if (ctx.remote) { + auto r = ctx.remote->dir_space(dir, ucType); + if (!r) return fail(r.error()); + *pullBytes = r.value(); + return ok(); + } + if (!ctx.local) return fail(openads::AE_INVALID_CONNECTION_HANDLE, ""); + auto abs = jail_local(ctx, dir); + if (!abs) return fail(openads::AE_ACCESS_DENIED, "path"); + auto r = openads::engine::fs_dir_space(*abs, ucType); + if (!r) return fail(r.error()); + *pullBytes = r.value(); + return ok(); +} + UNSIGNED32 ENTRYPOINT AdsGetFileSize(ADSHANDLE hConn, UNSIGNED8* pucName, UNSIGNED32* pulSize) { arc2_trace("AdsGetFileSize"); diff --git a/src/abi/adsfunc.c b/src/abi/adsfunc.c index 6909be9d..ca2f28b8 100644 --- a/src/abi/adsfunc.c +++ b/src/abi/adsfunc.c @@ -77,6 +77,101 @@ UNSIGNED32 OADSAPI oads_RenameFile( ADSHANDLE hConnect, return AdsRenameFile( hConnect, pucOld, pucNew ); } +UNSIGNED32 OADSAPI oads_CopyFile( ADSHANDLE hConnect, + UNSIGNED8 *pucSrc, + UNSIGNED8 *pucDst ) +{ + return AdsCopyFile( hConnect, pucSrc, pucDst ); +} + +UNSIGNED32 OADSAPI oads_LoadFile( ADSHANDLE hConnect, + UNSIGNED8 *pucName, + void *pBuf, + UNSIGNED32 *pulBufLen ) +{ + return AdsLoadFile( hConnect, pucName, pBuf, pulBufLen ); +} + +UNSIGNED32 OADSAPI oads_SaveFile( ADSHANDLE hConnect, + UNSIGNED8 *pucName, + const void *pBuf, + UNSIGNED32 ulLen ) +{ + return AdsSaveFile( hConnect, pucName, pBuf, ulLen ); +} + +UNSIGNED32 OADSAPI oads_FReadAt( ADSHANDLE hFile, + UNSIGNED64 ullOffset, + void *pBuf, + UNSIGNED32 ulLen, + UNSIGNED32 *pulRead ) +{ + return AdsFReadAt( hFile, ullOffset, pBuf, ulLen, pulRead ); +} + +UNSIGNED32 OADSAPI oads_FWriteAt( ADSHANDLE hFile, + UNSIGNED64 ullOffset, + const void *pBuf, + UNSIGNED32 ulLen, + UNSIGNED32 *pulWritten ) +{ + return AdsFWriteAt( hFile, ullOffset, pBuf, ulLen, pulWritten ); +} + +UNSIGNED32 OADSAPI oads_FFlush( ADSHANDLE hFile ) +{ + return AdsFFlush( hFile ); +} + +UNSIGNED32 OADSAPI oads_FEof( ADSHANDLE hFile, + UNSIGNED16 *pbEof ) +{ + return AdsFEof( hFile, pbEof ); +} + +UNSIGNED32 OADSAPI oads_SetFileAttr( ADSHANDLE hConnect, + UNSIGNED8 *pucName, + UNSIGNED32 ulAttr ) +{ + return AdsSetFileAttr( hConnect, pucName, ulAttr ); +} + +UNSIGNED32 OADSAPI oads_SetFileTime( ADSHANDLE hConnect, + UNSIGNED8 *pucName, + UNSIGNED16 usYear, + UNSIGNED8 ucMon, + UNSIGNED8 ucDay, + UNSIGNED8 ucHour, + UNSIGNED8 ucMin, + UNSIGNED8 ucSec ) +{ + return AdsSetFileTime( hConnect, pucName, usYear, ucMon, ucDay, + ucHour, ucMin, ucSec ); +} + +UNSIGNED32 OADSAPI oads_FTrunc( ADSHANDLE hFile, + UNSIGNED64 ullSize ) +{ + return AdsFTrunc( hFile, ullSize ); +} + +UNSIGNED32 OADSAPI oads_TempFile( ADSHANDLE hConnect, + UNSIGNED8 *pucDir, + UNSIGNED8 *pucPrefix, + UNSIGNED8 *pucName, + UNSIGNED16 *pusNameLen ) +{ + return AdsTempFile( hConnect, pucDir, pucPrefix, pucName, pusNameLen ); +} + +UNSIGNED32 OADSAPI oads_DirSpace( ADSHANDLE hConnect, + UNSIGNED8 *pucDir, + UNSIGNED8 ucType, + UNSIGNED64 *pullBytes ) +{ + return AdsDirSpace( hConnect, pucDir, ucType, pullBytes ); +} + UNSIGNED32 OADSAPI oads_GetFileSize( ADSHANDLE hConnect, UNSIGNED8 *pucName, UNSIGNED32 *pulSize ) diff --git a/src/engine/server_fs.cpp b/src/engine/server_fs.cpp index 999e6353..89287c0e 100644 --- a/src/engine/server_fs.cpp +++ b/src/engine/server_fs.cpp @@ -4,8 +4,18 @@ #include #include +#include #include +#include +#include #include +#include +#if defined(_WIN32) +#include +#include +#else +#include +#endif namespace fs = std::filesystem; @@ -142,6 +152,211 @@ util::Result fs_rename(const std::string& abs_old, return {}; } +util::Result fs_copy(const std::string& abs_src, + const std::string& abs_dst) { + std::error_code ec; + if (!fs::is_regular_file(abs_src, ec) || ec) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "file not found", abs_src); + bool same = fs::equivalent(abs_src, abs_dst, ec); + if (!ec && same) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "source and target are the same file", abs_src); + // Plain stream copy: MinGW's fs::copy_file is unreliable when the + // target already exists, and truncate-on-open is the wanted overwrite. + std::ifstream in(abs_src, std::ios::binary); + if (!in) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "open source failed", abs_src); + std::ofstream out(abs_dst, std::ios::binary | std::ios::trunc); + if (!out) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "open target failed", abs_dst); + out << in.rdbuf(); + out.flush(); + if (!out || in.bad()) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "copy failed", abs_src); + return {}; +} + +util::Result> fs_load(const std::string& abs_path) { + std::error_code ec; + if (!fs::is_regular_file(fs::path(abs_path), ec) || ec) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "not a regular file", abs_path); + auto sz = fs::file_size(fs::path(abs_path), ec); + if (ec) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "stat failed", abs_path); + if (sz > kMaxFsWholeFile) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "file too large for one call", abs_path); + std::ifstream in(abs_path, std::ios::binary); + if (!in) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "open failed", abs_path); + std::vector out(static_cast(sz)); + if (sz) + in.read(reinterpret_cast(out.data()), + static_cast(sz)); + if (in.bad() || static_cast(in.gcount()) != sz) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "read failed", abs_path); + return out; +} + +util::Result fs_save(const std::string& abs_path, + const std::uint8_t* data, std::uint64_t size) { + if (size > kMaxFsWholeFile) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "too large for one call", abs_path); + std::ofstream out(abs_path, std::ios::binary | std::ios::trunc); + if (!out) + return io_err(static_cast(openads::AE_ACCESS_DENIED), + "open failed", abs_path); + if (size) + out.write(reinterpret_cast(data), + static_cast(size)); + out.flush(); + if (!out) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "write failed", abs_path); + return {}; +} + +util::Result fs_set_attr(const std::string& abs_path, + std::uint32_t attr) { + std::error_code ec; + if (!fs::exists(fs::path(abs_path), ec) || ec) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "not found", abs_path); + using fs::perms; + if (attr & 0x01u) { + fs::permissions(abs_path, + perms::owner_write | perms::group_write | + perms::others_write, + fs::perm_options::remove, ec); + } else { + fs::permissions(abs_path, + perms::owner_write | perms::group_write | + perms::others_write, + fs::perm_options::add, ec); + } + if (ec) + return io_err(static_cast(openads::AE_ACCESS_DENIED), + "chmod failed", abs_path); + return {}; +} + +util::Result fs_set_time(const std::string& abs_path, + std::uint16_t year, std::uint8_t mon, + std::uint8_t day, std::uint8_t hh, + std::uint8_t mm, std::uint8_t ss) { + std::error_code ec; + if (!fs::exists(fs::path(abs_path), ec) || ec) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "not found", abs_path); + std::tm tmv{}; + tmv.tm_year = static_cast(year) - 1900; + tmv.tm_mon = static_cast(mon) - 1; + tmv.tm_mday = static_cast(day); + tmv.tm_hour = static_cast(hh); + tmv.tm_min = static_cast(mm); + tmv.tm_sec = static_cast(ss); + tmv.tm_isdst = -1; + std::time_t tt = std::mktime(&tmv); + if (tt == static_cast(-1)) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "bad date", abs_path); + auto sys = std::chrono::system_clock::from_time_t(tt); + // C++17 file_time_type clock is not system_clock on every library: + // shift from "now" instead of casting epochs. + auto ftime = fs::file_time_type::clock::now() + + (sys - std::chrono::system_clock::now()); + fs::last_write_time(abs_path, ftime, ec); + if (ec) + return io_err(static_cast(openads::AE_ACCESS_DENIED), + "utime failed", abs_path); + return {}; +} + +util::Result fs_truncate(const std::string& abs_path, + std::uint64_t size) { + std::error_code ec; + fs::resize_file(fs::path(abs_path), size, ec); + if (ec) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "truncate failed", abs_path); + return {}; +} + +util::Result fs_truncate(FsFile& file, std::uint64_t size) { + if (!file.writable) + return io_err(static_cast(openads::AE_ACCESS_DENIED), + "read-only handle", file.path); + file.stream.clear(); + auto pos = file.stream.tellg(); + file.stream.flush(); + if (!file.stream) + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "flush failed", file.path); + auto r = fs_truncate(file.path, size); + if (!r) return r; + file.stream.clear(); + if (pos >= 0) { + file.stream.seekg(pos); + file.stream.seekp(pos); + } + return {}; +} + +util::Result fs_tempfile(const std::string& abs_dir, + const std::string& prefix) { + std::error_code ec; + if (!fs::is_directory(fs::path(abs_dir), ec) || ec) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "not a directory", abs_dir); + std::string pfx = prefix.empty() ? "oa" : prefix; + // Keep the prefix path-safe: one dot and no separators. + for (auto& c : pfx) + if (c == '/' || c == '\\' || c == ':') c = '_'; + auto stamp = std::chrono::steady_clock::now().time_since_epoch().count(); + for (int i = 0; i < 100; ++i) { + std::ostringstream name; + name << pfx << std::hex << (stamp + i) << ".tmp"; + fs::path cand = fs::path(abs_dir) / name.str(); + if (fs::exists(cand, ec)) continue; + // Exclusive creation: never truncate another caller's file. +#if defined(_WIN32) + int fd = ::_open(cand.string().c_str(), + _O_CREAT | _O_EXCL | _O_WRONLY | _O_BINARY, + _S_IREAD | _S_IWRITE); + if (fd < 0) continue; + ::_close(fd); +#else + int fd = ::open(cand.string().c_str(), O_CREAT | O_EXCL | O_WRONLY, 0600); + if (fd < 0) continue; + ::close(fd); +#endif + return cand.string(); + } + return io_err(static_cast(openads::AE_INTERNAL_ERROR), + "no free temp name", abs_dir); +} + +util::Result fs_dir_space(const std::string& abs_dir, + std::uint8_t type) { + std::error_code ec; + auto sp = fs::space(fs::path(abs_dir), ec); + if (ec) + return io_err(static_cast(openads::AE_NO_FILE_FOUND), + "space query failed", abs_dir); + if (type == 1) return static_cast(sp.capacity); + if (type == 2) return static_cast(sp.free); + return static_cast(sp.available); +} + util::Result fs_size(const std::string& abs_path) { std::error_code ec; if (!fs::is_regular_file(abs_path, ec) || ec) @@ -252,6 +467,7 @@ util::Result> fs_open(const std::string& abs_path, bool create) { auto f = std::make_unique(); f->path = abs_path; + f->writable = create || (mode != ADS_FO_READ && mode != 3); std::ios::openmode om = std::ios::binary; if (create) { om |= std::ios::in | std::ios::out | std::ios::trunc; diff --git a/src/engine/server_fs.h b/src/engine/server_fs.h index 559e069e..66b24526 100644 --- a/src/engine/server_fs.h +++ b/src/engine/server_fs.h @@ -12,6 +12,8 @@ namespace openads::engine { inline constexpr std::uint32_t kMaxSessionFiles = 32; inline constexpr std::uint32_t kMaxFsIoChunk = 1024u * 1024u; +// Whole-file load/save cap (FileLoad / FileSave / fs_load / fs_save). +inline constexpr std::uint64_t kMaxFsWholeFile = 64ull * 1024ull * 1024ull; // FOpen mode bits (Harbour FO_* -ish). inline constexpr std::uint16_t ADS_FO_READ = 0x0000; @@ -30,6 +32,34 @@ util::Result fs_exists(const std::string& abs_path); util::Result fs_erase(const std::string& abs_path); util::Result fs_rename(const std::string& abs_old, const std::string& abs_new); +util::Result fs_copy(const std::string& abs_src, + const std::string& abs_dst); +// Whole-file read (hb_vfLoad). Fails with AE_INTERNAL_ERROR when the file +// exceeds kMaxFsWholeFile. +util::Result> fs_load(const std::string& abs_path); +// Whole-file write (hb_vfSave): creates or truncates. +util::Result fs_save(const std::string& abs_path, + const std::uint8_t* data, + std::uint64_t size); +// attr bit 0x01 = readonly (add or remove write permission); other bits +// are ignored. Directories keep their execute bits. +util::Result fs_set_attr(const std::string& abs_path, + std::uint32_t attr); +// Set modification time from local-time components. +util::Result fs_set_time(const std::string& abs_path, + std::uint16_t year, + std::uint8_t mon, std::uint8_t day, + std::uint8_t hh, std::uint8_t mm, + std::uint8_t ss); +util::Result fs_truncate(const std::string& abs_path, + std::uint64_t size); +// Create an empty uniquely named file under abs_dir whose name starts +// with prefix; returns the file name (not the full path). +util::Result fs_tempfile(const std::string& abs_dir, + const std::string& prefix); +// type 0 = free available, 1 = total capacity, 2 = free. +util::Result fs_dir_space(const std::string& abs_dir, + std::uint8_t type); util::Result fs_size(const std::string& abs_path); util::Result fs_stat_entry(const std::string& abs_path); // mask may be "subdir/*.dbf" or "*.txt"; base_dir is the jail-resolved @@ -43,7 +73,9 @@ util::Result fs_dir_remove(const std::string& abs_path); struct FsFile { std::fstream stream; std::string path; + bool writable = false; }; +util::Result fs_truncate(FsFile& file, std::uint64_t size); // create=true → create/truncate (FCreate). mode uses ADS_FO_*. util::Result> fs_open(const std::string& abs_path, diff --git a/src/network/client.cpp b/src/network/client.cpp index d28d7e01..8e6e47db 100644 --- a/src/network/client.cpp +++ b/src/network/client.cpp @@ -2920,6 +2920,11 @@ std::uint64_t read_u64_le(const std::uint8_t* p) { return v; } +void write_u64_le(std::uint64_t v, std::vector& out) { + for (int i = 0; i < 8; ++i) + out.push_back(static_cast((v >> (8 * i)) & 0xFF)); +} + } // namespace // File state can change on another connection or directly on the host. @@ -2963,6 +2968,200 @@ RemoteConnection::file_rename(const std::string& old_p, return {}; } +util::Result +RemoteConnection::file_copy(const std::string& src_p, + const std::string& dst_p) { + Frame req; + req.opcode = Opcode::FileCopy; + push_lp_str(req.payload, src_p); + push_lp_str(req.payload, dst_p); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileCopyAck) + return fs_wire_err(rep.value(), "FileCopy"); + return {}; +} + +util::Result> +RemoteConnection::file_load(const std::string& path) { + Frame req; + req.opcode = Opcode::FileLoad; + push_lp_str(req.payload, path); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileLoadAck || + rep.value().payload.size() < 4) + return fs_wire_err(rep.value(), "FileLoad"); + std::uint32_t n = read_u32_le(rep.value().payload.data()); + if (4ull + n > rep.value().payload.size()) + return util::Error{5000, 0, "FileLoad: short data", ""}; + return std::vector( + rep.value().payload.begin() + 4, + rep.value().payload.begin() + 4 + static_cast(n)); +} + +util::Result +RemoteConnection::file_save(const std::string& path, + const std::uint8_t* data, std::uint64_t size) { + Frame req; + req.opcode = Opcode::FileSave; + push_lp_str(req.payload, path); + write_u32_le(static_cast(size), req.payload); + if (data && size) + req.payload.insert(req.payload.end(), data, + data + static_cast(size)); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileSaveAck) + return fs_wire_err(rep.value(), "FileSave"); + return {}; +} + +util::Result> +RemoteConnection::fread_at(std::uint32_t file_id, std::uint64_t offset, + std::uint32_t nbytes) { + Frame req; + req.opcode = Opcode::FileReadAt; + write_u32_le(file_id, req.payload); + write_u64_le(offset, req.payload); + write_u32_le(nbytes, req.payload); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileReadAtAck || + rep.value().payload.size() < 4) + return fs_wire_err(rep.value(), "FileReadAt"); + std::uint32_t n = read_u32_le(rep.value().payload.data()); + if (4u + n > rep.value().payload.size()) + return util::Error{5000, 0, "FileReadAt: short data", ""}; + return std::vector( + rep.value().payload.begin() + 4, + rep.value().payload.begin() + 4 + static_cast(n)); +} + +util::Result +RemoteConnection::fwrite_at(std::uint32_t file_id, std::uint64_t offset, + const std::uint8_t* data, std::uint32_t n) { + Frame req; + req.opcode = Opcode::FileWriteAt; + write_u32_le(file_id, req.payload); + write_u64_le(offset, req.payload); + write_u32_le(n, req.payload); + if (data && n) + req.payload.insert(req.payload.end(), data, data + n); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileWriteAtAck || + rep.value().payload.size() < 4) + return fs_wire_err(rep.value(), "FileWriteAt"); + return read_u32_le(rep.value().payload.data()); +} + +util::Result +RemoteConnection::fflush(std::uint32_t file_id) { + Frame req; + req.opcode = Opcode::FileFlush; + write_u32_le(file_id, req.payload); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileFlushAck) + return fs_wire_err(rep.value(), "FileFlush"); + return {}; +} + +util::Result +RemoteConnection::feof(std::uint32_t file_id) { + Frame req; + req.opcode = Opcode::FileEof; + write_u32_le(file_id, req.payload); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileEofAck || + rep.value().payload.empty()) + return fs_wire_err(rep.value(), "FileEof"); + return rep.value().payload[0] != 0; +} + +util::Result +RemoteConnection::file_set_attr(const std::string& path, + std::uint32_t attr) { + Frame req; + req.opcode = Opcode::FileSetAttr; + push_lp_str(req.payload, path); + write_u32_le(attr, req.payload); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileSetAttrAck) + return fs_wire_err(rep.value(), "FileSetAttr"); + return {}; +} + +util::Result +RemoteConnection::file_set_time(const std::string& path, std::uint16_t year, + std::uint8_t mon, std::uint8_t day, + std::uint8_t hh, std::uint8_t mm, + std::uint8_t ss) { + Frame req; + req.opcode = Opcode::FileSetTime; + push_lp_str(req.payload, path); + req.payload.push_back(static_cast(year & 0xFF)); + req.payload.push_back(static_cast((year >> 8) & 0xFF)); + req.payload.push_back(mon); + req.payload.push_back(day); + req.payload.push_back(hh); + req.payload.push_back(mm); + req.payload.push_back(ss); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileSetTimeAck) + return fs_wire_err(rep.value(), "FileSetTime"); + return {}; +} + +util::Result +RemoteConnection::ftruncate(std::uint32_t file_id, std::uint64_t size) { + Frame req; + req.opcode = Opcode::FileTrunc; + write_u32_le(file_id, req.payload); + write_u64_le(size, req.payload); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileTruncAck) + return fs_wire_err(rep.value(), "FileTrunc"); + return {}; +} + +util::Result +RemoteConnection::file_temp(const std::string& dir, + const std::string& prefix) { + Frame req; + req.opcode = Opcode::FileTemp; + push_lp_str(req.payload, dir); + push_lp_str(req.payload, prefix); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::FileTempAck) + return fs_wire_err(rep.value(), "FileTemp"); + std::size_t pos = 0; + std::string name; + if (!read_lstr16(rep.value().payload, pos, name)) + return util::Error{5000, 0, "FileTemp: short ack", ""}; + return name; +} + +util::Result +RemoteConnection::dir_space(const std::string& dir, std::uint8_t type) { + Frame req; + req.opcode = Opcode::DirSpace; + push_lp_str(req.payload, dir); + req.payload.push_back(type); + auto rep = request(req); + if (!rep) return rep.error(); + if (rep.value().opcode != Opcode::DirSpaceAck || + rep.value().payload.size() < 8) + return fs_wire_err(rep.value(), "DirSpace"); + return read_u64_le(rep.value().payload.data()); +} + util::Result RemoteConnection::file_size(const std::string& path) { Frame req; diff --git a/src/network/client.h b/src/network/client.h index bbd1f46c..2601693f 100644 --- a/src/network/client.h +++ b/src/network/client.h @@ -422,6 +422,40 @@ class RemoteConnection { util::Result file_erase(const std::string& path); util::Result file_rename(const std::string& old_p, const std::string& new_p); + util::Result file_copy(const std::string& src_p, + const std::string& dst_p); + // hb_vf group 2 - wire-efficiency batch. + util::Result> + file_load(const std::string& path); + util::Result file_save(const std::string& path, + const std::uint8_t* data, + std::uint64_t size); + util::Result> + fread_at(std::uint32_t file_id, + std::uint64_t offset, + std::uint32_t nbytes); + util::Result fwrite_at(std::uint32_t file_id, + std::uint64_t offset, + const std::uint8_t* data, + std::uint32_t n); + util::Result fflush(std::uint32_t file_id); + util::Result feof(std::uint32_t file_id); + // hb_vf group 3 - metadata batch. + util::Result file_set_attr(const std::string& path, + std::uint32_t attr); + util::Result file_set_time(const std::string& path, + std::uint16_t year, + std::uint8_t mon, + std::uint8_t day, + std::uint8_t hh, + std::uint8_t mm, + std::uint8_t ss); + util::Result ftruncate(std::uint32_t file_id, + std::uint64_t size); + util::Result file_temp(const std::string& dir, + const std::string& prefix); + util::Result dir_space(const std::string& dir, + std::uint8_t type); util::Result file_size(const std::string& path); util::Result file_mtime( const std::string& path); diff --git a/src/network/session.cpp b/src/network/session.cpp index 352307c0..34d9c671 100644 --- a/src/network/session.cpp +++ b/src/network/session.cpp @@ -1702,10 +1702,17 @@ DispatchResult Session::dispatch(const Frame& f) { case Opcode::CreateIndex: case Opcode::Reindex: case Opcode::PackTable: case Opcode::ZapTable: case Opcode::FileExists: case Opcode::FileErase: case Opcode::FileRename: + case Opcode::FileCopy: + case Opcode::FileLoad: case Opcode::FileSave: + case Opcode::FileSetAttr: case Opcode::FileSetTime: + case Opcode::FileTemp: case Opcode::DirSpace: case Opcode::FileSize: case Opcode::FileMTime: case Opcode::Directory: case Opcode::DirExist: case Opcode::DirMake: case Opcode::DirRemove: case Opcode::FOpen: case Opcode::FCreate: case Opcode::FClose: case Opcode::FRead: case Opcode::FWrite: case Opcode::FSeek: + case Opcode::FileReadAt: case Opcode::FileWriteAt: + case Opcode::FileFlush: case Opcode::FileEof: + case Opcode::FileTrunc: case Opcode::ZipArchive: case Opcode::UnzipArchive: case Opcode::ZipList: if (!admin) return denied(); break; @@ -4229,6 +4236,13 @@ DispatchResult Session::dispatch(const Frame& f) { case Opcode::FileExists: case Opcode::FileErase: case Opcode::FileRename: + case Opcode::FileCopy: + case Opcode::FileLoad: + case Opcode::FileSave: + case Opcode::FileSetAttr: + case Opcode::FileSetTime: + case Opcode::FileTemp: + case Opcode::DirSpace: case Opcode::FileSize: case Opcode::FileMTime: case Opcode::Directory: @@ -4240,7 +4254,12 @@ DispatchResult Session::dispatch(const Frame& f) { case Opcode::FClose: case Opcode::FRead: case Opcode::FWrite: - case Opcode::FSeek: { + case Opcode::FSeek: + case Opcode::FileReadAt: + case Opcode::FileWriteAt: + case Opcode::FileFlush: + case Opcode::FileEof: + case Opcode::FileTrunc: { if (!srv_->enable_file_func()) { reply = err("file functions disabled", openads::AE_ACCESS_DENIED); @@ -4302,6 +4321,146 @@ DispatchResult Session::dispatch(const Frame& f) { break; } reply.opcode = Opcode::FileRenameAck; + } else if (f.opcode == Opcode::FileCopy) { + std::size_t pos = 0; + std::string src_p, dst_p; + if (!read_lstr16(f.payload, pos, src_p) || + !read_lstr16(f.payload, pos, dst_p)) { + reply = err("FileCopy: short payload"); break; + } + auto a = resolve_fs_client_path(src_p); + auto b = resolve_fs_client_path(dst_p); + if (!a || !b) { deny_path(src_p); break; } + auto r = openads::engine::fs_copy(*a, *b); + if (!r) { + reply = err("FileCopy", + static_cast(r.error().code)); + break; + } + reply.opcode = Opcode::FileCopyAck; + } else if (f.opcode == Opcode::FileLoad) { + std::size_t pos = 0; + std::string path; + if (!read_lstr16(f.payload, pos, path)) { + reply = err("FileLoad: short payload"); break; + } + auto abs = resolve_fs_client_path(path); + if (!abs) { deny_path(path); break; } + auto data = openads::engine::fs_load(*abs); + if (!data) { + reply = err("FileLoad", + static_cast(data.error().code)); + break; + } + reply.opcode = Opcode::FileLoadAck; + write_u32_le(static_cast(data.value().size()), + reply.payload); + reply.payload.insert(reply.payload.end(), + data.value().begin(), data.value().end()); + } else if (f.opcode == Opcode::FileSave) { + std::size_t pos = 0; + std::string path; + if (!read_lstr16(f.payload, pos, path) || + pos + 4 > f.payload.size()) { + reply = err("FileSave: short payload"); break; + } + std::uint32_t n = read_u32_le(f.payload.data() + pos); + pos += 4; + if (pos + n > f.payload.size()) { + reply = err("FileSave: short data"); break; + } + auto abs = resolve_fs_client_path(path); + if (!abs) { deny_path(path); break; } + auto r = openads::engine::fs_save( + *abs, f.payload.data() + pos, n); + if (!r) { + reply = err("FileSave", + static_cast(r.error().code)); + break; + } + reply.opcode = Opcode::FileSaveAck; + } else if (f.opcode == Opcode::FileSetAttr) { + std::size_t pos = 0; + std::string path; + if (!read_lstr16(f.payload, pos, path) || + pos + 4 > f.payload.size()) { + reply = err("FileSetAttr: short payload"); break; + } + std::uint32_t attr = read_u32_le(f.payload.data() + pos); + auto abs = resolve_fs_client_path(path); + if (!abs) { deny_path(path); break; } + auto r = openads::engine::fs_set_attr(*abs, attr); + if (!r) { + reply = err("FileSetAttr", + static_cast(r.error().code)); + break; + } + reply.opcode = Opcode::FileSetAttrAck; + } else if (f.opcode == Opcode::FileSetTime) { + std::size_t pos = 0; + std::string path; + if (!read_lstr16(f.payload, pos, path) || + pos + 7 > f.payload.size()) { + reply = err("FileSetTime: short payload"); break; + } + std::uint16_t year = static_cast( + f.payload[pos] | + (static_cast(f.payload[pos + 1]) << 8)); + auto abs = resolve_fs_client_path(path); + if (!abs) { deny_path(path); break; } + auto r = openads::engine::fs_set_time( + *abs, year, f.payload[pos + 2], f.payload[pos + 3], + f.payload[pos + 4], f.payload[pos + 5], + f.payload[pos + 6]); + if (!r) { + reply = err("FileSetTime", + static_cast(r.error().code)); + break; + } + reply.opcode = Opcode::FileSetTimeAck; + } else if (f.opcode == Opcode::FileTemp) { + std::size_t pos = 0; + std::string dir, prefix; + if (!read_lstr16(f.payload, pos, dir) || + !read_lstr16(f.payload, pos, prefix)) { + reply = err("FileTemp: short payload"); break; + } + if (dir.empty()) dir = "."; + auto abs = resolve_fs_client_path(dir); + if (!abs) { deny_path(dir); break; } + auto name = openads::engine::fs_tempfile(*abs, prefix); + if (!name) { + reply = err("FileTemp", + static_cast(name.error().code)); + break; + } + reply.opcode = Opcode::FileTempAck; + auto client_name = (std::filesystem::path( + openads::platform::fold_absolute_to_relative(dir)) / + std::filesystem::path(name.value()).filename()).generic_string(); + write_lstr16(client_name, reply.payload); + } else if (f.opcode == Opcode::DirSpace) { + std::size_t pos = 0; + std::string dir; + if (!read_lstr16(f.payload, pos, dir) || + pos + 1 > f.payload.size()) { + reply = err("DirSpace: short payload"); break; + } + std::uint8_t type = f.payload[pos]; + if (dir.empty()) dir = "."; + auto abs = resolve_fs_client_path(dir); + if (!abs) { deny_path(dir); break; } + auto bytes = openads::engine::fs_dir_space(*abs, type); + if (!bytes) { + reply = err("DirSpace", + static_cast(bytes.error().code)); + break; + } + reply.opcode = Opcode::DirSpaceAck; + std::uint64_t v = bytes.value(); + for (int i = 0; i < 8; ++i) + reply.payload.push_back( + static_cast((v >> (8 * i)) & 0xFF)); } else if (f.opcode == Opcode::FileSize) { std::size_t pos = 0; std::string path; @@ -4541,6 +4700,119 @@ DispatchResult Session::dispatch(const Frame& f) { it->second->stream.tellg()); reply.opcode = Opcode::FSeekAck; write_u32_le(pos, reply.payload); + } else if (f.opcode == Opcode::FileReadAt || + f.opcode == Opcode::FileWriteAt) { + if (f.payload.size() < 16) { + reply = err("FileReadAt: short payload"); break; + } + std::uint32_t id = read_u32_le(f.payload.data()); + std::uint64_t off = 0; + for (int i = 0; i < 8; ++i) + off |= static_cast( + f.payload[static_cast(4 + i)]) + << (8 * i); + std::uint32_t n = read_u32_le(f.payload.data() + 12); + if (n > openads::engine::kMaxFsIoChunk) + n = openads::engine::kMaxFsIoChunk; + auto it = files_.find(id); + if (it == files_.end()) { + reply = err("FileReadAt: bad handle", + openads::AE_INTERNAL_ERROR); + break; + } + // ONE shared fstream position, same rule as FSeek. + it->second->stream.clear(); + it->second->stream.seekg(static_cast(off), + std::ios::beg); + if (f.opcode == Opcode::FileReadAt) { + std::vector buf(n); + it->second->stream.read( + buf.data(), static_cast(n)); + auto got = static_cast( + it->second->stream.gcount()); + reply.opcode = Opcode::FileReadAtAck; + write_u32_le(got, reply.payload); + reply.payload.insert( + reply.payload.end(), buf.begin(), + buf.begin() + static_cast(got)); + } else { + if (16u + n > f.payload.size()) { + reply = err("FileWriteAt: short data"); break; + } + it->second->stream.write( + reinterpret_cast(f.payload.data() + 16), + static_cast(n)); + it->second->stream.flush(); + if (!it->second->stream) { + reply = err("FileWriteAt: io error", + openads::AE_INTERNAL_ERROR); + break; + } + reply.opcode = Opcode::FileWriteAtAck; + write_u32_le(n, reply.payload); + } + } else if (f.opcode == Opcode::FileFlush) { + if (f.payload.size() < 4) { + reply = err("FileFlush: short payload"); break; + } + std::uint32_t id = read_u32_le(f.payload.data()); + auto it = files_.find(id); + if (it == files_.end()) { + reply = err("FileFlush: bad handle", + openads::AE_INTERNAL_ERROR); + break; + } + it->second->stream.flush(); + if (!it->second->stream) { + reply = err("FileFlush: io error", + openads::AE_INTERNAL_ERROR); + break; + } + reply.opcode = Opcode::FileFlushAck; + } else if (f.opcode == Opcode::FileEof) { + if (f.payload.size() < 4) { + reply = err("FileEof: short payload"); break; + } + std::uint32_t id = read_u32_le(f.payload.data()); + auto it = files_.find(id); + if (it == files_.end()) { + reply = err("FileEof: bad handle", + openads::AE_INTERNAL_ERROR); + break; + } + it->second->stream.clear(); + auto cur = it->second->stream.tellg(); + it->second->stream.seekg(0, std::ios::end); + auto end = it->second->stream.tellg(); + it->second->stream.clear(); + if (cur >= 0) it->second->stream.seekg(cur); + reply.opcode = Opcode::FileEofAck; + reply.payload.push_back( + (cur < 0 || (end >= 0 && cur >= end)) ? 1 : 0); + } else if (f.opcode == Opcode::FileTrunc) { + if (f.payload.size() < 12) { + reply = err("FileTrunc: short payload"); break; + } + std::uint32_t id = read_u32_le(f.payload.data()); + std::uint64_t size = 0; + for (int i = 0; i < 8; ++i) + size |= static_cast( + f.payload[static_cast(4 + i)]) + << (8 * i); + auto it = files_.find(id); + if (it == files_.end()) { + reply = err("FileTrunc: bad handle", + openads::AE_INTERNAL_ERROR); + break; + } + auto r = openads::engine::fs_truncate(*it->second, size); + if (!r) { + reply = err("FileTrunc", + static_cast(r.error().code)); + break; + } + it->second->stream.clear(); + reply.opcode = Opcode::FileTruncAck; } break; } diff --git a/src/network/wire.cpp b/src/network/wire.cpp index 61bf29f4..e7b8ecc0 100644 --- a/src/network/wire.cpp +++ b/src/network/wire.cpp @@ -210,6 +210,30 @@ bool valid_opcode(std::uint8_t opcode) noexcept { case static_cast(Opcode::FileEraseAck): case static_cast(Opcode::FileRename): case static_cast(Opcode::FileRenameAck): + case static_cast(Opcode::FileCopy): + case static_cast(Opcode::FileCopyAck): + case static_cast(Opcode::FileEof): + case static_cast(Opcode::FileEofAck): + case static_cast(Opcode::FileLoad): + case static_cast(Opcode::FileLoadAck): + case static_cast(Opcode::FileSave): + case static_cast(Opcode::FileSaveAck): + case static_cast(Opcode::FileReadAt): + case static_cast(Opcode::FileReadAtAck): + case static_cast(Opcode::FileWriteAt): + case static_cast(Opcode::FileWriteAtAck): + case static_cast(Opcode::FileFlush): + case static_cast(Opcode::FileFlushAck): + case static_cast(Opcode::FileSetAttr): + case static_cast(Opcode::FileSetAttrAck): + case static_cast(Opcode::FileSetTime): + case static_cast(Opcode::FileSetTimeAck): + case static_cast(Opcode::FileTrunc): + case static_cast(Opcode::FileTruncAck): + case static_cast(Opcode::FileTemp): + case static_cast(Opcode::FileTempAck): + case static_cast(Opcode::DirSpace): + case static_cast(Opcode::DirSpaceAck): case static_cast(Opcode::FileSize): case static_cast(Opcode::FileSizeAck): case static_cast(Opcode::FileMTime): diff --git a/src/network/wire.h b/src/network/wire.h index 4239234f..b30d115d 100644 --- a/src/network/wire.h +++ b/src/network/wire.h @@ -27,6 +27,11 @@ enum class Opcode : std::uint8_t { Connect = 0x10, ConnectAck = 0x11, Disconnect = 0x12, + // FileEof: [u32 file_id] -> [u8 eof]. The 0xE0+ server-filesystem + // family and the 0x20s block around FileCopy are full; 0x1D/0x1E are + // the last free pair below 0x20. + FileEof = 0x1D, + FileEofAck = 0x1E, BeginAuth = 0x3D, BeginAuthAck = 0x3E, CompleteAuth = 0x3F, @@ -34,6 +39,49 @@ enum class Opcode : std::uint8_t { OpenTableAck = 0x21, CloseTable = 0x22, CloseTableAck = 0x23, + // FileCopy: [u16 srcLen][src][u16 dstLen][dst] → empty ack + // Server-filesystem family (0xE0+) is full, so copy lives here. + FileCopy = 0x24, + FileCopyAck = 0x25, + // hb_vf group 2 - wire-efficiency batch. + // FileLoad: [u16 nameLen][name] -> [u32 size][bytes] + // (whole file; server caps at kMaxFsWholeFile) + FileLoad = 0x26, + FileLoadAck = 0x27, + // FileSave: [u16 nameLen][name][u32 size][bytes] -> empty ack + // (creates or truncates, like hb_vfSave) + FileSave = 0x28, + FileSaveAck = 0x29, + // FileReadAt: [u32 file_id][u64 offset][u32 nbytes] -> [u32 nread][bytes] + FileReadAt = 0x2A, + FileReadAtAck = 0x2B, + // FileWriteAt: [u32 file_id][u64 offset][u32 nbytes][bytes] + // -> [u32 nwritten] + FileWriteAt = 0x2C, + FileWriteAtAck = 0x2D, + // FileFlush: [u32 file_id] -> empty ack (flush server buffers) + FileFlush = 0x2E, + FileFlushAck = 0x2F, + // hb_vf group 3 - metadata batch. + // FileSetAttr: [u16 nameLen][name][u32 attr] -> empty ack + // (bit 0x01 readonly honored; other bits ignored) + FileSetAttr = 0x34, + FileSetAttrAck = 0x35, + // FileSetTime: [u16 nameLen][name][u16 year][u8 mon][u8 day] + // [u8 hh][u8 mm][u8 ss] -> empty ack (local time) + FileSetTime = 0x36, + FileSetTimeAck = 0x37, + // FileTrunc: [u32 file_id][u64 size] -> empty ack + FileTrunc = 0x38, + FileTruncAck = 0x39, + // FileTemp: [u16 dirLen][dir][u16 prefixLen][prefix] + // -> [u16 nameLen][name] (created empty, name relative) + FileTemp = 0x3A, + FileTempAck = 0x3B, + // DirSpace: [u16 dirLen][dir][u8 type] -> [u64 bytes] + // type 0 = free available, 1 = total capacity, 2 = free + DirSpace = 0x3C, + DirSpaceAck = 0x1F, // 0x3D+ is taken; last free slot below 0x20 ExecuteSQL = 0x30, ExecuteSQLAck = 0x31, Fetch = 0x32, diff --git a/src/openads_ace.def b/src/openads_ace.def index ae32b216..bf174331 100644 --- a/src/openads_ace.def +++ b/src/openads_ace.def @@ -120,6 +120,18 @@ EXPORTS AdsCacheRecords AdsCheckExistence AdsRenameFile + AdsCopyFile + AdsLoadFile + AdsSaveFile + AdsFReadAt + AdsFWriteAt + AdsFFlush + AdsFEof + AdsSetFileAttr + AdsSetFileTime + AdsFTrunc + AdsTempFile + AdsDirSpace AdsGetFileSize AdsGetFileTime AdsGetFileDate @@ -145,6 +157,18 @@ EXPORTS oads_CheckExistence oads_DeleteFile oads_RenameFile + oads_CopyFile + oads_LoadFile + oads_SaveFile + oads_FReadAt + oads_FWriteAt + oads_FFlush + oads_FEof + oads_SetFileAttr + oads_SetFileTime + oads_FTrunc + oads_TempFile + oads_DirSpace oads_GetFileSize oads_GetFileTime oads_GetFileDate diff --git a/src/openads_ace_x86_stdcall.def b/src/openads_ace_x86_stdcall.def index 934f6efd..7a2b8627 100644 --- a/src/openads_ace_x86_stdcall.def +++ b/src/openads_ace_x86_stdcall.def @@ -27,6 +27,18 @@ EXPORTS oads_CheckExistence oads_DeleteFile oads_RenameFile + oads_CopyFile + oads_LoadFile + oads_SaveFile + oads_FReadAt + oads_FWriteAt + oads_FFlush + oads_FEof + oads_SetFileAttr + oads_SetFileTime + oads_FTrunc + oads_TempFile + oads_DirSpace oads_GetFileSize oads_GetFileTime oads_GetFileDate diff --git a/tests/unit/abi_oads_file_funcs_test.cpp b/tests/unit/abi_oads_file_funcs_test.cpp index e96b65af..668cd2f9 100644 --- a/tests/unit/abi_oads_file_funcs_test.cpp +++ b/tests/unit/abi_oads_file_funcs_test.cpp @@ -2,7 +2,7 @@ // // Exercises ALL 16 oads_*() C functions (the legacy filesystem aliases): // oads_FOpen, oads_FCreate, oads_FClose, oads_FRead, oads_FWrite, oads_FSeek, -// oads_CheckExistence, oads_DeleteFile, oads_RenameFile, +// oads_CheckExistence, oads_DeleteFile, oads_RenameFile, oads_CopyFile, // oads_GetFileSize, oads_GetFileDate, oads_GetFileTime, // oads_DirMake, oads_DirRemove, oads_DirExist, oads_Directory // @@ -491,6 +491,250 @@ TEST_CASE( "oads_* local: RenameFile renames a file" ) fs::remove_all( dir, ec ); } +TEST_CASE( "oads_* local: CopyFile copies a file" ) +{ + auto dir = fs::temp_directory_path() / "oads_file_copy"; + std::error_code ec; + fs::remove_all( dir, ec ); + fs::create_directories( dir ); + + ADSHANDLE h = local_connect( dir ); + ADSHANDLE hf = 0; + + // create source + REQUIRE( oads_FCreate( h, (UNSIGNED8*)"src.txt", 0, &hf ) == 0 ); + UNSIGNED32 nw = 0; + oads_FWrite( hf, "data", 4, &nw ); + oads_FClose( hf ); + + // copy + REQUIRE( oads_CopyFile( h, (UNSIGNED8*)"src.txt", + (UNSIGNED8*)"dst.txt" ) == 0 ); + + // source still exists + UNSIGNED16 usExists = 0; + REQUIRE( oads_CheckExistence( h, (UNSIGNED8*)"src.txt", &usExists ) == 0 ); + CHECK( usExists == 1 ); + + // target exists with same content + usExists = 0; + REQUIRE( oads_CheckExistence( h, (UNSIGNED8*)"dst.txt", &usExists ) == 0 ); + CHECK( usExists == 1 ); + ADSHANDLE hf2 = 0; + REQUIRE( oads_FOpen( h, (UNSIGNED8*)"dst.txt", ADS_READONLY, &hf2 ) == 0 ); + char buf[16] = {}; + UNSIGNED32 nr = 0; + oads_FRead( hf2, buf, sizeof( buf ), &nr ); + CHECK( nr == 4 ); + CHECK( std::string( buf, nr ) == "data" ); + oads_FClose( hf2 ); + + // overwrite: shorter content replaces, not appends + REQUIRE( oads_FCreate( h, (UNSIGNED8*)"src2.txt", 0, &hf ) == 0 ); + oads_FWrite( hf, "xy", 2, &nw ); + oads_FClose( hf ); + REQUIRE( oads_CopyFile( h, (UNSIGNED8*)"src2.txt", + (UNSIGNED8*)"dst.txt" ) == 0 ); + UNSIGNED32 ulSize = 0; + REQUIRE( oads_GetFileSize( h, (UNSIGNED8*)"dst.txt", &ulSize ) == 0 ); + CHECK( ulSize == 2 ); + + // missing source fails + CHECK( oads_CopyFile( h, (UNSIGNED8*)"gone.txt", + (UNSIGNED8*)"dst2.txt" ) != 0 ); + + REQUIRE( AdsDisconnect( h ) == 0 ); + fs::remove_all( dir, ec ); +} + +TEST_CASE( "oads_* local: Load Save ReadAt WriteAt Flush Eof" ) +{ + auto dir = fs::temp_directory_path() / "oads_file_g2"; + std::error_code ec; + fs::remove_all( dir, ec ); + fs::create_directories( dir ); + + ADSHANDLE h = local_connect( dir ); + + // FSave creates a whole file in one call + REQUIRE( oads_SaveFile( h, (UNSIGNED8*)"w.txt", "hello", 5 ) == 0 ); + UNSIGNED32 ulSize = 0; + REQUIRE( oads_GetFileSize( h, (UNSIGNED8*)"w.txt", &ulSize ) == 0 ); + CHECK( ulSize == 5 ); + + // FSave truncates an existing file + REQUIRE( oads_SaveFile( h, (UNSIGNED8*)"w.txt", "ab", 2 ) == 0 ); + ulSize = 0; + REQUIRE( oads_GetFileSize( h, (UNSIGNED8*)"w.txt", &ulSize ) == 0 ); + CHECK( ulSize == 2 ); + + // FLoad returns the whole content + char lbuf[64] = {}; + UNSIGNED32 ulCap = sizeof( lbuf ); + REQUIRE( oads_LoadFile( h, (UNSIGNED8*)"w.txt", lbuf, &ulCap ) == 0 ); + CHECK( ulCap == 2 ); + CHECK( std::string( lbuf, ulCap ) == "ab" ); + + // FLoad with a short buffer reports the needed size + char tiny[1] = {}; + UNSIGNED32 ulNeed = sizeof( tiny ); + CHECK( oads_LoadFile( h, (UNSIGNED8*)"w.txt", tiny, &ulNeed ) == + (UNSIGNED32) openads::AE_INSUFFICIENT_BUFFER ); + CHECK( ulNeed == 2 ); + + // FLoad of a missing file fails + UNSIGNED32 ulX = sizeof( lbuf ); + CHECK( oads_LoadFile( h, (UNSIGNED8*)"gone.txt", lbuf, &ulX ) != 0 ); + + // Positional I/O on a handle + ADSHANDLE hf = 0; + REQUIRE( oads_FCreate( h, (UNSIGNED8*)"p.txt", 0, &hf ) == 0 ); + UNSIGNED32 nw = 0; + REQUIRE( oads_FWrite( hf, "0123456789", 10, &nw ) == 0 ); + + char rbuf[8] = {}; + UNSIGNED32 nr = 0; + REQUIRE( oads_FReadAt( hf, 4, rbuf, 3, &nr ) == 0 ); + CHECK( nr == 3 ); + CHECK( std::string( rbuf, nr ) == "456" ); + + // ReadAt past the end reads nothing + nr = 99; + REQUIRE( oads_FReadAt( hf, 50, rbuf, 3, &nr ) == 0 ); + CHECK( nr == 0 ); + + // WriteAt overwrites in place; verify through the same handle + // (a second reader can lag uncommitted CRT writes on Windows). + REQUIRE( oads_FWriteAt( hf, 2, "XY", 2, &nw ) == 0 ); + CHECK( nw == 2 ); + REQUIRE( oads_FFlush( hf ) == 0 ); + char all[16] = {}; + UNSIGNED32 ulAll = 0; + REQUIRE( oads_FReadAt( hf, 0, all, 10, &ulAll ) == 0 ); + CHECK( ulAll == 10 ); + CHECK( std::string( all, ulAll ) == "01XY456789" ); + + // Eof: not at end, then seek to end and it flips + UNSIGNED16 bEof = 1; + REQUIRE( oads_FSeek( hf, 0, 0, &nw ) == 0 ); + REQUIRE( oads_FEof( hf, &bEof ) == 0 ); + CHECK( bEof == 0 ); + REQUIRE( oads_FSeek( hf, 0, 2, &nw ) == 0 ); + bEof = 0; + REQUIRE( oads_FEof( hf, &bEof ) == 0 ); + CHECK( bEof == 1 ); + + oads_FClose( hf ); + CHECK( oads_FEof( hf, &bEof ) != 0 ); // closed handle fails + + // After the close every byte is on disk for a fresh reader too. + char disk[16] = {}; + UNSIGNED32 ulDisk = sizeof( disk ); + REQUIRE( oads_LoadFile( h, (UNSIGNED8*)"p.txt", disk, &ulDisk ) == 0 ); + CHECK( std::string( disk, ulDisk ) == "01XY456789" ); + + REQUIRE( AdsDisconnect( h ) == 0 ); + fs::remove_all( dir, ec ); +} + +static void metadata_round_trip( ADSHANDLE h, const fs::path& dir ) +{ + REQUIRE( oads_SaveFile( h, (UNSIGNED8*)"m.txt", "0123456789", 10 ) == 0 ); + + // SetFileAttr: read-only clears write bits, clearing restores them + auto p = dir / "m.txt"; + REQUIRE( oads_SetFileAttr( h, (UNSIGNED8*)"m.txt", 1 ) == 0 ); + CHECK( ( fs::status( p ).permissions() & fs::perms::owner_write ) == + fs::perms::none ); + REQUIRE( oads_SetFileAttr( h, (UNSIGNED8*)"m.txt", 0 ) == 0 ); + CHECK( ( fs::status( p ).permissions() & fs::perms::owner_write ) != + fs::perms::none ); + CHECK( oads_SetFileAttr( h, (UNSIGNED8*)"gone.txt", 1 ) != 0 ); + + // SetFileTime: round-trip via GetFileDate/GetFileTime + REQUIRE( oads_SetFileTime( h, (UNSIGNED8*)"m.txt", + 2021, 3, 4, 5, 6, 7 ) == 0 ); + UNSIGNED8 aucDate[32] = {}; + UNSIGNED16 usLen = sizeof( aucDate ); + REQUIRE( oads_GetFileDate( h, (UNSIGNED8*)"m.txt", aucDate, &usLen ) == 0 ); + REQUIRE( usLen == 9 ); // existing getter includes the terminating NUL + CHECK( aucDate[8] == 0 ); + CHECK( std::string( (char*) aucDate, 8 ) == "20210304" ); + UNSIGNED8 aucTime[32] = {}; + usLen = sizeof( aucTime ); + REQUIRE( oads_GetFileTime( h, (UNSIGNED8*)"m.txt", aucTime, &usLen ) == 0 ); + CHECK( std::string( (char*) aucTime, usLen ).substr( 0, 5 ) == "05:06" ); + + // FTrunc shrinks, then grows while preserving the prefix. + ADSHANDLE hf = 0; + REQUIRE( oads_FCreate( h, (UNSIGNED8*)"t.txt", 0, &hf ) == 0 ); + UNSIGNED32 nw = 0; + REQUIRE( oads_FWrite( hf, "0123456789", 10, &nw ) == 0 ); + REQUIRE( oads_FTrunc( hf, 4 ) == 0 ); + UNSIGNED32 ulSize = 0; + REQUIRE( oads_GetFileSize( h, (UNSIGNED8*)"t.txt", &ulSize ) == 0 ); + CHECK( ulSize == 4 ); + char buf[16] = {}; + UNSIGNED32 nr = 0; + REQUIRE( oads_FReadAt( hf, 0, buf, 4, &nr ) == 0 ); + CHECK( nr == 4 ); + CHECK( std::string( buf, nr ) == "0123" ); + REQUIRE( oads_FTrunc( hf, 6 ) == 0 ); + ulSize = 0; + REQUIRE( oads_GetFileSize( h, (UNSIGNED8*)"t.txt", &ulSize ) == 0 ); + CHECK( ulSize == 6 ); + oads_FClose( hf ); + CHECK( oads_FTrunc( hf, 4 ) != 0 ); // closed handle fails + + // TempFile: unique names that exist, under the jail root + char t1[4096] = {}; + UNSIGNED16 t1len = sizeof( t1 ); + REQUIRE( oads_TempFile( h, (UNSIGNED8*)"", (UNSIGNED8*)"tmp", + (UNSIGNED8*)t1, &t1len ) == 0 ); + CHECK( t1len > 0 ); + ADSHANDLE ht = 0; + REQUIRE( oads_FOpen( h, (UNSIGNED8*)t1, ADS_READONLY, &ht ) == 0 ); + CHECK( oads_FTrunc( ht, 10 ) != 0 ); + REQUIRE( oads_FClose( ht ) == 0 ); + CHECK( fs::exists( dir / std::string( t1, t1len ) ) ); + CHECK( std::string( t1, t1len ).find( "tmp" ) != std::string::npos ); + char t2[4096] = {}; + UNSIGNED16 t2len = sizeof( t2 ); + REQUIRE( oads_TempFile( h, (UNSIGNED8*)"", (UNSIGNED8*)"tmp", + (UNSIGNED8*)t2, &t2len ) == 0 ); + CHECK( std::string( t2, t2len ) != std::string( t1, t1len ) ); + + // DirSpace: capacity positive, available/free no more than capacity + UNSIGNED64 ullCap = 0, ullAvail = 0, ullFree = 0; + REQUIRE( oads_DirSpace( h, (UNSIGNED8*)"", 1, &ullCap ) == 0 ); + REQUIRE( oads_DirSpace( h, (UNSIGNED8*)"", 0, &ullAvail ) == 0 ); + REQUIRE( oads_DirSpace( h, (UNSIGNED8*)"", 2, &ullFree ) == 0 ); + CHECK( ullCap > 0 ); + CHECK( ullAvail <= ullCap ); + CHECK( ullFree <= ullCap ); + + CHECK( oads_SetFileAttr( h, (UNSIGNED8*)"../outside.txt", 1 ) != 0 ); + CHECK( oads_SetFileTime( h, (UNSIGNED8*)"../outside.txt", 2021, 3, 4, 5, 6, 7 ) != 0 ); + UNSIGNED16 deniedLen = sizeof( t1 ); + CHECK( oads_TempFile( h, (UNSIGNED8*)"..", (UNSIGNED8*)"deny", (UNSIGNED8*)t1, &deniedLen ) != 0 ); + CHECK( oads_DirSpace( h, (UNSIGNED8*)"..", 0, &ullAvail ) != 0 ); +} + +TEST_CASE( "oads_* local: SetAttr SetTime FTrunc TempFile DirSpace" ) +{ + auto dir = fs::temp_directory_path() / "oads_file_g3"; + std::error_code ec; + fs::remove_all( dir, ec ); + fs::create_directories( dir ); + + ADSHANDLE h = local_connect( dir ); + + metadata_round_trip( h, dir ); + + REQUIRE( AdsDisconnect( h ) == 0 ); + fs::remove_all( dir, ec ); +} + TEST_CASE( "oads_* local: GetFileSize returns correct size" ) { auto dir = fs::temp_directory_path() / "oads_file_size"; @@ -640,6 +884,7 @@ TEST_CASE( "oads_* remote embedded: full round-trip" ) ADSHANDLE h = remote_connect_embedded( data, srv.port() ); oads_round_trip( h, "remote-embedded" ); + metadata_round_trip( h, data ); // verify file lands in server data dir, not client cwd CHECK_FALSE( fs::exists( app / "oads_test.txt" ) ); @@ -674,6 +919,13 @@ TEST_CASE( "oads_* remote embedded: EnableFileFunc off denies" ) INFO( "FOpen with file func disabled rc=", rc ); CHECK( rc != 0 ); + CHECK( oads_SetFileAttr( h, (UNSIGNED8*)"nope.txt", 0 ) != 0 ); + CHECK( oads_SetFileTime( h, (UNSIGNED8*)"nope.txt", 2021, 3, 4, 5, 6, 7 ) != 0 ); + char name[4096] = {}; + UNSIGNED16 nameLen = sizeof( name ); + CHECK( oads_TempFile( h, (UNSIGNED8*)"", (UNSIGNED8*)"no", (UNSIGNED8*)name, &nameLen ) != 0 ); + UNSIGNED64 bytes = 0; + CHECK( oads_DirSpace( h, (UNSIGNED8*)"", 0, &bytes ) != 0 ); REQUIRE( AdsDisconnect( h ) == 0 ); fs::remove_all( data ); }