/*
Updating applications via FTP
(c) 2008 Biel Maimo <!-- e --><a href="mailto:bmaimo@gmail.com">bmaimo@gmail.com</a><!-- e --> - bielsys.blogspot.com
FileTimes. basado sobre fuente publicacdo en foro FiveWin por Manuel Mercado
Time2Sec De contrib de Harbour HbCt
*/
#include "FiveWin.ch"
#INCLUDE "BOX.CH"
#INCLUDE "FILEIO.CH"
#INCLUDE "directry.ch"
STATIC cDirlocal //Directorio local del ejecutable
STATIC cDstZip := ""
STATIC cDirTempInstall := ""
STATIC cDirArchiv := ""
//----------------------------------------------------------------------------//
FUNCTION Main()
local cIp := ""
local cUser := ""
local cPW := ""
local cFTPFolder := "" //'/UpdFtp/'
local cZIPFile := ""
*----------------------------------------------------------
//FTP - Setup
cIp := 'ftp.asdfa-software.com'
cUser := "so"
cPW := "adfasd"
cFTPFolder := "/asdfaso/downloads/"
cZIPFile := "xCrmUpdate.zip"
*----------------------------------------------------------
//local Setup
cDstZip := "c:\x_zipUpdate"
cDirTempInstall := "c:\x_tempInstall"
cDirLocal := "c:\x_test"
cDirArchiv := "c:\x_archiv"
if lIsDir( cDirLocal ) =.F.
lMKDir( cDirLocal )
endif
memowrit( cDirLocal + "\" + "upDateRunning.txt", dtoc(date()) + " " + time() )
if lIsDir( cDstZip ) =.F.
lMKDir( cDstZip )
endif
if lIsDir( cDirTempInstall ) =.F.
lMKDir( cDirTempInstall )
endif
ChkUpdFtp( cIp, cFTPFolder , cUser, cPW ,cZIPFile )
Unzip( cDstZip, cDirTempInstall, cZIPFile )
InstallFiles( cDirTempInstall )
DELETEDIR( cDirTempInstall )
// DELETEDIR( cDirArchiv )
ferase (cDirLocal + "\" + "upDateRunning.txt")
MsgInfo('Update succesful')
RETURN NIL
//----------------------------------------------------------------------------//
function InstallFiles( cDirTempInstall )
LOCAL oDlg, oSay, lOk:=.F.
DEFINE DIALOG oDlg TITLE "Updating application" FROM 0,0 TO 10,50
@ 1.5,03 SAY oSay PROMPT "Copied bytes:" OF oDlg
oDlg:bStart := { || lOk := dir_recurs2( cDirTempInstall, oSay ), oDlg:END() }
ACTIVATE DIALOG oDlg CENTERED
RETURN NIL
//----------------------------------------------------------------------------//
function Unzip( cSrc, cDst, cZIPFile)
local DCOM
cSrc := cSrc + "\" + cZIPFile
DCOM := '7Z.exe x ' + cSrc + " -o" + cDst + " *.* -r"
WAITRUN(DCOM,1)
SYSREFRESH()
return nil
//----------------------------------------------------------------------------//
STATIC FUNCTION ChkUpdFtp( cIp, cFTPFolder, cUser, cPW, cZIPFile )
LOCAL oInternet, oFtp
LOCAL nSize2
LOCAL aFiles := {}
IF !Empty(cIP) .AND. !Empty(cFTPFolder) //.AND. File( cDirLocal + 'ActVer.exe' )
oInternet := tInternet():New()
oFTP := TFTP():New( cIp, oInternet,cUser, cPW )
IF !Empty( oFtp:hFtp)
FtpSetCurrentDirectory(oFTP:hFTP, cFTPFolder )
aFiles = oFTP:Directory( cZIPFile )
nSize2:= aFiles[1,2]
IF Len(aFiles)>0
IF MsgYesno( 'A new version is available, Download it?' )
IF fileUpdate(oFtp,Lower(aFiles[1,1]),aFiles[1,1],nSize2)
oFtp:END()
oInternet:END()
ENDIF
ENDIF
ENDIF
ENDIF
oFtp:END()
oInternet:END()
ENDIF
RETURN NIL
//----------------------------------------------------------------------------//
STATIC FUNCTION fileUpdate(oFtp,cSource,cFileName,nSize)
LOCAL oDlg, oSay, oBtnCancel, oMeter, lEnd:=.F., nAmount, lOk:=.F., lValRet:=.F.
DEFINE DIALOG oDlg TITLE "Updating application" FROM 0,0 TO 10,50
@ 0.5,03 SAY oSay PROMPT "Copied bytes:" OF oDlg
@ 02,01 METER oMeter VAR nAMount SIZE 180,20 TOTAL nSize OF oDlg
@ 03,12 BUTTON oBtnCancel PROMPT "&Cancelar" ACTION ( lEnd := .t., SysRefresh(), oDlg:End() )
oDlg:bStart := { || lOk := GetFile( cSource, nSize, oSay, oMeter, @lEnd, oDlg, oFTP ),;
oBtnCancel:SetText( "&Ok" ) ,oDlg:END() }
ACTIVATE DIALOG oDlg CENTERED
IF !lEnd .AND. lOk
lValRet:=.T.
ENDIF
RETURN lValRet
//----------------------------------------------------------------------------------------
STATIC FUNCTION GetFile( cSource, nSize, oSay, oMeter, lEnd, oDlg, oFtp )
LOCAL oFile, hTarget, lValRet:=.F.
LOCAL nBufSize,cBuffer,nBytes, nTotal:=0,nFile:=0
*----------------------------------------------------------
nBufSize := 4096
cBuffer := Space( nBufSize )
* oMeter:nTotal := nSize
hTarget := FCreate( cDstZip + '\' + cSource )
oFile := tFtpFile():New( cSource, oFtp )
oFile:OpenRead()
SysRefresh()
WHILE ( nBytes := Len( cBuffer := oFile:Read( nBufSize ) ) ) > 0 .and. ! lEnd
FWrite( hTarget, cBuffer, nBytes )
oSay:SetText( "Bytes copiados: " + ;
AllTrim( Str( nTotal += nBytes ) ) )
oMeter:Set( nTotal )
SysRefresh()
END
FClose( hTarget )
oFile:End()
RETURN nTotal == nSize
//----------------------------------------------------------------------------//
static function dir_recurs2( cPath, oSay )
local x
local aFiles := directory( cPath + '\*.*', 'D' )
local nFilCount := len( aFiles )
local cZiel := ""
local cTarget := ""
*----------------------------------------------------------
for x := 1 to nFilCount
if aFiles[ X, F_NAME ] <> '..'
if 'D' $ aFiles[ X, F_ATTR ]
makeDirectory2( cPath )
endif
if aFiles[ X, F_ATTR ] <> 'D'
cZiel := STRTRAN( cPath + "\" + aFiles[ X, F_NAME ], cDirTempInstall, cDirLocal )
cTarget := STRTRAN( cPath + "\" + aFiles[ X, F_NAME ], cDirTempInstall, cDirArchiv )
oSay:SetText( "Bytes copiados: " + aFiles[ X, F_NAME ] )
if file( cZiel ) = .t. // only if a file is present
MOVEFILE( cZiel , cTarget )
endif
IF filcopyraw( cPath + "\" + aFiles[ X, F_NAME ], cZiel )= .f.
Msginfo( "Error beim Update" + CRLF + ;
"Source " + cPath + "\" + aFiles[ X, F_NAME ]+ CRLF + ;
"Destination " + cZiel )
endif
endif
endif
if 'D' $ aFiles[ X, F_ATTR ]
if aFiles[ X, F_NAME ] <> '.'
dir_recurs2( cPath + '\' + aFiles[ X, F_NAME ], oSay )
endif
endif
next
return NIL
//----------------------------------------------------------------------------//
function makeDirectory2( cPfad )
local n, cResult := "", nTimes := StrCharCount( cPfad, "\" ) + 1
local cDst := ""
if lIsDir( cDirArchiv) = .F.
lMKDir(cDirArchiv)
endif
if lIsDir( cDirLocal) = .F.
lMKDir(cDirLocal)
endif
for n = 1 to nTimes
cResult := StrToken( cPfad, n, "\" )
if n > 2 //ohne Wurzel
cDst := cDst + "\" + cResult
if lIsDir( cDirArchiv + cDst) = .F.
lMKDir(cDirArchiv + cDst)
endif
if lIsDir( cDirLocal + cDst) = .F.
lMKDir( cDirLocal + cDst)
endif
endif
next
return nil
//----------------------------------------------------------------------------//
function filcopyraw( cSrc, cDst ) //from NagesWaraRao
local hSrc, hDst, nBytes
local nBuf := 64000
local cBuf := Space( nBuf )
local lCopied := .f.
if ( hSrc := FOpen( cSrc, 64 ) ) >= 0
if ( hDst := FCreate( cDst, 0 ) ) >= 0
do while .t.
nBytes := FRead( hSrc, @cBuf, nBuf )
if nBytes > 0
FWrite( hDst, cBuf, nBytes )
endif
if nBytes < nBuf
lCopied := .t.
exit
endif
enddo
fClose( hDst )
endif
fClose( hSrc )
endif
return lCopied
//----------------------------------------------------------------------------//
FUNCTION DELETEDIR( cDir )
LOCAL aDir, cName
LOCAL i
aDir = DIRECTORY( cDir + "\*.*", "HRD" )
FOR i = 1 TO LEN( aDir )
cName = aDir[ i, F_NAME ]
IF cName == "."; LOOP; ENDIF
IF cName == ".."; LOOP; ENDIF
cName = cDir + "\" + cName
IF "D" $ aDir[ i, F_ATTR ]
IF !DELETEDIR( cName )
RETURN .F.
ENDIF
ELSE
IF FERASE( cName ) = -1
? "File cannot be deleted " + cName + "."
RETURN .F.
ENDIF
ENDIF
NEXT
IF !LRMDIR( cDir )
? "Folder cannot be deleted " + cDir + "."
RETURN .F.
ENDIF
RETURN .T.
//----------------------------------------------------------------------------//
DLL32 FUNCTION MOVEFILE( cExistingFileName AS LPSTR, cNewFileName AS LPSTR ) AS BOOL PASCAL FROM "MoveFileA" LIB "kernel32.dll"
//----------------------------------------------------------------------------//
#pragma BEGINDUMP
#include <Windows.h>
#include <mapiwin.h>
#include <hbApi.h>
//nTime 1=Last Update, 2=Last Acces, 3=Creation, defecto last update
HB_FUNC( FILETIMES ) // params cFileName, nTime --> { nYear, nMonth, nDay, nHour, nMin, nSec }
{
LPSTR cFileName = hb_parc( 1 ) ;
int nTime = ( ISNUM( 2 ) ? hb_parni( 2 ) : 1 ) ; // defaults to 1
FILETIME ftCreate, ftAccess, ftWrite ;
SYSTEMTIME stTime ;
BOOL bRet ;
HANDLE hFile = CreateFile( cFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ) ;
if( ! hFile )
return ;
GetFileTime( (HANDLE) hFile, &ftCreate, &ftAccess, &ftWrite ) ;
switch( nTime )
{
case 1 : // last update
FileTimeToSystemTime( &ftWrite, &stTime ) ;
break ;
case 2 : // last access
FileTimeToSystemTime( &ftAccess, &stTime ) ;
break ;
case 3 : // creation
FileTimeToSystemTime( &ftCreate, &stTime ) ;
break ;
default : // last update
FileTimeToSystemTime( &ftWrite, &stTime ) ;
break ;
}
SystemTimeToTzSpecificLocalTime( NULL, &stTime, &stTime ) ;
CloseHandle( hFile ) ;
hb_reta( 6 ) ;
hb_storni( stTime.wYear, -1, 1 ) ;
hb_storni( stTime.wMonth, -1, 2 ) ;
hb_storni( stTime.wDay, -1, 3 ) ;
hb_storni( stTime.wHour, -1, 4 ) ;
hb_storni( stTime.wMinute, -1, 5 ) ;
hb_storni( stTime.wSecond, -1, 6 ) ;
}
#define FA_RDONLY 1 /* R */
#define FA_HIDDEN 2 /* H */
#define FA_SYSTEM 4 /* S */
#define FA_LABEL 8 /* V */
#define FA_DIREC 16 /* D */
#define FA_ARCH 32 /* A */
#define FA_NORMAL 0
HB_FUNC(FILESIZE)
{
LPCTSTR szFile;
DWORD dwFlags=FILE_ATTRIBUTE_ARCHIVE;
HANDLE hFind;
WIN32_FIND_DATA hFilesFind;
int iAttr;
if (hb_pcount() >=1){
szFile=hb_parc(1);
if (ISNUM(2)) {
iAttr=hb_parnl(2);
}
else{
iAttr=63;
}
if( iAttr & FA_RDONLY )
dwFlags |= FILE_ATTRIBUTE_READONLY;
if( iAttr & FA_HIDDEN )
dwFlags |= FILE_ATTRIBUTE_HIDDEN;
if( iAttr & FA_SYSTEM )
dwFlags |= FILE_ATTRIBUTE_SYSTEM;
if( iAttr & FA_NORMAL )
dwFlags |= FILE_ATTRIBUTE_NORMAL;
hFind = FindFirstFile(szFile,&hFilesFind);
if (hFind != INVALID_HANDLE_VALUE){
if (dwFlags & hFilesFind.dwFileAttributes) {
if(hFilesFind.nFileSizeHigh>0)
hb_retnl((hFilesFind.nFileSizeHigh*MAXDWORD)+hFilesFind.nFileSizeLow);
else
hb_retnl(hFilesFind.nFileSizeLow);
}
else
hb_retnl(-1);
}
}
}
#pragma ENDDUMP