From 07960b8b7d80396b71b269bdf76b040fe906fcfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Czerpak?= Date: Wed, 12 Mar 2014 00:18:12 +0100 Subject: [PATCH] 2014-03-12 00:18 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * src/rdd/hbdbsort.c * casting * src/rtl/filesys.c + added additional method to extract file time in MS-Windows version of hb_fsGetFileTime() * include/hbapifs.h * src/rtl/vfile.c + added C functions to manipulate PRG level hb_vf*() file pointer items * include/hbapifs.h * src/rtl/filebuf.c + added C functions to create/extract file IO handle from raw file handle + added C functions to check if file IO handle points to local file * contrib/hbmzip/mzip.c * changed hb_fs*() API to hb_file*() API ! fixed file time setting in *nix builds --- ChangeLog.txt | 21 ++ contrib/hbmzip/mzip.c | 492 +++++++++++++++++++++++------------------- include/hbapifs.h | 12 +- src/rdd/hbdbsort.c | 6 +- src/rtl/filebuf.c | 51 +++++ src/rtl/filesys.c | 31 ++- src/rtl/vfile.c | 32 ++- 7 files changed, 406 insertions(+), 239 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 807ce7fb58..cc8db1a086 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -10,6 +10,27 @@ * Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment */ +2014-03-12 00:18 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) + * src/rdd/hbdbsort.c + * casting + + * src/rtl/filesys.c + + added additional method to extract file time in MS-Windows version of + hb_fsGetFileTime() + + * include/hbapifs.h + * src/rtl/vfile.c + + added C functions to manipulate PRG level hb_vf*() file pointer items + + * include/hbapifs.h + * src/rtl/filebuf.c + + added C functions to create/extract file IO handle from raw file handle + + added C functions to check if file IO handle points to local file + + * contrib/hbmzip/mzip.c + * changed hb_fs*() API to hb_file*() API + ! fixed file time setting in *nix builds + 2014-03-11 09:38 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * include/hbdbsort.h * include/hbrddcdx.h diff --git a/contrib/hbmzip/mzip.c b/contrib/hbmzip/mzip.c index 88273fa58a..9c19217fbb 100644 --- a/contrib/hbmzip/mzip.c +++ b/contrib/hbmzip/mzip.c @@ -158,6 +158,29 @@ static unzFile hb_unzipfileParam( int iParam ) return NULL; } +static PHB_FILE hb_fileHandleParam( int iParam, HB_BOOL * pfFree ) +{ + PHB_FILE pFile = NULL; + + * pfFree = HB_FALSE; + if( HB_ISNUM( iParam ) ) + { + HB_FHANDLE hFile = hb_numToHandle( hb_parnint( iParam ) ); + if( hFile != FS_ERROR ) + { + pFile = hb_fileFromHandle( hFile ); + * pfFree = HB_TRUE; + } + } + else + pFile = hb_fileParam( iParam ); + + if( pFile != NULL ) + return pFile; + + hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); + return NULL; +} static HB_FATTR hb_translateExtAttr( const char * szFileName, HB_FATTR ulExtAttr ) { @@ -631,19 +654,19 @@ HB_FUNC( HB_UNZIPFILECLOSE ) * */ -static HB_BOOL hb_zipGetFileInfoFromHandle( HB_FHANDLE hFile, HB_U32 * pulCRC, HB_BOOL * pfText ) +static HB_BOOL hb_zipGetFileInfoFromHandle( PHB_FILE pFile, HB_U32 * pulCRC, HB_BOOL * pfText ) { HB_BOOL fText = pfText != NULL, fResult = HB_FALSE; HB_U32 ulCRC = 0; - if( hFile != FS_ERROR ) + if( pFile != NULL ) { 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 ); + nRead = hb_fileRead( pFile, pString, HB_Z_IOBUF_SIZE, -1 ); if( nRead > 0 ) { ulCRC = crc32( ulCRC, pString, ( uInt ) nRead ); @@ -681,15 +704,17 @@ static HB_BOOL hb_zipGetFileInfoFromHandle( HB_FHANDLE hFile, HB_U32 * pulCRC, H return fResult; } -static HB_BOOL hb_zipGetFileInfo( const char * szFileName, HB_U32 * pulCRC, HB_BOOL * pfText ) +static HB_BOOL hb_zipGetFileInfo( const char * pszFileName, HB_U32 * pulCRC, HB_BOOL * pfText ) { - HB_FHANDLE hFile; - HB_BOOL fResult; + PHB_FILE pFile; + HB_BOOL fResult; - hFile = hb_fsOpen( szFileName, FO_READ ); - fResult = hb_zipGetFileInfoFromHandle( hFile, pulCRC, pfText ); - if( hFile != FS_ERROR ) - hb_fsClose( hFile ); + pFile = hb_fileExtOpen( pszFileName, NULL, + FO_READ | FO_SHARED | FO_PRIVATE | FXO_SHARELOCK, + NULL, NULL ); + fResult = hb_zipGetFileInfoFromHandle( pFile, pulCRC, pfText ); + if( pFile != NULL ) + hb_fileClose( pFile ); return fResult; } @@ -716,7 +741,7 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, const char * szFileName = hb_parc( iParamFileName ); const char * szName = hb_parc( iParamZipName ); char * pString; - HB_FHANDLE hFile; + PHB_FILE pFile; HB_SIZE nLen; HB_FATTR ulExtAttr; zip_fileinfo zfi; @@ -724,9 +749,7 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, HB_BOOL fError; HB_BOOL fText; HB_U32 ulCRC; - - uLong flags = 0; - + uLong flags = 0; void * hZipName = NULL; void * hComment = NULL; char * szZipName; @@ -737,6 +760,7 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, ulExtAttr = 0; #if defined( HB_OS_WIN ) + if( hb_fileIsLocalName( szFileName ) ) { LPTSTR lpFileNameFree; LPCTSTR lpFileName = HB_FSNAMECONV( szFileName, &lpFileNameFree ); @@ -744,10 +768,9 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, 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 ) ); + ulExtAttr = attr & ( FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | + FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY | + FILE_ATTRIBUTE_ARCHIVE ); } else fError = HB_TRUE; @@ -755,7 +778,45 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, if( lpFileNameFree ) hb_xfree( lpFileNameFree ); } + else +#elif defined( HB_OS_OS2 ) + if( hb_fileIsLocalName( szFileName ) ) + { + FILESTATUS3 fs3; + APIRET ulrc; + char * pszFree; + + ulrc = DosQueryPathInfo( ( PCSZ ) hb_fsNameConv( szFileName, &pszFree ), FIL_STANDARD, &fs3, sizeof( fs3 ) ); + + if( pszFree ) + hb_xfree( pszFree ); + + if( ulrc == NO_ERROR ) + { + if( fs3.attrFile & FILE_READONLY ) + ulExtAttr |= HB_FA_READONLY; + if( fs3.attrFile & FILE_HIDDEN ) + ulExtAttr |= HB_FA_HIDDEN; + if( fs3.attrFile & FILE_SYSTEM ) + ulExtAttr |= HB_FA_SYSTEM; + if( fs3.attrFile & FILE_DIRECTORY ) + ulExtAttr |= HB_FA_DIRECTORY; + if( fs3.attrFile & FILE_ARCHIVED ) + ulExtAttr |= HB_FA_ARCHIVE; + + 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 #elif defined( HB_OS_UNIX ) + if( hb_fileIsLocalName( szFileName ) ) { struct stat statbuf; struct tm st; @@ -805,89 +866,57 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, 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 ); - } - } + else #endif + { + HB_FATTR attr; + long lJulian, lMillisec; + + if( ! hb_fileAttrGet( szFileName, &attr ) ) + ulExtAttr = 0x81B60020; /* HB_FA_ARCHIVE | rw-rw-rw- */ + else + { +#if defined( HB_OS_UNIX ) + if( attr & HB_FA_DIRECTORY ) + ulExtAttr |= 0x40000000; + else + { + ulExtAttr |= 0x80000000; + ulExtAttr |= HB_FA_ARCHIVE; + } + /* Harbour uses the same binary values for unix access rights and + * DOS/WIN/OS2 attributes so we can use them directly + */ + ulExtAttr |= attr & ( HB_FA_RWXU | HB_FA_RWXG | HB_FA_RWXO ); +#endif + ulExtAttr |= attr & ( HB_FA_READONLY | HB_FA_HIDDEN | HB_FA_SYSTEM | + HB_FA_DIRECTORY | HB_FA_ARCHIVE ); + } + + if( hb_fileTimeGet( szFileName, &lJulian, &lMillisec ) ) + { + int iYear, iMonth, iDay; + int iHour, iMinute, iSecond, iMSec; + + hb_dateDecode( lJulian, &iYear, &iMonth, &iDay ); + hb_timeDecode( lMillisec, &iHour, &iMinute, &iSecond, &iMSec ); + + zfi.tmz_date.tm_sec = iSecond; + zfi.tmz_date.tm_min = iMinute; + zfi.tmz_date.tm_hour = iHour; + zfi.tmz_date.tm_mday = iDay; + zfi.tmz_date.tm_mon = iMonth - 1; + zfi.tmz_date.tm_year = iYear; + } + } if( fError ) return -200; +#if ! defined( HB_OS_UNIX ) + ulExtAttr = hb_translateExtAttr( szFileName, ulExtAttr ); +#endif + if( ! HB_ISCHAR( iParamZipName ) ) iParamZipName = iParamFileName; @@ -951,16 +980,18 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, } else { - hFile = hb_fsOpen( szFileName, FO_READ ); - - if( hFile != FS_ERROR ) + pFile = hb_fileExtOpen( szFileName, NULL, + FO_READ | FO_SHARED | FO_PRIVATE | FXO_SHARELOCK, + NULL, NULL ); + if( pFile != NULL ) { #if defined( HB_OS_WIN ) + if( hb_fileIsLocal( pFile ) ) { FILETIME ftutc, ft; SYSTEMTIME st; - if( GetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), NULL, NULL, &ftutc ) && + if( GetFileTime( ( HANDLE ) hb_fileHandle( pFile ), NULL, NULL, &ftutc ) && FileTimeToLocalFileTime( &ftutc, &ft ) && FileTimeToSystemTime( &ft, &st ) ) { @@ -986,14 +1017,14 @@ static int hb_zipStoreFile( zipFile hZip, int iParamFileName, int iParamZipName, if( iResult == 0 ) { pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); - while( ( nLen = hb_fsReadLarge( hFile, pString, HB_Z_IOBUF_SIZE ) ) > 0 ) + while( ( nLen = hb_fileRead( pFile, pString, HB_Z_IOBUF_SIZE, -1 ) ) > 0 ) zipWriteInFileInZip( hZip, pString, ( unsigned ) nLen ); hb_xfree( pString ); zipCloseFileInZip( hZip ); } - hb_fsClose( hFile ); + hb_fileClose( pFile ); } else iResult = -200 - hb_fsError(); @@ -1026,7 +1057,7 @@ HB_FUNC( HB_ZIPSTOREFILE ) } -static int hb_zipStoreFileHandle( zipFile hZip, HB_FHANDLE hFile, int iParamZipName, const char * szPassword, int iParamComment, HB_BOOL fUnicode ) +static int hb_zipStoreFileHandle( zipFile hZip, PHB_FILE pFile, int iParamZipName, const char * szPassword, int iParamComment, HB_BOOL fUnicode ) { HB_SIZE nLen; zip_fileinfo zfi; @@ -1041,7 +1072,7 @@ static int hb_zipStoreFileHandle( zipFile hZip, HB_FHANDLE hFile, int iParamZipN char * szZipName; const char * szComment; - if( hFile == FS_ERROR ) + if( pFile == NULL ) return -200; if( fUnicode ) @@ -1077,7 +1108,7 @@ static int hb_zipStoreFileHandle( zipFile hZip, HB_FHANDLE hFile, int iParamZipN ulCRC = 0; fText = HB_FALSE; - if( szPassword && hb_zipGetFileInfoFromHandle( hFile, &ulCRC, &fText ) ) + if( szPassword && hb_zipGetFileInfoFromHandle( pFile, &ulCRC, &fText ) ) zfi.internal_fa = fText ? 1 : 0; else /* TODO: zip.exe test: 0 for binary file, 1 for text. Does not depend on @@ -1091,8 +1122,8 @@ static int hb_zipStoreFileHandle( zipFile hZip, HB_FHANDLE hFile, int iParamZipN if( iResult == 0 ) { char * pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); - hb_fsSeek( hFile, 0, FS_SET ); - while( ( nLen = hb_fsReadLarge( hFile, pString, HB_Z_IOBUF_SIZE ) ) > 0 ) + hb_fileSeek( pFile, 0, FS_SET ); + while( ( nLen = hb_fileRead( pFile, pString, HB_Z_IOBUF_SIZE, -1 ) ) > 0 ) zipWriteInFileInZip( hZip, pString, ( unsigned ) nLen ); hb_xfree( pString ); @@ -1114,14 +1145,22 @@ static int hb_zipStoreFileHandle( zipFile hZip, HB_FHANDLE hFile, int iParamZipN /* hb_zipStoreFileHandle( hZip, fhnd, cZipName, [ cPassword ], [ cComment ], [ lUnicode ] ) --> nError */ HB_FUNC( HB_ZIPSTOREFILEHANDLE ) { - HB_FHANDLE hFile = hb_numToHandle( hb_parnint( 2 ) ); - - if( hFile != FS_ERROR && HB_ISCHAR( 3 ) ) + if( HB_ISCHAR( 3 ) ) { zipFile hZip = hb_zipfileParam( 1 ); if( hZip ) - hb_retni( hb_zipStoreFileHandle( hZip, hFile, 3, hb_parc( 4 ), 5, hb_parl( 6 ) ) ); + { + HB_BOOL fFree; + PHB_FILE pFile = hb_fileHandleParam( 2, &fFree ); + + if( pFile != NULL ) + { + hb_retni( hb_zipStoreFileHandle( hZip, pFile, 3, hb_parc( 4 ), 5, hb_parl( 6 ) ) ); + if( fFree ) + hb_fileDetach( pFile ); + } + } } else hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); @@ -1130,13 +1169,13 @@ HB_FUNC( HB_ZIPSTOREFILEHANDLE ) static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, const char * szPassword ) { - char szNameRaw[ HB_PATH_MAX * 3 ]; + char szNameRaw[ HB_PATH_MAX * 3 ]; char * szName; HB_SIZE nPos, nLen; char cSep, * pString; unz_file_info ufi; - int iResult; - HB_FHANDLE hFile; + int iResult; + PHB_FILE pFile; iResult = unzGetCurrentFileInfo( hUnzip, &ufi, szNameRaw, sizeof( szNameRaw ) - 1, NULL, 0, NULL, 0 ); @@ -1180,7 +1219,7 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, if( ( cSep == '\\' || cSep == '/' ) && nPos < nLen - 1 ) { szName[ nPos ] = '\0'; - hb_fsMkDir( szName ); + hb_fileDirMake( szName ); szName[ nPos ] = cSep; } nPos++; @@ -1188,23 +1227,25 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, if( ufi.external_fa & 0x40000000 ) /* DIRECTORY */ { - hb_fsMkDir( szName ); - iResult = UNZ_OK; + if( ! hb_fileDirMake( szName ) ) + iResult = -200 - hb_fsError(); } else { - hFile = hb_fsCreate( szName, FC_NORMAL ); - - if( hFile != FS_ERROR ) + pFile = hb_fileExtOpen( szName, NULL, + FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE | + FXO_TRUNCATE | FXO_SHARELOCK, NULL, NULL ); + if( pFile != NULL ) { 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_fileWrite( pFile, pString, ( HB_SIZE ) iResult, -1 ); hb_xfree( pString ); #if defined( HB_OS_WIN ) + if( hb_fileIsLocal( pFile ) ) { FILETIME ftutc, ft; SYSTEMTIME st; @@ -1220,12 +1261,12 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, if( SystemTimeToFileTime( &st, &ft ) && LocalFileTimeToFileTime( &ft, &ftutc ) ) { - SetFileTime( ( HANDLE ) hb_fsGetOsHandle( hFile ), &ftutc, &ftutc, &ftutc ); + SetFileTime( ( HANDLE ) hb_fileHandle( pFile ), &ftutc, &ftutc, &ftutc ); } } #endif - hb_fsClose( hFile ); + hb_fileClose( pFile ); } else iResult = -200 - hb_fsError(); @@ -1233,6 +1274,7 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, unzCloseCurrentFile( hUnzip ); #if defined( HB_OS_WIN ) + if( hb_fileIsLocalName( szName ) ) { LPTSTR lpFileNameFree; LPCTSTR lpFileName = HB_FSNAMECONV( szName, &lpFileNameFree ); @@ -1242,71 +1284,9 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, 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 - } - + else #elif defined( HB_OS_OS2 ) + if( hb_fileIsLocalName( szName ) ) { FILESTATUS3 fs3; APIRET ulrc; @@ -1350,11 +1330,67 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, const char * szFileName, if( pszFree ) hb_xfree( pszFree ); } -#else + else +#elif defined( HB_OS_UNIX ) + if( hb_fileIsLocalName( szName ) ) { - hb_fsSetAttr( szName, ufi.external_fa ); + struct utimbuf utim; + struct tm st; + + char * pszFree; + const char * szNameOS = hb_fsNameConv( szName, &pszFree ); + + HB_FATTR ulAttr = ufi.external_fa; + + if( ( ulAttr & 0xFFFF0000 ) == 0 ) + ulAttr = hb_translateExtAttr( szName, ulAttr ); + + ( void ) 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 ) ); + 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; + + utim.actime = utim.modtime = mktime( &st ); + ( void ) utime( szNameOS, &utim ); + + if( pszFree ) + hb_xfree( pszFree ); } + else #endif + { + long lJulian, lMillisec; + HB_FATTR ulAttr = ufi.external_fa; + + lJulian = hb_dateEncode( ufi.tmu_date.tm_year, ufi.tmu_date.tm_mon + 1, + ufi.tmu_date.tm_mday ); + lMillisec = hb_timeEncode( ufi.tmu_date.tm_hour, ufi.tmu_date.tm_min, + ufi.tmu_date.tm_sec, 0 ); + hb_fileTimeSet( szName, lJulian, lMillisec ); + +#if defined( HB_OS_UNIX ) + if( ( ulAttr & 0xFFFF0000 ) == 0 ) + ulAttr = hb_translateExtAttr( szName, ulAttr ); + ulAttr &= 0x01FF0000; +#else + ulAttr &= 0xFF; +#endif + hb_fileAttrSet( szName, ulAttr ); + } hb_xfree( szName ); @@ -1372,12 +1408,12 @@ HB_FUNC( HB_UNZIPEXTRACTCURRENTFILE ) } -static int hb_unzipExtractCurrentFileToHandle( unzFile hUnzip, HB_FHANDLE hFile, const char * szPassword ) +static int hb_unzipExtractCurrentFileToHandle( unzFile hUnzip, PHB_FILE pFile, const char * szPassword ) { unz_file_info ufi; int iResult; - if( hFile == FS_ERROR ) + if( pFile == NULL ) return -200; iResult = unzGetCurrentFileInfo( hUnzip, &ufi, NULL, 0, @@ -1392,38 +1428,34 @@ static int hb_unzipExtractCurrentFileToHandle( unzFile hUnzip, HB_FHANDLE hFile, if( ! ( ufi.external_fa & 0x40000000 ) ) /* DIRECTORY */ { - if( hFile != FS_ERROR ) - { - char * pString = ( char * ) hb_xgrab( HB_Z_IOBUF_SIZE ); + 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 ); + while( ( iResult = unzReadCurrentFile( hUnzip, pString, HB_Z_IOBUF_SIZE ) ) > 0 ) + hb_fileWrite( pFile, pString, ( HB_SIZE ) iResult, -1 ); - hb_xfree( pString ); + hb_xfree( pString ); #if defined( HB_OS_WIN ) + if( hb_fileIsLocal( pFile ) ) + { + 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 ) ) { - 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 ); - } + SetFileTime( ( HANDLE ) hb_fileHandle( pFile ), &ftutc, &ftutc, &ftutc ); } -#endif } - else - iResult = -200 - hb_fsError(); +#endif } unzCloseCurrentFile( hUnzip ); @@ -1437,7 +1469,17 @@ HB_FUNC( HB_UNZIPEXTRACTCURRENTFILETOHANDLE ) unzFile hUnzip = hb_unzipfileParam( 1 ); if( hUnzip ) - hb_retni( hb_unzipExtractCurrentFileToHandle( hUnzip, hb_numToHandle( hb_parnint( 2 ) ), hb_parc( 3 ) ) ); + { + HB_BOOL fFree; + PHB_FILE pFile = hb_fileHandleParam( 2, &fFree ); + + if( pFile != NULL ) + { + hb_retni( hb_unzipExtractCurrentFileToHandle( hUnzip, pFile, hb_parc( 3 ) ) ); + if( fFree ) + hb_fileDetach( pFile ); + } + } } @@ -1446,7 +1488,7 @@ static int hb_zipDeleteFile( const char * szZipFile, const char * szFileMask ) char szTempFile[ HB_PATH_MAX ]; char szCurrFile[ HB_PATH_MAX * 3 ]; PHB_FNAME pFileName; - HB_FHANDLE hFile; + PHB_FILE pFile; unzFile hUnzip; zipFile hZip; unz_global_info ugi; @@ -1474,11 +1516,11 @@ static int hb_zipDeleteFile( const char * szZipFile, const char * szFileMask ) return UNZ_ERRNO; pFileName = hb_fsFNameSplit( szZipFile ); - hFile = hb_fsCreateTemp( pFileName->szPath, NULL, FC_NORMAL, szTempFile ); + pFile = hb_fileCreateTemp( pFileName->szPath, NULL, FC_NORMAL, szTempFile ); hZip = NULL; - if( hFile != FS_ERROR ) + if( pFile != NULL ) { - hb_fsClose( hFile ); + hb_fileClose( pFile ); hZip = zipOpen( szTempFile, APPEND_STATUS_CREATE ); } hb_xfree( pFileName ); @@ -1639,14 +1681,14 @@ static int hb_zipDeleteFile( const char * szZipFile, const char * szFileMask ) hb_xfree( pszGlobalComment ); if( iResult != UNZ_OK ) - hb_fsDelete( szTempFile ); + hb_fileDelete( szTempFile ); else { - hb_fsDelete( szZipFile ); + hb_fileDelete( szZipFile ); if( iFilesLeft == 0 ) - hb_fsDelete( szTempFile ); - else if( ! hb_fsRename( szTempFile, szZipFile ) ) + hb_fileDelete( szTempFile ); + else if( ! hb_fileRename( szTempFile, szZipFile ) ) iResult = UNZ_ERRNO; } diff --git a/include/hbapifs.h b/include/hbapifs.h index c53a520a33..e62524b97b 100644 --- a/include/hbapifs.h +++ b/include/hbapifs.h @@ -404,8 +404,18 @@ extern HB_EXPORT PHB_FILE hb_fileCreateTempEx( char * pszName, const char * pszPrefix, const char * pszExt, HB_FATTR ulAttr ); - extern HB_EXPORT PHB_FILE hb_filePOpen( const char * pszFileName, const char * pszMode ); +extern HB_EXPORT PHB_FILE hb_fileFromHandle( HB_FHANDLE hFile ); +extern HB_EXPORT HB_BOOL hb_fileDetach( PHB_FILE pFile ); +extern HB_EXPORT HB_BOOL hb_fileIsLocal( PHB_FILE pFile ); +extern HB_EXPORT HB_BOOL hb_fileIsLocalName( const char * pszFileName ); + +/* interface to PRG level hb_vf*() file pointer items */ +extern HB_EXPORT PHB_FILE hb_fileParam( int iParam ); +extern HB_EXPORT PHB_FILE hb_fileItemGet( PHB_ITEM pItem ); +extern HB_EXPORT PHB_ITEM hb_fileItemPut( PHB_ITEM pItem, PHB_FILE pFile ); +extern HB_EXPORT void hb_fileItemClear( PHB_ITEM pItem ); + /* wrapper to fopen() which calls hb_fsNameConv() */ extern HB_EXPORT FILE * hb_fopen( const char *path, const char *mode ); diff --git a/src/rdd/hbdbsort.c b/src/rdd/hbdbsort.c index 8a05d51c06..601588f484 100644 --- a/src/rdd/hbdbsort.c +++ b/src/rdd/hbdbsort.c @@ -85,11 +85,11 @@ void hb_dbQSortExit( LPDBQUICKSORT pQuickSort ) HB_BOOL hb_dbQSortAdvance( LPDBQUICKSORT pQuickSort, HB_USHORT uiCount ) { - HB_USHORT uiSize; + HB_SIZE nSize; /* Write chunk */ - uiSize = uiCount * pQuickSort->uiRecordLen; - return hb_fileWrite( pQuickSort->pFile, pQuickSort->pBuffer, uiSize, -1 ) == uiSize; + nSize = ( HB_SIZE ) uiCount * pQuickSort->uiRecordLen; + return hb_fileWrite( pQuickSort->pFile, pQuickSort->pBuffer, nSize, -1 ) == nSize; } static HB_BOOL hb_dbQSortIsLess( LPDBQUICKSORT pQuickSort, HB_ULONG ulRecNo1, HB_ULONG ulRecNo2 ) diff --git a/src/rtl/filebuf.c b/src/rtl/filebuf.c index 2d924206bc..2383291241 100644 --- a/src/rtl/filebuf.c +++ b/src/rtl/filebuf.c @@ -1223,6 +1223,57 @@ PHB_FILE hb_fileCreateTempEx( char * pszName, return pFile; } +PHB_FILE hb_fileFromHandle( HB_FHANDLE hFile ) +{ + return hb_fileNew( hFile, HB_FALSE, HB_FALSE, 0, 0, HB_FALSE ); +} + +HB_BOOL hb_fileDetach( PHB_FILE pFile ) +{ + if( pFile ) + { + if( pFile->pFuncs == s_fileMethods() ) + { + pFile->hFile = FS_ERROR; + s_fileClose( pFile ); + return HB_TRUE; + } +#if defined( HB_OS_UNIX ) + else if( pFile->pFuncs == s_fileposMethods() ) + { + PHB_FILEPOS pFilePos = ( PHB_FILEPOS ) pFile; + + pFilePos->pFile->hFile = FS_ERROR; + s_fileposClose( pFile ); + return HB_TRUE; + } +#endif + } + + return HB_FALSE; +} + +HB_BOOL hb_fileIsLocal( PHB_FILE pFile ) +{ + if( pFile ) + { +#if defined( HB_OS_UNIX ) + if( pFile->pFuncs == s_fileMethods() || + pFile->pFuncs == s_fileposMethods() ) +#else + if( pFile->pFuncs == s_fileMethods() ) +#endif + return HB_TRUE; + } + + return HB_FALSE; +} + +HB_BOOL hb_fileIsLocalName( const char * pszFileName ) +{ + return s_fileFindDrv( pszFileName ) >= 0; +} + PHB_FILE hb_filePOpen( const char * pszFileName, const char * pszMode ) { PHB_FILE pFile = NULL; diff --git a/src/rtl/filesys.c b/src/rtl/filesys.c index 10a5c38db3..af0284c783 100644 --- a/src/rtl/filesys.c +++ b/src/rtl/filesys.c @@ -1337,12 +1337,11 @@ HB_BOOL hb_fsGetFileTime( const char * pszFileName, long * plJulian, long * plMi #if defined( HB_OS_WIN ) { HB_FHANDLE hFile = hb_fsOpen( pszFileName, FO_READ | FO_SHARED ); + FILETIME ft, local_ft; + SYSTEMTIME st; if( hFile != FS_ERROR ) { - FILETIME ft, local_ft; - SYSTEMTIME st; - if( GetFileTime( DosToWinHandle( hFile ), NULL, NULL, &ft ) && FileTimeToLocalFileTime( &ft, &local_ft ) && FileTimeToSystemTime( &local_ft, &st ) ) @@ -1355,6 +1354,32 @@ HB_BOOL hb_fsGetFileTime( const char * pszFileName, long * plJulian, long * plMi hb_fsSetIOError( fResult, 0 ); hb_fsClose( hFile ); } + else + { + WIN32_FIND_DATA findFileData; + HANDLE hFindFile; + LPCTSTR lpFileName; + LPTSTR lpFree; + + lpFileName = HB_FSNAMECONV( pszFileName, &lpFree ); + hFindFile = FindFirstFile( lpFileName, &findFileData ); + if( lpFree ) + hb_xfree( lpFree ); + + if( hFindFile != INVALID_HANDLE_VALUE ) + { + if( FileTimeToLocalFileTime( &findFileData.ftLastWriteTime, &local_ft ) && + FileTimeToSystemTime( &local_ft, &st ) ) + { + *plJulian = hb_dateEncode( st.wYear, st.wMonth, st.wDay ); + *plMillisec = hb_timeEncode( st.wHour, st.wMinute, st.wSecond, st.wMilliseconds ); + + fResult = HB_TRUE; + } + hb_fsSetIOError( fResult, 0 ); + FindClose( hFindFile ); + } + } } #elif defined( HB_OS_UNIX ) || defined( HB_OS_OS2 ) || defined( HB_OS_DOS ) || defined( __GNUC__ ) { diff --git a/src/rtl/vfile.c b/src/rtl/vfile.c index 8660130afe..2f89c5ab4d 100644 --- a/src/rtl/vfile.c +++ b/src/rtl/vfile.c @@ -71,7 +71,7 @@ static const HB_GC_FUNCS s_gcFileFuncs = hb_gcDummyMark }; -static PHB_FILE hb_fileParam( int iParam ) +PHB_FILE hb_fileParam( int iParam ) { PHB_FILE * fileHolder = ( PHB_FILE * ) hb_parptrGC( &s_gcFileFuncs, iParam ); @@ -82,6 +82,29 @@ static PHB_FILE hb_fileParam( int iParam ) return NULL; } +PHB_FILE hb_fileItemGet( PHB_ITEM pItem ) +{ + PHB_FILE * fileHolder = ( PHB_FILE * ) hb_itemGetPtrGC( pItem, &s_gcFileFuncs ); + + return fileHolder ? *fileHolder : NULL; +} + +PHB_ITEM hb_fileItemPut( PHB_ITEM pItem, PHB_FILE pFile ) +{ + PHB_FILE * fileHolder = ( PHB_FILE * ) hb_gcAllocate( sizeof( PHB_FILE ), + &s_gcFileFuncs ); + * fileHolder = pFile; + return hb_itemPutPtrGC( pItem, fileHolder ); +} + +void hb_fileItemClear( PHB_ITEM pItem ) +{ + PHB_FILE * fileHolder = ( PHB_FILE * ) hb_itemGetPtrGC( pItem, &s_gcFileFuncs ); + + if( fileHolder ) + * fileHolder = NULL; +} + static PHB_FILE * hb_fileParamPtr( int iParam ) { PHB_FILE * fileHolder = ( PHB_FILE * ) hb_parptrGC( &s_gcFileFuncs, iParam ); @@ -96,12 +119,7 @@ static PHB_FILE * hb_fileParamPtr( int iParam ) static void hb_fileReturn( PHB_FILE pFile ) { if( pFile ) - { - PHB_FILE * fileHolder = ( PHB_FILE * ) hb_gcAllocate( sizeof( PHB_FILE ), - &s_gcFileFuncs ); - * fileHolder = pFile; - hb_retptrGC( fileHolder ); - } + hb_fileItemPut( hb_param( -1, HB_IT_ANY ), pFile ); else hb_ret(); }