HB_BOOL hb_fsCopy( const char * pszSource, const char * pszDest ) { HB_BOOL fResult = HB_FALSE; PHB_FILE pSrcFile; if( ( pSrcFile = hb_fileExtOpen( pszSource, NULL, FO_READ | FO_SHARED | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { PHB_FILE pDstFile; HB_ERRCODE errCode; if( ( pDstFile = hb_fileExtOpen( pszDest, NULL, FXO_TRUNCATE | FO_READWRITE | FO_EXCLUSIVE | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { void * pbyBuffer = hb_xgrab( HB_FSCOPY_BUFFERSIZE ); for( ;; ) { HB_SIZE nBytesRead; if( ( nBytesRead = hb_fileRead( pSrcFile, pbyBuffer, HB_FSCOPY_BUFFERSIZE, -1 ) ) > 0 && nBytesRead != ( HB_SIZE ) FS_ERROR ) { if( nBytesRead != hb_fileWrite( pDstFile, pbyBuffer, nBytesRead, -1 ) ) { errCode = hb_fsError(); break; } } else { errCode = hb_fsError(); fResult = errCode == 0; break; } } hb_xfree( pbyBuffer ); hb_fileClose( pDstFile ); } else errCode = hb_fsError(); hb_fileClose( pSrcFile ); if( fResult ) { HB_FATTR ulAttr; if( hb_fileAttrGet( pszSource, &ulAttr ) ) hb_fileAttrSet( pszDest, ulAttr ); } hb_fsSetError( errCode ); } return fResult; }
HB_BYTE * hb_fileLoadData( PHB_FILE pFile, HB_SIZE nMaxSize, HB_SIZE * pnSize ) { HB_BYTE * pFileBuf = NULL; HB_SIZE nSize = 0, nRead, nBufSize; HB_FOFFSET nFileSize = hb_fileSize( pFile ); if( nFileSize == FS_ERROR || ( nFileSize == 0 && hb_fsError() == HB_FILE_ERR_UNSUPPORTED ) ) { for( nBufSize = 0;; ) { if( nBufSize == nSize ) { nBufSize += nBufSize == 0 ? HB_FILELOAD_BUFFERSIZE : nBufSize >> 1; if( nMaxSize > 0 && nBufSize > nMaxSize ) { nBufSize = nMaxSize; if( nBufSize == nSize ) break; } pFileBuf = ( HB_BYTE * ) hb_xrealloc( pFileBuf, nBufSize ); } nRead = hb_fileRead( pFile, pFileBuf + nSize, nBufSize - nSize, -1 ); if( nRead == 0 || nRead == ( HB_SIZE ) FS_ERROR ) break; nSize += nRead; } }
/* writes a line of data to the file, including the terminating EOL */ static int _writeLine( PFT_TEXT ft_text, const char * theData, HB_SIZE iDataLen ) { int err = 0; if( hb_fileWrite( ft_text->handles[ ft_text->area ], theData, iDataLen, -1 ) != iDataLen ) { err = 1; ft_text->error[ ft_text->area ] = hb_fsError(); } else if( ! _writeeol( ft_text->handles[ ft_text->area ] ) ) { err = 1; ft_text->error[ ft_text->area ] = hb_fsError(); } return err; }
/* * Perform a write of WorkArea memory to the data store. */ static HB_ERRCODE hb_delimGoCold( DELIMAREAP pArea ) { HB_TRACE(HB_TR_DEBUG, ("hb_delimGoCold(%p)", pArea)); if( pArea->fRecordChanged ) { ULONG ulSize = hb_delimEncodeBuffer( pArea ); if( hb_fileWriteAt( pArea->pFile, pArea->pBuffer, ulSize, pArea->ulRecordOffset ) != ulSize ) { PHB_ITEM pError = hb_errNew(); hb_errPutGenCode( pError, EG_WRITE ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_WRITE ) ); hb_errPutSubCode( pError, EDBF_WRITE ); hb_errPutOsCode( pError, hb_fsError() ); hb_errPutFileName( pError, pArea->szFileName ); SELF_ERROR( ( AREAP ) pArea, pError ); hb_itemRelease( pError ); return HB_FAILURE; } pArea->ulFileSize += ulSize; pArea->ulNextOffset = pArea->ulFileSize; pArea->fRecordChanged = FALSE; pArea->fFlush = TRUE; } return HB_SUCCESS; }
/* * Perform a write of WorkArea memory to the data store. */ static HB_ERRCODE hb_sdfGoCold( SDFAREAP pArea ) { HB_TRACE( HB_TR_DEBUG, ( "hb_sdfGoCold(%p)", pArea ) ); if( pArea->fRecordChanged ) { HB_SIZE nWrite = pArea->uiRecordLen + pArea->uiEolLen; if( hb_fileWriteAt( pArea->pFile, pArea->pRecord, nWrite, pArea->nRecordOffset ) != nWrite ) { PHB_ITEM pError = hb_errNew(); hb_errPutGenCode( pError, EG_WRITE ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_WRITE ) ); hb_errPutSubCode( pError, EDBF_WRITE ); hb_errPutOsCode( pError, hb_fsError() ); hb_errPutFileName( pError, pArea->szFileName ); SELF_ERROR( &pArea->area, pError ); hb_itemRelease( pError ); return HB_FAILURE; } pArea->nFileSize += nWrite; pArea->nNextOffset = pArea->nFileSize; pArea->fRecordChanged = HB_FALSE; pArea->fFlush = HB_TRUE; } return HB_SUCCESS; }
static HB_ERRCODE s_srvFsError( void ) { HB_ERRCODE errCode = hb_fsError(); if( errCode == 0 ) errCode = NETIO_ERR_FILE_IO; return errCode; }
static int hb_unzipExtractCurrentFileToHandle( unzFile hUnzip, HB_FHANDLE hFile, const char * szPassword ) { unz_file_info ufi; int iResult; if( hFile == FS_ERROR ) return -200; iResult = unzGetCurrentFileInfo( hUnzip, &ufi, NULL, HB_PATH_MAX - 1, NULL, 0, NULL, 0 ); if( iResult != UNZ_OK ) return iResult; iResult = unzOpenCurrentFilePassword( hUnzip, szPassword ); if( iResult != UNZ_OK ) return iResult; if( ! ( ufi.external_fa & 0x40000000 ) ) /* DIRECTORY */ { if( hFile != FS_ERROR ) { char * pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); while( ( iResult = unzReadCurrentFile( hUnzip, pString, HB_Z_IOBUF_SIZE ) ) > 0 ) hb_fsWriteLarge( hFile, pString, ( HB_SIZE ) iResult ); hb_xfree( pString ); #if defined( HB_OS_WIN ) { FILETIME ftutc, ft; SYSTEMTIME st; st.wSecond = ( WORD ) ufi.tmu_date.tm_sec; st.wMinute = ( WORD ) ufi.tmu_date.tm_min; st.wHour = ( WORD ) ufi.tmu_date.tm_hour; st.wDay = ( WORD ) ufi.tmu_date.tm_mday; st.wMonth = ( WORD ) ufi.tmu_date.tm_mon + 1; st.wYear = ( WORD ) ufi.tmu_date.tm_year; st.wMilliseconds = 0; if( SystemTimeToFileTime( &st, &ft ) && LocalFileTimeToFileTime( &ft, &ftutc ) ) { SetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), &ftutc, &ftutc, &ftutc ); } } #endif } else iResult = -200 - hb_fsError(); } unzCloseCurrentFile( hUnzip ); return iResult; }
HB_EXPORT HB_FOFFSET hb_fileSizeGet( const char * pszFileName, HB_BOOL bUseDirEntry ) { int i = s_fileFindDrv( pszFileName ); if( i >= 0 ) { HB_ERRCODE uiError; HB_FOFFSET nSize = 0; if( bUseDirEntry ) { PHB_ITEM pDir = hb_fileDirectory( pszFileName, "HS" ); uiError = hb_fsError(); if( pDir ) { PHB_ITEM pEntry = hb_arrayGetItemPtr( pDir, 1 ); if( pEntry ) nSize = hb_arrayGetNInt( pEntry, F_SIZE ); hb_itemRelease( pDir ); } } else { PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, FO_READ | FO_COMPAT, NULL, NULL ); if( pFile ) { nSize = hb_fileSize( pFile ); uiError = hb_fsError(); hb_fileClose( pFile ); } else uiError = hb_fsError(); } hb_fsSetFError( uiError ); return nSize; } return hb_fsFSize( pszFileName, bUseDirEntry ); }
/* deletes xxx bytes from the current file, beginning at the current record */ static int _del_buff( PFT_TEXT ft_text, HB_ISIZ iLen ) { char * WriteBuff = ( char * ) hb_xgrab( BUFFSIZE ); HB_FOFFSET fpRead, fpWrite; HB_ISIZ WriteLen; HB_ISIZ SaveLen; /* initialize file pointers */ fpWrite = ft_text->offset[ ft_text->area ]; fpRead = ft_text->offset[ ft_text->area ] + iLen; /* do initial load of buffer */ hb_fsSeekLarge( ft_text->handles[ ft_text->area ], fpRead, FS_SET ); WriteLen = hb_fsRead( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE ); fpRead += WriteLen; ft_text->error[ ft_text->area ] = 0; while( WriteLen > 0 ) { /* position to beginning of write area */ hb_fsSeekLarge( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ); SaveLen = hb_fsWriteLarge( ft_text->handles[ ft_text->area ], WriteBuff, WriteLen ); /* move write pointer */ fpWrite += SaveLen; if( SaveLen != WriteLen ) { /* error, fetch errcode and quit */ ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* return to read area and read another buffer */ hb_fsSeekLarge( ft_text->handles[ ft_text->area ], fpRead, FS_SET ); WriteLen = hb_fsRead( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE ); fpRead += WriteLen; } /* store length in bytes, set legacy EOF marker */ ft_text->lastbyte[ ft_text->area ] = hb_fsSeekLarge( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ); hb_fsWrite( ft_text->handles[ ft_text->area ], WriteBuff, 0 ); /* clear last_rec so next gobot will recount the records */ ft_text->last_rec[ ft_text->area ] = 0; hb_fsSeekLarge( ft_text->handles[ ft_text->area ], ft_text->offset[ ft_text->area ], FS_SET ); hb_xfree( WriteBuff ); return ft_text->error[ ft_text->area ]; }
static PHRB_BODY hb_hrbLoadFromFile( const char * szHrb, HB_USHORT usMode ) { char szFileName[ HB_PATH_MAX ]; PHRB_BODY pHrbBody = NULL; PHB_FNAME pFileName; HB_FHANDLE hFile; /* Create full filename */ pFileName = hb_fsFNameSplit( szHrb ); if( pFileName->szExtension == NULL && hb_stackSetStruct()->HB_SET_DEFEXTENSIONS ) { pFileName->szExtension = ".hrb"; } hb_fsFNameMerge( szFileName, pFileName ); hb_xfree( pFileName ); /* Open as binary */ do { hFile = hb_fsOpen( szFileName, FO_READ ); } while( hFile == FS_ERROR && hb_errRT_BASE_Ext1( EG_OPEN, 6102, NULL, szFileName, hb_fsError(), EF_CANDEFAULT | EF_CANRETRY, HB_ERR_ARGS_BASEPARAMS ) == E_RETRY ); if( hFile != FS_ERROR ) { HB_SIZE nBodySize = hb_fsSeek( hFile, 0, FS_END ); if( nBodySize ) { char * pbyBuffer; pbyBuffer = ( char * ) hb_xgrab( nBodySize + sizeof( char ) + 1 ); hb_fsSeek( hFile, 0, FS_SET ); hb_fsReadLarge( hFile, pbyBuffer, nBodySize ); pbyBuffer[ nBodySize ] = '\0'; pHrbBody = hb_hrbLoad( ( const char * ) pbyBuffer, nBodySize, usMode, szFileName ); hb_xfree( pbyBuffer ); } hb_fsClose( hFile ); } return pHrbBody; }
static HB_BOOL hb_zipGetFileInfoFromHandle( HB_FHANDLE hFile, HB_U32 * pulCRC, HB_BOOL * pfText ) { HB_BOOL fText = pfText != NULL, fResult = HB_FALSE; HB_U32 ulCRC = 0; if( hFile != FS_ERROR ) { unsigned char * pString = ( unsigned char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); HB_SIZE nRead, u; do { nRead = hb_fsReadLarge( hFile, pString, HB_Z_IOBUF_SIZE ); if( nRead > 0 ) { ulCRC = crc32( ulCRC, pString, ( uInt ) nRead ); if( fText ) { for( u = 0; u < nRead; ++u ) { if( pString[ u ] < 0x20 ? ( pString[ u ] != HB_CHAR_HT && pString[ u ] != HB_CHAR_LF && pString[ u ] != HB_CHAR_CR && pString[ u ] != HB_CHAR_EOF ) : ( pString[ u ] >= 0x7F && pString[ u ] < 0xA0 && pString[ u ] != ( unsigned char ) HB_CHAR_SOFT1 ) ) { fText = HB_FALSE; break; } } } } } while( nRead == HB_Z_IOBUF_SIZE ); fResult = ( hb_fsError() == 0 ); hb_xfree( pString ); } if( pulCRC ) *pulCRC = ulCRC; if( pfText ) *pfText = fText; return fResult; }
PHB_ITEM hb_errRT_FileError( PHB_ITEM pError, const char * szSubSystem, HB_ERRCODE errGenCode, HB_ERRCODE errSubCode, const char * szFileName ) { if( ! pError ) { pError = hb_errNew(); hb_errPutSeverity( pError, ES_ERROR ); hb_errPutSubSystem( pError, szSubSystem ? szSubSystem : HB_ERR_SS_BASE ); hb_errPutFlags( pError, EF_CANRETRY | EF_CANDEFAULT ); hb_errPutFileName( pError, szFileName ); } hb_errPutGenCode( pError, errGenCode ); hb_errPutDescription( pError, hb_langDGetErrorDesc( errGenCode ) ); hb_errPutSubCode( pError, errSubCode ); hb_errPutOsCode( pError, hb_fsError() ); return pError; }
static PHB_FILE hb_sxSemOpen( char * szFileName, HB_BOOL * pfNewFile ) { PHB_FILE pFile; int i = 0; do { pFile = hb_fileExtOpen( szFileName, ".sem", FO_READWRITE | FO_EXCLUSIVE | FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, NULL, NULL ); if( pFile != NULL ) break; if( pfNewFile ) { pFile = hb_fileExtOpen( szFileName, ".sem", FXO_UNIQUE | FO_READWRITE | FO_EXCLUSIVE | FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME, NULL, NULL ); if( pFile != NULL ) { *pfNewFile = HB_TRUE; break; } } else { HB_ERRCODE errCode = hb_fsError(); if( errCode != 5 && errCode != 32 && errCode != 33 ) break; } hb_idleSleep( 0.01 ); } while( ++i < 25 ); return pFile; }
static PHB_FILE s_fileExtOpen( PHB_FILE_FUNCS pFuncs, const char * pszFileName, const char * pDefExt, HB_USHORT uiExFlags, const char * pPaths, PHB_ITEM pError ) { PHB_FILE pFile = NULL; #if defined( HB_OS_UNIX ) HB_BOOL fResult, fSeek = HB_FALSE; # if defined( HB_USE_LARGEFILE64 ) struct stat64 statbuf; # else struct stat statbuf; # endif #endif HB_BOOL fShared, fReadonly; HB_FHANDLE hFile; char * pszFile; HB_SYMBOL_UNUSED( pFuncs ); fShared = ( uiExFlags & ( FO_DENYREAD | FO_DENYWRITE | FO_EXCLUSIVE ) ) == 0; fReadonly = ( uiExFlags & ( FO_READ | FO_WRITE | FO_READWRITE ) ) == FO_READ; pszFile = hb_fsExtName( pszFileName, pDefExt, uiExFlags, pPaths ); hb_vmUnlock(); #if defined( HB_OS_UNIX ) # if defined( HB_USE_LARGEFILE64 ) fResult = stat64( ( char * ) pszFile, &statbuf ) == 0; # else fResult = stat( ( char * ) pszFile, &statbuf ) == 0; # endif hb_fsSetIOError( fResult, 0 ); if( fResult ) { hb_threadEnterCriticalSection( &s_fileMtx ); pFile = hb_fileFind( statbuf.st_dev, statbuf.st_ino ); if( pFile ) { if( ! fShared || ! pFile->shared || ( uiExFlags & FXO_TRUNCATE ) != 0 ) fResult = HB_FALSE; else if( ! fReadonly && pFile->readonly ) pFile = NULL; else pFile->used++; if( ( uiExFlags & FXO_NOSEEKPOS ) == 0 ) { # if defined( HB_OS_VXWORKS ) fSeek = ! S_ISFIFO( statbuf.st_mode ); # else fSeek = ! S_ISFIFO( statbuf.st_mode ) && ! S_ISSOCK( statbuf.st_mode ); # endif } } hb_threadLeaveCriticalSection( &s_fileMtx ); } if( pFile ) { if( ! fResult ) { hb_fsSetError( ( uiExFlags & FXO_TRUNCATE ) ? 5 : 32 ); pFile = NULL; } else if( uiExFlags & FXO_COPYNAME ) hb_strncpy( ( char * ) pszFileName, pszFile, HB_PATH_MAX - 1 ); if( pError ) { hb_errPutFileName( pError, pszFile ); if( ! fResult ) { hb_errPutOsCode( pError, hb_fsError() ); hb_errPutGenCode( pError, ( HB_ERRCODE ) ( ( uiExFlags & FXO_TRUNCATE ) ? EG_CREATE : EG_OPEN ) ); } } } else #endif { hFile = hb_fsExtOpen( pszFileName, pDefExt, uiExFlags, pPaths, pError ); if( hFile != FS_ERROR ) { HB_ULONG device = 0, inode = 0; #if defined( HB_OS_UNIX ) # if defined( HB_USE_LARGEFILE64 ) if( fstat64( hFile, &statbuf ) == 0 ) # else if( fstat( hFile, &statbuf ) == 0 ) # endif { device = ( HB_ULONG ) statbuf.st_dev; inode = ( HB_ULONG ) statbuf.st_ino; if( ( uiExFlags & FXO_NOSEEKPOS ) == 0 ) { # if defined( HB_OS_VXWORKS ) fSeek = ! S_ISFIFO( statbuf.st_mode ); # else fSeek = ! S_ISFIFO( statbuf.st_mode ) && ! S_ISSOCK( statbuf.st_mode ); # endif } } #endif hb_threadEnterCriticalSection( &s_fileMtx ); pFile = hb_fileNew( hFile, fShared, fReadonly, device, inode, HB_TRUE ); if( pFile->hFile != hFile ) { if( pFile->hFileRO == FS_ERROR && ! fReadonly && pFile->readonly ) { pFile->hFileRO = pFile->hFile; pFile->hFile = hFile; pFile->readonly = HB_FALSE; hFile = FS_ERROR; } if( pFile->uiLocks == 0 ) { #if ! defined( HB_USE_SHARELOCKS ) || defined( HB_USE_BSDLOCKS ) if( pFile->hFileRO != FS_ERROR ) { hb_fsClose( pFile->hFileRO ); pFile->hFileRO = FS_ERROR; } #endif if( hFile != FS_ERROR ) { hb_fsClose( hFile ); hFile = FS_ERROR; #if defined( HB_USE_SHARELOCKS ) && ! defined( HB_USE_BSDLOCKS ) /* TOFIX: possible race condition */ hb_fsLockLarge( hFile, HB_SHARELOCK_POS, HB_SHARELOCK_SIZE, FL_LOCK | FLX_SHARED ); #endif } } } else hFile = FS_ERROR; hb_threadLeaveCriticalSection( &s_fileMtx ); if( hFile != FS_ERROR ) { /* TOFIX: possible race condition in MT mode, * close() is not safe due to existing locks * which are removed. */ hb_fsClose( hFile ); } } } hb_xfree( pszFile ); #if defined( HB_OS_UNIX ) if( pFile && fSeek ) pFile = hb_fileposNew( pFile ); #endif hb_vmLock(); return pFile; }
static int hb_zipStoreFile( zipFile hZip, const char * szFileName, const char * szName, const char * szPassword, const char * szComment ) { char * szZipName, * pString; HB_FHANDLE hFile; HB_SIZE nLen; HB_FATTR ulExtAttr; zip_fileinfo zfi; int iResult; HB_BOOL fError; HB_BOOL fText; HB_U32 ulCRC; if( szName ) { /* change path separators to '/' */ szZipName = hb_strdup( szName ); nLen = strlen( szZipName ); pString = szZipName; while( nLen-- ) { if( pString[ nLen ] == '\\' ) pString[ nLen ] = '/'; } } else { /* get file name */ szZipName = hb_strdup( szFileName ); nLen = strlen( szZipName ); pString = szZipName; while( nLen-- ) { if( pString[ nLen ] == '/' || pString[ nLen ] == '\\' ) { memmove( szZipName, &pString[ nLen + 1 ], strlen( szZipName ) - nLen ); break; } } } memset( &zfi, 0, sizeof( zfi ) ); fError = HB_FALSE; ulExtAttr = 0; #if defined( HB_OS_WIN ) { LPTSTR lpFileNameFree; LPCTSTR lpFileName = HB_FSNAMECONV( szFileName, &lpFileNameFree ); DWORD attr = GetFileAttributes( lpFileName ); if( attr != INVALID_FILE_ATTRIBUTES ) { ulExtAttr = hb_translateExtAttr( szFileName, attr & ( FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ARCHIVE ) ); } else fError = HB_TRUE; if( lpFileNameFree ) hb_xfree( lpFileNameFree ); } #elif defined( HB_OS_UNIX ) { struct stat statbuf; struct tm st; char * pszFree; if( stat( hb_fsNameConv( szFileName, &pszFree ), &statbuf ) == 0 ) { if( S_ISDIR( statbuf.st_mode ) ) { ulExtAttr |= 0x40000000; ulExtAttr |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */ } else { ulExtAttr |= 0x80000000; ulExtAttr |= 0x20; /* FILE_ATTRIBUTE_ARCHIVE */ } ulExtAttr |= ( ( statbuf.st_mode & S_IXOTH ) ? 0x00010000 : 0 ) | ( ( statbuf.st_mode & S_IWOTH ) ? 0x00020000 : 0 ) | ( ( statbuf.st_mode & S_IROTH ) ? 0x00040000 : 0 ) | ( ( statbuf.st_mode & S_IXGRP ) ? 0x00080000 : 0 ) | ( ( statbuf.st_mode & S_IWGRP ) ? 0x00100000 : 0 ) | ( ( statbuf.st_mode & S_IRGRP ) ? 0x00200000 : 0 ) | ( ( statbuf.st_mode & S_IXUSR ) ? 0x00400000 : 0 ) | ( ( statbuf.st_mode & S_IWUSR ) ? 0x00800000 : 0 ) | ( ( statbuf.st_mode & S_IRUSR ) ? 0x01000000 : 0 ); # if defined( HB_HAS_LOCALTIME_R ) localtime_r( &statbuf.st_mtime, &st ); # else st = *localtime( &statbuf.st_mtime ); # endif zfi.tmz_date.tm_sec = st.tm_sec; zfi.tmz_date.tm_min = st.tm_min; zfi.tmz_date.tm_hour = st.tm_hour; zfi.tmz_date.tm_mday = st.tm_mday; zfi.tmz_date.tm_mon = st.tm_mon; zfi.tmz_date.tm_year = st.tm_year; } else fError = HB_TRUE; if( pszFree ) hb_xfree( pszFree ); } #elif defined( HB_OS_DOS ) { # if defined( __DJGPP__ ) || defined( __RSX32__ ) || defined( __GNUC__ ) int attr; char * pszFree; attr = _chmod( hb_fsNameConv( szFileName, &pszFree ), 0, 0 ); if( pszFree ) hb_xfree( pszFree ); if( attr != -1 ) # else HB_FATTR attr; if( hb_fsGetAttr( szFileName, &attr ) ) # endif { ulExtAttr = attr & ( HB_FA_READONLY | HB_FA_HIDDEN | HB_FA_SYSTEM | HB_FA_DIRECTORY | HB_FA_ARCHIVE ); ulExtAttr = hb_translateExtAttr( szFileName, ulExtAttr ); } else fError = HB_TRUE; } #elif defined( HB_OS_OS2 ) { FILESTATUS3 fs3; APIRET ulrc; HB_FATTR ulAttr; char * pszFree; ulrc = DosQueryPathInfo( ( PCSZ ) hb_fsNameConv( szFileName, &pszFree ), FIL_STANDARD, &fs3, sizeof( fs3 ) ); if( pszFree ) hb_xfree( pszFree ); if( ulrc == NO_ERROR ) { ulAttr = 0; if( fs3.attrFile & FILE_READONLY ) ulAttr |= HB_FA_READONLY; if( fs3.attrFile & FILE_HIDDEN ) ulAttr |= HB_FA_HIDDEN; if( fs3.attrFile & FILE_SYSTEM ) ulAttr |= HB_FA_SYSTEM; if( fs3.attrFile & FILE_DIRECTORY ) ulAttr |= HB_FA_DIRECTORY; if( fs3.attrFile & FILE_ARCHIVED ) ulAttr |= HB_FA_ARCHIVE; ulExtAttr = hb_translateExtAttr( szFileName, ulAttr ); zfi.tmz_date.tm_sec = fs3.ftimeLastWrite.twosecs * 2; zfi.tmz_date.tm_min = fs3.ftimeLastWrite.minutes; zfi.tmz_date.tm_hour = fs3.ftimeLastWrite.hours; zfi.tmz_date.tm_mday = fs3.fdateLastWrite.day; zfi.tmz_date.tm_mon = fs3.fdateLastWrite.month; zfi.tmz_date.tm_year = fs3.fdateLastWrite.year + 1980; } else fError = HB_TRUE; } #else { HB_FATTR attr; if( ! hb_fsGetAttr( szFileName, &attr ) ) ulExtAttr = 0x81B60020; /* FILE_ATTRIBUTE_ARCHIVE | rw-rw-rw- */ else { ulExtAttr = attr & ( HB_FA_READONLY | HB_FA_HIDDEN | HB_FA_SYSTEM | HB_FA_DIRECTORY | HB_FA_ARCHIVE ); ulExtAttr = hb_translateExtAttr( szFileName, ulExtAttr ); } } #endif if( fError ) { hb_xfree( szZipName ); return -200; } fText = HB_FALSE; ulCRC = 0; zfi.external_fa = ulExtAttr; /* TODO: zip.exe test: 0 for binary file, 1 for text. Does not depend on extension. We should analyse content of file to determine this??? */ zfi.internal_fa = 0; if( ulExtAttr & 0x40000000 ) { iResult = zipOpenNewFileInZip3( hZip, szZipName, &zfi, NULL, 0, NULL, 0, szComment, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, szPassword, ulCRC ); if( iResult == 0 ) zipCloseFileInZip( hZip ); } else { hFile = hb_fsOpen( szFileName, FO_READ ); if( hFile != FS_ERROR ) { #if defined( HB_OS_WIN ) { FILETIME ftutc, ft; SYSTEMTIME st; if( GetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), NULL, NULL, &ftutc ) && FileTimeToLocalFileTime( &ftutc, &ft ) && FileTimeToSystemTime( &ft, &st ) ) { zfi.tmz_date.tm_sec = st.wSecond; zfi.tmz_date.tm_min = st.wMinute; zfi.tmz_date.tm_hour = st.wHour; zfi.tmz_date.tm_mday = st.wDay; zfi.tmz_date.tm_mon = st.wMonth - 1; zfi.tmz_date.tm_year = st.wYear; } } #endif if( szPassword ) { if( hb_zipGetFileInfo( szFileName, &ulCRC, &fText ) ) zfi.internal_fa = fText ? 1 : 0; } iResult = zipOpenNewFileInZip3( hZip, szZipName, &zfi, NULL, 0, NULL, 0, szComment, Z_DEFLATED, Z_DEFAULT_COMPRESSION, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, szPassword, ulCRC ); if( iResult == 0 ) { pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); while( ( nLen = hb_fsReadLarge( hFile, pString, HB_Z_IOBUF_SIZE ) ) > 0 ) zipWriteInFileInZip( hZip, pString, ( unsigned ) nLen ); hb_xfree( pString ); zipCloseFileInZip( hZip ); } hb_fsClose( hFile ); } else iResult = -200 - hb_fsError(); } hb_xfree( szZipName ); return iResult; }
HB_FHANDLE hb_fsProcessOpen( const char * pszFileName, HB_FHANDLE * phStdin, HB_FHANDLE * phStdout, HB_FHANDLE * phStderr, HB_BOOL fDetach, HB_ULONG * pulPID ) { HB_FHANDLE hPipeIn [ 2 ] = { FS_ERROR, FS_ERROR }, hPipeOut[ 2 ] = { FS_ERROR, FS_ERROR }, hPipeErr[ 2 ] = { FS_ERROR, FS_ERROR }; HB_FHANDLE hResult = FS_ERROR; HB_ERRCODE errCode; HB_BOOL fError = HB_FALSE; HB_TRACE( HB_TR_DEBUG, ( "hb_fsProcessOpen(%s, %p, %p, %p, %d, %p)", pszFileName, phStdin, phStdout, phStderr, fDetach, pulPID ) ); if( phStdin != NULL ) fError = ! hb_fsPipeCreate( hPipeIn ); if( ! fError && phStdout != NULL ) fError = ! hb_fsPipeCreate( hPipeOut ); if( ! fError && phStderr != NULL ) { if( phStdout == phStderr ) { hPipeErr[ 0 ] = hPipeOut[ 0 ]; hPipeErr[ 1 ] = hPipeOut[ 1 ]; } else fError = ! hb_fsPipeCreate( hPipeErr ); } if( ! fError ) { #if defined( HB_OS_WIN ) PROCESS_INFORMATION pi; STARTUPINFO si; DWORD dwFlags = 0; LPTSTR lpCommand = HB_CHARDUP( pszFileName ); # if ! defined( HB_OS_WIN_CE ) if( phStdin != NULL ) SetHandleInformation( ( HANDLE ) hb_fsGetOsHandle( hPipeIn [ 1 ] ), HANDLE_FLAG_INHERIT, 0 ); if( phStdout != NULL ) SetHandleInformation( ( HANDLE ) hb_fsGetOsHandle( hPipeOut[ 0 ] ), HANDLE_FLAG_INHERIT, 0 ); if( phStderr != NULL && phStdout != phStderr ) SetHandleInformation( ( HANDLE ) hb_fsGetOsHandle( hPipeErr[ 0 ] ), HANDLE_FLAG_INHERIT, 0 ); # endif memset( &pi, 0, sizeof( pi ) ); memset( &si, 0, sizeof( si ) ); si.cb = sizeof( si ); # ifdef STARTF_USESTDHANDLES si.dwFlags = STARTF_USESTDHANDLES; # endif if( fDetach ) { # ifdef STARTF_USESHOWWINDOW si.dwFlags |= STARTF_USESHOWWINDOW; # endif si.wShowWindow = SW_HIDE; si.hStdInput = ( HANDLE ) hb_fsGetOsHandle( hPipeIn [ 0 ] ); si.hStdOutput = ( HANDLE ) hb_fsGetOsHandle( hPipeOut[ 1 ] ); si.hStdError = ( HANDLE ) hb_fsGetOsHandle( hPipeErr[ 1 ] ); # ifdef DETACHED_PROCESS dwFlags |= DETACHED_PROCESS; # endif } else { si.hStdInput = phStdin ? ( HANDLE ) hb_fsGetOsHandle( hPipeIn [ 0 ] ) : GetStdHandle( STD_INPUT_HANDLE ); si.hStdOutput = phStdout ? ( HANDLE ) hb_fsGetOsHandle( hPipeOut[ 1 ] ) : GetStdHandle( STD_OUTPUT_HANDLE ); si.hStdError = phStderr ? ( HANDLE ) hb_fsGetOsHandle( hPipeErr[ 1 ] ) : GetStdHandle( STD_ERROR_HANDLE ); } fError = ! CreateProcess( NULL, /* lpAppName */ lpCommand, NULL, /* lpProcessAttr */ NULL, /* lpThreadAttr */ TRUE, /* bInheritHandles */ dwFlags, /* dwCreationFlags */ NULL, /* lpEnvironment */ NULL, /* lpCurrentDirectory */ &si, &pi ); hb_fsSetIOError( ! fError, 0 ); hb_xfree( lpCommand ); if( ! fError ) { if( phStdin != NULL ) { *phStdin = ( HB_FHANDLE ) hPipeIn[ 1 ]; hPipeIn[ 1 ] = FS_ERROR; } if( phStdout != NULL ) { *phStdout = ( HB_FHANDLE ) hPipeOut[ 0 ]; hPipeOut[ 0 ] = FS_ERROR; } if( phStderr != NULL ) { *phStderr = ( HB_FHANDLE ) hPipeErr[ 0 ]; hPipeErr[ 0 ] = FS_ERROR; } if( pulPID ) *pulPID = pi.dwProcessId; CloseHandle( pi.hThread ); hResult = ( HB_FHANDLE ) pi.hProcess; } #elif defined( HB_OS_OS2 ) HFILE hNull = ( HFILE ) FS_ERROR; ULONG ulState = 0; APIRET ret = NO_ERROR; PID pid = ( PID ) -1; PHB_GT pGT; if( fDetach && ( ! phStdin || ! phStdout || ! phStderr ) ) { HB_FHANDLE hFile; ret = hb_fsOS2DosOpen( "NUL:", &hFile, &ulState, 0, FILE_NORMAL, OPEN_ACCESS_READWRITE, OPEN_ACTION_OPEN_IF_EXISTS ); if( ret == NO_ERROR ) hNull = ( HFILE ) hFile; } if( ret == NO_ERROR && phStdin != NULL ) { ret = DosQueryFHState( hPipeIn[ 1 ], &ulState ); if( ret == NO_ERROR && ( ulState & OPEN_FLAGS_NOINHERIT ) == 0 ) ret = DosSetFHState( hPipeIn[ 1 ], ( ulState & 0xFF00 ) | OPEN_FLAGS_NOINHERIT ); } if( ret == NO_ERROR && phStdout != NULL ) { ret = DosQueryFHState( hPipeOut[ 0 ], &ulState ); if( ret == NO_ERROR && ( ulState & OPEN_FLAGS_NOINHERIT ) == 0 ) ret = DosSetFHState( hPipeOut[ 0 ], ( ulState & 0xFF00 ) | OPEN_FLAGS_NOINHERIT ); } if( ret == NO_ERROR && phStderr != NULL && phStdout != phStderr ) { ret = DosQueryFHState( hPipeErr[ 0 ], &ulState ); if( ret == NO_ERROR && ( ulState & OPEN_FLAGS_NOINHERIT ) == 0 ) ret = DosSetFHState( hPipeErr[ 0 ], ( ulState & 0xFF00 ) | OPEN_FLAGS_NOINHERIT ); } if( ret == NO_ERROR && ( pGT = hb_gt_Base() ) != NULL ) { ULONG ulStateIn, ulStateOut, ulStateErr; HFILE hStdIn, hStdErr, hStdOut, hDup; ulStateIn = ulStateOut = ulStateErr = OPEN_FLAGS_NOINHERIT; hStdIn = hStdErr = hStdOut = ( HFILE ) FS_ERROR; if( ret == NO_ERROR && ( phStdin != NULL || fDetach ) ) { hDup = 0; ret = DosDupHandle( hDup, &hStdIn ); if( ret == NO_ERROR ) { ret = DosQueryFHState( hStdIn, &ulStateIn ); if( ret == NO_ERROR && ( ulStateIn & OPEN_FLAGS_NOINHERIT ) == 0 ) ret = DosSetFHState( hStdIn, ( ulStateIn & 0xFF00 ) | OPEN_FLAGS_NOINHERIT ); if( ret == NO_ERROR ) ret = DosDupHandle( phStdin != NULL ? ( HFILE ) hPipeIn[ 0 ] : hNull, &hDup ); } } if( ret == NO_ERROR && ( phStdout != NULL || fDetach ) ) { hDup = 1; ret = DosDupHandle( hDup, &hStdOut ); if( ret == NO_ERROR ) { ret = DosQueryFHState( hStdOut, &ulStateOut ); if( ret == NO_ERROR && ( ulStateOut & OPEN_FLAGS_NOINHERIT ) == 0 ) ret = DosSetFHState( hStdOut, ( ulStateOut & 0xFF00 ) | OPEN_FLAGS_NOINHERIT ); if( ret == NO_ERROR ) ret = DosDupHandle( phStdout != NULL ? ( HFILE ) hPipeOut[ 1 ] : hNull, &hDup ); } } if( ret == NO_ERROR && ( phStderr != NULL || fDetach ) ) { hDup = 2; ret = DosDupHandle( hDup, &hStdErr ); if( ret == NO_ERROR ) { ret = DosQueryFHState( hStdErr, &ulStateErr ); if( ret == NO_ERROR && ( ulStateErr & OPEN_FLAGS_NOINHERIT ) == 0 ) ret = DosSetFHState( hStdErr, ( ulStateErr & 0xFF00 ) | OPEN_FLAGS_NOINHERIT ); if( ret == NO_ERROR ) ret = DosDupHandle( phStderr != NULL ? ( HFILE ) hPipeErr[ 1 ] : hNull, &hDup ); } } if( ret == NO_ERROR ) { char * pArgs = hb_buildArgsOS2( pszFileName, &ret ); char uchLoadError[ CCHMAXPATH ] = { 0 }; RESULTCODES ChildRC = { 0, 0 }; if( pArgs ) { ret = DosExecPgm( uchLoadError, sizeof( uchLoadError ), fDetach ? EXEC_BACKGROUND : EXEC_ASYNCRESULT, ( PCSZ ) pArgs, NULL /* env */, &ChildRC, ( PCSZ ) pArgs ); if( ret == NO_ERROR ) pid = ChildRC.codeTerminate; hb_freeArgsOS2( pArgs ); } } if( hNull != ( HFILE ) FS_ERROR ) DosClose( hNull ); if( hStdIn != ( HFILE ) FS_ERROR ) { hDup = 0; DosDupHandle( hStdIn, &hDup ); DosClose( hStdIn ); if( ( ulStateIn & OPEN_FLAGS_NOINHERIT ) == 0 ) DosSetFHState( hDup, ulStateIn & 0xFF00 ); } if( hStdOut != ( HFILE ) FS_ERROR ) { hDup = 1; DosDupHandle( hStdOut, &hDup ); DosClose( hStdOut ); if( ( ulStateOut & OPEN_FLAGS_NOINHERIT ) == 0 ) DosSetFHState( hDup, ulStateOut & 0xFF00 ); } if( hStdErr != ( HFILE ) FS_ERROR ) { hDup = 2; DosDupHandle( hStdErr, &hDup ); DosClose( hStdErr ); if( ( ulStateErr & OPEN_FLAGS_NOINHERIT ) == 0 ) DosSetFHState( hDup, ulStateErr & 0xFF00 ); } hb_gt_BaseFree( pGT ); } else { if( hNull != ( HFILE ) FS_ERROR ) DosClose( hNull ); if( ret == NO_ERROR ) ret = ( APIRET ) FS_ERROR; } fError = ret != NO_ERROR; if( ! fError ) { if( phStdin != NULL ) { *phStdin = ( HB_FHANDLE ) hPipeIn[ 1 ]; hPipeIn[ 1 ] = FS_ERROR; } if( phStdout != NULL ) { *phStdout = ( HB_FHANDLE ) hPipeOut[ 0 ]; hPipeOut[ 0 ] = FS_ERROR; } if( phStderr != NULL ) { *phStderr = ( HB_FHANDLE ) hPipeErr[ 0 ]; hPipeErr[ 0 ] = FS_ERROR; } if( pulPID ) *pulPID = pid; hResult = ( HB_FHANDLE ) pid; } hb_fsSetError( ( HB_ERRCODE ) ret ); #elif defined( HB_OS_UNIX ) && \ ! defined( HB_OS_VXWORKS ) && ! defined( HB_OS_SYMBIAN ) char ** argv = hb_buildArgs( pszFileName ); pid_t pid = fork(); if( pid == -1 ) fError = HB_TRUE; else if( pid != 0 ) /* parent process */ { if( phStdin != NULL ) { *phStdin = ( HB_FHANDLE ) hPipeIn[ 1 ]; hPipeIn[ 1 ] = FS_ERROR; } if( phStdout != NULL ) { *phStdout = ( HB_FHANDLE ) hPipeOut[ 0 ]; hPipeOut[ 0 ] = FS_ERROR; } if( phStderr != NULL ) { *phStderr = ( HB_FHANDLE ) hPipeErr[ 0 ]; hPipeErr[ 0 ] = FS_ERROR; } if( pulPID ) *pulPID = pid; hResult = ( HB_FHANDLE ) pid; } else /* child process */ { if( fDetach && ( ! phStdin || ! phStdout || ! phStderr ) ) { HB_FHANDLE hNull = open( "/dev/null", O_RDWR ); if( ! phStdin ) dup2( hNull, 0 ); if( ! phStdout ) dup2( hNull, 1 ); if( ! phStderr ) dup2( hNull, 2 ); if( hNull != FS_ERROR ) hb_fsClose( hNull ); } if( phStdin != NULL ) { dup2( hPipeIn[ 0 ], 0 ); hb_fsClose( hPipeIn[ 1 ] ); } if( phStdout != NULL ) { dup2( hPipeOut[ 1 ], 1 ); hb_fsClose( hPipeOut[ 0 ] ); } if( phStderr != NULL ) { dup2( hPipeErr[ 1 ], 2 ); if( phStdout != phStderr ) hb_fsClose( hPipeErr[ 0 ] ); } /* close all non std* handles */ { int iMaxFD, i; iMaxFD = sysconf( _SC_OPEN_MAX ); if( iMaxFD < 3 ) iMaxFD = 1024; for( i = 3; i < iMaxFD; ++i ) hb_fsClose( i ); } /* reset extended process attributes */ if( setuid( getuid() ) == -1 ) {} if( setgid( getgid() ) == -1 ) {} /* execute command */ { # if defined( __WATCOMC__ ) execvp( argv[ 0 ], ( const char ** ) argv ); # else execvp( argv[ 0 ], argv ); # endif exit( -1 ); } } hb_fsSetIOError( ! fError, 0 ); hb_freeArgs( argv ); #elif defined( HB_OS_OS2 ) || defined( HB_OS_WIN ) int hStdIn, hStdOut, hStdErr; char ** argv; int pid; hStdIn = dup( 0 ); hStdOut = dup( 1 ); hStdErr = dup( 2 ); if( fDetach && ( ! phStdin || ! phStdout || ! phStderr ) ) { HB_FHANDLE hNull = open( "NUL:", O_RDWR ); if( ! phStdin ) dup2( hNull, 0 ); if( ! phStdout ) dup2( hNull, 1 ); if( ! phStderr ) dup2( hNull, 2 ); if( hNull != FS_ERROR ) close( hNull ); } if( phStdin != NULL ) dup2( hPipeIn[ 0 ], 0 ); if( phStdout != NULL ) dup2( hPipeOut[ 1 ], 1 ); if( phStderr != NULL ) dup2( hPipeErr[ 1 ], 2 ); argv = hb_buildArgs( pszFileName ); #if defined( _MSC_VER ) || defined( __LCC__ ) || \ defined( __XCC__ ) || defined( __POCC__ ) pid = _spawnvp( _P_NOWAIT, argv[ 0 ], argv ); #elif defined( __MINGW32__ ) || defined( __WATCOMC__ ) pid = spawnvp( P_NOWAIT, argv[ 0 ], ( const char * const * ) argv ); #else pid = spawnvp( P_NOWAIT, argv[ 0 ], ( char * const * ) argv ); #endif hb_freeArgs( argv ); dup2( hStdIn, 0 ); close( hStdIn ); dup2( hStdOut, 1 ); close( hStdOut ); dup2( hStdErr, 2 ); close( hStdErr ); if( pid < 0 ) fError = HB_TRUE; else if( pid != 0 ) /* parent process */ { if( phStdin != NULL ) { *phStdin = ( HB_FHANDLE ) hPipeIn[ 1 ]; hPipeIn[ 1 ] = FS_ERROR; } if( phStdout != NULL ) { *phStdout = ( HB_FHANDLE ) hPipeOut[ 0 ]; hPipeOut[ 0 ] = FS_ERROR; } if( phStderr != NULL ) { *phStderr = ( HB_FHANDLE ) hPipeErr[ 0 ]; hPipeErr[ 0 ] = FS_ERROR; } if( pulPID ) *pulPID = pid; hResult = ( HB_FHANDLE ) pid; } hb_fsSetIOError( ! fError, 0 ); #else int iTODO; /* TODO: for given platform */ HB_SYMBOL_UNUSED( pszFileName ); HB_SYMBOL_UNUSED( fDetach ); HB_SYMBOL_UNUSED( pulPID ); hb_fsSetError( ( HB_ERRCODE ) FS_ERROR ); #endif } errCode = hb_fsError(); if( hPipeIn[ 0 ] != FS_ERROR ) hb_fsClose( hPipeIn[ 0 ] ); if( hPipeIn[ 1 ] != FS_ERROR ) hb_fsClose( hPipeIn[ 1 ] ); if( hPipeOut[ 0 ] != FS_ERROR ) hb_fsClose( hPipeOut[ 0 ] ); if( hPipeOut[ 1 ] != FS_ERROR ) hb_fsClose( hPipeOut[ 1 ] ); if( phStdout != phStderr ) { if( hPipeErr[ 0 ] != FS_ERROR ) hb_fsClose( hPipeErr[ 0 ] ); if( hPipeErr[ 1 ] != FS_ERROR ) hb_fsClose( hPipeErr[ 1 ] ); } hb_fsSetError( errCode ); return hResult; }
/* the contents of the inserted bytes are indeterminate, i.e. you'll have to write to them before they mean anything */ static int _ins_buff( PFT_TEXT ft_text, HB_ISIZ iLen ) { char * ReadBuff = ( char * ) hb_xgrab( BUFFSIZE ); char * WriteBuff = ( char * ) hb_xgrab( BUFFSIZE ); char * SaveBuff; HB_FOFFSET fpRead, fpWrite; HB_ISIZ WriteLen, ReadLen; HB_ISIZ SaveLen; HB_ISIZ iLenRemaining = iLen; /* set target move distance, this allows iLen to be greater than BUFFSIZE */ iLen = HB_MIN( iLenRemaining, BUFFSIZE ); iLenRemaining -= iLen; /* initialize file pointers */ fpRead = ft_text->offset[ ft_text->area ]; fpWrite = ft_text->offset[ ft_text->area ] + iLen; /* do initial load of both buffers */ WriteLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE, fpRead ) ); fpRead += WriteLen; ReadLen = hb_fileResult( hb_fileRead( ft_text->handles[ ft_text->area ], ReadBuff, BUFFSIZE, -1 ) ); fpRead += ReadLen; ft_text->error[ ft_text->area ] = 0; while( ! ft_text->error[ ft_text->area ] && iLen > 0 ) { while( WriteLen > 0 ) { /* position to beginning of write area */ if( hb_fileSeek( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ) != fpWrite ) { ft_text->error[ ft_text->area ] = hb_fsError(); break; } if( ( SaveLen = hb_fileResult( hb_fileWrite( ft_text->handles[ ft_text->area ], WriteBuff, WriteLen, -1 ) ) ) == 0 ) { ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* move write pointer */ fpWrite += SaveLen; if( SaveLen != WriteLen ) { /* error, fetch errcode and quit */ ft_text->error[ ft_text->area ] = hb_fsError(); break; } #if 0 WriteLen = SaveLen; #endif /* swap buffers */ SaveBuff = WriteBuff; WriteBuff = ReadBuff; ReadBuff = SaveBuff; WriteLen = ReadLen; /* return to read area and read another buffer */ ReadLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], ReadBuff, BUFFSIZE, fpRead ) ); fpRead += ReadLen; } iLen = HB_MIN( iLenRemaining, BUFFSIZE ); iLenRemaining -= iLen; } /* store length in bytes, set legacy EOF marker */ ft_text->lastbyte[ ft_text->area ] = hb_fileSeek( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ); hb_fileWrite( ft_text->handles[ ft_text->area ], WriteBuff, 0, -1 ); /* clear last_rec so next gobot will recount the records */ ft_text->last_rec[ ft_text->area ] = 0; hb_fileSeek( ft_text->handles[ ft_text->area ], ft_text->offset[ ft_text->area ], FS_SET ); hb_xfree( ReadBuff ); hb_xfree( WriteBuff ); return ft_text->error[ ft_text->area ]; }
static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, const char * szPassword ) { char szName[ HB_PATH_MAX ]; HB_SIZE nPos, nLen; char cSep, * pString; unz_file_info ufi; int iResult; HB_FHANDLE hFile; iResult = unzGetCurrentFileInfo( hUnzip, &ufi, szName, HB_PATH_MAX - 1, NULL, 0, NULL, 0 ); if( iResult != UNZ_OK ) return iResult; iResult = unzOpenCurrentFilePassword( hUnzip, szPassword ); if( iResult != UNZ_OK ) return iResult; if( szFileName ) hb_strncpy( szName, szFileName, sizeof( szName ) - 1 ); nLen = strlen( szName ); /* Test shows that files in subfolders can be stored to zip file without explicitly adding folder. So, let's create a required path */ nPos = 1; while( nPos < nLen ) { cSep = szName[ nPos ]; /* allow both path separators, ignore terminating path separator */ if( ( cSep == '\\' || cSep == '/' ) && nPos < nLen - 1 ) { szName[ nPos ] = '\0'; hb_fsMkDir( szName ); szName[ nPos ] = cSep; } nPos++; } if( ufi.external_fa & 0x40000000 ) /* DIRECTORY */ { hb_fsMkDir( szName ); iResult = UNZ_OK; } else { hFile = hb_fsCreate( szName, FC_NORMAL ); if( hFile != FS_ERROR ) { pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); while( ( iResult = unzReadCurrentFile( hUnzip, pString, HB_Z_IOBUF_SIZE ) ) > 0 ) hb_fsWriteLarge( hFile, pString, ( HB_SIZE ) iResult ); hb_xfree( pString ); #if defined( HB_OS_WIN ) { FILETIME ftutc, ft; SYSTEMTIME st; st.wSecond = ( WORD ) ufi.tmu_date.tm_sec; st.wMinute = ( WORD ) ufi.tmu_date.tm_min; st.wHour = ( WORD ) ufi.tmu_date.tm_hour; st.wDay = ( WORD ) ufi.tmu_date.tm_mday; st.wMonth = ( WORD ) ufi.tmu_date.tm_mon + 1; st.wYear = ( WORD ) ufi.tmu_date.tm_year; st.wMilliseconds = 0; if( SystemTimeToFileTime( &st, &ft ) && LocalFileTimeToFileTime( &ft, &ftutc ) ) { SetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), &ftutc, &ftutc, &ftutc ); } } #endif hb_fsClose( hFile ); } else iResult = -200 - hb_fsError(); } unzCloseCurrentFile( hUnzip ); #if defined( HB_OS_WIN ) { LPTSTR lpFileNameFree; LPCTSTR lpFileName = HB_FSNAMECONV( szName, &lpFileNameFree ); SetFileAttributes( ( LPCTSTR ) lpFileName, ufi.external_fa & 0xFF ); if( lpFileNameFree ) hb_xfree( lpFileNameFree ); } #elif defined( HB_OS_UNIX ) || defined( __DJGPP__ ) { struct utimbuf utim; struct tm st; time_t tim; char * pszFree; const char * szNameOS = hb_fsNameConv( szName, &pszFree ); # if defined( __DJGPP__ ) _chmod( szNameOS, 1, ufi.external_fa & 0xFF ); # else HB_FATTR ulAttr = ufi.external_fa; if( ( ulAttr & 0xFFFF0000 ) == 0 ) ulAttr = hb_translateExtAttr( szName, ulAttr ); chmod( szNameOS, ( ( ulAttr & 0x00010000 ) ? S_IXOTH : 0 ) | ( ( ulAttr & 0x00020000 ) ? S_IWOTH : 0 ) | ( ( ulAttr & 0x00040000 ) ? S_IROTH : 0 ) | ( ( ulAttr & 0x00080000 ) ? S_IXGRP : 0 ) | ( ( ulAttr & 0x00100000 ) ? S_IWGRP : 0 ) | ( ( ulAttr & 0x00200000 ) ? S_IRGRP : 0 ) | ( ( ulAttr & 0x00400000 ) ? S_IXUSR : 0 ) | ( ( ulAttr & 0x00800000 ) ? S_IWUSR : 0 ) | ( ( ulAttr & 0x01000000 ) ? S_IRUSR : 0 ) ); # endif memset( &st, 0, sizeof( st ) ); st.tm_sec = ufi.tmu_date.tm_sec; st.tm_min = ufi.tmu_date.tm_min; st.tm_hour = ufi.tmu_date.tm_hour; st.tm_mday = ufi.tmu_date.tm_mday; st.tm_mon = ufi.tmu_date.tm_mon; st.tm_year = ufi.tmu_date.tm_year - 1900; tim = mktime( &st ); # if defined( HB_HAS_LOCALTIME_R ) gmtime_r( &tim, &st ); # else st = *gmtime( &tim ); # endif utim.actime = utim.modtime = mktime( &st ); utime( szNameOS, &utim ); if( pszFree ) hb_xfree( pszFree ); } #elif defined( HB_OS_DOS ) { # if defined( __RSX32__ ) || defined( __GNUC__ ) char * pszFree; _chmod( hb_fsNameConv( szName, &pszFree ), 1, ufi.external_fa & 0xFF ); if( pszFree ) hb_xfree( pszFree ); # else hb_fsSetAttr( szName, ufi.external_fa & 0xFF ); # endif } #elif defined( HB_OS_OS2 ) { FILESTATUS3 fs3; APIRET ulrc; HB_FATTR ulAttr = FILE_NORMAL; int iAttr = ufi.external_fa & 0xFF; char * pszFree; const char * szNameOS = hb_fsNameConv( szName, &pszFree ); if( iAttr & HB_FA_READONLY ) ulAttr |= FILE_READONLY; if( iAttr & HB_FA_HIDDEN ) ulAttr |= FILE_HIDDEN; if( iAttr & HB_FA_SYSTEM ) ulAttr |= FILE_SYSTEM; if( iAttr & HB_FA_ARCHIVE ) ulAttr |= FILE_ARCHIVED; ulrc = DosQueryPathInfo( ( PCSZ ) szNameOS, FIL_STANDARD, &fs3, sizeof( fs3 ) ); if( ulrc == NO_ERROR ) { FDATE fdate; FTIME ftime; fdate.year = ufi.tmu_date.tm_year - 1980; fdate.month = ufi.tmu_date.tm_mon; fdate.day = ufi.tmu_date.tm_mday; ftime.hours = ufi.tmu_date.tm_hour; ftime.minutes = ufi.tmu_date.tm_min; ftime.twosecs = ufi.tmu_date.tm_sec / 2; fs3.attrFile = ulAttr; fs3.fdateCreation = fs3.fdateLastAccess = fs3.fdateLastWrite = fdate; fs3.ftimeCreation = fs3.ftimeLastAccess = fs3.ftimeLastWrite = ftime; ulrc = DosSetPathInfo( ( PCSZ ) szNameOS, FIL_STANDARD, &fs3, sizeof( fs3 ), DSPI_WRTTHRU ); } if( pszFree ) hb_xfree( pszFree ); } #else { hb_fsSetAttr( szName, ufi.external_fa ); } #endif return iResult; }
/* internal routine to do buffer skips. Passing a positive value performs a downward skip, a negative number does an upward skip. Passing 0 skips to the end of file. Returns a long indicating the number of records skipped */ static long _ft_skip( long iRecs ) { PFT_TEXT ft_text = ( PFT_TEXT ) hb_stackGetTSD( &s_ft_text ); HB_ISIZ iByteCount; HB_ISIZ iBytesRead, iBytesRemaining; char * cPtr; long iSkipped = 0; char * cBuff = ( char * ) hb_xgrab( BUFFSIZE ); HB_FOFFSET fpOffset = ft_text->offset[ ft_text->area ]; ft_text->isBof[ ft_text->area ] = HB_FALSE; ft_text->isEof[ ft_text->area ] = HB_FALSE; ft_text->error[ ft_text->area ] = 0; /* iRecs is zero if they want to find the EOF, start a top of file */ if( iRecs == 0 ) { fpOffset = 0; ft_text->recno[ ft_text->area ] = 1; } if( iRecs >= 0 ) { do { cPtr = cBuff; /* read a chunk */ if( ( iBytesRead = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], cBuff, BUFFSIZE, fpOffset ) ) ) == 0 ) { /* buffer is empty thus EOF, set vars and quit */ ft_text->isEof[ ft_text->area ] = HB_TRUE; ft_text->last_rec[ ft_text->area ] = ft_text->recno[ ft_text->area ]; ft_text->last_off[ ft_text->area ] = ft_text->offset[ ft_text->area ]; ft_text->error[ ft_text->area ] = hb_fsError(); break; } iBytesRemaining = iBytesRead; /* parse the buffer while there's still stuff in it */ do { /* get count of chars in this line */ iByteCount = _findeol( cPtr, iBytesRemaining, NULL ); if( iByteCount > 0 && iByteCount != iBytesRemaining ) { /* found an EOL, iByteCount points to first char of next record */ iBytesRemaining -= iByteCount; fpOffset += iByteCount; cPtr += iByteCount; ft_text->offset[ ft_text->area ] = fpOffset; ft_text->recno[ ft_text->area ]++; iSkipped++; if( iRecs && ( iSkipped == iRecs ) ) iBytesRemaining = iBytesRead = 0; } else { /* no more EOLs in this buffer, or EOL is last chars in the buffer */ /* check for EOF */ if( iBytesRead != BUFFSIZE ) { /* buffer was not full, thus EOF, set vars and quit */ iBytesRemaining = 0; ft_text->last_rec[ ft_text->area ] = ft_text->recno[ ft_text->area ]; ft_text->last_off[ ft_text->area ] = ft_text->offset[ ft_text->area ]; if( iRecs ) ft_text->isEof[ ft_text->area ] = HB_TRUE; } else { /* buffer was full, so probably not EOF, but maybe EOL straddled end of buffer, so back up pointer a bit before doing the next read */ fpOffset = hb_fileSeek( ft_text->handles[ ft_text->area ], 0, FS_RELATIVE ) - 1; iBytesRemaining = 0; } } } while( iBytesRemaining > 0 ); } while( iBytesRead == BUFFSIZE ); } else { /* skip backwards */ iRecs = -iRecs; if( ft_text->recno[ ft_text->area ] > iRecs ) { do { /* calc offset to read area of file ahead of current pointer */ fpOffset = HB_MAX( ft_text->offset[ ft_text->area ] - BUFFSIZE, 0 ); /* read a chunk */ if( ( iBytesRead = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], cBuff, BUFFSIZE, fpOffset ) ) ) == 0 ) { /* buffer is empty thus file is zero len, set vars and quit */ ft_text->isBof[ ft_text->area ] = HB_TRUE; ft_text->isEof[ ft_text->area ] = HB_TRUE; ft_text->recno[ ft_text->area ] = 0; ft_text->offset[ ft_text->area ] = 0; ft_text->last_rec[ ft_text->area ] = 0; ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* set pointer within buffer */ iBytesRemaining = ( int ) ( ft_text->offset[ ft_text->area ] - fpOffset ); cPtr = cBuff + iBytesRemaining; /* parse the buffer while there's still stuff in it */ do { /* get count of chars in this line */ iByteCount = _findbol( cPtr, iBytesRemaining ); if( iByteCount > 0 ) { /* found an EOL, iByteCount points to first char of next record */ iBytesRemaining -= iByteCount; ft_text->offset[ ft_text->area ] -= iByteCount; cPtr -= iByteCount; fpOffset = ft_text->offset[ ft_text->area ]; ft_text->recno[ ft_text->area ]--; iSkipped++; if( iSkipped == iRecs ) iBytesRemaining = iBytesRead = 0; } else { /* no more EOLs in this buffer so we're either at BOF or record crosses buffer boundary */ /* check for BOF */ if( iBytesRead != BUFFSIZE ) { /* buffer was not full, thus BOF, set vars and quit */ iBytesRemaining = 0; ft_text->offset[ ft_text->area ] = 0; ft_text->recno[ ft_text->area ] = 1; ft_text->isBof[ ft_text->area ] = HB_TRUE; } else { /* buffer was full, so not BOF */ iBytesRemaining = 0; } } } while( iBytesRemaining > 0 ); } while( fpOffset > 0 && iBytesRead == BUFFSIZE ); } else { ft_text->offset[ ft_text->area ] = 0; ft_text->recno[ ft_text->area ] = 1; ft_text->isBof[ ft_text->area ] = HB_TRUE; } } hb_xfree( cBuff ); return iSkipped; }
static HB_BOOL hb_copyfile( const char * szSource, const char * szDest, PHB_ITEM pBlock ) { HB_BOOL bRetVal = HB_FALSE; HB_FHANDLE fhndSource; HB_TRACE( HB_TR_DEBUG, ( "hb_copyfile(%s, %s)", szSource, szDest ) ); while( ( fhndSource = hb_spOpen( szSource, FO_READ | FO_SHARED | FO_PRIVATE ) ) == FS_ERROR ) { HB_USHORT uiAction = hb_errRT_BASE_Ext1( EG_OPEN, 2012, NULL, szSource, hb_fsError(), EF_CANDEFAULT | EF_CANRETRY, 0 ); if( uiAction != E_RETRY ) break; } if( fhndSource != FS_ERROR ) { HB_FHANDLE fhndDest; while( ( fhndDest = hb_spCreate( szDest, FC_NORMAL ) ) == FS_ERROR ) { HB_USHORT uiAction = hb_errRT_BASE_Ext1( EG_CREATE, 2012, NULL, szDest, hb_fsError(), EF_CANDEFAULT | EF_CANRETRY, 0 ); if( uiAction != E_RETRY ) break; } if( fhndDest != FS_ERROR ) { #if defined( HB_OS_UNIX ) struct stat struFileInfo; int iSuccess = fstat( fhndSource, &struFileInfo ); #endif HB_BYTE * buffer = ( HB_BYTE * ) hb_xgrab( BUFFER_SIZE ); HB_SIZE nRead; bRetVal = HB_TRUE; if( hb_itemType( pBlock ) != HB_IT_BLOCK ) pBlock = NULL; while( ( nRead = hb_fsReadLarge( fhndSource, buffer, BUFFER_SIZE ) ) != 0 ) { while( hb_fsWriteLarge( fhndDest, buffer, nRead ) != nRead ) { HB_USHORT uiAction = hb_errRT_BASE_Ext1( EG_WRITE, 2016, NULL, szDest, hb_fsError(), EF_CANDEFAULT | EF_CANRETRY, 0 ); if( uiAction != E_RETRY ) { bRetVal = HB_FALSE; break; } } if( pBlock ) { PHB_ITEM pCnt = hb_itemPutNS( NULL, nRead ); hb_vmEvalBlockV( pBlock, 1, pCnt ); hb_itemRelease( pCnt ); } } hb_xfree( buffer ); #if defined( HB_OS_UNIX ) if( iSuccess == 0 ) fchmod( fhndDest, struFileInfo.st_mode ); #endif hb_fsClose( fhndDest ); } hb_fsClose( fhndSource ); } return bRetVal; }
HB_BOOL hb_fsLink( const char * pszExisting, const char * pszNewFile ) { HB_BOOL fResult; if( pszExisting && pszNewFile ) { hb_vmUnlock(); #if defined( HB_OS_WIN ) && ! defined( HB_OS_WIN_CE ) { typedef BOOL ( WINAPI * _HB_CREATEHARDLINK )( LPCTSTR, LPCTSTR, LPSECURITY_ATTRIBUTES ); static _HB_CREATEHARDLINK s_pCreateHardLink = NULL; if( ! s_pCreateHardLink ) s_pCreateHardLink = ( _HB_CREATEHARDLINK ) GetProcAddress( GetModuleHandle( TEXT( "kernel32.dll" ) ), HB_WINAPI_FUNCTION_NAME( "CreateHardLink" ) ); if( s_pCreateHardLink ) { LPCTSTR lpFileName, lpExistingFileName; LPTSTR lpFileNameFree, lpExistingFileNameFree; lpFileName = HB_FSNAMECONV( pszNewFile, &lpFileNameFree ); lpExistingFileName = HB_FSNAMECONV( pszExisting, &lpExistingFileNameFree ); fResult = s_pCreateHardLink( lpFileName, lpExistingFileName, NULL ) != 0; hb_fsSetIOError( fResult, 0 ); hb_fsSetFError( hb_fsError() ); if( lpFileNameFree ) hb_xfree( lpFileNameFree ); if( lpExistingFileNameFree ) hb_xfree( lpExistingFileNameFree ); } else { hb_fsSetFError( 1 ); fResult = HB_FALSE; } } #elif defined( HB_OS_UNIX ) { char * pszExistingFree; char * pszNewFileFree; pszExisting = hb_fsNameConv( pszExisting, &pszExistingFree ); pszNewFile = hb_fsNameConv( pszNewFile, &pszNewFileFree ); fResult = ( link( pszExisting, pszNewFile ) == 0 ); hb_fsSetIOError( fResult, 0 ); hb_fsSetFError( hb_fsError() ); if( pszExistingFree ) hb_xfree( pszExistingFree ); if( pszNewFileFree ) hb_xfree( pszNewFileFree ); } #else { hb_fsSetFError( 1 ); fResult = HB_FALSE; } #endif hb_vmLock(); } else { hb_fsSetFError( 2 ); fResult = HB_FALSE; } return fResult; }
/* NOTE: Caller must free the pointer, if not NULL */ char * hb_fsLinkRead( const char * pszFile ) { char * pszLink = NULL; if( pszFile ) { hb_vmUnlock(); #if defined( HB_OS_WIN ) && ! defined( HB_OS_WIN_CE ) { typedef DWORD ( WINAPI * _HB_GETFINALPATHNAMEBYHANDLE )( HANDLE, LPTSTR, DWORD, DWORD ); static _HB_GETFINALPATHNAMEBYHANDLE s_pGetFinalPathNameByHandle = NULL; #ifndef VOLUME_NAME_DOS #define VOLUME_NAME_DOS 0x0 #endif #ifndef VOLUME_NAME_GUID #define VOLUME_NAME_GUID 0x1 #endif #ifndef VOLUME_NAME_NT #define VOLUME_NAME_NT 0x2 #endif #ifndef VOLUME_NAME_NONE #define VOLUME_NAME_NONE 0x4 #endif #ifndef FILE_NAME_NORMALIZED #define FILE_NAME_NORMALIZED 0x0 #endif #ifndef FILE_NAME_OPENED #define FILE_NAME_OPENED 0x8 #endif if( ! s_pGetFinalPathNameByHandle ) s_pGetFinalPathNameByHandle = ( _HB_GETFINALPATHNAMEBYHANDLE ) GetProcAddress( GetModuleHandle( TEXT( "kernel32.dll" ) ), HB_WINAPI_FUNCTION_NAME( "GetFinalPathNameByHandle" ) ); if( s_pGetFinalPathNameByHandle ) { LPCTSTR lpFileName; LPTSTR lpFileNameFree; HANDLE hFile; DWORD dwAttr; HB_BOOL fDir; lpFileName = HB_FSNAMECONV( pszFile, &lpFileNameFree ); dwAttr = GetFileAttributes( lpFileName ); fDir = ( dwAttr != INVALID_FILE_ATTRIBUTES ) && ( dwAttr & FILE_ATTRIBUTE_DIRECTORY ); hFile = CreateFile( lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, fDir ? ( FILE_ATTRIBUTE_DIRECTORY | FILE_FLAG_BACKUP_SEMANTICS ) : FILE_ATTRIBUTE_NORMAL, NULL ); if( hFile == INVALID_HANDLE_VALUE ) { hb_fsSetIOError( HB_FALSE, 0 ); hb_fsSetFError( hb_fsError() ); } else { DWORD size; TCHAR lpLink[ HB_PATH_MAX ]; size = s_pGetFinalPathNameByHandle( hFile, lpLink, HB_PATH_MAX, VOLUME_NAME_DOS ); if( size < HB_PATH_MAX ) { if( size > 0 ) { lpLink[ size ] = TEXT( '\0' ); pszLink = HB_OSSTRDUP( lpLink ); } hb_fsSetIOError( HB_TRUE, 0 ); hb_fsSetFError( hb_fsError() ); } else hb_fsSetFError( 1 ); } if( lpFileNameFree ) hb_xfree( lpFileNameFree ); } else hb_fsSetFError( 1 ); } #elif defined( HB_OS_UNIX ) { char * pszFileFree; size_t size; pszFile = hb_fsNameConv( pszFile, &pszFileFree ); pszLink = ( char * ) hb_xgrab( HB_PATH_MAX + 1 ); size = readlink( pszFile, pszLink, HB_PATH_MAX ); hb_fsSetIOError( size != ( size_t ) -1, 0 ); hb_fsSetFError( hb_fsError() ); if( size == ( size_t ) -1 ) { hb_xfree( pszLink ); pszLink = NULL; } else { pszLink[ size ] = '\0'; /* Convert from OS codepage */ pszLink = ( char * ) hb_osDecodeCP( pszLink, NULL, NULL ); } if( pszFileFree ) hb_xfree( pszFileFree ); } #else { hb_fsSetFError( 1 ); } #endif hb_vmLock(); } else hb_fsSetFError( 2 ); return pszLink; }
HB_BOOL hb_fsLinkSym( const char * pszTarget, const char * pszNewFile ) { HB_BOOL fResult; if( pszTarget && pszNewFile ) { hb_vmUnlock(); #if defined( HB_OS_WIN ) && ! defined( HB_OS_WIN_CE ) { typedef BOOL ( WINAPI * _HB_CREATESYMBOLICLINK )( LPCTSTR, LPCTSTR, DWORD ); static _HB_CREATESYMBOLICLINK s_pCreateSymbolicLink = NULL; #ifndef SYMBOLIC_LINK_FLAG_DIRECTORY #define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1 #endif if( ! s_pCreateSymbolicLink ) s_pCreateSymbolicLink = ( _HB_CREATESYMBOLICLINK ) GetProcAddress( GetModuleHandle( TEXT( "kernel32.dll" ) ), HB_WINAPI_FUNCTION_NAME( "CreateSymbolicLink" ) ); if( s_pCreateSymbolicLink ) { LPCTSTR lpSymlinkFileName, lpTargetFileName; LPTSTR lpSymlinkFileNameFree, lpTargetFileNameFree; DWORD dwAttr; HB_BOOL fDir; lpSymlinkFileName = HB_FSNAMECONV( pszNewFile, &lpSymlinkFileNameFree ); lpTargetFileName = HB_FSNAMECONV( pszTarget, &lpTargetFileNameFree ); dwAttr = GetFileAttributes( lpTargetFileName ); fDir = ( dwAttr != INVALID_FILE_ATTRIBUTES ) && ( dwAttr & FILE_ATTRIBUTE_DIRECTORY ); fResult = s_pCreateSymbolicLink( lpSymlinkFileName, lpTargetFileName, fDir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0 ) != 0; hb_fsSetIOError( fResult, 0 ); hb_fsSetFError( hb_fsError() ); if( lpSymlinkFileNameFree ) hb_xfree( lpSymlinkFileNameFree ); if( lpTargetFileNameFree ) hb_xfree( lpTargetFileNameFree ); } else { hb_fsSetFError( 1 ); fResult = HB_FALSE; } } #elif defined( HB_OS_UNIX ) { char * pszTargetFree; char * pszNewFileFree; pszTarget = hb_fsNameConv( pszTarget, &pszTargetFree ); pszNewFile = hb_fsNameConv( pszNewFile, &pszNewFileFree ); fResult = ( symlink( pszTarget, pszNewFile ) == 0 ); hb_fsSetIOError( fResult, 0 ); hb_fsSetFError( hb_fsError() ); if( pszTargetFree ) hb_xfree( pszTargetFree ); if( pszNewFileFree ) hb_xfree( pszNewFileFree ); } #else { hb_fsSetFError( 1 ); fResult = HB_FALSE; } #endif hb_vmLock(); } else { hb_fsSetFError( 2 ); fResult = HB_FALSE; } return fResult; }
/* * Open a data store in the WorkArea. */ static HB_ERRCODE hb_sdfOpen( SDFAREAP pArea, LPDBOPENINFO pOpenInfo ) { PHB_ITEM pError = NULL; PHB_FNAME pFileName; HB_ERRCODE errCode; HB_USHORT uiFlags; HB_BOOL fRetry; char szFileName[ HB_PATH_MAX ]; char szAlias[ HB_RDD_MAX_ALIAS_LEN + 1 ]; HB_TRACE( HB_TR_DEBUG, ( "hb_sdfOpen(%p,%p)", pArea, pOpenInfo ) ); pArea->fShared = HB_TRUE; /* pOpenInfo->fShared; */ pArea->fReadonly = HB_TRUE; /* pOpenInfo->fReadonly; */ if( pOpenInfo->cdpId ) { pArea->area.cdPage = hb_cdpFindExt( pOpenInfo->cdpId ); if( ! pArea->area.cdPage ) pArea->area.cdPage = hb_vmCDP(); } else pArea->area.cdPage = hb_vmCDP(); uiFlags = ( pArea->fReadonly ? FO_READ : FO_READWRITE ) | ( pArea->fShared ? FO_DENYNONE : FO_EXCLUSIVE ); pFileName = hb_fsFNameSplit( pOpenInfo->abName ); /* Add default file name extension if necessary */ if( hb_setGetDefExtension() && ! pFileName->szExtension ) { PHB_ITEM pFileExt = hb_itemPutC( NULL, NULL ); SELF_INFO( &pArea->area, DBI_TABLEEXT, pFileExt ); pFileName->szExtension = hb_itemGetCPtr( pFileExt ); hb_fsFNameMerge( szFileName, pFileName ); hb_itemRelease( pFileExt ); } else { hb_strncpy( szFileName, pOpenInfo->abName, sizeof( szFileName ) - 1 ); } /* Create default alias if necessary */ if( ! pOpenInfo->atomAlias && pFileName->szName ) { const char * szName = strrchr( pFileName->szName, ':' ); if( szName == NULL ) szName = pFileName->szName; else ++szName; hb_strncpyUpperTrim( szAlias, szName, sizeof( szAlias ) - 1 ); pOpenInfo->atomAlias = szAlias; } hb_xfree( pFileName ); /* Try open */ do { pArea->pFile = hb_fileExtOpen( szFileName, NULL, uiFlags | FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME | FXO_NOSEEKPOS, NULL, pError ); if( ! pArea->pFile ) { if( ! pError ) { pError = hb_errNew(); hb_errPutGenCode( pError, EG_OPEN ); hb_errPutSubCode( pError, EDBF_OPEN_DBF ); hb_errPutOsCode( pError, hb_fsError() ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_OPEN ) ); hb_errPutFileName( pError, szFileName ); hb_errPutFlags( pError, EF_CANRETRY | EF_CANDEFAULT ); } fRetry = ( SELF_ERROR( &pArea->area, pError ) == E_RETRY ); } else fRetry = HB_FALSE; } while( fRetry ); if( pError ) hb_itemRelease( pError ); if( ! pArea->pFile ) return HB_FAILURE; errCode = SUPER_OPEN( &pArea->area, pOpenInfo ); if( errCode != HB_SUCCESS ) { SELF_CLOSE( &pArea->area ); return HB_FAILURE; } hb_sdfInitArea( pArea, szFileName ); /* Position cursor at the first record */ return SELF_GOTOP( &pArea->area ); }
static PHB_FILE s_fileExtOpen( PHB_FILE_FUNCS pFuncs, const char * pszFileName, const char * pDefExt, HB_FATTR nExFlags, const char * pPaths, PHB_ITEM pError ) { PHB_FILE pFile = NULL; #if defined( HB_OS_UNIX ) HB_BOOL fSeek = HB_FALSE; # if defined( HB_USE_LARGEFILE64 ) struct stat64 statbuf; # else struct stat statbuf; # endif #endif HB_BOOL fResult, fShared; int iMode; HB_FHANDLE hFile; char * pszFile; HB_SYMBOL_UNUSED( pFuncs ); fShared = ( nExFlags & ( FO_DENYREAD | FO_DENYWRITE | FO_EXCLUSIVE ) ) == 0; iMode = ( int ) ( nExFlags & ( FO_READ | FO_WRITE | FO_READWRITE ) ); pszFile = hb_fsExtName( pszFileName, pDefExt, nExFlags, pPaths ); hb_vmUnlock(); #if ! defined( HB_OS_UNIX ) fResult = HB_TRUE; #else # if defined( HB_USE_SHARELOCKS ) && ! defined( HB_USE_BSDLOCKS ) if( nExFlags & FXO_SHARELOCK ) { if( iMode == FO_WRITE && fShared ) { if( access( ( char * ) pszFile, R_OK ) == 0 || access( ( char * ) pszFile, F_OK ) != 0 ) { nExFlags = ( nExFlags ^ FO_WRITE ) | FO_READWRITE; iMode = FO_READWRITE; } else nExFlags ^= FXO_SHARELOCK; } else if( iMode == FO_READ && ! fShared ) { nExFlags &= ~ ( HB_FATTR ) ( FO_DENYREAD | FO_DENYWRITE | FO_EXCLUSIVE ); fShared = HB_TRUE; } } # endif hb_threadEnterCriticalSection( &s_fileMtx ); # if defined( HB_USE_LARGEFILE64 ) fResult = stat64( ( char * ) pszFile, &statbuf ) == 0; # else fResult = stat( ( char * ) pszFile, &statbuf ) == 0; # endif hb_fsSetIOError( fResult, 0 ); if( fResult ) { pFile = hb_fileFind( ( HB_ULONG ) statbuf.st_dev, ( HB_ULONG ) statbuf.st_ino ); if( pFile ) { if( ! fShared || ! pFile->shared || ( nExFlags & FXO_TRUNCATE ) != 0 ) { fResult = HB_FALSE; pFile = NULL; } else if( pFile->mode != FO_READWRITE && pFile->mode != iMode ) { iMode = FO_READWRITE; pFile = NULL; } else { pFile->used++; if( ( nExFlags & FXO_NOSEEKPOS ) == 0 ) { # if defined( HB_OS_VXWORKS ) fSeek = ! S_ISFIFO( statbuf.st_mode ); # else fSeek = ! S_ISFIFO( statbuf.st_mode ) && ! S_ISSOCK( statbuf.st_mode ); # endif } } } } else fResult = HB_TRUE; if( fResult && pFile == NULL ) #endif /* HB_OS_UNIX */ { hFile = hb_fsExtOpen( pszFile, NULL, nExFlags & ~ ( HB_FATTR ) ( FXO_DEFAULTS | FXO_COPYNAME ), NULL, NULL ); if( hFile != FS_ERROR ) { HB_ULONG device = 0, inode = 0; #if ! defined( HB_OS_UNIX ) hb_threadEnterCriticalSection( &s_fileMtx ); #else # if defined( HB_USE_LARGEFILE64 ) if( fstat64( hFile, &statbuf ) == 0 ) # else if( fstat( hFile, &statbuf ) == 0 ) # endif { device = ( HB_ULONG ) statbuf.st_dev; inode = ( HB_ULONG ) statbuf.st_ino; if( ( nExFlags & FXO_NOSEEKPOS ) == 0 ) { # if defined( HB_OS_VXWORKS ) fSeek = ! S_ISFIFO( statbuf.st_mode ); # else fSeek = ! S_ISFIFO( statbuf.st_mode ) && ! S_ISSOCK( statbuf.st_mode ); # endif } } #endif /* HB_OS_UNIX */ pFile = hb_fileNew( hFile, fShared, iMode, device, inode, HB_TRUE ); if( pFile->hFile != hFile ) { if( pFile->mode != FO_READWRITE && iMode == FO_READWRITE ) { HB_FHANDLE hTemp = pFile->hFileRO; pFile->hFileRO = pFile->hFile; pFile->hFile = hFile; pFile->mode = iMode; hFile = hTemp; } if( ! fShared || ! pFile->shared || pFile->mode != FO_READWRITE ) { fResult = HB_FALSE; if( pFile->hFileRO == FS_ERROR && pFile->uiLocks != 0 ) { pFile->hFileRO = hFile; hFile = FS_ERROR; } } if( pFile->uiLocks == 0 ) { #if ! defined( HB_USE_SHARELOCKS ) || defined( HB_USE_BSDLOCKS ) if( pFile->hFileRO != FS_ERROR ) { hb_fsClose( pFile->hFileRO ); pFile->hFileRO = FS_ERROR; } #endif if( hFile != FS_ERROR ) { hb_fsClose( hFile ); hFile = FS_ERROR; #if defined( HB_USE_SHARELOCKS ) && ! defined( HB_USE_BSDLOCKS ) /* TOFIX: possible race condition */ hb_fsLockLarge( pFile->hFile, HB_SHARELOCK_POS, HB_SHARELOCK_SIZE, FL_LOCK | FLX_SHARED ); #endif } } if( !fResult ) { if( pFile ) { --pFile->used; pFile = NULL; } if( hFile != FS_ERROR ) { /* TOFIX: possible race condition in MT mode, * close() is not safe due to existing locks * which are removed. */ hb_fsClose( hFile ); } } } #if ! defined( HB_OS_UNIX ) hb_threadLeaveCriticalSection( &s_fileMtx ); #endif } } #if defined( HB_OS_UNIX ) hb_threadLeaveCriticalSection( &s_fileMtx ); if( pFile && fSeek ) pFile = hb_fileposNew( pFile ); #endif if( ! fResult ) hb_fsSetError( ( nExFlags & FXO_TRUNCATE ) ? 5 : 32 ); if( ( nExFlags & FXO_COPYNAME ) != 0 && pFile ) hb_strncpy( ( char * ) HB_UNCONST( pszFileName ), pszFile, HB_PATH_MAX - 1 ); if( pError ) { hb_errPutFileName( pError, pszFile ); if( ! fResult ) { hb_errPutOsCode( pError, hb_fsError() ); hb_errPutGenCode( pError, ( HB_ERRCODE ) ( ( nExFlags & FXO_TRUNCATE ) ? EG_CREATE : EG_OPEN ) ); } } hb_xfree( pszFile ); hb_vmLock(); return pFile; }
HB_BOOL hb_fsCopy( const char * pszSource, const char * pszDest ) { HB_ERRCODE errCode; HB_BOOL bRetVal; HB_FHANDLE fhndSource; HB_FHANDLE fhndDest; /* TODO: Change to use hb_fileExtOpen() */ if( ( fhndSource = hb_fsExtOpen( pszSource, NULL, FO_READ | FXO_SHARELOCK, NULL, NULL ) ) != FS_ERROR ) { /* TODO: Change to use hb_fileExtOpen() */ if( ( fhndDest = hb_fsExtOpen( pszDest, NULL, FXO_TRUNCATE | FO_READWRITE | FO_EXCLUSIVE | FXO_SHARELOCK, NULL, NULL ) ) != FS_ERROR ) { #if defined( HB_OS_UNIX ) struct stat struFileInfo; int iSuccess = fstat( fhndSource, &struFileInfo ); #endif HB_SIZE nBytesRead; void * pbyBuffer = hb_xgrab( HB_FSCOPY_BUFFERSIZE ); for( ;; ) { if( ( nBytesRead = hb_fsReadLarge( fhndSource, pbyBuffer, HB_FSCOPY_BUFFERSIZE ) ) > 0 ) { if( nBytesRead != hb_fsWriteLarge( fhndDest, pbyBuffer, nBytesRead ) ) { errCode = hb_fsError(); bRetVal = HB_FALSE; break; } } else { errCode = hb_fsError(); bRetVal = ( errCode == 0 ); break; } } hb_xfree( pbyBuffer ); #if defined( HB_OS_UNIX ) if( iSuccess == 0 ) fchmod( fhndDest, struFileInfo.st_mode ); #endif hb_fsClose( fhndDest ); } else { errCode = hb_fsError(); bRetVal = HB_FALSE; } hb_fsClose( fhndSource ); } else { errCode = hb_fsError(); bRetVal = HB_FALSE; } hb_fsSetFError( errCode ); return bRetVal; }
/* * Create a data store in the specified WorkArea. */ static HB_ERRCODE hb_sdfCreate( SDFAREAP pArea, LPDBOPENINFO pCreateInfo ) { HB_ERRCODE errCode; PHB_FNAME pFileName; PHB_ITEM pError = NULL; HB_BOOL fRetry; char szFileName[ HB_PATH_MAX ]; HB_TRACE( HB_TR_DEBUG, ( "hb_sdfCreate(%p,%p)", pArea, pCreateInfo ) ); pArea->fShared = HB_FALSE; /* pCreateInfo->fShared; */ pArea->fReadonly = HB_FALSE; /* pCreateInfo->fReadonly */ if( pCreateInfo->cdpId ) { pArea->area.cdPage = hb_cdpFindExt( pCreateInfo->cdpId ); if( ! pArea->area.cdPage ) pArea->area.cdPage = hb_vmCDP(); } else pArea->area.cdPage = hb_vmCDP(); pFileName = hb_fsFNameSplit( pCreateInfo->abName ); if( hb_setGetDefExtension() && ! pFileName->szExtension ) { PHB_ITEM pItem = hb_itemPutC( NULL, NULL ); SELF_INFO( &pArea->area, DBI_TABLEEXT, pItem ); pFileName->szExtension = hb_itemGetCPtr( pItem ); hb_fsFNameMerge( szFileName, pFileName ); hb_itemRelease( pItem ); } else { hb_strncpy( szFileName, pCreateInfo->abName, sizeof( szFileName ) - 1 ); } hb_xfree( pFileName ); /* Try create */ do { pArea->pFile = hb_fileExtOpen( szFileName, NULL, FO_READWRITE | FO_EXCLUSIVE | FXO_TRUNCATE | FXO_DEFAULTS | FXO_SHARELOCK | FXO_COPYNAME | FXO_NOSEEKPOS, NULL, pError ); if( ! pArea->pFile ) { if( ! pError ) { pError = hb_errNew(); hb_errPutGenCode( pError, EG_CREATE ); hb_errPutSubCode( pError, EDBF_CREATE_DBF ); hb_errPutOsCode( pError, hb_fsError() ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_CREATE ) ); hb_errPutFileName( pError, szFileName ); hb_errPutFlags( pError, EF_CANRETRY | EF_CANDEFAULT ); } fRetry = ( SELF_ERROR( &pArea->area, pError ) == E_RETRY ); } else fRetry = HB_FALSE; } while( fRetry ); if( pError ) hb_itemRelease( pError ); if( ! pArea->pFile ) return HB_FAILURE; errCode = SUPER_CREATE( &pArea->area, pCreateInfo ); if( errCode != HB_SUCCESS ) { SELF_CLOSE( &pArea->area ); return errCode; } hb_sdfInitArea( pArea, szFileName ); /* Position cursor at the first record */ return SELF_GOTOP( &pArea->area ); }