| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- // ftp.cpp : Implementation of Cftp
- /*************************************************************************/
- /*_$ Include Files */
- /*************************************************************************/
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/timeb.h>
- #include <stdlib.h>
- #include <process.h>
- #include <sys/stat.h>
- #include <io.h>
- #include <time.h>
- #include "assert.h"
- #include "ftp.h"
- #include "winsock.h"
- #include <time.h>
- #include <direct.h>
- #include <errno.h>
- //#include "wlib/wstring.h"
- #include "DownloadDebug.h"
- #ifndef _DEBUG
- #define _DEBUG
- #endif
- /*************************************************************************/
- /*_$ External References */
- /*************************************************************************/
- /*************************************************************************/
- /*_$ Private Datatypes */
- /*************************************************************************/
- /*************************************************************************/
- /*_$ Public Data */
- /*************************************************************************/
- /*************************************************************************/
- /*_$ Private Data */
- /*************************************************************************/
- //static HOSTENT gThreadHost;
- struct sockaddr_in gThreadAddress;
- static int gThreadFlag;
- /*************************************************************************/
- /*_$ Private Prototypes */
- /*************************************************************************/
- /*************************************************************************/
- /*_$ Private Macros */
- /*************************************************************************/
- /* Status flags */
- #define FTPSTAT_INIT 0
- #define FTPSTAT_CONNECTING 10
- #define FTPSTAT_WAITINGSERVEROK 15
- #define FTPSTAT_CONNECTED 20
- #define FTPSTAT_SENDINGUSER 30
- #define FTPSTAT_SENTUSER 40
- #define FTPSTAT_SENDINGPASS 50
- #define FTPSTAT_LOGGEDIN 60
- #define FTPSTAT_LOGGINGOFF 70
- #define FTPSTAT_SENDINGQUIT 80
- #define FTPSTAT_SENTQUIT 90
- #define FTPSTAT_FILEFOUND 100
- #define FTPSTAT_SENDINGCWD 110
- #define FTPSTAT_SENTCWD 120
- #define FTPSTAT_SENTPORT 130
- #define FTPSTAT_SENDINGLIST 140
- #define FTPSTAT_SENTLIST 150
- #define FTPSTAT_LISTDATAOPEN 160
- #define FTPSTAT_LISTDATARECVD 170
- #define FTPSTAT_FILEOPEN 180
- #define FTPSTAT_SENTFILEPORT 190
- #define FTPSTAT_SENDINGTYPE 200
- #define FTPSTAT_SENTTYPE 210
- #define FTPSTAT_SENDINGREST 220
- #define FTPSTAT_SENTREST 230
- #define FTPSTAT_SENDINGRETR 240
- #define FTPSTAT_SENTRETR 250
- #define FTPSTAT_FILEDATAOPEN 260
- #define FTPSTAT_FILEDATACLOSED 270
- #define FTPSTAT_TRANSFERREPLY 280
- // Utility functions
- bool Prepare_Directories(const char *rootdir, const char *filename);
- //
- // This is here as a hack...
- // Some firewalls break the TCP stack so non-blocking sockets don't work right...
- //
- static bool Use_Non_Blocking_Mode(void)
- {
- HKEY regKey;
- LONG regRetval;
- DWORD bufsiz=0;
- DWORD type=0;
- DWORD value=0;
- // Try and open the named key
- regRetval=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Westwood\\Earth And Beyond Beta 2",0,KEY_READ,®Key);
- if (regRetval!=ERROR_SUCCESS)
- return(TRUE); // default TRUE
- // Fetch the flag
- bufsiz=sizeof(value);
- type=REG_DWORD;
- regRetval=RegQueryValueEx(regKey, "UseNonBlockingFTP", 0, &type, (BYTE*) &value, &bufsiz);
- RegCloseKey(regKey);
- if ((regRetval!=ERROR_SUCCESS) || (type != REG_DWORD))
- return(TRUE);
- return(bool(value));
- }
- /////////////////////////////////////////////////////////////////////////////
- // Cftp
- /*$_Function****************************************************************
- * $_Name :
- * Cftp::Cftp()
- *
- * $_Description :
- * Constructor for the ftp class - initialises more or less all data
- * members to zero.
- *
- * $_Parameters :
- * None.
- *
- * $_ReturnValue :
- * None.
- *
- ***************************************************************************/
- Cftp::Cftp()
- {
- ZeroStuff();
- }
- /*$_Function****************************************************************
- * $_Name :
- * Cftp::Cftp()
- *
- * $_Description :
- * Destructor for the ftp class - close sockets behind us as we leave.
- *
- * $_Parameters :
- * None.
- *
- * $_ReturnValue :
- * None.
- *
- ***************************************************************************/
- Cftp::~Cftp()
- {
- CloseSockets();
- if (m_pfLocalFile)
- {
- fclose(m_pfLocalFile);
- m_pfLocalFile = NULL;
- }
- }
- void Cftp::CloseSockets(void)
- {
- if( m_iDataSocket )
- {
- closesocket( m_iDataSocket );
- m_iDataSocket=0;
- }
- if( m_iCommandSocket )
- {
- closesocket( m_iCommandSocket );
- m_iCommandSocket=0;
- }
- }
- void Cftp::ZeroStuff(void)
- {
- m_iBytesRead = 0;
- m_iFileSize = 0;
- m_szRemoteFilePath[ 0 ] = (char)0;
- m_szRemoteFileName[ 0 ] = (char)0;
- m_szLocalFilePath[ 0 ] = (char)0;
- m_szLocalFileName[ 0 ] = (char)0;
- m_szServerName[ 0 ] = (char)0;
- m_szUserName[ 0 ] = (char)0;
- m_szPassword[ 0 ] = (char)0;
- m_iDataSocket = 0;
- m_iCommandSocket = 0;
- m_iFilePos = 0;
- m_iStatus = FTPSTAT_INIT;
- m_sendNewPortStatus = 0;
- m_pfLocalFile = NULL;
- m_findStart = 0;
- memset(&m_CommandSockAddr, 0, sizeof(m_CommandSockAddr));
- memset(&m_DataSockAddr, 0, sizeof(m_DataSockAddr));
- }
- DWORD WINAPI gethostbynameA( void * szName )
- {
- HOSTENT *he = gethostbyname( (const char *)szName );
- //////DBGMSG("Hostname copy start");
- if (he)
- memcpy((char *)&(gThreadAddress.sin_addr), he->h_addr, he->h_length );
- else
- memcpy((char *)&(gThreadAddress.sin_addr),"",strlen("")+1);
- /////DBGMSG("Hostname copy complete");
- gThreadFlag = 1;
- return 0;
- }
- int Cftp::AsyncGetHostByName(char * szName, struct sockaddr_in &address )
- {
- static int stat = 0;
- static unsigned long threadid;
- if( stat == 0 )
- {
- /* Kick off gethostname thread */
- gThreadFlag = 0;
- memset(&gThreadAddress,0,sizeof(gThreadAddress));
- if( CreateThread( NULL, 0, gethostbynameA, szName, 0, &threadid ) == NULL )
- {
- return( FTP_FAILED );
- }
- stat = 1;
- }
- if( stat == 1 )
- {
- if( gThreadFlag )
- {
- /* Thread finished */
- address = gThreadAddress;
- address.sin_family=AF_INET;
- stat = 0;
- return( FTP_SUCCEEDED );
- }
- }
- return( FTP_TRYING );
- }
- /*$_Function****************************************************************
- * $_Name :
- * HRESULT Cftp::ConnectToServer(LPCSTR szServerName)
- *
- * $_Description :
- * Overloaded funciton that makes a connection to a server. Will probably
- * fail on (at least) the first call, as it may take a while for the server
- * to send it's "ready" reply.
- *
- * $_Parameters :
- * -> const char * szServerName
- * Optionally, the name of the server to connect to. This must be specified
- * in the first call to the function.
- *
- * $_ReturnValue :
- * FTP_TRYING until the connection has succeeded, then FTP_SUCCEEDED.
- * FTP_FAILED on error.
- *
- ***************************************************************************/
- HRESULT Cftp::ConnectToServer(LPCSTR szServerName)
- {
- struct sockaddr_in address;
- unsigned long uTemp;
- unsigned long serverIP;
- char buffer[ 256 ];
- int iReply, error;
- strncpy( m_szServerName, szServerName, 128 );
- if( m_iStatus == FTPSTAT_INIT )
- {
- /* Find the IP address of the server */
- serverIP = inet_addr( m_szServerName );
- memset( &m_CommandSockAddr, 0, sizeof( m_CommandSockAddr ) );
- m_CommandSockAddr.sin_family = AF_INET;
- if( serverIP == INADDR_NONE )
- {
- /* It's an FQDN - hopefully. */
- ////////DBGMSG("Async gethostbyname");
- if( AsyncGetHostByName( m_szServerName, address ) == FTP_TRYING )
- {
- return( FTP_TRYING );
- }
- //////DBGMSG("Got hostbyname");
- if( address.sin_addr.s_addr == 0 )
- {
- ///////DBGMSG("gethostbyname failed");
- return( FTP_FAILED );
- }
- m_CommandSockAddr=address;
- ///////memcpy( (char *)&(m_CommandSockAddr.sin_addr), he.h_addr, he.h_length );
- serverIP = m_CommandSockAddr.sin_addr.s_addr;
- //////DBGMSG("ServerIP = "<<serverIP);
- /////DBGMSG("Memcpy OK");
- }
- else
- {
- m_CommandSockAddr.sin_addr.s_addr = serverIP;
- }
- /* Set port number. */
- m_CommandSockAddr.sin_port = htons( 21 );
- /* Open a socket. */
- m_iCommandSocket = socket( AF_INET, SOCK_STREAM, 0 );
- if( m_iCommandSocket < 0 )
- {
- return( FTP_FAILED );
- }
- //////DBGMSG("Socket created");
- /* Set socket to non-blocking */
- uTemp = 1;
- if (Use_Non_Blocking_Mode()) {
- if( ioctlsocket( m_iCommandSocket, FIONBIO, &uTemp ) == SOCKET_ERROR )
- {
- return( FTP_FAILED );
- }
- }
- // Go ahead and advance the state....
- m_iStatus = FTPSTAT_CONNECTING;
- // Start the connection process....
- if( connect( m_iCommandSocket, (struct sockaddr *)&m_CommandSockAddr, sizeof( m_CommandSockAddr ) ) == -1 )
- {
- error = WSAGetLastError();
- if( ( error == WSAEWOULDBLOCK ) || ( error == WSAEINVAL ) || ( error == WSAEALREADY ) )
- {
- return( FTP_TRYING );
- }
- if( error != WSAEISCONN )
- {
- ////////DBGMSG("Connect failed");
- closesocket( m_iCommandSocket );
- return( FTP_FAILED );
- }
- }
- }
- if( m_iStatus == FTPSTAT_CONNECTING )
- {
- // Check to see if we're connected
- fd_set wset;
- fd_set eset;
- FD_ZERO(&wset);
- FD_ZERO(&eset);
- FD_SET(m_iCommandSocket,&wset);
- FD_SET(m_iCommandSocket,&eset);
- timeval tv;
- tv.tv_sec=0;
- tv.tv_usec=0;
- int retval=select(m_iCommandSocket+1,0,&wset,&eset,&tv);
- if (retval == 0) // not ready yet....
- return(FTP_TRYING);
- if (FD_ISSET(m_iCommandSocket, &eset)) {
- // OK, the exception flag is set, guess we couldn't connect....
- closesocket( m_iCommandSocket );
- return( FTP_FAILED );
- }
- if (FD_ISSET(m_iCommandSocket, &wset)) {
- // OK, the write flag is set, we're good to go...
- m_iStatus = FTPSTAT_WAITINGSERVEROK;
- }
- }
- if( m_iStatus == FTPSTAT_WAITINGSERVEROK )
- {
- iReply=0;
- if (RecvReply( buffer, 256, &iReply ) != FTP_SUCCEEDED)
- return(FTP_TRYING);
- if (iReply != FTPREPLY_SERVEROK)
- return(FTP_FAILED);
- m_iStatus = FTPSTAT_CONNECTED;
- return( FTP_SUCCEEDED );
- }
- return( FTP_FAILED );
- }
- /*$_Function****************************************************************
- * $_Name :
- * STDMETHOD Cftp::LoginToServer( LPCSTR szUserName, LPCSTR szPassword )
- *
- * $_Description :
- * Logs into a server. ConnectToServer() must have succeeded before this
- * call is made.
- *
- * $_Parameters :
- * -> LPCSTR szUserName
- * The username under which to log in.
- *
- * -> LPCSTR szPassword
- * The password to use.
- *
- * $_ReturnValue :
- * FTP_TRYING until the login has succeeded, then FTP_SUCCEEDED.
- * FTP_FAILED on error.
- *
- ***************************************************************************/
- HRESULT Cftp::LoginToServer( LPCSTR szUserName, LPCSTR szPassword )
- {
- char command[ 256 ];
- int iReply;
- strncpy( m_szUserName, szUserName, 128 );
- strncpy( m_szPassword, szPassword, 128 );
- memset( command, 0, 256 );
- if( m_iStatus == FTPSTAT_CONNECTED )
- {
- sprintf( command, "USER %s\r\n", m_szUserName );
- if( SendCommand( command, 7 + strlen( m_szUserName ) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENDINGUSER;
- }
- if( m_iStatus == FTPSTAT_SENDINGUSER )
- {
- if(RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED )
- return( FTP_TRYING );
- if (iReply != FTPREPLY_PASSWORD)
- return( FTP_FAILED );
- m_iStatus = FTPSTAT_SENTUSER;
- }
- if( m_iStatus == FTPSTAT_SENTUSER )
- {
- sprintf( command, "PASS %s\r\n", m_szPassword );
- if( SendCommand( command, 7 + strlen( m_szPassword ) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENDINGPASS;
- }
- if( m_iStatus == FTPSTAT_SENDINGPASS )
- {
- if( RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED )
- {
- return( FTP_TRYING );
- }
- if( iReply != FTPREPLY_LOGGEDIN )
- {
- if( iReply == FTPREPLY_CONTROLCLOSED )
- {
- return( FTP_FAILED );
- }
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_LOGGEDIN;
- return( FTP_SUCCEEDED );
- }
- return( FTP_FAILED );
- }
- /*$_Function****************************************************************
- * $_Name :
- * STDMETHOD Cftp::LogoffFromServer( void )
- *
- * $_Description :
- * Logs off from a server. LoginToServer() must have succeeded before this
- * call is made.
- *
- * $_Parameters :
- * None.
- *
- * $_ReturnValue :
- * FTP_TRYING until the logoff has succeeded, then FTP_SUCCEEDED.
- * FTP_FAILED on error.
- *
- ***************************************************************************/
- HRESULT Cftp::LogoffFromServer( void )
- {
- char command[ 256 ];
- int iReply, iRetCode;
- memset( command, 0, 256 );
- switch( m_iStatus )
- {
- case FTPSTAT_LOGGINGOFF:
- if( SendCommand( "QUIT\r\n", 6 ) == FTP_SUCCEEDED )
- {
- m_iStatus = FTPSTAT_SENDINGQUIT;
- }
- else
- {
- return( FTP_FAILED );
- }
- case FTPSTAT_SENDINGQUIT:
- {
- char buf[ 50 ];
- if( ( iReply = RecvReply( buf, 50, &iRetCode ) ) == FTP_SUCCEEDED )
- {
- if( iRetCode == 221 )
- {
- //m_iStatus = FTPSTAT_SENTQUIT;
- //m_iStatus = FTPSTAT_INIT; // NAK
- CloseSockets();
- ZeroStuff();
- return( FTP_SUCCEEDED );
- }
- }
- else
- {
- if( iReply == FTP_FAILED )
- {
- return( FTP_FAILED );
- }
- return( FTP_TRYING );
- }
- }
- default:
- m_iStatus = FTPSTAT_LOGGINGOFF;
- return( FTP_TRYING );
- }
- }
- /*$_Function****************************************************************
- * $_Name :
- * HRESULT Cftp::FindFile( LPCSTR szRemoteFileName, int * piSize )
- *
- * $_Description :
- * Locates a file on the remote server.
- *
- * $_Parameters :
- * -> LPCSTR szRemoteFileName
- * Full path and file name of the file to be downloaded.
- *
- * <- int * piSize
- * A pointer to an integer in which to return the size of the file, in bytes.
- *
- * $_ReturnValue :
- * FTP_SUCCEEDED on success. Otherwise FTP_TRYING, or FTP_FAILED on error.
- *
- * $_Warnings :
- * This function will continue to return FTP_TRYING if the file does not exist on
- * the server.
- *
- ***************************************************************************/
- HRESULT Cftp::FindFile( LPCSTR szRemoteFileName, int * piSize )
- {
- char command[ 256 ];
- static char listline[ 256 ];
- int i, iReply;
- char ext[ 10 ];
- if (m_findStart==0)
- m_findStart=time(NULL);
- if((time(NULL)-m_findStart) > 30) // try for 30 seconds
- {
- /////////DBGMSG("FindFile: Tried for too long");
- m_findStart=0;
- return( FTP_FAILED );
- }
- //strcpy(m_szRemoteFilePath, "/"); // start at home
- _splitpath( szRemoteFileName, NULL, m_szRemoteFilePath+strlen(m_szRemoteFilePath),
- m_szRemoteFileName, ext );
- strcat( m_szRemoteFileName, ext );
- for( i = 0; i < (int)strlen( m_szRemoteFilePath ); i++ )
- {
- if( m_szRemoteFilePath[ i ] == '\\' )
- {
- m_szRemoteFilePath[ i ] = '/';
- }
- }
- memset( command, 0, 256 );
- /* Change directory */
- if( ( m_iStatus == FTPSTAT_LOGGEDIN ) || ( m_iStatus == FTPSTAT_FILEFOUND ) )
- {
- sprintf( command, "CWD %s\r\n", m_szRemoteFilePath );
- if( SendCommand( command, 6 + strlen( m_szRemoteFilePath ) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENDINGCWD;
- }
- /* Get reply */
- if( m_iStatus == FTPSTAT_SENDINGCWD )
- {
- HRESULT reply=RecvReply( command, 256, &iReply );
- if ((reply == FTP_SUCCEEDED) && (iReply == 550)) {
- m_findStart=0;
- return(FTP_FAILED);
- }
- if( ( reply != FTP_SUCCEEDED ) ||
- ( iReply != FTPREPLY_CWDOK ) )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENTCWD;
- }
- /* Set up data connection port */
- if( m_iStatus == FTPSTAT_SENTCWD )
- {
- i = 0;
- while( SendNewPort() == FTP_TRYING )
- {
- i++;
- if( i == 1000 )
- {
- return( FTP_TRYING );
- }
- }
- m_iStatus = FTPSTAT_SENTPORT;
- }
- /* Get directory listing for file. */
- if( m_iStatus == FTPSTAT_SENTPORT )
- {
- sprintf( command, "LIST %s\r\n", m_szRemoteFileName );
- if( SendCommand( command, 7 + strlen( m_szRemoteFileName ) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENDINGLIST;
- }
- /* Get reply */
- if( m_iStatus == FTPSTAT_SENDINGLIST )
- {
- if( ( RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED ) ||
- ( iReply != FTPREPLY_OPENASCII ) )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENTLIST;
- }
- /* Open data connection */
- if( m_iStatus == FTPSTAT_SENTLIST )
- {
- i = OpenDataConnection();
- if( i != FTP_SUCCEEDED )
- {
- /////////DBGMSG("FindFile: OpenDataConnection failed: "<<i);
- m_findStart=0;
- return( i );
- }
- m_iStatus = FTPSTAT_LISTDATAOPEN;
- memset( listline, 0, 256 );
- }
- /* Get data */
- if( m_iStatus == FTPSTAT_LISTDATAOPEN )
- {
- RecvData( listline, 256 );
- if( strlen( listline ) == 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_LISTDATARECVD;
- }
- /* Wait for confirmation of end of data stream */
- if( m_iStatus == FTPSTAT_LISTDATARECVD )
- {
- if( ( RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED ) ||
- ( iReply != FTPREPLY_COMPLETE ) )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_FILEFOUND;
- }
- CloseDataConnection();
- m_findStart=0;
- /* Is the file there? */
- if( strncmp( listline, m_szRemoteFileName, sizeof( m_szRemoteFileName ) ) == 0 )
- {
- /* No */
- ////////DBGMSG("FindFile: File not in list: "<<listline);
- return( FTP_FAILED );
- }
- /* Yes - find out the size, in bytes */
- if( sscanf( &listline[ 32 ], " %d ", &i ) == 1 )
- {
- if( piSize != NULL )
- {
- *piSize = i;
- m_iFileSize = i;
- }
- return( FTP_SUCCEEDED );
- }
-
- ////////DBGMSG("Default fail case: "<<listline);
- return( FTP_FAILED );
- }
- /*$_Function****************************************************************
- * $_Name :
- * HRESULT Cftp::SendCommand( LPCSTR pCommand, int iSize )
- *
- * $_Description :
- * Sends a command to the server. Commands are strings terminated with
- * "\r\n"
- *
- * $_Parameters :
- * LPCSTR pCommand
- * A pointer to a command
- *
- * int iSize
- * The size of the command in bytes.
- *
- * $_ReturnValue :
- * FTP_SUCCEEDED on success, or FTP_FAILED on error.
- *
- ***************************************************************************/
- HRESULT Cftp::SendCommand( LPCSTR pCommand, int iSize )
- {
- int i;
-
- i = send( m_iCommandSocket, pCommand, iSize, 0 );
- if( i > 0 )
- {
- #ifdef _DEBUG
- OutputDebugString( "-->" );
- OutputDebugString( pCommand );
- #endif
- return( FTP_SUCCEEDED );
- }
- else
- {
- return( FTP_FAILED );
- }
- }
- /*$_Function****************************************************************
- * $_Name :
- * HRESULT Cftp::RecvReply( LPCSTR pReplyBuffer, int iSize, int * piRetCode )
- *
- * $_Description :
- * Receives a reply from the server and extracts the numerical reply
- * code.
- *
- * $_Parameters :
- * -> LPCSTR pReplyBuffer
- * A buffer for the reply - must be specified and be at least 4 bytes in
- * length.
- *
- * -> iSize
- * The size of the reply buffer.
- *
- * <- piRetCode
- * A pointer to an integer to store the numeric return code for the message received.
- *
- * $_ReturnValue :
- * FTP_SUCCEEDED on success, FTP_FAILED otherwise.
- *
- ***************************************************************************/
- HRESULT Cftp::RecvReply( LPCSTR pReplyBuffer, int iSize, int * piRetCode )
- {
- int readval;
- char * pc = (char *)pReplyBuffer;
- while(1) {
- // Preview whats on the socket so we can make the decision if there is enough
- // there to use.
- readval = recv(m_iCommandSocket, pc, iSize-1, MSG_PEEK);
- pc[iSize-1]=0;
- if (readval > 0)
- pc[readval]=0;
- // Did the endpoint close?
- if (readval < 0) {
- if ((WSAGetLastError() == WSAECONNRESET ) || ((WSAGetLastError() == WSAENOTCONN)))
- readval=0;
- else {
- // nothing to read right now....
- return(FTP_TRYING);
- }
- }
- if (readval == 0) {
- // endpoint closed
- *piRetCode=FTPREPLY_CONTROLCLOSED;
- return(FTP_SUCCEEDED); // we succeeded in getting a reply
- }
- if (readval < 3) // minimal response
- return(FTP_TRYING);
- // Verify that this is a complete line, if not we will keep trying til
- // we have one.
- char *end=strstr(pc, "\r\n");
- if (end == 0)
- return(FTP_TRYING);
- // OK, we've got a line, pull it from the socket...
- recv(m_iCommandSocket, pc, end-pc+strlen("\r\n"), 0);
- #ifdef _DEBUG
- OutputDebugString( "<--" );
- OutputDebugString( pc );
- #endif
- // Is this an unrecognized command? It's probably in response to our BOGUSCOMMAND, ignore it.
- // If we got this for some other reason then this FTP server doesn't support our required command set.
- // If this were a general purpose FTP library we probably couldn't get away with this because it's pretty lame.
- const char *BOGUS_RESPONSE="500";
- if (_strnicmp(pc, BOGUS_RESPONSE, strlen(BOGUS_RESPONSE))==0)
- continue;
- // If the line starts with a non-digit then we ignore it...
- if (isdigit(pc[0])==0)
- continue;
- if (piRetCode)
- *piRetCode=atol(pc);
- return( FTP_SUCCEEDED );
- }
- }
- /*$_Function****************************************************************
- * $_Name :
- * unsigned long MyIPAddress( int sockfd )
- *
- * $_Description :
- * Finds the IP address of the local host.
- *
- * $_Parameters :
- * -> int sockfd
- * An optional socket descriptor - if this is given and the socket is connected
- * then the answer will guaranteed to be on the same interface as the connected
- * socket.
- *
- * $_ReturnValue :
- * A unsigned integer containing the IP address of the local host.
- *
- ***************************************************************************/
- unsigned long MyIPAddress( int sockfd )
- {
- int test = 99;
- int i;
- char pBuffer[ 256 ];
- char * pAddr;
- struct hostent * pHE;
- unsigned long ip;
- struct sockaddr_in sin;
- if( sockfd != -1 )
- {
- i = sizeof( sin );
- getsockname( sockfd, (struct sockaddr *)&sin, &i );
- ip = sin.sin_addr.S_un.S_addr;
- }
- else
- {
- /* Get this machine's name. */
- if( gethostname( pBuffer, 256 ) != 0 )
- {
- return( FTP_FAILED );
- }
- /* Look up the machine in "DNS" or whatever. */
- pHE = gethostbyname( pBuffer );
- if( pHE == NULL )
- {
- return( FTP_FAILED );
- }
- /* Find a good IP address. */
- ip = 0;
- i = 0;
-
- while( ( pAddr = pHE->h_addr_list[ i++ ] ) != NULL )
- {
- ip = *((unsigned long *)pAddr );
- if( ( ip != 0 ) && ( ip != inet_addr( "127.0.0.1" ) ) )
- {
- break;
- }
- else
- {
- ip = 0;
- }
- }
- }
- if( ip == 0 )
- {
- return( FTP_FAILED );
- }
- return( ip );
- }
- /*$_Function****************************************************************
- * $_Name :
- * int Cftp::SendNewPort( void )
- *
- * $_Description :
- * Sends a PORT command to the server specifying a new ephemeral port to
- * use for a data connection. Opens the port in the process.
- *
- * $_Parameters :
- * bool reset
- * Should be TRUE for the first attempt, FALSE subsequently.
- *
- * $_ReturnValue :
- * FTP_SUCCEEDED on success, FTP_FAILED on error, FTP_TRYING while
- * going through the stages.
- *
- ***************************************************************************/
- int Cftp::SendNewPort( void )
- {
- unsigned long uTemp;
- char command[ 256 ];
- int i, iReply;
- /* Open a socket */
- if( m_sendNewPortStatus == 0 )
- {
- m_iDataSocket = socket( AF_INET, SOCK_STREAM, 0 );
- if( m_iDataSocket < 0 )
- {
- return( FTP_FAILED );
- }
- /* Set socket to non-blocking */
- uTemp = 1;
- if (Use_Non_Blocking_Mode()) {
- if( ioctlsocket( m_iCommandSocket, FIONBIO, &uTemp ) == SOCKET_ERROR )
- {
- return( FTP_FAILED );
- }
- }
- memset( &m_DataSockAddr, 0, sizeof( m_DataSockAddr ) );
- m_DataSockAddr.sin_family = AF_INET;
- m_DataSockAddr.sin_addr.s_addr = htonl( INADDR_ANY );
- m_DataSockAddr.sin_port = 0; // Winsock fills in an ephemeral port during bind...
- if( bind( m_iDataSocket, (struct sockaddr *)&m_DataSockAddr, sizeof( m_DataSockAddr ) ) < 0 )
- {
- return( FTP_FAILED );
- }
- i = sizeof( m_DataSockAddr);
- getsockname( m_iDataSocket, (struct sockaddr *)&m_DataSockAddr, &i );
- listen( m_iDataSocket, 5 );
- // Set socket to non-blocking
- uTemp = 1;
- if (Use_Non_Blocking_Mode()) {
- if( ioctlsocket( m_iDataSocket, FIONBIO, &uTemp ) == SOCKET_ERROR )
- {
- return( FTP_FAILED );
- }
- }
- m_sendNewPortStatus = 1;
- }
- /* Send a port command. */
- if( m_sendNewPortStatus == 1 )
- {
- memset( command, 0, 256 );
- i = MyIPAddress( m_iCommandSocket );
- if( i == FTP_FAILED )
- {
- return( FTP_FAILED );
- }
- sprintf( command, "PORT %d,%d,%d,%d,%d,%d\r\n",
- i & 0xFF,
- ( i >> 8 ) & 0xFF,
- ( i >> 16 ) & 0xFF,
- ( i >> 24 ) & 0xFF,
- m_DataSockAddr.sin_port & 0xFF,
- m_DataSockAddr.sin_port >> 8 );
- if( SendCommand( command, strlen(command) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_sendNewPortStatus = 2;
- }
- /* Get reply */
- if( m_sendNewPortStatus == 2 )
- {
- if( ( RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED ) ||
- ( iReply != FTPREPLY_PORTOK ) )
- {
- return( FTP_TRYING );
- }
- m_sendNewPortStatus = 0;
- }
- return( FTP_SUCCEEDED );
- }
- /*$_Function****************************************************************
- * $_Name :
- * int Cftp::OpenDataConnection()
- *
- * $_Description :
- * Opens the data connection to the server. Actually, the socket for the
- * data connection should have been opened by a call to SendNewPort before
- * this method is called - this just calls accept.
- *
- * $_Parameters :
- * None.
- *
- * $_ReturnValue :
- * FTP_SUCCEEDED on success, FTP_FAILED otherwise.
- *
- * $_Warnings :
- * This call may block.
- *
- ***************************************************************************/
- int Cftp::OpenDataConnection()
- {
- int iNewSocket;
- if( m_iDataSocket == 0 )
- {
- return( FTP_FAILED );
- }
- if( ( iNewSocket = accept( m_iDataSocket, NULL, 0 ) ) < 0 )
- {
- if( WSAGetLastError() != (WSAEWOULDBLOCK ) )
- {
- return( FTP_FAILED );
- }
- else
- {
- return( FTP_TRYING );
- }
- }
- m_iDataSocket = iNewSocket;
- return( FTP_SUCCEEDED );
- }
- /*$_Function****************************************************************
- * $_Name :
- * void Cftp::CloseDataConnection()
- *
- * $_Description :
- * Closes the data connection.
- *
- * $_Parameters :
- * None.
- *
- * $_ReturnValue :
- * None.
- *
- ***************************************************************************/
- void Cftp::CloseDataConnection()
- {
- closesocket( m_iDataSocket );
- m_iDataSocket = 0;
- }
- /*$_Function****************************************************************
- * $_Name :
- * HRESULT Cftp::DisconnectFromServer()
- *
- * $_Description :
- * Disconnects from the server.
- *
- * $_Parameters :
- * None.
- *
- * $_ReturnValue :
- * None.
- *
- ***************************************************************************/
- HRESULT Cftp::DisconnectFromServer()
- {
- // Make the download file read-only so it's still there the next time
- // we're run.
- closesocket( m_iCommandSocket );
- return( FTP_SUCCEEDED );
- }
- /*$_Function****************************************************************
- * $_Name :
- * int Cftp::SendData( char * pData, int iSize )
- *
- * $_Description :
- * Sends data to the server.
- *
- * $_Parameters :
- * -> char * pData
- * A pointer to some data to be sent.
- *
- * -> int iSize
- * The size, in bytes, of the data.
- *
- * $_ReturnValue :
- * The number of bytes sent, or -1 on error.
- *
- ***************************************************************************/
- int Cftp::SendData( char * pData, int iSize )
- {
- return( send( m_iDataSocket, pData, iSize, 0 ) );
- }
- /*$_Function****************************************************************
- * $_Name :
- * int Cftp::RecvData( char * pData, int iSize )
- *
- * $_Description :
- * Receives data from the server.
- *
- * $_Parameters :
- * -> LPCSTR pData
- * A pointer to a buffer for the data to be received.
- *
- * -> int iSize
- * The size, in bytes, of the data buffer.
- *
- * $_ReturnValue :
- * The number of bytes received, or -1 on error.
- *
- ***************************************************************************/
- int Cftp::RecvData( char * pData, int iSize )
- {
- /***** testing...
- int readval = recv(m_iDataSocket, pData, iSize, MSG_PEEK);
- if (readval <= 0) // If there's nothing to read, don't read now...
- return(readval);
- ******/
- return( recv( m_iDataSocket, pData, iSize, 0 ) );
- }
- /*$_Function****************************************************************
- * $_Name :
- * HRESULT Cftp::GetNextFileBlock( LPCSTR szLocalFileName, int * piTotalRead )
- *
- * $_Description :
- * Retrieves the next block of data from the remote file and writes it into
- * the local file. Deals with opening the local file and restarting a
- * failed transfer if FileRecoveryPosition() returns non-zero. To prevent
- * a restart after FileRecoveryPosition() returns non-zero, call
- * Cftp::RestartFrom() with 0 as the offset parameter.
- *
- * The file is initally written to a temporary file and then copied to the local
- * file name when the transfer is complete. If the transfer is halted, the
- * temporary file will be used as the starting point for subsequent retries.
- *
- * $_Parameters :
- * -> LPCSTR szLocalFileName
- * A pointer to a string containing the local file name for the transferred file.
- *
- * <- int * piTotalRead
- * A pointer to an integer in which to store the total number of bytes
- * transferred so far.
- *
- * $_ReturnValue :
- * FTP_TRYING while the transfer is in progress, FTP_SUCCEEDED on completion,
- * FTP_FAILED on error.
- *
- ***************************************************************************/
- HRESULT Cftp::GetNextFileBlock( LPCSTR szLocalFileName, int * piTotalRead )
- {
- char command[ 256 ];
- int read, totread = 0;
- const int max_data_len=10240;
- char data[ max_data_len ];
- int res, iReply;
- char downloadfilename[256];
- GetDownloadFilename(szLocalFileName, downloadfilename);
- //char str[ 256 ];
- strncpy( m_szLocalFileName, szLocalFileName, 256 );
- // Open local file
- if( m_iStatus == FTPSTAT_FILEFOUND )
- {
- if( m_iFilePos == 0 )
- {
- if( ( m_pfLocalFile = fopen( downloadfilename, "wb" ) ) == NULL )
- {
- return( FTP_FAILED );
- }
- }
- else
- {
- if( ( m_pfLocalFile = fopen( downloadfilename, "ab" ) ) == NULL )
- {
- return( FTP_FAILED );
- }
- }
- m_iStatus = FTPSTAT_FILEOPEN;
- }
- // Send the PORT command
- if( m_iStatus == FTPSTAT_FILEOPEN )
- {
- if( SendNewPort() == FTP_SUCCEEDED )
- {
- m_iStatus = FTPSTAT_SENTFILEPORT;
- }
- else
- {
- return( FTP_TRYING );
- }
- }
- // Set type to BINARY
- if( m_iStatus == FTPSTAT_SENTFILEPORT )
- {
- sprintf( command, "TYPE I\r\n");
- if( SendCommand( command, strlen( command ) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENDINGTYPE;
- }
- /* Get reply */
- if( m_iStatus == FTPSTAT_SENDINGTYPE )
- {
- if( ( RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED ) ||
- ( iReply != FTPREPLY_TYPEOK ) )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENTTYPE;
- }
- // Send the RESTART command - if we are restarting a previous transfer.
- if( m_iStatus == FTPSTAT_SENTTYPE )
- {
- if( m_iFilePos == 0 )
- {
- m_iStatus = FTPSTAT_SENTREST;
- }
- else
- {
- sprintf( command, "REST %d\r\n", m_iFilePos );
- if( SendCommand( command, strlen( command ) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENDINGREST;
- }
- }
- /* Get reply */
- if( m_iStatus == FTPSTAT_SENDINGREST )
- {
- HRESULT res=RecvReply( command, 256, &iReply );
- if ((res == FTP_FAILED) || (iReply == 502)) // unsupported command
- {
- m_iFilePos=0;
- fclose(m_pfLocalFile);
- m_pfLocalFile = fopen( downloadfilename, "wb" );
- }
- else if( ( res != FTP_SUCCEEDED ) ||
- ( iReply != FTPREPLY_RESTARTOK ) )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENTREST;
- }
- // Send the RETR command
- if( m_iStatus == FTPSTAT_SENTREST )
- {
- sprintf( command, "RETR %s\r\n", m_szRemoteFileName );
- if( SendCommand( command, strlen( command ) ) < 0 )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENDINGRETR;
- }
- /* Get reply */
- if( m_iStatus == FTPSTAT_SENDINGRETR )
- {
- if( ( RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED ) ||
- ( iReply != FTPREPLY_OPENBINARY ) )
- {
- return( FTP_TRYING );
- }
- m_iStatus = FTPSTAT_SENTRETR;
- }
- /* Open data connection */
- if( m_iStatus == FTPSTAT_SENTRETR )
- {
- res = OpenDataConnection();
- if( res != FTP_SUCCEEDED )
- {
- return( res );
- }
- m_iStatus = FTPSTAT_FILEDATAOPEN;
- }
- /* Get data */
- if( m_iStatus == FTPSTAT_FILEDATAOPEN )
- {
- do
- {
- read = RecvData( data, max_data_len );
- if( read > 0 )
- {
- fwrite( data, 1, read, m_pfLocalFile );
- totread += read;
- }
- }
- while( ( read > 0 ) && ( totread < (max_data_len * 20) ) ); // Don't read too much -
- // must not block for too long
- m_iFilePos += totread; // update read position
- if( piTotalRead != NULL )
- *piTotalRead = m_iFilePos;
- // look for all notices about endpoint closed...
- if (read < 0) {
- int wsa_err=WSAGetLastError();
- if ((wsa_err == WSAECONNRESET) || (wsa_err == WSAENOTCONN))
- read = 0; // just treat it as a close....
- }
- /* See if the download has finished */
- if( read == 0 )
- {
- // Data connection reset
- if( m_iFilePos != m_iFileSize ) {
- // Haven't got all the data
- return( FTP_FAILED );
- } else {
- // got all the data and the connection is closed so mark it so
- m_iStatus = FTPSTAT_FILEDATACLOSED;
- }
- } else {
- return( FTP_TRYING );
- }
- }
- // if we have gotten to here, we have all the data and the connection is
- // closed. The case where the connection was closed but we didn't get
- // everything is handled above.
- if( m_iStatus == FTPSTAT_FILEDATACLOSED ) {
- CloseDataConnection();
- fclose( m_pfLocalFile );
- m_pfLocalFile = NULL;
- /*
- * Move the file from the temporary download location to its
- * final resting place
- */
- char downloadfilename[256];
- GetDownloadFilename(m_szLocalFileName, downloadfilename);
- // Make sure the path exists for the new file
- char curdir[256];
- _getcwd(curdir,256);
- Prepare_Directories(curdir, m_szLocalFileName);
- DEBUG_LOG(("CWD: %s\n", curdir));
- if( rename( downloadfilename, m_szLocalFileName ) != 0 ) {
- DEBUG_LOG(("First rename of %s to %s failed with errno of %d\n", downloadfilename, m_szLocalFileName, errno));
- /* Error moving file - remove file that's already there and try again. */
- _chmod( m_szLocalFileName, _S_IWRITE | _S_IREAD); // make sure it's not readonly
- DEBUG_LOG(("_chmod of %s failed with errno of %d\n", m_szLocalFileName, errno));
- remove( m_szLocalFileName );
- DEBUG_LOG(("remove of %s failed with errno of %d\n", m_szLocalFileName, errno));
- if( rename( downloadfilename, m_szLocalFileName ) != 0 ) {
- DEBUG_LOG(("Second rename of %s to %s failed with errno of %d\n", downloadfilename, m_szLocalFileName, errno));
- return( FTP_FAILED );
- }
- }
- // Send a bogus command. If the firewall has closed our ftp command channel connection this
- // will allow us to detect it. (Obviously the data channel is closed or we wouldn't be here)
- // There are cases (firewall) where the connection will go away without having a close command
- // but writing to it will allow us to detect this case.
- // We will ignore the response because we don't know in which order the server will respond
- SendCommand("BOGUSCOMMAND\r\n", strlen("BOGUSCOMMAND\r\n"));
- OutputDebugString("File closed, waiting for response.\n");
- m_iStatus = FTPSTAT_TRANSFERREPLY;
- }
- if( m_iStatus == FTPSTAT_TRANSFERREPLY ) {
- iReply=0;
- // Get the file complete reply
- if (RecvReply( command, 256, &iReply ) != FTP_SUCCEEDED )
- return(FTP_TRYING);
- if (iReply == FTPREPLY_CONTROLCLOSED) {
- m_iStatus = FTPSTAT_INIT;
- m_iFilePos=0;
- CloseSockets();
- } else {
- m_iStatus = FTPSTAT_LOGGEDIN;
- m_iFilePos=0;
- }
- return(FTP_SUCCEEDED);
- }
- return( FTP_FAILED );
- }
- /*$_Function****************************************************************
- * $_Name :
- * HRESULT Cftp::FileRecoveryPosition( LPCSTR szLocalFileName )
- *
- * $_Description :
- * Finds the size of the specified file. This size can be used
- *
- * $_Parameters :
- * char & szLocalFileName
- * The full path name of the local file.
- *
- * $_ReturnValue :
- * The size of that file on the local disk. 0 if the file does not exist.
- *
- ***************************************************************************/
- //
- // Do we have this file in the download directory? If so then it's a partial download.
- //
- //
- HRESULT Cftp::FileRecoveryPosition( LPCSTR szLocalFileName, LPCSTR szRegistryRoot )
- {
- char downloadfilename[256];
- GetDownloadFilename(szLocalFileName, downloadfilename);
- FILE *testfp = fopen( downloadfilename, "rb" );
- if( testfp == NULL )
- {
- m_iFilePos = 0;
- return 0;
- }
- fseek( testfp, 0, SEEK_END );
- m_iFilePos = ftell( testfp );
- fclose( testfp );
- return( m_iFilePos );
- }
- /*************8
- FILE * testfp;
- HKEY hkey;
- unsigned char regfilename[ 256 ];
- char regkey[ 512 ];
- unsigned long t1, t2;
- if( ( szRegistryRoot == NULL ) || ( szLocalFileName == NULL ) )
- {
- // Bail out
- return( 0 );
- }
- // Concatenate the registry key together
- strcpy( regkey, szRegistryRoot );
- if( regkey[ strlen( regkey ) - 1 ] != '\\' )
- {
- strcat( regkey, "\\Download" );
- }
- else
- {
- strcat( regkey, "Download" );
- }
- if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, (LPCTSTR)regkey,
- 0, KEY_ALL_ACCESS, &hkey ) != ERROR_SUCCESS )
- {
- // Key doesn't exist, create it
- if( RegCreateKey( HKEY_LOCAL_MACHINE, (LPCTSTR)regkey,
- &hkey ) != ERROR_SUCCESS )
- {
- // Error, bail out
- return( 0 );
- }
- RegSetValueEx( hkey, (LPCTSTR)"File", 0, REG_SZ, (const unsigned char *)szLocalFileName, strlen( szLocalFileName ) + 1 );
- RegCloseKey(hkey);
- return( 0 );
- }
- t2 = 256;
- if( RegQueryValueEx( hkey, (LPCTSTR)"File", 0, &t1, regfilename, &t2 ) != ERROR_SUCCESS )
- {
- RegSetValueEx( hkey, (LPCTSTR)"File", 0, REG_SZ, (const unsigned char *)szLocalFileName, strlen( szLocalFileName ) + 1 );
- RegCloseKey(hkey);
- return( 0 );
- }
- if( strcmp( szLocalFileName, (const char *)regfilename ) == 0 )
- {
- // File previously downloaded
- testfp = fopen( FTP_TEMPFILENAME, "rb" );
- if( testfp == NULL )
- {
- m_iFilePos = 0;
- RegCloseKey(hkey);
- return 0;
- }
- fseek( testfp, 0, SEEK_END );
- m_iFilePos = ftell( testfp );
- fclose( testfp );
- RegCloseKey(hkey);
- return( m_iFilePos );
- }
- // Download file doesn't exist
- RegSetValueEx( hkey, (LPCTSTR)"File", 0, REG_SZ, (const unsigned char *)szLocalFileName, strlen( szLocalFileName ) + 1 );
- RegCloseKey(hkey);
- // get rid of any temp downloads
- _unlink(FTP_TEMPFILENAME);
- return( 0 );
- }
- **************************************/
- //
- // convert a local name to a temp filename to use for downloading
- //
- void Cftp::GetDownloadFilename(const char *localname, char *downloadname)
- {
- char *name = strdup(localname);
- char *s = name;
- while (*s)
- {
- if (*s == '\\' || *s == '.' || *s == ' ')
- *s = '_';
- ++s;
- }
- sprintf(downloadname,"download\\%s_%d.tmp",name,m_iFileSize);
- free(name);
- /*
- Wstring name;
- name.set(localname);
- name.replace("\\","_");
- name.replace(".","_");
- name.replace(" ","_");
- sprintf(downloadname,"download\\%s_%d.tmp",name.get(),m_iFileSize);
- */
- return;
- }
- //
- // Build all the subdirectories for a given filename
- //
- bool Prepare_Directories(const char *rootdir, const char *filename)
- {
- char tempstr[256];
- char newdir[256];
- const char *cptr=filename;
- while(cptr=strchr(cptr,'\\'))
- {
- memset(tempstr,0,256);
- strncpy(tempstr,filename,cptr-filename);
- sprintf(newdir,"%s\\%s",rootdir, tempstr);
- if (!CreateDirectory(newdir, NULL))
- return false;
- //if ((_mkdir(newdir) == -1) && ((errno == ENOENT || errno==EACCES)))
- //return(false);
- cptr++;
- }
- return(true);
- }
|