Return TypeFunction nameArguments
hzEcodehzFtpHost::GetAll(void)

Declared in file: hzFtpClient.h
Defined in file : hzFtpClient.cpp

Function Logic:

0:START 1:!m_bInit 2:Fatal 3:hzFtpHost::_hz_FtpDnhist::Load ReadDir rc 4:rc!=E_OK 5:Return rc 6:nIndex 7:de hzDirent::strName hzMapS::Insert 8:hzFtpClient::Initialize rc 9:rc!=E_OK 10:Err2Txt 11:Return rc 12:hzFtpClient::StartSession rc 13:rc!=E_OK 14:Err2Txt 15:Return rc 16:hzFtpClient::SetRemoteDir rc 17:rc!=E_OK 18:rc==E_NOTFOUND 19:hzFtpClient::QuitSession 20:Return rc 21:Return rc 22:hzFtpClient::SetLocalDir rc 23:rc!=E_OK 24:hzFtpClient::QuitSession 25:Return rc 26:hzFtpClient::GetDirList rc 27:rc!=E_OK 28:rc==E_PROTOCOL 29:Return rc 30:hzVect::Count Err2Txt 31:Return rc 32:listing.Count()==0 33:hzFtpClient::QuitSession 34:Return rc 35:hzVect::Count nListed nIndex 36:nIndex 37:de hzDirent::strName hzMapS::Exists 38:!mapFilesCurr.Exists(de.strName()) 39:hzVect::Count hzDirent::txtName 40:hzDirent::strName de_a 41:de_a.Size()==de.Size() 42:de_a.Mtime()>=de.Mtime() 43:hzVect::Count hzDirent::txtName items 44:hzVect::Count hzDirent::txtName hzDirent::strName 45:!de.strName() 46:hzFtpClient::QuitSession 47:Return rc 48:!de.Size() 49:hzDirent::strName localfile hzFtpClient::FileDownload rc 50:rc!=E_OK 51:rc==E_OPENFAIL||rc==E_WRITEFAIL 52:hzDirent::txtName Err2Txt 53:Return rc 54:hzFtpClient::QuitSession 55:Return rc 56:hzDirent::Mtime hzDirent::Size hzDirent::strName hzFtpHost::_hz_FtpDnhist::Append items hzDirent::txtName strstr 57:strstr(de.txtName(),.zip) 58:cmd sprintf system sys_rc 59:sys_rc 60:cmd 61:Return rc 62:unlink cmd 63:hzFtpClient::QuitSession 64:(nDnloads+nAlready)==nListed 65:ReadDir rc 66:rc!=E_OK 67:Return rc 68:hzXDate::SysDateTime hzXDate::AsEpoch eIs eHi nIndex 69:nIndex 70:de 71:eHi 72:hzDirent::Mtime eHi 73:eHi>(eIs-600) 74:Return rc

Function body:

hzEcode hzFtpHost::GetAll (void)
{
   _hzfunc("hzFtpSite::GetAll") ;
   hzMapS  <hzString,hzDirent> mapFilesCurr ;
   hzVect  <hzDirent>          vecFilesCurr ;
   hzVect  <hzDirent>  listing ;
   hzFtpClient ftp ;
   hzDirent    de ;
   hzDirent    de_a ;
   hzString    localfile ;
   hzString    rento ;
   uint32_t    nIndex ;
   uint32_t    nAlready = 0;
   uint32_t    nDnloads = 0;
   uint32_t    nListed = 0;
   int32_t     sys_rc ;
   char*       cmd ;
   hzEcode     rc = E_OK ;
   /*
   **  ** Check we have everything we need: The publication and the olddata and newdata dirs
   **      */
   if (!m_bInit)
       Fatal("Permenant Failure: No FTP Parameters\n") ;
   threadLog("Collecting from server [%s,%s,(%s)] (user=%s, pass=%s) to dir %s ->", *m_Host, *m_Source, *m_Criteria, *m_Username, *m_Password, *m_Repos) ;
   threadLog("OK GO!\n") ;
   dnh.Load(mapFilesCurr, m_Repos) ;
   rc = ReadDir(vecFilesCurr, *m_Repos) ;
   if (rc != E_OK)
   {
       threadLog("Failed to read local dir (%s)\n", *m_Repos) ;
       return rc ;
   }
   for (nIndex = 0; nIndex < vecFilesCurr.Count() ; nIndex++)
   {
       de = vecFilesCurr[nIndex] ;
       mapFilesCurr.Insert(de.strName(), de) ;
   }
   rc = ftp.Initialize(m_Host, m_Username, m_Password) ;
   if (rc != E_OK)
   {
       threadLog("Could not INIT FTP session to server [%s] (user=%s, pass=%s). Error is %s\n", *m_Host, *m_Username, *m_Password, Err2Txt(rc)) ;
       return rc ;
   }
   rc = ftp.StartSession() ;
   if (rc != E_OK)
   {
       threadLog("Could not start FTP session to server [%s] (user=%s, pass=%s). Error is %s\n", *m_Host, *m_Username, *m_Password, Err2Txt(rc)) ;
       return rc ;
   }
   rc = ftp.SetRemoteDir(m_Source) ;
   if (rc != E_OK)
   {
       if (rc == E_NOTFOUND)
       {
           threadLog("Permenant Failure: Expected source directory %s has not been found on the server\n", *m_Source) ;
           ftp.QuitSession() ;
           return rc ;
       }
       threadLog("Temporary error: Could not CD to %s\n", *m_Source) ;
       return rc ;
   }
   rc = ftp.SetLocalDir(m_Repos) ;
   if (rc != E_OK)
   {
       threadLog("Permenant Failure: Expected target directory %s has not been found or is otherwise in error on the server\n",
           *m_Repos) ;
       ftp.QuitSession() ;
       return rc ;
   }
   rc = ftp.GetDirList(listing, m_Criteria) ;
   if (rc != E_OK)
   {
       if (rc == E_PROTOCOL)
       {
           threadLog("Permanent error: Could not get a listing. Protocol error\n") ;
           return rc ;
       }
       threadLog("We have %d files in server listing but an error of %s - will rerun\n", listing.Count(), Err2Txt(rc)) ;
       return rc ;
   }
   if (listing.Count() == 0)
   {
       threadLog("Temporary error: Got directory listing but there were no files matching [%s]\n", *m_Criteria) ;
       ftp.QuitSession() ;
       return rc ;
   }
   nListed = listing.Count() ;
   for (nIndex = 0; nIndex < listing.Count() ; nIndex++)
   {
       de = listing[nIndex] ;
       if (!mapFilesCurr.Exists(de.strName()))
           threadLog("Downloading file (%d of %d) %s\n", nIndex, listing.Count(), de.txtName()) ;
       else
       {
           de_a = mapFilesCurr[de.strName()] ;
           if (de_a.Size() == de.Size())
           {
               if (de_a.Mtime() >&eq; de.Mtime())
               {
                   threadLog("Skiping file (%d of %d) %s\n", nIndex, listing.Count(), de.txtName()) ;
                   nAlready++ ;
                   continue ;
               }
           }
           threadLog("Re-fetching file (%d of %d) %s\n", nIndex, listing.Count(), de.txtName()) ;
       }
       if (!de.strName())
       {
           threadLog("Critical error: An Un-named meta file has been found in the listing. Corruption suspected\n") ;
           ftp.QuitSession() ;
           return rc ;
       }
       if (!de.Size())
           continue ;
       localfile = m_Repos + "/" + de.strName() ;
       rc = ftp.FileDownload(de) ;
       if (rc != E_OK)
       {
           if (rc == E_OPENFAIL || rc == E_WRITEFAIL)
           {
               threadLog("Problem opening/writing file (%s) (error=%s)\n", de.txtName(), Err2Txt(rc)) ;
               return rc ;
           }
           ftp.QuitSession() ;
           return rc ;
       }
       dnh.Append(de.Mtime(), de.Size(), de.strName()) ;
       nDnloads++ ;
       if (strstr(de.txtName(), ".zip"))
       {
           cmd = new char[300];
           sprintf(cmd, "/usr/bin/unzip -q -j -n -d %s %s", *m_Repos, *localfile) ;
           sys_rc = system(cmd) ;
           if (sys_rc)
           {
               threadLog("System call [%s] returns %d\n", cmd, sys_rc) ;
               delete cmd ;
               return rc ;
           }
           threadLog("System call [%s] returns OK\n", cmd) ;
           unlink(*localfile) ;
           delete cmd ;
       }
   }
   ftp.QuitSession() ;
   if ((nDnloads + nAlready) == nListed)
   {
       rc = ReadDir(vecFilesCurr, *m_Repos) ;
       if (rc != E_OK)
       {
           threadLog("Failed to read target dir (%s) after download - delaying for 15 mins\n", *m_Repos) ;
           return rc ;
       }
       else
       {
           hzXDate     now ;
           uint32_t    eHi ;
           uint32_t    eIs ;
           now.SysDateTime() ;
           eIs = now.AsEpoch() ;
           eHi = 0;
           for (nIndex = 0; nIndex < vecFilesCurr.Count() ; nIndex++)
           {
               de = vecFilesCurr[nIndex] ;
               if (eHi < de.Mtime())
                   eHi = de.Mtime() ;
           }
           if (eHi > (eIs - 600))
               threadLog("Oldest file less than 10 mins old - delaying just in case\n") ;
       }
   }
   else
   {
       threadLog("Total of %d listed on server. We have %d already and %d were downloaded. %d files remain outstanding\n",
           nListed, nAlready, nDnloads, (nListed - nAlready - nDnloads)) ;
   }
   return rc ;
}